KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > templates > ElemCopy


1 /*
2  * Copyright 1999-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 /*
17  * $Id: ElemCopy.java,v 1.20 2004/02/16 20:32:32 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.xalan.transformer.ClonerToResultTree;
24 import org.apache.xalan.transformer.TransformerImpl;
25 import org.apache.xml.dtm.DTM;
26 import org.apache.xalan.serialize.SerializerUtils;
27 import org.apache.xml.serializer.SerializationHandler;
28 import org.apache.xpath.XPathContext;
29
30 /**
31  * Implement xsl:copy.
32  * <pre>
33  * <!ELEMENT xsl:copy %template;>
34  * <!ATTLIST xsl:copy
35  * %space-att;
36  * use-attribute-sets %qnames; #IMPLIED
37  * >
38  * </pre>
39  * @see <a HREF="http://www.w3.org/TR/xslt#copying">copying in XSLT Specification</a>
40  * @xsl.usage advanced
41  */

42 public class ElemCopy extends ElemUse
43 {
44
45   /**
46    * Get an int constant identifying the type of element.
47    * @see org.apache.xalan.templates.Constants
48    *
49    * @return The token ID for this element
50    */

51   public int getXSLToken()
52   {
53     return Constants.ELEMNAME_COPY;
54   }
55
56   /**
57    * Return the node name.
58    *
59    * @return This element's name
60    */

61   public String JavaDoc getNodeName()
62   {
63     return Constants.ELEMNAME_COPY_STRING;
64   }
65
66   /**
67    * The xsl:copy element provides an easy way of copying the current node.
68    * Executing this function creates a copy of the current node into the
69    * result tree.
70    * <p>The namespace nodes of the current node are automatically
71    * copied as well, but the attributes and children of the node are not
72    * automatically copied. The content of the xsl:copy element is a
73    * template for the attributes and children of the created node;
74    * the content is instantiated only for nodes of types that can have
75    * attributes or children (i.e. root nodes and element nodes).</p>
76    * <p>The root node is treated specially because the root node of the
77    * result tree is created implicitly. When the current node is the
78    * root node, xsl:copy will not create a root node, but will just use
79    * the content template.</p>
80    *
81    * @param transformer non-null reference to the the current transform-time state.
82    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
83    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
84    *
85    * @throws TransformerException
86    */

87   public void execute(
88           TransformerImpl transformer)
89             throws TransformerException JavaDoc
90   {
91                 XPathContext xctxt = transformer.getXPathContext();
92       
93     try
94     {
95       int sourceNode = xctxt.getCurrentNode();
96       xctxt.pushCurrentNode(sourceNode);
97       DTM dtm = xctxt.getDTM(sourceNode);
98       short nodeType = dtm.getNodeType(sourceNode);
99
100       if ((DTM.DOCUMENT_NODE != nodeType) && (DTM.DOCUMENT_FRAGMENT_NODE != nodeType))
101       {
102         SerializationHandler rthandler = transformer.getSerializationHandler();
103
104         if (TransformerImpl.S_DEBUG)
105           transformer.getTraceManager().fireTraceEvent(this);
106             
107         // TODO: Process the use-attribute-sets stuff
108
ClonerToResultTree.cloneToResultTree(sourceNode, nodeType, dtm,
109                                              rthandler, false);
110
111         if (DTM.ELEMENT_NODE == nodeType)
112         {
113           super.execute(transformer);
114           SerializerUtils.processNSDecls(rthandler, sourceNode, nodeType, dtm);
115           transformer.executeChildTemplates(this, true);
116           
117           String JavaDoc ns = dtm.getNamespaceURI(sourceNode);
118           String JavaDoc localName = dtm.getLocalName(sourceNode);
119           transformer.getResultTreeHandler().endElement(ns, localName,
120                                                         dtm.getNodeName(sourceNode));
121         }
122         if (TransformerImpl.S_DEBUG)
123           transformer.getTraceManager().fireTraceEndEvent(this);
124       }
125       else
126       {
127         if (TransformerImpl.S_DEBUG)
128           transformer.getTraceManager().fireTraceEvent(this);
129
130         super.execute(transformer);
131         transformer.executeChildTemplates(this, true);
132
133         if (TransformerImpl.S_DEBUG)
134           transformer.getTraceManager().fireTraceEndEvent(this);
135       }
136     }
137     catch(org.xml.sax.SAXException JavaDoc se)
138     {
139       throw new TransformerException JavaDoc(se);
140     }
141     finally
142     {
143       xctxt.popCurrentNode();
144     }
145   }
146 }
147
Popular Tags