Skip to content

Java 11 compatibility #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
language: java
dist: trusty
dist: bionic
sudo: false

jdk:
- oraclejdk8
- openjdk7
# - oraclejdk8 # deactivated b/c travis does not support this version in any distro > trusty
- oraclejdk11
- openjdk8
- openjdk11

#branches:
# only:
Expand Down
2 changes: 1 addition & 1 deletion nb-configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>true</de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>
<de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>false</de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>
<de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>true</de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>
<netbeans.hint.jdkPlatform>JDK_1.7</netbeans.hint.jdkPlatform>
<netbeans.hint.jdkPlatform>JDK_1.8</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>
63 changes: 55 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,37 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.2</version>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.2</version>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.8.1</version>
</plugin>
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>formatter-maven-plugin</artifactId>
Expand All @@ -100,6 +115,29 @@
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>3.1</version>
</requireMavenVersion>
<requireJavaVersion>
<version>1.8,11</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
Expand All @@ -115,15 +153,18 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<version>3.2.0</version>
<configuration>
<source>8</source>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -154,14 +195,20 @@
<autoReleaseAfterClose>false</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!-- We redefine the signature generation process, whic is enabled by default,
but cannot be performed by typical users. Enable it using -Dskip.signature=false -->
<skip.signature>true</skip.signature>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathFactoryConfigurationException;
import javax.xml.xpath.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
Expand All @@ -44,8 +41,56 @@ public class XMLPrettyPrinter {
public static int IDENT_AMOUNT = 4;

/**
* This function formats all tags (and their children) which are marked with
* the autoformat="true" attribute and removes this attribute.
* This functions autoformats the text content of the child nodes of the
* node which have the {@code autoformat="true"} attribute set. The
* autoformatting results in having the textContent of the node aligned to
* the depth of indentation of its parent node. The
* {@code autoformat="true"} is also removed from the tree.
*
* <br>
* <br>
*
* <b>Note:</b> the function also removes all blank text from nodes which
* may merge {@code <a></a>} into {@code <a/>}.
*
* <br>
* <br>
*
* <b>Caution:</b> the function literally takes the textContent of the child
* nodes of the autoformat node. This removes any other nodes from that
* child node and leaves only the textContent left.
*
* <br>
* <br>
*
* <h3>Example</h3>
*
* Using this XML as input:
*
* <pre>
* {@code
* <root autoformat="true">
* <textnode>
* this text should
* be aligned
* </textnode>
* </root>
* }
* </pre>
*
* Results in this output XML:
*
* <pre>
* {@code
* <root>
* <textnode>
* this text sould
* be aligned
* </textnode>
* </root>
* }
* </pre>
*
*
* @param input
* @return
Expand All @@ -55,25 +100,36 @@ public class XMLPrettyPrinter {
* @throws IOException
* @throws TransformerException
* @throws XPathExpressionException
* @throws XPathFactoryConfigurationException
*/
public static String prettyPrintXML(String input) throws TransformerConfigurationException,
ParserConfigurationException, SAXException, IOException, TransformerException, XPathExpressionException,
XPathFactoryConfigurationException {
ParserConfigurationException, SAXException, IOException, TransformerException, XPathExpressionException {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(IDENT_AMOUNT));
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StreamResult result = new StreamResult(new StringWriter());
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new InputSource(new StringReader(input)));

// clear out blank text nodes otherwise the formatter will preserver
// them and add additional newlinesq
XPathExpression emptyTextNodeXPath = XPathFactory.newInstance().newXPath()
.compile("//text()[normalize-space()='']");
NodeList blankNodesList = (NodeList) emptyTextNodeXPath.evaluate(doc, XPathConstants.NODESET);
for (int j = 0; j < blankNodesList.getLength(); ++j) {
Node blankNode = blankNodesList.item(j);
blankNode.getParentNode().removeChild(blankNode);
}

XPathExpression xpathDepth = XPathFactory.newInstance().newXPath().compile("count(ancestor-or-self::*)");
XPathExpression toBeFormatted = XPathFactory.newInstance().newXPath().compile("//*[@autoformat = \'true\']/*");
NodeList textNodes = (NodeList) toBeFormatted.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < textNodes.getLength(); i++) {
Node node = textNodes.item(i);
XPathExpression toBeFormatted = XPathFactory.newInstance().newXPath().compile("//*[@autoformat = 'true']/*");

NodeList toBeFormattedNodeList = (NodeList) toBeFormatted.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < toBeFormattedNodeList.getLength(); i++) {
Node node = toBeFormattedNodeList.item(i);

String content = node.getTextContent();
double doubleDepth = (Double) xpathDepth.evaluate(textNodes.item(i), XPathConstants.NUMBER);
double doubleDepth = (Double) xpathDepth.evaluate(toBeFormattedNodeList.item(i), XPathConstants.NUMBER);
int depth = (int) doubleDepth;
String emptyString = createEmptyString(depth);
String newContent = content.replaceAll("\n", ("\n" + emptyString));
Expand All @@ -84,7 +140,6 @@ public static String prettyPrintXML(String input) throws TransformerConfiguratio
node.setTextContent(newContent);
Element element = (Element) node.getParentNode();
element.removeAttribute("autoformat");

}
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,55 @@ public void testPrettyPrintXML() throws Exception {
String result = XMLPrettyPrinter.prettyPrintXML(input);
assertEquals(expected.trim(), result.trim());
}

/**
* Test of prettyPrintXML method, of class XMLPrettyPrinter.
*/
@Test
public void testPrettyPrintXMLNoAutoformat() throws Exception {
String input = "<modifiableByteArray>\n" + " <originalValue>\n"
+ "FF 01 02 03 FF 01 02 03 FF 01 02 03 FF 01 02 03\n" + "FF 01 02 03\n" + "</originalValue>\n"
+ "</modifiableByteArray>";
String result = XMLPrettyPrinter.prettyPrintXML(input);
assertEquals(input.trim(), result.trim());
}

/**
* Test of prettyPrintXML method, of class XMLPrettyPrinter.
*/
@Test
public void testPrettyPrintXMLWhitespaceOnlyNodes() throws Exception {
String input = "<modifiableByteArray autoformat=\"true\">\n" + " <originalValue>\n\n\n"
+ " </originalValue>\n" + "</modifiableByteArray>";
String expected = "<modifiableByteArray>\n" + " <originalValue/>\n" + "</modifiableByteArray>";
String result = XMLPrettyPrinter.prettyPrintXML(input);
assertEquals(result.trim(), expected.trim());
}

/**
* Test of prettyPrintXML method, of class XMLPrettyPrinter.
*/
@Test
public void testPrettyPrintXMLWhitespaceOnlyNodesNoAutoformat() throws Exception {
String input = "<modifiableByteArray>\n" + " <originalValue>\n\n\n" + " </originalValue>\n"
+ "</modifiableByteArray>";
String expected = "<modifiableByteArray>\n" + " <originalValue/>\n" + "</modifiableByteArray>";
String result = XMLPrettyPrinter.prettyPrintXML(input);
assertEquals(result.trim(), expected.trim());
}

/**
* Test of prettyPrintXML method, of class XMLPrettyPrinter.
*/
@Test
public void testPrettyPrintXMLMultiInheritance() throws Exception {
String input = "<modifiableByteArray autoformat=\"true\">\n" + " <originalValue><foo><bar><baz>\n"
+ "FF 01 02 03 FF 01 02 03 FF 01 02 03 FF 01 02 03\n" + "FF 01 02 03\n"
+ "</baz></bar></foo></originalValue>\n" + "</modifiableByteArray>";
String expected = "" + "<modifiableByteArray>\n" + " <originalValue>\n"
+ " FF 01 02 03 FF 01 02 03 FF 01 02 03 FF 01 02 03\n" + " FF 01 02 03\n"
+ " </originalValue>\n" + "</modifiableByteArray>";
String result = XMLPrettyPrinter.prettyPrintXML(input);
assertEquals(expected.trim(), result.trim());
}
}