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.xml.serializer.SerializationHandler; 26 import org.apache.xml.utils.QName; 27 import org.apache.xml.utils.XMLChar; 28 import org.apache.xpath.XPathContext; 29 import org.xml.sax.SAXException ; 30 31 45 public class ElemElement extends ElemUse 46 { 47 48 54 protected AVT m_name_avt = null; 55 56 64 public void setName(AVT v) 65 { 66 m_name_avt = v; 67 } 68 69 77 public AVT getName() 78 { 79 return m_name_avt; 80 } 81 82 89 protected AVT m_namespace_avt = null; 90 91 100 public void setNamespace(AVT v) 101 { 102 m_namespace_avt = v; 103 } 104 105 114 public AVT getNamespace() 115 { 116 return m_namespace_avt; 117 } 118 119 125 public void compose(StylesheetRoot sroot) throws TransformerException 126 { 127 super.compose(sroot); 128 129 StylesheetRoot.ComposeState cstate = sroot.getComposeState(); 130 java.util.Vector vnames = cstate.getVariableNames(); 131 if(null != m_name_avt) 132 m_name_avt.fixupVariables(vnames, cstate.getGlobalsSize()); 133 if(null != m_namespace_avt) 134 m_namespace_avt.fixupVariables(vnames, cstate.getGlobalsSize()); 135 } 136 137 138 144 public int getXSLToken() 145 { 146 return Constants.ELEMNAME_ELEMENT; 147 } 148 149 154 public String getNodeName() 155 { 156 return Constants.ELEMNAME_ELEMENT_STRING; 157 } 158 159 169 protected String resolvePrefix(SerializationHandler rhandler, 170 String prefix, String nodeNamespace) 171 throws TransformerException 172 { 173 174 return prefix; 183 } 184 185 197 public void execute( 198 TransformerImpl transformer) 199 throws TransformerException 200 { 201 202 if (TransformerImpl.S_DEBUG) 203 transformer.getTraceManager().fireTraceEvent(this); 204 205 SerializationHandler rhandler = transformer.getSerializationHandler(); 206 XPathContext xctxt = transformer.getXPathContext(); 207 int sourceNode = xctxt.getCurrentNode(); 208 209 210 String nodeName = m_name_avt == null ? null : m_name_avt.evaluate(xctxt, sourceNode, this); 211 212 String prefix = null; 213 String nodeNamespace = ""; 214 215 if ((nodeName != null) && (!m_name_avt.isSimple()) && (!XMLChar.isValidQName(nodeName))) 217 { 218 transformer.getMsgMgr().warn( 219 this, XSLTErrorResources.WG_ILLEGAL_ATTRIBUTE_VALUE, 220 new Object []{ Constants.ATTRNAME_NAME, nodeName }); 221 222 nodeName = null; 223 } 224 225 else if (nodeName != null) 226 { 227 prefix = QName.getPrefixPart(nodeName); 228 229 if (null != m_namespace_avt) 230 { 231 nodeNamespace = m_namespace_avt.evaluate(xctxt, sourceNode, this); 232 if (null == nodeNamespace || 233 (prefix != null && prefix.length()>0 && nodeNamespace.length()== 0) ) 234 transformer.getMsgMgr().error( 235 this, XSLTErrorResources.ER_NULL_URI_NAMESPACE); 236 else 237 { 238 240 prefix = resolvePrefix(rhandler, prefix, nodeNamespace); 241 if (null == prefix) 242 prefix = ""; 243 244 if (prefix.length() > 0) 245 nodeName = (prefix + ":" + QName.getLocalPart(nodeName)); 246 else 247 nodeName = QName.getLocalPart(nodeName); 248 } 249 } 250 251 else 254 { 255 try 256 { 257 nodeNamespace = getNamespaceForPrefix(prefix); 259 260 264 if ( (null == nodeNamespace) && (prefix.length() == 0) ) 265 nodeNamespace = ""; 266 else if (null == nodeNamespace) 267 { 268 transformer.getMsgMgr().warn( 269 this, XSLTErrorResources.WG_COULD_NOT_RESOLVE_PREFIX, 270 new Object []{ prefix }); 271 272 nodeName = null; 273 } 274 275 } 276 catch (Exception ex) 277 { 278 transformer.getMsgMgr().warn( 279 this, XSLTErrorResources.WG_COULD_NOT_RESOLVE_PREFIX, 280 new Object []{ prefix }); 281 282 nodeName = null; 283 } 284 } 285 } 286 287 constructNode(nodeName, prefix, nodeNamespace, transformer); 288 289 if (TransformerImpl.S_DEBUG) 290 transformer.getTraceManager().fireTraceEndEvent(this); 291 } 292 293 308 void constructNode( 309 String nodeName, String prefix, String nodeNamespace, TransformerImpl transformer) 310 throws TransformerException 311 { 312 313 boolean shouldAddAttrs; 314 315 try 316 { 317 SerializationHandler rhandler = transformer.getResultTreeHandler(); 318 319 if (null == nodeName) 320 { 321 shouldAddAttrs = false; 322 } 323 else 324 { 325 if (null != prefix) 326 { 327 rhandler.startPrefixMapping(prefix, nodeNamespace, true); 328 } 329 330 rhandler.startElement(nodeNamespace, QName.getLocalPart(nodeName), 331 nodeName); 332 333 super.execute(transformer); 334 335 shouldAddAttrs = true; 336 } 337 338 transformer.executeChildTemplates(this, shouldAddAttrs); 339 340 if (null != nodeName) 342 { 343 rhandler.endElement(nodeNamespace, QName.getLocalPart(nodeName), 344 nodeName); 345 if (null != prefix) 346 { 347 rhandler.endPrefixMapping(prefix); 348 } 349 } 350 } 351 catch (SAXException se) 352 { 353 throw new TransformerException (se); 354 } 355 } 356 357 361 protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs) 362 { 363 if(callAttrs) 364 { 365 if(null != m_name_avt) 366 m_name_avt.callVisitors(visitor); 367 368 if(null != m_namespace_avt) 369 m_namespace_avt.callVisitors(visitor); 370 } 371 372 super.callChildVisitors(visitor, callAttrs); 373 } 374 375 } 376 | Popular Tags |