1 16 19 package org.apache.xalan.templates; 20 21 import javax.xml.transform.TransformerException ; 22 23 import org.apache.xalan.extensions.ExtensionHandler; 24 import org.apache.xalan.extensions.ExtensionsTable; 25 import org.apache.xalan.res.XSLMessages; 26 import org.apache.xalan.res.XSLTErrorResources; 27 import org.apache.xalan.transformer.TransformerImpl; 28 import org.apache.xpath.XPathContext; 29 import org.xml.sax.SAXException ; 30 31 36 public class ElemExtensionCall extends ElemLiteralResult 37 { 38 39 41 String m_extns; 42 43 45 String m_lang; 46 47 49 String m_srcURL; 50 51 53 String m_scriptSrc; 54 55 57 ElemExtensionDecl m_decl = null; 58 59 65 public int getXSLToken() 66 { 67 return Constants.ELEMNAME_EXTENSIONCALL; 68 } 69 70 75 76 82 88 public void compose(StylesheetRoot sroot) throws TransformerException 89 { 90 super.compose(sroot); 91 m_extns = this.getNamespace(); 92 m_decl = getElemExtensionDecl(sroot, m_extns); 93 if (m_decl == null) 96 sroot.getExtensionNamespacesManager().registerExtension(m_extns); 97 } 98 99 108 private ElemExtensionDecl getElemExtensionDecl(StylesheetRoot stylesheet, 109 String namespace) 110 { 111 112 ElemExtensionDecl decl = null; 113 int n = stylesheet.getGlobalImportCount(); 114 115 for (int i = 0; i < n; i++) 116 { 117 Stylesheet imported = stylesheet.getGlobalImport(i); 118 119 for (ElemTemplateElement child = imported.getFirstChildElem(); 120 child != null; child = child.getNextSiblingElem()) 121 { 122 if (Constants.ELEMNAME_EXTENSIONDECL == child.getXSLToken()) 123 { 124 decl = (ElemExtensionDecl) child; 125 126 String prefix = decl.getPrefix(); 127 String declNamespace = child.getNamespaceForPrefix(prefix); 128 129 if (namespace.equals(declNamespace)) 130 { 131 return decl; 132 } 133 } 134 } 135 } 136 137 return null; 138 } 139 140 149 private void executeFallbacks( 150 TransformerImpl transformer) 151 throws TransformerException 152 { 153 for (ElemTemplateElement child = m_firstChild; child != null; 154 child = child.m_nextSibling) 155 { 156 if (child.getXSLToken() == Constants.ELEMNAME_FALLBACK) 157 { 158 try 159 { 160 transformer.pushElemTemplateElement(child); 161 ((ElemFallback) child).executeFallback(transformer); 162 } 163 finally 164 { 165 transformer.popElemTemplateElement(); 166 } 167 } 168 } 169 170 } 171 172 177 private boolean hasFallbackChildren() 178 { 179 for (ElemTemplateElement child = m_firstChild; child != null; 180 child = child.m_nextSibling) 181 { 182 if (child.getXSLToken() == Constants.ELEMNAME_FALLBACK) 183 return true; 184 } 185 186 return false; 187 } 188 189 190 199 public void execute(TransformerImpl transformer) 200 throws TransformerException 201 { 202 203 if (TransformerImpl.S_DEBUG) 204 transformer.getTraceManager().fireTraceEvent(this); 205 try 206 { 207 transformer.getResultTreeHandler().flushPending(); 208 209 ExtensionsTable etable = transformer.getExtensionsTable(); 210 ExtensionHandler nsh = etable.get(m_extns); 211 212 if (null == nsh) 213 { 214 if (hasFallbackChildren()) 215 { 216 executeFallbacks(transformer); 217 } 218 else 219 { 220 TransformerException te = new TransformerException (XSLMessages.createMessage( 221 XSLTErrorResources.ER_CALL_TO_EXT_FAILED, new Object []{getNodeName()})); 222 transformer.getErrorListener().fatalError(te); 223 } 224 225 return; 226 } 227 228 try 229 { 230 nsh.processElement(this.getLocalName(), this, transformer, 231 getStylesheet(), this); 232 } 233 catch (Exception e) 234 { 235 236 if (hasFallbackChildren()) 237 executeFallbacks(transformer); 238 else 239 { 240 if(e instanceof TransformerException ) 241 { 242 TransformerException te = (TransformerException )e; 243 if(null == te.getLocator()) 244 te.setLocator(this); 245 246 transformer.getErrorListener().fatalError(te); 247 } 248 else if (e instanceof RuntimeException ) 249 { 250 transformer.getErrorListener().fatalError(new TransformerException (e)); 251 } 252 else 253 { 254 transformer.getErrorListener().warning(new TransformerException (e)); 255 } 256 } 257 } 258 } 259 catch(TransformerException e) 260 { 261 transformer.getErrorListener().fatalError(e); 262 } 263 catch(SAXException se) { 264 throw new TransformerException (se); 265 } 266 if (TransformerImpl.S_DEBUG) 267 transformer.getTraceManager().fireTraceEndEvent(this); 268 } 269 270 277 public String getAttribute(String rawName) 278 { 279 280 AVT avt = getLiteralResultAttribute(rawName); 281 282 if ((null != avt) && avt.getRawName().equals(rawName)) 283 { 284 return avt.getSimpleString(); 285 } 286 287 return null; 288 } 289 290 303 public String getAttribute( 304 String rawName, org.w3c.dom.Node sourceNode, TransformerImpl transformer) 305 throws TransformerException 306 { 307 308 AVT avt = getLiteralResultAttribute(rawName); 309 310 if ((null != avt) && avt.getRawName().equals(rawName)) 311 { 312 XPathContext xctxt = transformer.getXPathContext(); 313 314 return avt.evaluate(xctxt, 315 xctxt.getDTMHandleFromNode(sourceNode), 316 this); 317 } 318 319 return null; 320 } 321 322 329 protected boolean accept(XSLTVisitor visitor) 330 { 331 return visitor.visitExtensionElement(this); 332 } 333 334 335 } 336 | Popular Tags |