Permalink
Browse files

initial pass at unit testing

  • Loading branch information...
1 parent 7d1fd77 commit d5879e1ec74e0f09a8cef743453d7695967a1982 @joakime committed Feb 1, 2012
@@ -3,6 +3,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
+import java.io.Writer;
import java.util.Comparator;
import javax.xml.parsers.DocumentBuilder;
@@ -43,18 +44,30 @@ public void setAttributeSorter(Comparator<String> attributeSorter)
public void writePretty(File xmlFile, Document doc) throws IOException
{
FileWriter writer = null;
- XmlPrettyWriter pretty = null;
try
{
System.out.printf("Writing XML: %s%n",xmlFile);
writer = new FileWriter(xmlFile);
+ writePretty(writer,doc);
+ }
+ finally
+ {
+ IO.close(writer);
+ }
+ }
+
+ public void writePretty(Writer writer, Document doc)
+ {
+ XmlPrettyWriter pretty = null;
+ try
+ {
pretty = new XmlPrettyWriter(writer);
+ pretty.setAttributeSorter(attributeSorter);
pretty.write(doc);
}
finally
{
IO.close(pretty);
- IO.close(writer);
}
}
}
@@ -19,14 +19,12 @@
import org.w3c.dom.Text;
import com.erdfelt.maven.xmlfresh.util.BasicAttrComparator;
-import com.erdfelt.maven.xmlfresh.util.WeightedAttrComparator;
public class XmlPrettyWriter implements Closeable
{
private Comparator<String> attributeSorter;
private int level = 0;
private PrintWriter out;
- private boolean wroteText = false;
public XmlPrettyWriter(Writer writer)
{
@@ -81,7 +79,7 @@ private CharSequence escapeAttrValue(String rawtext)
private Map<String, String> getAttrMap(Element elem)
{
- Map<String, String> attrmap = new TreeMap<String, String>(WeightedAttrComparator.INSTANCE);
+ Map<String, String> attrmap = new TreeMap<String, String>(attributeSorter);
NamedNodeMap nnm = elem.getAttributes();
int len = nnm.getLength();
@@ -137,15 +135,17 @@ public void write(Document doc)
out.println(); // last EOL
}
- private void writeComment(Comment node)
+ private boolean writeComment(Comment node)
{
String rawtext = node.getNodeValue();
if (StringUtils.isNotBlank(rawtext))
{
outNewLine();
out.printf("<!--%s-->",rawtext);
- wroteText = true;
+ return true;
}
+
+ return false;
}
private void writeElement(Element elem)
@@ -172,14 +172,15 @@ private void writeElement(Element elem)
int length = nodes.getLength();
if (length > 0)
{
+ boolean hasOutput = false;
out.printf(">");
level++;
for (int i = 0; i < length; i++)
{
- writeNode(nodes.item(i));
+ hasOutput ^= writeNode(nodes.item(i));
}
level--;
- if (!wroteText)
+ if (hasOutput)
{
outNewLine();
}
@@ -191,31 +192,32 @@ private void writeElement(Element elem)
}
}
- private void writeNode(Node node)
+ private boolean writeNode(Node node)
{
- wroteText = false;
switch (node.getNodeType())
{
case Node.ELEMENT_NODE:
writeElement((Element)node);
- break;
+ return true;
case Node.TEXT_NODE:
- writeText((Text)node);
- break;
+ return writeText((Text)node);
case Node.COMMENT_NODE:
- writeComment((Comment)node);
- break;
+ return writeComment((Comment)node);
}
+
+ return false;
}
- private void writeText(Text node)
+ private boolean writeText(Text node)
{
- String rawtext = node.getNodeValue();
+ String rawtext = node.getNodeValue().trim();
if (StringUtils.isNotBlank(rawtext))
{
outXmlEncoded(rawtext);
- wroteText = true;
+ return true;
}
+
+ return false;
}
private void writeXmlDeclaration(Document doc)
@@ -0,0 +1,37 @@
+package com.erdfelt.maven.xmlfresh;
+
+import java.io.File;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
+import org.junit.Test;
+import org.w3c.dom.Document;
+
+import com.erdfelt.maven.xmlfresh.util.WeightedAttrComparator;
+
+public class XmlFormatterTest
+{
+ @Test
+ public void testFormatSample() throws Exception
+ {
+ String formatted = formatXml("sample-raw.xml");
+ System.out.println("## formatted\n" + formatted);
+ }
+
+ private String formatXml(String filename) throws Exception
+ {
+ File inputXml = MavenTestingUtils.getTestResourceFile(filename);
+ XmlFormatter formatter = new XmlFormatter();
+ WeightedAttrComparator weightedSorter = new WeightedAttrComparator();
+ List<String> importantNames = new ArrayList<String>();
+ importantNames.add("id");
+ weightedSorter.setImportantNames(importantNames);
+ formatter.setAttributeSorter(weightedSorter);
+ Document doc = formatter.read(inputXml);
+ StringWriter writer = new StringWriter();
+ formatter.writePretty(writer,doc);
+ return writer.toString();
+ }
+}
@@ -1,6 +1,14 @@
<?xml version="1.0"?>
<fruits>
<fruit color="red" id="apple">apple</fruit>
- <fruit color="green" id="pear">pear</fruit>
+ <fruit color="green" id="pear"> pear</fruit>
<fruit color="yellow" type="cavendish" id="banana">banana</fruit>
+ <fruit>cherry</fruit>
+ <fruit>
+
+
+ </fruit>
+ <fruit color="clear">
+ </fruit>
+ <fruit id="pomegranate"><seeds /></fruit>
</fruits>

0 comments on commit d5879e1

Please sign in to comment.