View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      // Tag interface
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      // Properties
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  }