dion 2004/08/26 21:37:45
Modified: jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml
CopyTag.java CopyOfTag.java
jelly/jelly-tags/xml/src/test/org/apache/commons/jelly/tags/xml
suite.jelly
Log:
Apply JELLY-28
Revision Changes Path
1.5 +15 -2
jakarta-commons/jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml/CopyTag.java
Index: CopyTag.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml/CopyTag.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- CopyTag.java 25 Feb 2004 01:31:50 -0000 1.4
+++ CopyTag.java 27 Aug 2004 04:37:45 -0000 1.5
@@ -34,6 +34,10 @@
/** The XPath expression to evaluate. */
private XPath select;
+
+ /** Should we output lexical XML data like entity names?
+ */
+ private boolean lexical;
public CopyTag() {
}
@@ -53,8 +57,14 @@
if ( node instanceof Element ) {
Element element = (Element) node;
- SAXWriter saxWriter = new SAXWriter(output, output);
-
+ SAXWriter saxWriter;
+
+ if (lexical) {
+ saxWriter = new SAXWriter(output, output);
+ } else {
+ saxWriter = new SAXWriter(output);
+ }
+
saxWriter.writeOpen(element);
invokeBody(output);
saxWriter.writeClose(element);
@@ -76,5 +86,8 @@
/** Sets the XPath expression to evaluate. */
public void setSelect(XPath select) {
this.select = select;
+ }
+ public void setLexical(boolean lexical) {
+ this.lexical = lexical;
}
}
1.5 +42 -16
jakarta-commons/jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml/CopyOfTag.java
Index: CopyOfTag.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml/CopyOfTag.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- CopyOfTag.java 25 Feb 2004 01:31:50 -0000 1.4
+++ CopyOfTag.java 27 Aug 2004 04:37:45 -0000 1.5
@@ -36,7 +36,12 @@
/** The XPath expression to evaluate. */
private XPath select;
-
+
+ /** Should we output lexical XML data like comments
+ * or entity names?
+ */
+ private boolean lexical;
+
public CopyOfTag() {
}
@@ -49,23 +54,40 @@
throw new MissingAttributeException( "select" );
}
- SAXWriter saxWriter = new SAXWriter(output, output);
- try {
- List nodes = select.selectNodes(xpathContext);
- for (Iterator iter = nodes.iterator(); iter.hasNext(); ) {
- Object object = iter.next();
- if ( object instanceof Node ) {
- saxWriter.write( (Node) object );
- }
- else if (object != null ) {
- output.write( object.toString() );
- }
- }
+ SAXWriter saxWriter;
+
+ if (lexical) {
+ saxWriter = new SAXWriter(output, output);
+ } else {
+ saxWriter = new SAXWriter(output);
}
- catch (SAXException e) {
- throw new JellyTagException(e);
+
+ Object obj;
+ try {
+ obj = select.evaluate(xpathContext);
} catch (JaxenException e) {
- throw new JellyTagException(e);
+ throw new JellyTagException("Failed to evaluate XPath expression", e);
+ }
+
+ try {
+ if (obj instanceof List) {
+ List nodes = (List) obj;
+ for (Iterator iter = nodes.iterator(); iter.hasNext(); ) {
+ Object object = iter.next();
+ if ( object instanceof Node ) {
+ saxWriter.write( (Node) object );
+ }
+ else if (object != null ) {
+ saxWriter.write( object.toString() );
+ }
+ }
+ } else if (obj instanceof Node) {
+ saxWriter.write( (Node) obj );
+ } else {
+ saxWriter.write( obj.toString() );
+ }
+ } catch (SAXException e) {
+ throw new JellyTagException("Failed to write XML output.", e);
}
}
@@ -74,5 +96,9 @@
/** Sets the XPath expression to evaluate. */
public void setSelect(XPath select) {
this.select = select;
+ }
+
+ public void setLexical(boolean lexical) {
+ this.lexical = lexical;
}
}
1.11 +31 -9
jakarta-commons/jelly/jelly-tags/xml/src/test/org/apache/commons/jelly/tags/xml/suite.jelly
Index: suite.jelly
===================================================================
RCS file:
/home/cvs/jakarta-commons/jelly/jelly-tags/xml/src/test/org/apache/commons/jelly/tags/xml/suite.jelly,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- suite.jelly 12 Aug 2004 06:45:03 -0000 1.10
+++ suite.jelly 27 Aug 2004 04:37:45 -0000 1.11
@@ -220,7 +220,7 @@
</test:case>
- <test:case name="testComment">
+ <test:case name="testCommentWithLexical">
<x:parse var="doc">
<foo>
@@ -232,13 +232,31 @@
<test:assert xpath="$doc/foo"/>
- <j:set var="t"><x:copyOf select="$doc/foo/comment()"/></j:set>
+ <j:set var="t"><x:copyOf select="$doc/foo/comment()"
lexical="true"/></j:set>
<test:assertEquals expected="<!--this is a comment-->"
actual="${t.trim()}"/>
</test:case>
+
+ <test:case name="testCommentWithoutLexical">
+
+ <x:parse var="doc">
+ <foo>
+ <!-- this will not be output -->
+
+ <x:comment text="this is a comment"/>
+ </foo>
+ </x:parse>
+
+ <test:assert xpath="$doc/foo"/>
- <test:case name="testCommentWithTextAttribute">
+ <j:set var="t"><x:copyOf select="string($doc/foo/comment())"
lexical="true"/></j:set>
+
+ <test:assertEquals expected="this is a comment" actual="${t.trim()}"/>
+
+ </test:case>
+
+ <test:case name="testCommentWithTextAttributeWithLexical">
<x:parse var="doc">
<foo>
@@ -250,7 +268,7 @@
<test:assert xpath="$doc/foo"/>
- <j:set var="t"><x:copyOf select="$doc/foo/comment()"/></j:set>
+ <j:set var="t"><x:copyOf select="$doc/foo/comment()"
lexical="true"/></j:set>
<test:assertEquals expected="<!--this is a comment-->"
actual="${t.trim()}"/>
</test:case>
@@ -278,13 +296,17 @@
<j:set var="blopSingleText"><x:expr select="$blopSingle/text()"/></j:set>
<test:assertEquals actual="${blopSingleText}" expected="blop0"/>
</test:case>
-
- <!-- dom4j is busted -->
+
<test:case name="testEntities">
<x:parse var="doc" xml="entity.xml"/>
- <x:set var="value" select="string($doc/a)"/>
- <j:set var="expected">y</j:set>
- <!--test:assertEquals actual="${value}" expected="${expected}"/-->
+
+ <!--just the entity is output-->
+ <j:set var="value"><x:copyOf select="$doc/a/node()"/></j:set>
+ <test:assertEquals actual="${value}" expected="y"/>
+
+ <!--both the entity and it's lexical name are properly output-->
+ <j:set var="value"><x:copyOf select="$doc/a/node()" lexical="true"/></j:set>
+ <test:assertEquals actual="${value}" expected="&x;y"/>
</test:case>
</test:suite>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]