1 16 19 package org.apache.xalan.templates; 20 21 import javax.xml.transform.TransformerException ; 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 44 public class ElemCopyOf extends ElemTemplateElement 45 { 46 47 51 public XPath m_selectExpression = null; 52 53 59 public void setSelect(XPath expr) 60 { 61 m_selectExpression = expr; 62 } 63 64 70 public XPath getSelect() 71 { 72 return m_selectExpression; 73 } 74 75 81 public void compose(StylesheetRoot sroot) throws TransformerException 82 { 83 super.compose(sroot); 84 85 StylesheetRoot.ComposeState cstate = sroot.getComposeState(); 86 m_selectExpression.fixupVariables(cstate.getVariableNames(), cstate.getGlobalsSize()); 87 } 88 89 95 public int getXSLToken() 96 { 97 return Constants.ELEMNAME_COPY_OF; 98 } 99 100 105 public String getNodeName() 106 { 107 return Constants.ELEMNAME_COPY_OF_STRING; 108 } 109 110 122 public void execute( 123 TransformerImpl transformer) 124 throws TransformerException 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 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 DTMIterator nl = value.iter(); 159 160 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 (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 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 208 } 209 catch(org.xml.sax.SAXException se) 210 { 211 throw new TransformerException (se); 212 } 213 finally 214 { 215 if (TransformerImpl.S_DEBUG) 216 transformer.getTraceManager().fireTraceEndEvent(this); 217 } 218 219 } 220 221 228 public ElemTemplateElement appendChild(ElemTemplateElement newChild) 229 { 230 231 error(XSLTErrorResources.ER_CANNOT_ADD, 232 new Object []{ newChild.getNodeName(), 233 this.getNodeName() }); 235 return null; 237 } 238 239 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 |