001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: ElemCopy.java 468643 2006-10-28 06:56:03Z minchau $
020 */
021 package org.apache.xalan.templates;
022
023 import javax.xml.transform.TransformerException;
024
025 import org.apache.xalan.transformer.ClonerToResultTree;
026 import org.apache.xalan.transformer.TransformerImpl;
027 import org.apache.xml.dtm.DTM;
028 import org.apache.xalan.serialize.SerializerUtils;
029 import org.apache.xml.serializer.SerializationHandler;
030 import org.apache.xpath.XPathContext;
031
032 /**
033 * Implement xsl:copy.
034 * <pre>
035 * <!ELEMENT xsl:copy %template;>
036 * <!ATTLIST xsl:copy
037 * %space-att;
038 * use-attribute-sets %qnames; #IMPLIED
039 * >
040 * </pre>
041 * @see <a href="http://www.w3.org/TR/xslt#copying">copying in XSLT Specification</a>
042 * @xsl.usage advanced
043 */
044 public class ElemCopy extends ElemUse
045 {
046 static final long serialVersionUID = 5478580783896941384L;
047
048 /**
049 * Get an int constant identifying the type of element.
050 * @see org.apache.xalan.templates.Constants
051 *
052 * @return The token ID for this element
053 */
054 public int getXSLToken()
055 {
056 return Constants.ELEMNAME_COPY;
057 }
058
059 /**
060 * Return the node name.
061 *
062 * @return This element's name
063 */
064 public String getNodeName()
065 {
066 return Constants.ELEMNAME_COPY_STRING;
067 }
068
069 /**
070 * The xsl:copy element provides an easy way of copying the current node.
071 * Executing this function creates a copy of the current node into the
072 * result tree.
073 * <p>The namespace nodes of the current node are automatically
074 * copied as well, but the attributes and children of the node are not
075 * automatically copied. The content of the xsl:copy element is a
076 * template for the attributes and children of the created node;
077 * the content is instantiated only for nodes of types that can have
078 * attributes or children (i.e. root nodes and element nodes).</p>
079 * <p>The root node is treated specially because the root node of the
080 * result tree is created implicitly. When the current node is the
081 * root node, xsl:copy will not create a root node, but will just use
082 * the content template.</p>
083 *
084 * @param transformer non-null reference to the the current transform-time state.
085 *
086 * @throws TransformerException
087 */
088 public void execute(
089 TransformerImpl transformer)
090 throws TransformerException
091 {
092 XPathContext xctxt = transformer.getXPathContext();
093
094 try
095 {
096 int sourceNode = xctxt.getCurrentNode();
097 xctxt.pushCurrentNode(sourceNode);
098 DTM dtm = xctxt.getDTM(sourceNode);
099 short nodeType = dtm.getNodeType(sourceNode);
100
101 if ((DTM.DOCUMENT_NODE != nodeType) && (DTM.DOCUMENT_FRAGMENT_NODE != nodeType))
102 {
103 SerializationHandler rthandler = transformer.getSerializationHandler();
104
105 if (transformer.getDebug())
106 transformer.getTraceManager().fireTraceEvent(this);
107
108 // TODO: Process the use-attribute-sets stuff
109 ClonerToResultTree.cloneToResultTree(sourceNode, nodeType, dtm,
110 rthandler, false);
111
112 if (DTM.ELEMENT_NODE == nodeType)
113 {
114 super.execute(transformer);
115 SerializerUtils.processNSDecls(rthandler, sourceNode, nodeType, dtm);
116 transformer.executeChildTemplates(this, true);
117
118 String ns = dtm.getNamespaceURI(sourceNode);
119 String localName = dtm.getLocalName(sourceNode);
120 transformer.getResultTreeHandler().endElement(ns, localName,
121 dtm.getNodeName(sourceNode));
122 }
123 if (transformer.getDebug())
124 transformer.getTraceManager().fireTraceEndEvent(this);
125 }
126 else
127 {
128 if (transformer.getDebug())
129 transformer.getTraceManager().fireTraceEvent(this);
130
131 super.execute(transformer);
132 transformer.executeChildTemplates(this, true);
133
134 if (transformer.getDebug())
135 transformer.getTraceManager().fireTraceEndEvent(this);
136 }
137 }
138 catch(org.xml.sax.SAXException se)
139 {
140 throw new TransformerException(se);
141 }
142 finally
143 {
144 xctxt.popCurrentNode();
145 }
146 }
147 }