Skip to content

Commit ec892b2

Browse files
Merge pull request #43 from RUB-NDS/java-11-compatibility
Java 11 compatibility
2 parents 7e275dd + 39e5a17 commit ec892b2

File tree

5 files changed

+182
-28
lines changed

5 files changed

+182
-28
lines changed

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
language: java
2-
dist: trusty
2+
dist: bionic
33
sudo: false
44

55
jdk:
6-
- oraclejdk8
7-
- openjdk7
6+
# - oraclejdk8 # deactivated b/c travis does not support this version in any distro > trusty
7+
- oraclejdk11
88
- openjdk8
9+
- openjdk11
910

1011
#branches:
1112
# only:

nb-configuration.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
<de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>true</de-markiewb-netbeans-plugins-eclipse-formatter.eclipseFormatterEnabled>
2323
<de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>false</de-markiewb-netbeans-plugins-eclipse-formatter.enableFormatAsSaveAction>
2424
<de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>true</de-markiewb-netbeans-plugins-eclipse-formatter.useProjectSettings>
25-
<netbeans.hint.jdkPlatform>JDK_1.7</netbeans.hint.jdkPlatform>
25+
<netbeans.hint.jdkPlatform>JDK_1.8</netbeans.hint.jdkPlatform>
2626
</properties>
2727
</project-shared-configuration>

pom.xml

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,37 @@
6666
<dependency>
6767
<groupId>junit</groupId>
6868
<artifactId>junit</artifactId>
69-
<version>4.12</version>
69+
<version>4.13.1</version>
7070
<scope>test</scope>
7171
</dependency>
7272
<dependency>
7373
<groupId>org.apache.logging.log4j</groupId>
7474
<artifactId>log4j-api</artifactId>
75-
<version>2.13.2</version>
75+
<version>2.13.3</version>
7676
</dependency>
7777
<dependency>
7878
<groupId>org.apache.logging.log4j</groupId>
7979
<artifactId>log4j-core</artifactId>
80-
<version>2.13.2</version>
80+
<version>2.13.3</version>
81+
</dependency>
82+
<dependency>
83+
<groupId>javax.xml.bind</groupId>
84+
<artifactId>jaxb-api</artifactId>
85+
<version>2.3.1</version>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.glassfish.jaxb</groupId>
89+
<artifactId>jaxb-runtime</artifactId>
90+
<version>2.3.3</version>
8191
</dependency>
8292
</dependencies>
8393
<build>
8494
<plugins>
95+
<plugin>
96+
<groupId>org.codehaus.mojo</groupId>
97+
<artifactId>versions-maven-plugin</artifactId>
98+
<version>2.8.1</version>
99+
</plugin>
85100
<plugin>
86101
<groupId>net.revelc.code</groupId>
87102
<artifactId>formatter-maven-plugin</artifactId>
@@ -100,6 +115,29 @@
100115
</executions>
101116
</plugin>
102117
<plugin>
118+
<groupId>org.apache.maven.plugins</groupId>
119+
<artifactId>maven-enforcer-plugin</artifactId>
120+
<version>3.0.0-M3</version>
121+
<executions>
122+
<execution>
123+
<id>default-cli</id>
124+
<goals>
125+
<goal>enforce</goal>
126+
</goals>
127+
<configuration>
128+
<rules>
129+
<requireMavenVersion>
130+
<version>3.1</version>
131+
</requireMavenVersion>
132+
<requireJavaVersion>
133+
<version>1.8,11</version>
134+
</requireJavaVersion>
135+
</rules>
136+
</configuration>
137+
</execution>
138+
</executions>
139+
</plugin>
140+
<plugin>
103141
<groupId>org.apache.maven.plugins</groupId>
104142
<artifactId>maven-source-plugin</artifactId>
105143
<version>3.0.1</version>
@@ -115,15 +153,18 @@
115153
<plugin>
116154
<groupId>org.apache.maven.plugins</groupId>
117155
<artifactId>maven-javadoc-plugin</artifactId>
118-
<version>2.10.4</version>
156+
<version>3.2.0</version>
157+
<configuration>
158+
<source>8</source>
159+
</configuration>
119160
<executions>
120161
<execution>
121162
<id>attach-javadocs</id>
122163
<goals>
123164
<goal>jar</goal>
124165
</goals>
125-
</execution>
126-
</executions>
166+
</execution>
167+
</executions>
127168
</plugin>
128169
<plugin>
129170
<groupId>org.apache.maven.plugins</groupId>
@@ -154,14 +195,20 @@
154195
<autoReleaseAfterClose>false</autoReleaseAfterClose>
155196
</configuration>
156197
</plugin>
198+
<plugin>
199+
<groupId>org.apache.maven.plugins</groupId>
200+
<artifactId>maven-compiler-plugin</artifactId>
201+
<configuration>
202+
<source>8</source>
203+
<target>8</target>
204+
</configuration>
205+
</plugin>
157206
</plugins>
158207
</build>
159208

160209
<properties>
161210
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
162211
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
163-
<maven.compiler.source>1.7</maven.compiler.source>
164-
<maven.compiler.target>1.7</maven.compiler.target>
165212
<!-- We redefine the signature generation process, whic is enabled by default,
166213
but cannot be performed by typical users. Enable it using -Dskip.signature=false -->
167214
<skip.signature>true</skip.signature>

src/main/java/de/rub/nds/modifiablevariable/util/XMLPrettyPrinter.java

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
import javax.xml.transform.TransformerFactory;
2222
import javax.xml.transform.dom.DOMSource;
2323
import javax.xml.transform.stream.StreamResult;
24-
import javax.xml.xpath.XPathConstants;
25-
import javax.xml.xpath.XPathExpression;
26-
import javax.xml.xpath.XPathExpressionException;
27-
import javax.xml.xpath.XPathFactory;
28-
import javax.xml.xpath.XPathFactoryConfigurationException;
24+
import javax.xml.xpath.*;
25+
2926
import org.w3c.dom.Document;
3027
import org.w3c.dom.Element;
3128
import org.w3c.dom.NamedNodeMap;
@@ -44,8 +41,56 @@ public class XMLPrettyPrinter {
4441
public static int IDENT_AMOUNT = 4;
4542

4643
/**
47-
* This function formats all tags (and their children) which are marked with
48-
* the autoformat="true" attribute and removes this attribute.
44+
* This functions autoformats the text content of the child nodes of the
45+
* node which have the {@code autoformat="true"} attribute set. The
46+
* autoformatting results in having the textContent of the node aligned to
47+
* the depth of indentation of its parent node. The
48+
* {@code autoformat="true"} is also removed from the tree.
49+
*
50+
* <br>
51+
* <br>
52+
*
53+
* <b>Note:</b> the function also removes all blank text from nodes which
54+
* may merge {@code <a></a>} into {@code <a/>}.
55+
*
56+
* <br>
57+
* <br>
58+
*
59+
* <b>Caution:</b> the function literally takes the textContent of the child
60+
* nodes of the autoformat node. This removes any other nodes from that
61+
* child node and leaves only the textContent left.
62+
*
63+
* <br>
64+
* <br>
65+
*
66+
* <h3>Example</h3>
67+
*
68+
* Using this XML as input:
69+
*
70+
* <pre>
71+
* {@code
72+
* <root autoformat="true">
73+
* <textnode>
74+
* this text should
75+
* be aligned
76+
* </textnode>
77+
* </root>
78+
* }
79+
* </pre>
80+
*
81+
* Results in this output XML:
82+
*
83+
* <pre>
84+
* {@code
85+
* <root>
86+
* <textnode>
87+
* this text sould
88+
* be aligned
89+
* </textnode>
90+
* </root>
91+
* }
92+
* </pre>
93+
*
4994
*
5095
* @param input
5196
* @return
@@ -55,25 +100,36 @@ public class XMLPrettyPrinter {
55100
* @throws IOException
56101
* @throws TransformerException
57102
* @throws XPathExpressionException
58-
* @throws XPathFactoryConfigurationException
59103
*/
60104
public static String prettyPrintXML(String input) throws TransformerConfigurationException,
61-
ParserConfigurationException, SAXException, IOException, TransformerException, XPathExpressionException,
62-
XPathFactoryConfigurationException {
105+
ParserConfigurationException, SAXException, IOException, TransformerException, XPathExpressionException {
63106
Transformer transformer = TransformerFactory.newInstance().newTransformer();
64107
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
65108
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(IDENT_AMOUNT));
66109
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
67110
StreamResult result = new StreamResult(new StringWriter());
68111
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
69112
.parse(new InputSource(new StringReader(input)));
113+
114+
// clear out blank text nodes otherwise the formatter will preserver
115+
// them and add additional newlinesq
116+
XPathExpression emptyTextNodeXPath = XPathFactory.newInstance().newXPath()
117+
.compile("//text()[normalize-space()='']");
118+
NodeList blankNodesList = (NodeList) emptyTextNodeXPath.evaluate(doc, XPathConstants.NODESET);
119+
for (int j = 0; j < blankNodesList.getLength(); ++j) {
120+
Node blankNode = blankNodesList.item(j);
121+
blankNode.getParentNode().removeChild(blankNode);
122+
}
123+
70124
XPathExpression xpathDepth = XPathFactory.newInstance().newXPath().compile("count(ancestor-or-self::*)");
71-
XPathExpression toBeFormatted = XPathFactory.newInstance().newXPath().compile("//*[@autoformat = \'true\']/*");
72-
NodeList textNodes = (NodeList) toBeFormatted.evaluate(doc, XPathConstants.NODESET);
73-
for (int i = 0; i < textNodes.getLength(); i++) {
74-
Node node = textNodes.item(i);
125+
XPathExpression toBeFormatted = XPathFactory.newInstance().newXPath().compile("//*[@autoformat = 'true']/*");
126+
127+
NodeList toBeFormattedNodeList = (NodeList) toBeFormatted.evaluate(doc, XPathConstants.NODESET);
128+
for (int i = 0; i < toBeFormattedNodeList.getLength(); i++) {
129+
Node node = toBeFormattedNodeList.item(i);
130+
75131
String content = node.getTextContent();
76-
double doubleDepth = (Double) xpathDepth.evaluate(textNodes.item(i), XPathConstants.NUMBER);
132+
double doubleDepth = (Double) xpathDepth.evaluate(toBeFormattedNodeList.item(i), XPathConstants.NUMBER);
77133
int depth = (int) doubleDepth;
78134
String emptyString = createEmptyString(depth);
79135
String newContent = content.replaceAll("\n", ("\n" + emptyString));
@@ -84,7 +140,6 @@ public static String prettyPrintXML(String input) throws TransformerConfiguratio
84140
node.setTextContent(newContent);
85141
Element element = (Element) node.getParentNode();
86142
element.removeAttribute("autoformat");
87-
88143
}
89144
DOMSource source = new DOMSource(doc);
90145
transformer.transform(source, result);

src/test/java/de/rub/nds/modifiablevariable/util/XMLPrettyPrinterTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,55 @@ public void testPrettyPrintXML() throws Exception {
3030
String result = XMLPrettyPrinter.prettyPrintXML(input);
3131
assertEquals(expected.trim(), result.trim());
3232
}
33+
34+
/**
35+
* Test of prettyPrintXML method, of class XMLPrettyPrinter.
36+
*/
37+
@Test
38+
public void testPrettyPrintXMLNoAutoformat() throws Exception {
39+
String input = "<modifiableByteArray>\n" + " <originalValue>\n"
40+
+ "FF 01 02 03 FF 01 02 03 FF 01 02 03 FF 01 02 03\n" + "FF 01 02 03\n" + "</originalValue>\n"
41+
+ "</modifiableByteArray>";
42+
String result = XMLPrettyPrinter.prettyPrintXML(input);
43+
assertEquals(input.trim(), result.trim());
44+
}
45+
46+
/**
47+
* Test of prettyPrintXML method, of class XMLPrettyPrinter.
48+
*/
49+
@Test
50+
public void testPrettyPrintXMLWhitespaceOnlyNodes() throws Exception {
51+
String input = "<modifiableByteArray autoformat=\"true\">\n" + " <originalValue>\n\n\n"
52+
+ " </originalValue>\n" + "</modifiableByteArray>";
53+
String expected = "<modifiableByteArray>\n" + " <originalValue/>\n" + "</modifiableByteArray>";
54+
String result = XMLPrettyPrinter.prettyPrintXML(input);
55+
assertEquals(result.trim(), expected.trim());
56+
}
57+
58+
/**
59+
* Test of prettyPrintXML method, of class XMLPrettyPrinter.
60+
*/
61+
@Test
62+
public void testPrettyPrintXMLWhitespaceOnlyNodesNoAutoformat() throws Exception {
63+
String input = "<modifiableByteArray>\n" + " <originalValue>\n\n\n" + " </originalValue>\n"
64+
+ "</modifiableByteArray>";
65+
String expected = "<modifiableByteArray>\n" + " <originalValue/>\n" + "</modifiableByteArray>";
66+
String result = XMLPrettyPrinter.prettyPrintXML(input);
67+
assertEquals(result.trim(), expected.trim());
68+
}
69+
70+
/**
71+
* Test of prettyPrintXML method, of class XMLPrettyPrinter.
72+
*/
73+
@Test
74+
public void testPrettyPrintXMLMultiInheritance() throws Exception {
75+
String input = "<modifiableByteArray autoformat=\"true\">\n" + " <originalValue><foo><bar><baz>\n"
76+
+ "FF 01 02 03 FF 01 02 03 FF 01 02 03 FF 01 02 03\n" + "FF 01 02 03\n"
77+
+ "</baz></bar></foo></originalValue>\n" + "</modifiableByteArray>";
78+
String expected = "" + "<modifiableByteArray>\n" + " <originalValue>\n"
79+
+ " FF 01 02 03 FF 01 02 03 FF 01 02 03 FF 01 02 03\n" + " FF 01 02 03\n"
80+
+ " </originalValue>\n" + "</modifiableByteArray>";
81+
String result = XMLPrettyPrinter.prettyPrintXML(input);
82+
assertEquals(expected.trim(), result.trim());
83+
}
3384
}

0 commit comments

Comments
 (0)