Skip to content

Commit

Permalink
Pulled XmlNodePrinter out of the XmlTemplateEngine.
Browse files Browse the repository at this point in the history
Removed test cases from Eclises '.classpath'.


git-svn-id: http://svn.codehaus.org/groovy/trunk/groovy/groovy-core@2542 a5544e8c-8a19-0410-ba12-f9af4593a198
  • Loading branch information
Christian Stein committed Jul 13, 2005
1 parent f7ce7a5 commit c7ee606
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 161 deletions.
2 changes: 0 additions & 2 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main"/>
<classpathentry kind="src" path="src/test"/>
<classpathentry kind="lib" path="target/test-classes"/>
<classpathentry kind="var" path="MAVEN_REPO/ant/jars/ant-1.6.2.jar"/>
<classpathentry kind="var" path="MAVEN_REPO/ant/jars/ant-junit-1.6.2.jar"/>
<classpathentry kind="var" path="MAVEN_REPO/ant/jars/ant-launcher-1.6.2.jar"/>
Expand Down
179 changes: 20 additions & 159 deletions src/main/groovy/text/XmlTemplateEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
import groovy.lang.Writable;
import groovy.util.IndentPrinter;
import groovy.util.Node;
import groovy.util.XmlNodePrinter;
import groovy.util.XmlParser;
import groovy.xml.QName;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
Expand All @@ -34,99 +32,14 @@
*/
public class XmlTemplateEngine extends TemplateEngine {

private static class XmlPrinter {
private static class GspPrinter extends XmlNodePrinter {

protected final IndentPrinter out;

public XmlPrinter() {
this(new IndentPrinter(new PrintWriter(new OutputStreamWriter(System.out))));
}

public XmlPrinter(PrintWriter out, String indent) {
public GspPrinter(PrintWriter out, String indent) {
this(new IndentPrinter(out, indent));
}

public XmlPrinter(IndentPrinter out) {
if (out == null) {
throw new IllegalArgumentException("Argument 'IndentPrinter out' must not be null!");
}
this.out = out;
}

public String getNameOfNode(Node node) {
if (node == null) {
throw new IllegalArgumentException("Node must not be null!");
}
Object name = node.name();
if (name instanceof QName) {
QName qname = (QName) name;
return /*qname.getPrefix() + ":" +*/qname.getLocalPart();
}
return name.toString();
}

public boolean isEmptyElement(Node node) {
if (node == null) {
throw new IllegalArgumentException("Node must not be null!");
}
if (!node.children().isEmpty()) {
return false;
}
String text = node.text();
if (text.length() > 0) {
return false;
}
return true;
}

public void print(Node node) {
/*
* Handle empty elements like '<br/>', '<img/> or '<hr noshade="noshade"/>.
*/
if (isEmptyElement(node)) {
printLineBegin();
out.print("<");
out.print(getNameOfNode(node));
printNameAttributes(node.attributes());
out.print("/>");
printLineEnd("empty element"); // "node named '" + node.name() + "'"
return;
}

/*
* Handle GSP tag element!
*/
Object name = node.name();
if (name != null && name instanceof QName) {
/*
* FIXME Somethings wrong with the SAX- or XMLParser. Prefix should only contain 'gsp'?!
*/
String s = ((QName) name).getPrefix();
if (s.startsWith("gsp:")) {
s = s.substring(4); // 4 = "gsp:".length()
if (s.length() == 0) {
throw new RuntimeException("No local part after 'gsp:' given in node " + node);
}
printGroovyTag(s, node.text());
return;
}
}

/*
* Handle normal element like <html> ... </html>.
*/
Object value = node.value();
if (value instanceof List) {
printName(node, true);
printList((List) value);
printName(node, false);
return;
}

/*
* Still here?!
*/
throw new RuntimeException("Unsupported node value: " + node.value());
public GspPrinter(IndentPrinter out) {
super(out, "\\\"");
}

protected void printGroovyTag(String tag, String text) {
Expand All @@ -151,10 +64,6 @@ protected void printLineBegin() {
out.printIndent();
}

protected void printLineEnd() {
printLineEnd(null);
}

protected void printLineEnd(String comment) {
out.print("\\n\");");
if (comment != null) {
Expand All @@ -164,72 +73,23 @@ protected void printLineEnd(String comment) {
out.print("\n");
}

protected void printList(List list) {
out.incrementIndent();
for (Iterator iter = list.iterator(); iter.hasNext();) {
Object value = iter.next();
/*
* If the current value is a node, recurse into that node.
*/
if (value instanceof Node) {
print((Node) value);
continue;
}
protected boolean printSpecialNode(Node node) {
Object name = node.name();
if (name != null && name instanceof QName) {
/*
* Print out "simple" text nodes.
* FIXME Somethings wrong with the SAX- or XMLParser. Prefix should only contain 'gsp'?!
*/
printLineBegin();
out.print(InvokerHelper.toString(value));
printLineEnd();
}
out.decrementIndent();
}

protected void printName(Node node, boolean begin) {
if (node == null) {
throw new NullPointerException("Node must not be null.");
}
Object name = node.name();
if (name == null) {
throw new NullPointerException("Name must not be null.");
}
printLineBegin();
out.print("<");
if (!begin) {
out.print("/");
}
out.print(getNameOfNode(node));
if (begin) {
printNameAttributes(node.attributes());
}
out.print(">");
printLineEnd();
}

protected void printNameAttributes(Map attributes) {
if (attributes == null || attributes.isEmpty()) {
return;
}
out.print(" ");
boolean first = true;
for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
if (first) {
first = false;
} else {
out.print(" ");
}
out.print(entry.getKey().toString());
out.print("=");
Object value = entry.getValue();
if (value instanceof String) {
out.print("\\\"");
out.print((String) value);
out.print("\\\"");
continue;
String s = ((QName) name).getPrefix();
if (s.startsWith("gsp:")) {
s = s.substring(4); // 4 = "gsp:".length()
if (s.length() == 0) {
throw new RuntimeException("No local part after 'gsp:' given in node " + node);
}
printGroovyTag(s, node.text());
return true;
}
out.print(InvokerHelper.toString(value));
}
return false;
}

}
Expand Down Expand Up @@ -320,10 +180,11 @@ public Template createTemplate(Reader reader) throws CompilationFailedException,
}

// new NodePrinter().print(root);
new XmlNodePrinter().print(root);

StringWriter writer = new StringWriter(1024);
writer.write("/* Generated by XmlTemplateEngine */\n");
new XmlPrinter(new PrintWriter(writer), DEFAULT_INDENTION).print(root);
new GspPrinter(new PrintWriter(writer), DEFAULT_INDENTION).print(root);
String scriptText = writer.toString();

// System.err.println("\n-\n" + scriptText + "\n-\n");
Expand Down
Loading

0 comments on commit c7ee606

Please sign in to comment.