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.processor;
  58. import org.apache.xalan.templates.ElemLiteralResult;
  59. import org.apache.xalan.templates.ElemElement;
  60. import org.apache.xalan.templates.ElemTemplateElement;
  61. import org.apache.xalan.templates.Stylesheet;
  62. import org.apache.xalan.templates.ElemExtensionCall;
  63. import org.apache.xalan.templates.ElemTemplate;
  64. import org.apache.xalan.templates.ElemExsltFunction;
  65. import org.apache.xalan.templates.ElemExsltFuncResult;
  66. import org.apache.xalan.templates.ElemFallback;
  67. import org.apache.xalan.templates.ElemVariable;
  68. import org.apache.xalan.templates.ElemParam;
  69. import org.apache.xalan.templates.ElemValueOf;
  70. import org.apache.xalan.templates.ElemText;
  71. import org.apache.xalan.templates.ElemTextLiteral;
  72. import org.apache.xalan.templates.ElemApplyImport;
  73. import org.apache.xalan.templates.ElemApplyTemplates;
  74. import org.apache.xalan.templates.ElemAttribute;
  75. import org.apache.xalan.templates.ElemCallTemplate;
  76. import org.apache.xalan.templates.ElemComment;
  77. import org.apache.xalan.templates.ElemCopy;
  78. import org.apache.xalan.templates.ElemCopyOf;
  79. import org.apache.xalan.templates.ElemNumber;
  80. import org.apache.xalan.templates.ElemPI;
  81. import org.apache.xalan.templates.Constants;
  82. import org.apache.xpath.XPath;
  83. import org.apache.xalan.templates.StylesheetRoot;
  84. import javax.xml.transform.SourceLocator;
  85. import javax.xml.transform.TransformerException;
  86. import javax.xml.transform.TransformerConfigurationException;
  87. import org.xml.sax.Attributes;
  88. import org.xml.sax.SAXException;
  89. import org.w3c.dom.Node;
  90. import org.w3c.dom.NodeList;
  91. import org.apache.xalan.res.XSLTErrorResources;
  92. /**
  93. * <meta name="usage" content="internal"/>
  94. * This class processes parse events for an exslt func:function element.
  95. */
  96. public class ProcessorExsltFunction extends ProcessorTemplateElem
  97. {
  98. /**
  99. * Start an ElemExsltFunction. Verify that it is top level and that it has a name attribute with a
  100. * namespace.
  101. */
  102. public void startElement(
  103. StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
  104. throws SAXException
  105. {
  106. //System.out.println("ProcessorFunction.startElement()");
  107. String msg = "";
  108. if (!(handler.getElemTemplateElement() instanceof Stylesheet))
  109. {
  110. msg = "func:function element must be top level.";
  111. handler.error(msg, new SAXException(msg));
  112. }
  113. super.startElement(handler, uri, localName, rawName, attributes);
  114. String val = attributes.getValue("name");
  115. int indexOfColon = val.indexOf(":");
  116. if (indexOfColon > 0)
  117. {
  118. String prefix = val.substring(0, indexOfColon);
  119. String localVal = val.substring(indexOfColon + 1);
  120. String ns = handler.getNamespaceSupport().getURI(prefix);
  121. //if (ns.length() > 0)
  122. // System.out.println("fullfuncname " + ns + localVal);
  123. }
  124. else
  125. {
  126. msg = "func:function name must have namespace";
  127. handler.error(msg, new SAXException(msg));
  128. }
  129. }
  130. /**
  131. * Must include; super doesn't suffice!
  132. */
  133. protected void appendAndPush(
  134. StylesheetHandler handler, ElemTemplateElement elem)
  135. throws SAXException
  136. {
  137. //System.out.println("ProcessorFunction appendAndPush()" + elem);
  138. super.appendAndPush(handler, elem);
  139. //System.out.println("originating node " + handler.getOriginatingNode());
  140. elem.setDOMBackPointer(handler.getOriginatingNode());
  141. handler.getStylesheet().setTemplate((ElemTemplate) elem);
  142. }
  143. /**
  144. * End an ElemExsltFunction, and verify its validity.
  145. */
  146. public void endElement(
  147. StylesheetHandler handler, String uri, String localName, String rawName)
  148. throws SAXException
  149. {
  150. ElemTemplateElement function = handler.getElemTemplateElement();
  151. SourceLocator locator = handler.getLocator();
  152. validate(function, handler); // may throw exception
  153. super.endElement(handler, uri, localName, rawName);
  154. }
  155. /**
  156. * Non-recursive traversal of FunctionElement tree based on TreeWalker to verify that
  157. * there are no literal result elements except within a func:result element and that
  158. * the func:result element does not contain any following siblings except xsl:fallback.
  159. */
  160. public void validate(ElemTemplateElement elem, StylesheetHandler handler)
  161. throws SAXException
  162. {
  163. String msg = "";
  164. while (elem != null)
  165. {
  166. //System.out.println("elem " + elem);
  167. if (elem instanceof ElemExsltFuncResult
  168. && elem.getNextSiblingElem() != null
  169. && !(elem.getNextSiblingElem() instanceof ElemFallback))
  170. {
  171. msg = "func:result has an illegal following sibling (only xsl:fallback allowed)";
  172. handler.error(msg, new SAXException(msg));
  173. }
  174. if((elem instanceof ElemApplyImport
  175. || elem instanceof ElemApplyTemplates
  176. || elem instanceof ElemAttribute
  177. || elem instanceof ElemCallTemplate
  178. || elem instanceof ElemComment
  179. || elem instanceof ElemCopy
  180. || elem instanceof ElemCopyOf
  181. || elem instanceof ElemElement
  182. || elem instanceof ElemLiteralResult
  183. || elem instanceof ElemNumber
  184. || elem instanceof ElemPI
  185. || elem instanceof ElemText
  186. || elem instanceof ElemTextLiteral
  187. || elem instanceof ElemValueOf)
  188. && !(ancestorIsOk(elem)))
  189. {
  190. msg ="misplaced literal result in a func:function container.";
  191. handler.error(msg, new SAXException(msg));
  192. }
  193. ElemTemplateElement nextElem = elem.getFirstChildElem();
  194. while (nextElem == null)
  195. {
  196. nextElem = elem.getNextSiblingElem();
  197. if (nextElem == null)
  198. elem = elem.getParentElem();
  199. if (elem == null || elem instanceof ElemExsltFunction)
  200. return; // ok
  201. }
  202. elem = nextElem;
  203. }
  204. }
  205. /**
  206. * Verify that a literal result belongs to a result element, a variable,
  207. * or a parameter.
  208. */
  209. boolean ancestorIsOk(ElemTemplateElement child)
  210. {
  211. while (child.getParentElem() != null && !(child.getParentElem() instanceof ElemExsltFunction))
  212. {
  213. ElemTemplateElement parent = child.getParentElem();
  214. if (parent instanceof ElemExsltFuncResult
  215. || parent instanceof ElemVariable
  216. || parent instanceof ElemParam)
  217. return true;
  218. child = parent;
  219. }
  220. return false;
  221. }
  222. }