1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.templates;
  58. import java.io.*;
  59. import java.util.*;
  60. //import org.w3c.dom.*;
  61. import org.xml.sax.*;
  62. import org.xml.sax.helpers.*;
  63. import java.util.StringTokenizer;
  64. import org.apache.xml.utils.QName;
  65. import org.apache.xml.utils.NameSpace;
  66. import org.apache.xml.utils.StringToStringTable;
  67. import org.apache.xpath.XPathContext;
  68. import org.apache.xml.utils.MutableAttrListImpl;
  69. import org.apache.xalan.res.XSLMessages;
  70. import org.apache.xpath.XPathContext;
  71. import org.apache.xalan.res.XSLTErrorResources;
  72. import org.apache.xalan.extensions.ExtensionHandler;
  73. import org.apache.xalan.extensions.ExtensionsTable;
  74. import org.apache.xalan.transformer.TransformerImpl;
  75. import javax.xml.transform.TransformerException;
  76. import org.apache.xml.dtm.DTM;
  77. /**
  78. * <meta name="usage" content="advanced"/>
  79. * Implement an extension element.
  80. * @see <a href="http://www.w3.org/TR/xslt#extension-element">extension-element in XSLT Specification</a>
  81. */
  82. public class ElemExtensionCall extends ElemLiteralResult
  83. {
  84. /** The Namespace URI for this extension call element.
  85. * @serial */
  86. String m_extns;
  87. /** Language used by extension.
  88. * @serial */
  89. String m_lang;
  90. /** URL pointing to extension.
  91. * @serial */
  92. String m_srcURL;
  93. /** Source for script.
  94. * @serial */
  95. String m_scriptSrc;
  96. /** Declaration for Extension element.
  97. * @serial */
  98. ElemExtensionDecl m_decl = null;
  99. /**
  100. * Get an int constant identifying the type of element.
  101. * @see org.apache.xalan.templates.Constants
  102. *
  103. *@return The token ID for this element
  104. */
  105. public int getXSLToken()
  106. {
  107. return Constants.ELEMNAME_EXTENSIONCALL;
  108. }
  109. /**
  110. * Return the node name.
  111. *
  112. * @return The element's name
  113. */
  114. // public String getNodeName()
  115. // {
  116. // TODO: Need prefix.
  117. // return localPart;
  118. // }
  119. /**
  120. * This function is called after everything else has been
  121. * recomposed, and allows the template to set remaining
  122. * values that may be based on some other property that
  123. * depends on recomposition.
  124. */
  125. public void compose(StylesheetRoot sroot) throws TransformerException
  126. {
  127. super.compose(sroot);
  128. m_extns = this.getNamespace();
  129. m_decl = getElemExtensionDecl(sroot, m_extns);
  130. // Register the extension namespace if the extension does not have
  131. // an ElemExtensionDecl ("component").
  132. if (m_decl == null)
  133. sroot.getExtensionNamespacesManager().registerExtension(m_extns);
  134. }
  135. /**
  136. * Return the ElemExtensionDecl for this extension element
  137. *
  138. *
  139. * @param stylesheet Stylesheet root associated with this extension element
  140. * @param namespace Namespace associated with this extension element
  141. *
  142. * @return the ElemExtensionDecl for this extension element.
  143. */
  144. private ElemExtensionDecl getElemExtensionDecl(StylesheetRoot stylesheet,
  145. String namespace)
  146. {
  147. ElemExtensionDecl decl = null;
  148. int n = stylesheet.getGlobalImportCount();
  149. for (int i = 0; i < n; i++)
  150. {
  151. Stylesheet imported = stylesheet.getGlobalImport(i);
  152. for (ElemTemplateElement child = imported.getFirstChildElem();
  153. child != null; child = child.getNextSiblingElem())
  154. {
  155. if (Constants.ELEMNAME_EXTENSIONDECL == child.getXSLToken())
  156. {
  157. decl = (ElemExtensionDecl) child;
  158. String prefix = decl.getPrefix();
  159. String declNamespace = child.getNamespaceForPrefix(prefix);
  160. if (namespace.equals(declNamespace))
  161. {
  162. return decl;
  163. }
  164. }
  165. }
  166. }
  167. return null;
  168. }
  169. /**
  170. * Execute the fallbacks when an extension is not available.
  171. *
  172. * @param transformer non-null reference to the the current transform-time state.
  173. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  174. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  175. *
  176. * @throws TransformerException
  177. */
  178. public void executeFallbacks(
  179. TransformerImpl transformer)
  180. throws TransformerException
  181. {
  182. for (ElemTemplateElement child = m_firstChild; child != null;
  183. child = child.m_nextSibling)
  184. {
  185. if (child.getXSLToken() == Constants.ELEMNAME_FALLBACK)
  186. {
  187. try
  188. {
  189. transformer.pushElemTemplateElement(child);
  190. ((ElemFallback) child).executeFallback(transformer);
  191. }
  192. finally
  193. {
  194. transformer.popElemTemplateElement();
  195. }
  196. }
  197. }
  198. }
  199. /**
  200. * Return true if this extension element has a <xsl:fallback> child element.
  201. *
  202. * @return true if this extension element has a <xsl:fallback> child element.
  203. */
  204. public boolean hasFallbackChildren()
  205. {
  206. for (ElemTemplateElement child = m_firstChild; child != null;
  207. child = child.m_nextSibling)
  208. {
  209. if (child.getXSLToken() == Constants.ELEMNAME_FALLBACK)
  210. return true;
  211. }
  212. return false;
  213. }
  214. /**
  215. * Execute an extension.
  216. *
  217. * @param transformer non-null reference to the the current transform-time state.
  218. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  219. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  220. *
  221. * @throws TransformerException
  222. */
  223. public void execute(TransformerImpl transformer)
  224. throws TransformerException
  225. {
  226. try
  227. {
  228. transformer.getResultTreeHandler().flushPending();
  229. ExtensionsTable etable = transformer.getExtensionsTable();
  230. ExtensionHandler nsh = etable.get(m_extns);
  231. if (null == nsh)
  232. {
  233. if (hasFallbackChildren())
  234. {
  235. executeFallbacks(transformer);
  236. }
  237. else
  238. {
  239. TransformerException te = new TransformerException(XSLMessages.createMessage(
  240. XSLTErrorResources.ER_CALL_TO_EXT_FAILED, new Object[]{getNodeName()}));
  241. transformer.getErrorListener().fatalError(te);
  242. }
  243. return;
  244. }
  245. try
  246. {
  247. nsh.processElement(this.getLocalName(), this, transformer,
  248. getStylesheet(), this);
  249. }
  250. catch (Exception e)
  251. {
  252. if (hasFallbackChildren())
  253. executeFallbacks(transformer);
  254. else
  255. {
  256. if(e instanceof TransformerException)
  257. {
  258. TransformerException te = (TransformerException)e;
  259. if(null == te.getLocator())
  260. te.setLocator(this);
  261. transformer.getErrorListener().fatalError(te);
  262. }
  263. else if (e instanceof RuntimeException)
  264. {
  265. transformer.getErrorListener().fatalError(new TransformerException(e));
  266. }
  267. else
  268. {
  269. transformer.getErrorListener().warning(new TransformerException(e));
  270. }
  271. }
  272. }
  273. }
  274. catch(org.xml.sax.SAXException se)
  275. {
  276. transformer.getErrorListener().fatalError(new TransformerException(se));
  277. }
  278. }
  279. /**
  280. * Return the raw value of the attribute.
  281. *
  282. * @param rawName Raw name of the attribute to get
  283. *
  284. * @return the raw value of the attribute or null if not found
  285. */
  286. public String getAttribute(String rawName)
  287. {
  288. AVT avt = getLiteralResultAttribute(rawName);
  289. if ((null != avt) && avt.getRawName().equals(rawName))
  290. {
  291. return avt.getSimpleString();
  292. }
  293. return null;
  294. }
  295. /**
  296. * Return the value of the attribute interpreted as an Attribute
  297. * Value Template (in other words, you can use curly expressions
  298. * such as href="http://{website}".
  299. *
  300. * @param rawName Raw name of the attribute to get
  301. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  302. * @param transformer non-null reference to the the current transform-time state.
  303. *
  304. * @return the value of the attribute
  305. *
  306. * @throws TransformerException
  307. */
  308. public String getAttribute(
  309. String rawName, org.w3c.dom.Node sourceNode, TransformerImpl transformer)
  310. throws TransformerException
  311. {
  312. AVT avt = getLiteralResultAttribute(rawName);
  313. if ((null != avt) && avt.getRawName().equals(rawName))
  314. {
  315. XPathContext xctxt = transformer.getXPathContext();
  316. return avt.evaluate(xctxt,
  317. xctxt.getDTMHandleFromNode(sourceNode),
  318. this);
  319. }
  320. return null;
  321. }
  322. /**
  323. * Accept a visitor and call the appropriate method
  324. * for this class.
  325. *
  326. * @param visitor The visitor whose appropriate method will be called.
  327. * @return true if the children of the object should be visited.
  328. */
  329. protected boolean accept(XSLTVisitor visitor)
  330. {
  331. return visitor.visitExtensionElement(this);
  332. }
  333. }