KickJava   Java API By Example, From Geeks To Geeks.

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


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: ElemCopyOf.java,v 1.19 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.res.XSLTErrorResources;
24 import org.apache.xalan.transformer.TransformerImpl;
25 import org.apache.xalan.transformer.TreeWalker2Result;
26 import org.apache.xml.dtm.DTM;
27 import org.apache.xml.dtm.DTMIterator;
28 import org.apache.xml.dtm.ref.DTMTreeWalker;
29 import org.apache.xalan.serialize.SerializerUtils;
30 import org.apache.xml.serializer.SerializationHandler;
31 import org.apache.xpath.XPath;
32 import org.apache.xpath.XPathContext;
33 import org.apache.xpath.objects.XObject;
34
35 /**
36  * Implement xsl:copy-of.
37  * <pre>
38  * <!ELEMENT xsl:copy-of EMPTY>
39  * <!ATTLIST xsl:copy-of select %expr; #REQUIRED>
40  * </pre>
41  * @see <a HREF="http://www.w3.org/TR/xslt#copy-of">copy-of in XSLT Specification</a>
42  * @xsl.usage advanced
43  */

44 public class ElemCopyOf extends ElemTemplateElement
45 {
46
47   /**
48    * The required select attribute contains an expression.
49    * @serial
50    */

51   public XPath m_selectExpression = null;
52
53   /**
54    * Set the "select" attribute.
55    * The required select attribute contains an expression.
56    *
57    * @param expr Expression for select attribute
58    */

59   public void setSelect(XPath expr)
60   {
61     m_selectExpression = expr;
62   }
63
64   /**
65    * Get the "select" attribute.
66    * The required select attribute contains an expression.
67    *
68    * @return Expression for select attribute
69    */

70   public XPath getSelect()
71   {
72     return m_selectExpression;
73   }
74   
75   /**
76    * This function is called after everything else has been
77    * recomposed, and allows the template to set remaining
78    * values that may be based on some other property that
79    * depends on recomposition.
80    */

81   public void compose(StylesheetRoot sroot) throws TransformerException JavaDoc
82   {
83     super.compose(sroot);
84     
85     StylesheetRoot.ComposeState cstate = sroot.getComposeState();
86     m_selectExpression.fixupVariables(cstate.getVariableNames(), cstate.getGlobalsSize());
87   }
88
89   /**
90    * Get an int constant identifying the type of element.
91    * @see org.apache.xalan.templates.Constants
92    *
93    * @return The token ID for this element
94    */

95   public int getXSLToken()
96   {
97     return Constants.ELEMNAME_COPY_OF;
98   }
99
100   /**
101    * Return the node name.
102    *
103    * @return The element's name
104    */

105   public String JavaDoc getNodeName()
106   {
107     return Constants.ELEMNAME_COPY_OF_STRING;
108   }
109
110   /**
111    * The xsl:copy-of element can be used to insert a result tree
112    * fragment into the result tree, without first converting it to
113    * a string as xsl:value-of does (see [7.6.1 Generating Text with
114    * xsl:value-of]).
115    *
116    * @param transformer non-null reference to the the current transform-time state.
117    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
118    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
119    *
120    * @throws TransformerException
121    */

122   public void execute(
123           TransformerImpl transformer)
124             throws TransformerException JavaDoc
125   {
126     if (TransformerImpl.S_DEBUG)
127         transformer.getTraceManager().fireTraceEvent(this);
128
129     try
130     {
131       XPathContext xctxt = transformer.getXPathContext();
132       int sourceNode = xctxt.getCurrentNode();
133       XObject value = m_selectExpression.execute(xctxt, sourceNode, this);
134
135       if (TransformerImpl.S_DEBUG)
136         transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
137                                                         "select", m_selectExpression, value);
138
139       SerializationHandler handler = transformer.getSerializationHandler();
140
141       if (null != value)
142                         {
143         int type = value.getType();
144         String JavaDoc s;
145
146         switch (type)
147         {
148         case XObject.CLASS_BOOLEAN :
149         case XObject.CLASS_NUMBER :
150         case XObject.CLASS_STRING :
151           s = value.str();
152
153           handler.characters(s.toCharArray(), 0, s.length());
154           break;
155         case XObject.CLASS_NODESET :
156
157           // System.out.println(value);
158
DTMIterator nl = value.iter();
159
160           // Copy the tree.
161
DTMTreeWalker tw = new TreeWalker2Result(transformer, handler);
162           int pos;
163
164           while (DTM.NULL != (pos = nl.nextNode()))
165           {
166             DTM dtm = xctxt.getDTMManager().getDTM(pos);
167             short t = dtm.getNodeType(pos);
168
169             // If we just copy the whole document, a startDoc and endDoc get
170
// generated, so we need to only walk the child nodes.
171
if (t == DTM.DOCUMENT_NODE)
172             {
173               for (int child = dtm.getFirstChild(pos); child != DTM.NULL;
174                    child = dtm.getNextSibling(child))
175               {
176                 tw.traverse(child);
177               }
178             }
179             else if (t == DTM.ATTRIBUTE_NODE)
180             {
181               SerializerUtils.addAttribute(handler, pos);
182             }
183             else
184             {
185               tw.traverse(pos);
186             }
187           }
188           // nl.detach();
189
break;
190         case XObject.CLASS_RTREEFRAG :
191           SerializerUtils.outputResultTreeFragment(
192             handler, value, transformer.getXPathContext());
193           break;
194         default :
195           
196           s = value.str();
197
198           handler.characters(s.toCharArray(), 0, s.length());
199           break;
200         }
201       }
202                         
203       // I don't think we want this. -sb
204
// if (TransformerImpl.S_DEBUG)
205
// transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
206
// "endSelect", m_selectExpression, value);
207

208     }
209     catch(org.xml.sax.SAXException JavaDoc se)
210     {
211       throw new TransformerException JavaDoc(se);
212     }
213     finally
214     {
215       if (TransformerImpl.S_DEBUG)
216         transformer.getTraceManager().fireTraceEndEvent(this);
217     }
218
219   }
220
221   /**
222    * Add a child to the child list.
223    *
224    * @param newChild Child to add to this node's child list
225    *
226    * @return Child just added to child list
227    */

228   public ElemTemplateElement appendChild(ElemTemplateElement newChild)
229   {
230
231     error(XSLTErrorResources.ER_CANNOT_ADD,
232           new Object JavaDoc[]{ newChild.getNodeName(),
233                         this.getNodeName() }); //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
234

235     //" to " + this.m_elemName);
236
return null;
237   }
238   
239   /**
240    * Call the children visitors.
241    * @param visitor The visitor whose appropriate method will be called.
242    */

243   protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs)
244   {
245     if(callAttrs)
246         m_selectExpression.getExpression().callVisitors(m_selectExpression, visitor);
247     super.callChildVisitors(visitor, callAttrs);
248   }
249
250 }
251
Popular Tags