1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.xml;
17
18 import org.apache.commons.jelly.JellyTagException;
19 import org.apache.commons.jelly.XMLOutput;
20 import org.apache.commons.jelly.xpath.XPathTagSupport;
21 import org.dom4j.Element;
22 import org.dom4j.io.SAXWriter;
23 import org.jaxen.JaxenException;
24 import org.jaxen.XPath;
25 import org.xml.sax.SAXException;
26
27 /***
28 * A tag which performs a copy operation like the XSLT tag,
29 * performing a shallow copy of the element and its attributes but no content.
30 *
31 * @author James Strachan
32 */
33 public class CopyTag extends XPathTagSupport {
34
35 /*** The XPath expression to evaluate. */
36 private XPath select;
37
38 /*** Should we output lexical XML data like entity names?
39 */
40 private boolean lexical;
41
42 public CopyTag() {
43 }
44
45
46
47 public void doTag(XMLOutput output) throws JellyTagException {
48 Object xpathContext = getXPathContext();
49
50 Object node = xpathContext;
51
52 try {
53 if (select != null) {
54 node = select.selectSingleNode(xpathContext);
55 }
56
57 if ( node instanceof Element ) {
58 Element element = (Element) node;
59
60 SAXWriter saxWriter;
61
62 if (lexical) {
63 saxWriter = new SAXWriter(output, output);
64 } else {
65 saxWriter = new SAXWriter(output);
66 }
67
68 saxWriter.writeOpen(element);
69 invokeBody(output);
70 saxWriter.writeClose(element);
71 }
72 else {
73 invokeBody(output);
74 }
75 }
76 catch (SAXException e) {
77 throw new JellyTagException(e);
78 }
79 catch (JaxenException e) {
80 throw new JellyTagException(e);
81 }
82 }
83
84
85
86 /*** Sets the XPath expression to evaluate. */
87 public void setSelect(XPath select) {
88 this.select = select;
89 }
90 public void setLexical(boolean lexical) {
91 this.lexical = lexical;
92 }
93 }