Skip to content

Commit a1324cc

Browse files
committed
Fixed xml prettyprinting issue and introduced autoformat attribute
1 parent 9e16c08 commit a1324cc

File tree

5 files changed

+81
-45
lines changed

5 files changed

+81
-45
lines changed

src/main/java/de/rub/nds/modifiablevariable/ModifiableVariable.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,25 @@
1010

1111
import java.io.Serializable;
1212
import javax.xml.bind.annotation.XmlAnyElement;
13+
import javax.xml.bind.annotation.XmlAttribute;
1314
import javax.xml.bind.annotation.XmlRootElement;
1415
import javax.xml.bind.annotation.XmlTransient;
1516

1617
/**
1718
* The base abstract class for modifiable variables, including the getValue
1819
* function.
19-
*
20+
*
2021
* The class needs to be defined transient to allow propOrder definition in
2122
* subclasses, see:
2223
* http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html
23-
*
24+
*
2425
*/
2526
@XmlRootElement
2627
@XmlTransient
2728
public abstract class ModifiableVariable<E> implements Serializable {
2829

30+
protected Boolean autoformat = null;
31+
2932
private VariableModification<E> modification = null;
3033

3134
private boolean createRandomModification;
@@ -36,6 +39,15 @@ public ModifiableVariable() {
3639

3740
}
3841

42+
@XmlAttribute(required = false)
43+
public Boolean getAutoformat() {
44+
return autoformat;
45+
}
46+
47+
public void setAutoformat(Boolean autoformat) {
48+
this.autoformat = autoformat;
49+
}
50+
3951
public void setModification(VariableModification<E> modification) {
4052
this.modification = modification;
4153
}

src/main/java/de/rub/nds/modifiablevariable/bytearray/ModifiableByteArray.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
@XmlType(propOrder = { "originalValue", "modification", "assertEquals" })
2727
public class ModifiableByteArray extends ModifiableVariable<byte[]> implements Serializable {
2828

29+
public ModifiableByteArray() {
30+
autoformat = true;
31+
}
32+
2933
private byte[] originalValue;
3034

3135
@Override
@@ -88,10 +92,12 @@ public String toString() {
8892

8993
@Override
9094
public boolean equals(Object o) {
91-
if (this == o)
95+
if (this == o) {
9296
return true;
93-
if (!(o instanceof ModifiableByteArray))
97+
}
98+
if (!(o instanceof ModifiableByteArray)) {
9499
return false;
100+
}
95101

96102
ModifiableByteArray that = (ModifiableByteArray) o;
97103

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import javax.xml.xpath.XPathFactory;
2828
import javax.xml.xpath.XPathFactoryConfigurationException;
2929
import org.w3c.dom.Document;
30+
import org.w3c.dom.Element;
31+
import org.w3c.dom.NamedNodeMap;
32+
import org.w3c.dom.Node;
3033
import org.w3c.dom.NodeList;
3134
import org.xml.sax.InputSource;
3235
import org.xml.sax.SAXException;
@@ -40,6 +43,20 @@ public class XMLPrettyPrinter {
4043

4144
public static int IDENT_AMOUNT = 4;
4245

46+
/**
47+
* This function formats all tags (and their children) which are marked with
48+
* the autoformat="true" attribute and removes this attribute.
49+
*
50+
* @param input
51+
* @return
52+
* @throws TransformerConfigurationException
53+
* @throws ParserConfigurationException
54+
* @throws SAXException
55+
* @throws IOException
56+
* @throws TransformerException
57+
* @throws XPathExpressionException
58+
* @throws XPathFactoryConfigurationException
59+
*/
4360
public static String prettyPrintXML(String input) throws TransformerConfigurationException,
4461
ParserConfigurationException, SAXException, IOException, TransformerException, XPathExpressionException,
4562
XPathFactoryConfigurationException {
@@ -50,24 +67,25 @@ public static String prettyPrintXML(String input) throws TransformerConfiguratio
5067
StreamResult result = new StreamResult(new StringWriter());
5168
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
5269
.parse(new InputSource(new StringReader(input)));
53-
XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("//*[count(./*) = 0]");
5470
XPathExpression xpathDepth = XPathFactory.newInstance().newXPath().compile("count(ancestor-or-self::*)");
55-
56-
NodeList textNodes = (NodeList) xpath.evaluate(doc, XPathConstants.NODESET);
57-
71+
XPathExpression toBeFormatted = XPathFactory.newInstance().newXPath().compile("//*[@autoformat = \'true\']/*");
72+
NodeList textNodes = (NodeList) toBeFormatted.evaluate(doc, XPathConstants.NODESET);
5873
for (int i = 0; i < textNodes.getLength(); i++) {
59-
String content = textNodes.item(i).getTextContent();
74+
Node node = textNodes.item(i);
75+
String content = node.getTextContent();
6076
double doubleDepth = (Double) xpathDepth.evaluate(textNodes.item(i), XPathConstants.NUMBER);
6177
int depth = (int) doubleDepth;
6278
String emptyString = createEmptyString(depth);
6379
String newContent = content.replaceAll("\n", ("\n" + emptyString));
64-
if (newContent.length() > content.length()) {
80+
if (newContent.length() > content.length()
81+
&& newContent.substring(newContent.length() - IDENT_AMOUNT, newContent.length()).trim().equals("")) {
6582
newContent = newContent.substring(0, newContent.length() - IDENT_AMOUNT);
6683
}
67-
textNodes.item(i).setTextContent(newContent);
84+
node.setTextContent(newContent);
85+
Element element = (Element) node.getParentNode();
86+
element.removeAttribute("autoformat");
6887

6988
}
70-
7189
DOMSource source = new DOMSource(doc);
7290
transformer.transform(source, result);
7391
return result.getWriter().toString();

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public void testLongToUint32Bytes() {
4141
public void testIntToBytes() {
4242
int toParse = 5717;
4343
byte[] result = ArrayConverter.intToBytes(toParse, 2);
44-
assertArrayEquals("The conversion result of 5717 should be {0x16} {0x55}", new byte[]{0x16, 0x55}, result);
44+
assertArrayEquals("The conversion result of 5717 should be {0x16} {0x55}", new byte[] { 0x16, 0x55 }, result);
4545
}
4646

4747
/**
4848
* Test of bytesToInt method, of class ArrayConverter.
4949
*/
5050
@Test
5151
public void testBytesToInt() {
52-
byte[] toParse = {0x16, 0x55};
52+
byte[] toParse = { 0x16, 0x55 };
5353
int expectedResult = 5717;
5454
assertEquals("The conversion result of {0x16, 0x55} should be 5717", expectedResult,
5555
ArrayConverter.bytesToInt(toParse));
@@ -75,17 +75,17 @@ public void testBytesToLong() {
7575
*/
7676
@Test
7777
public void testBytesToHexString_byteArr() {
78-
byte[] toTest = new byte[]{0x00, 0x11, 0x22, 0x33, 0x44};
78+
byte[] toTest = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44 };
7979
assertEquals("00 11 22 33 44", ArrayConverter.bytesToHexString(toTest));
80-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
80+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
8181
assertEquals("00 01 02 03 04 05 06 07 08", ArrayConverter.bytesToHexString(toTest));
82-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
82+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 };
8383
assertEquals("00 01 02 03 04 05 06 07 08 09 10", ArrayConverter.bytesToHexString(toTest));
84-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
85-
0x07,};
84+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
85+
0x07, };
8686
assertEquals("\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07", ArrayConverter.bytesToHexString(toTest));
87-
toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
88-
0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,};
87+
toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
88+
0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, };
8989
assertEquals(
9090
"\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07",
9191
ArrayConverter.bytesToHexString(toTest));
@@ -96,9 +96,9 @@ public void testBytesToHexString_byteArr() {
9696
*/
9797
@Test
9898
public void testBytesToHexString_byteArr_boolean() {
99-
byte[] toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04,
100-
0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
101-
0x06, 0x07};
99+
byte[] toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04,
100+
0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
101+
0x06, 0x07 };
102102
assertEquals("00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07",
103103
ArrayConverter.bytesToHexString(toTest, false));
104104
assertEquals(
@@ -118,9 +118,9 @@ public void testBytesToHexString_3args() {
118118
*/
119119
@Test
120120
public void testBytesToRawHexString() {
121-
byte[] toTest = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04,
122-
0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
123-
0x06, 0x07};
121+
byte[] toTest = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04,
122+
0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
123+
0x06, 0x07 };
124124
assertEquals("0001020304050607000102030405060700010203040506070001020304050607",
125125
ArrayConverter.bytesToRawHexString(toTest));
126126
}
@@ -173,13 +173,13 @@ public void testConvertListToArray() {
173173
@Test
174174
public void testHexStringToByteArray() {
175175
String hex = "01";
176-
assertArrayEquals("Testing simple one byte hex value", new byte[]{0x01},
176+
assertArrayEquals("Testing simple one byte hex value", new byte[] { 0x01 },
177177
ArrayConverter.hexStringToByteArray(hex));
178178
hex = "FF";
179-
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[]{(byte) 0xff},
179+
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[] { (byte) 0xff },
180180
ArrayConverter.hexStringToByteArray(hex));
181181
hex = "FFFFFF";
182-
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff},
182+
assertArrayEquals("Testing one byte hex value > 0x7f", new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff },
183183
ArrayConverter.hexStringToByteArray(hex));
184184
}
185185

@@ -189,7 +189,7 @@ public void testBigIntegerToNullPaddedByteArray() {
189189

190190
assertArrayEquals("Check zero output size", new byte[0],
191191
ArrayConverter.bigIntegerToNullPaddedByteArray(test, 0));
192-
assertArrayEquals("Check check output size smaller than input", new byte[]{(byte) 0xEC},
192+
assertArrayEquals("Check check output size smaller than input", new byte[] { (byte) 0xEC },
193193
ArrayConverter.bigIntegerToNullPaddedByteArray(test, 1));
194194
assertArrayEquals("Check output size bigger than input size",
195195
ArrayConverter.hexStringToByteArray("0000000000000000000000001D42C86F7923DFEC"),
@@ -217,24 +217,24 @@ public void testLongToUint48Bytes() {
217217
@Test
218218
public void testBytesToHexString_ModifiableByteArray() {
219219
ModifiableByteArray toTest = new ModifiableByteArray();
220-
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x11, 0x22, 0x33, 0x44});
220+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44 });
221221
assertEquals("00 11 22 33 44", ArrayConverter.bytesToHexString(toTest));
222222

223-
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
224-
0x06, 0x07, 0x08});
223+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
224+
0x06, 0x07, 0x08 });
225225
assertEquals("00 01 02 03 04 05 06 07 08", ArrayConverter.bytesToHexString(toTest));
226226

227-
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
228-
0x06, 0x07, 0x08, 0x09, 0x10});
227+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
228+
0x06, 0x07, 0x08, 0x09, 0x10 });
229229
assertEquals("00 01 02 03 04 05 06 07 08 09 10", ArrayConverter.bytesToHexString(toTest));
230230

231-
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
232-
0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,});
231+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
232+
0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, });
233233
assertEquals("\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07", ArrayConverter.bytesToHexString(toTest));
234234

235-
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
236-
0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
237-
0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,});
235+
toTest = ModifiableVariableFactory.safelySetValue(toTest, new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
236+
0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
237+
0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, });
238238
assertEquals(
239239
"\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07\n00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07",
240240
ArrayConverter.bytesToHexString(toTest));
@@ -245,9 +245,9 @@ public void testBytesToHexString_ModifiableByteArray() {
245245
*/
246246
@Test
247247
public void testReverseByteOrder() {
248-
byte[] array = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04};
248+
byte[] array = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04 };
249249

250-
assertArrayEquals("Testing byte order reversion", new byte[]{0x04, 0x03, 0x02, 0x01, 0x00},
250+
assertArrayEquals("Testing byte order reversion", new byte[] { 0x04, 0x03, 0x02, 0x01, 0x00 },
251251
ArrayConverter.reverseByteOrder(array));
252252
}
253253

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public class XMLPrettyPrinterTest {
2121
*/
2222
@Test
2323
public void testPrettyPrintXML() throws Exception {
24-
String input = "<modifiableByteArray>\n" + " <originalValue>\n"
24+
String input = "<modifiableByteArray autoformat=\"true\">\n" + " <originalValue>\n"
2525
+ "FF 01 02 03 FF 01 02 03 FF 01 02 03 FF 01 02 03\n" + "FF 01 02 03\n" + "</originalValue>\n"
2626
+ "</modifiableByteArray>";
2727
String expected = "<modifiableByteArray>\n" + " <originalValue>\n"
2828
+ " FF 01 02 03 FF 01 02 03 FF 01 02 03 FF 01 02 03\n" + " FF 01 02 03\n"
2929
+ " </originalValue>\n" + "</modifiableByteArray>";
3030
String result = XMLPrettyPrinter.prettyPrintXML(input);
31-
31+
System.out.println(result);
3232
assertEquals(expected.trim(), result.trim());
3333
}
3434
}

0 commit comments

Comments
 (0)