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 org.w3c.dom.*;
  59. import org.apache.xml.dtm.DTM;
  60. import org.xml.sax.*;
  61. import org.apache.xpath.*;
  62. import org.apache.xml.utils.QName;
  63. import org.apache.xalan.res.XSLTErrorResources;
  64. import org.apache.xalan.transformer.TransformerImpl;
  65. import javax.xml.transform.TransformerException;
  66. /**
  67. * <meta name="usage" content="advanced"/>
  68. * Implement xsl:processing-instruction.
  69. * <pre>
  70. * <!ELEMENT xsl:processing-instruction %char-template;>
  71. * <!ATTLIST xsl:processing-instruction
  72. * name %avt; #REQUIRED
  73. * %space-att;
  74. * >
  75. * </pre>
  76. * @see <a href="http://www.w3.org/TR/xslt#section-Creating-Processing-Instructions">section-Creating-Processing-Instructions in XSLT Specification</a>
  77. */
  78. public class ElemPI extends ElemTemplateElement
  79. {
  80. /**
  81. * The xsl:processing-instruction element has a required name
  82. * attribute that specifies the name of the processing instruction node.
  83. * The value of the name attribute is interpreted as an
  84. * attribute value template.
  85. * @serial
  86. */
  87. private AVT m_name_atv = null;
  88. /**
  89. * Set the "name" attribute.
  90. * DJD
  91. *
  92. * @param v Value for the name attribute
  93. */
  94. public void setName(AVT v)
  95. {
  96. m_name_atv = v;
  97. }
  98. /**
  99. * Get the "name" attribute.
  100. * DJD
  101. *
  102. * @return The value of the "name" attribute
  103. */
  104. public AVT getName()
  105. {
  106. return m_name_atv;
  107. }
  108. /**
  109. * This function is called after everything else has been
  110. * recomposed, and allows the template to set remaining
  111. * values that may be based on some other property that
  112. * depends on recomposition.
  113. */
  114. public void compose(StylesheetRoot sroot) throws TransformerException
  115. {
  116. super.compose(sroot);
  117. java.util.Vector vnames = sroot.getComposeState().getVariableNames();
  118. if(null != m_name_atv)
  119. m_name_atv.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
  120. }
  121. /**
  122. * Get an int constant identifying the type of element.
  123. * @see org.apache.xalan.templates.Constants
  124. *
  125. * @return The token ID for the element
  126. */
  127. public int getXSLToken()
  128. {
  129. return Constants.ELEMNAME_PI;
  130. }
  131. /**
  132. * Return the node name.
  133. *
  134. * @return The element's name
  135. */
  136. public String getNodeName()
  137. {
  138. return Constants.ELEMNAME_PI_STRING;
  139. }
  140. /**
  141. * Create a processing instruction in the result tree.
  142. * The content of the xsl:processing-instruction element is a
  143. * template for the string-value of the processing instruction node.
  144. * @see <a href="http://www.w3.org/TR/xslt#section-Creating-Processing-Instructions">section-Creating-Processing-Instructions in XSLT Specification</a>
  145. *
  146. * @param transformer non-null reference to the the current transform-time state.
  147. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  148. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  149. *
  150. * @throws TransformerException
  151. */
  152. public void execute(
  153. TransformerImpl transformer)
  154. throws TransformerException
  155. {
  156. if (TransformerImpl.S_DEBUG)
  157. transformer.getTraceManager().fireTraceEvent(this);
  158. XPathContext xctxt = transformer.getXPathContext();
  159. int sourceNode = xctxt.getCurrentNode();
  160. String piName = m_name_atv == null ? null : m_name_atv.evaluate(xctxt, sourceNode, this);
  161. // Ignore processing instruction if name is null
  162. if (piName == null) return;
  163. if (piName.equalsIgnoreCase("xml"))
  164. {
  165. transformer.getMsgMgr().warn(
  166. this, XSLTErrorResources.WG_PROCESSINGINSTRUCTION_NAME_CANT_BE_XML,
  167. new Object[]{ Constants.ATTRNAME_NAME, piName });
  168. return;
  169. }
  170. // Only check if an avt was used (ie. this wasn't checked at compose time.)
  171. // Ignore processing instruction, if invalid
  172. else if ((!m_name_atv.isSimple()) && (!isValidNCName(piName)))
  173. {
  174. transformer.getMsgMgr().warn(
  175. this, XSLTErrorResources.WG_PROCESSINGINSTRUCTION_NOTVALID_NCNAME,
  176. new Object[]{ Constants.ATTRNAME_NAME, piName });
  177. return;
  178. }
  179. // Note the content model is:
  180. // <!ENTITY % instructions "
  181. // %char-instructions;
  182. // | xsl:processing-instruction
  183. // | xsl:comment
  184. // | xsl:element
  185. // | xsl:attribute
  186. // ">
  187. String data = transformer.transformToString(this);
  188. try
  189. {
  190. transformer.getResultTreeHandler().processingInstruction(piName, data);
  191. }
  192. catch(org.xml.sax.SAXException se)
  193. {
  194. throw new TransformerException(se);
  195. }
  196. if (TransformerImpl.S_DEBUG)
  197. transformer.getTraceManager().fireTraceEndEvent(this);
  198. }
  199. /**
  200. * Add a child to the child list.
  201. *
  202. * @param newChild Child to add to child list
  203. *
  204. * @return The child just added to the child list
  205. *
  206. * @throws DOMException
  207. */
  208. public ElemTemplateElement appendChild(ElemTemplateElement newChild)
  209. {
  210. int type = ((ElemTemplateElement) newChild).getXSLToken();
  211. switch (type)
  212. {
  213. // char-instructions
  214. case Constants.ELEMNAME_TEXTLITERALRESULT :
  215. case Constants.ELEMNAME_APPLY_TEMPLATES :
  216. case Constants.ELEMNAME_APPLY_IMPORTS :
  217. case Constants.ELEMNAME_CALLTEMPLATE :
  218. case Constants.ELEMNAME_FOREACH :
  219. case Constants.ELEMNAME_VALUEOF :
  220. case Constants.ELEMNAME_COPY_OF :
  221. case Constants.ELEMNAME_NUMBER :
  222. case Constants.ELEMNAME_CHOOSE :
  223. case Constants.ELEMNAME_IF :
  224. case Constants.ELEMNAME_TEXT :
  225. case Constants.ELEMNAME_COPY :
  226. case Constants.ELEMNAME_VARIABLE :
  227. case Constants.ELEMNAME_MESSAGE :
  228. // instructions
  229. // case Constants.ELEMNAME_PI:
  230. // case Constants.ELEMNAME_COMMENT:
  231. // case Constants.ELEMNAME_ELEMENT:
  232. // case Constants.ELEMNAME_ATTRIBUTE:
  233. break;
  234. default :
  235. error(XSLTErrorResources.ER_CANNOT_ADD,
  236. new Object[]{ newChild.getNodeName(),
  237. this.getNodeName() }); //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
  238. //" to " + this.m_elemName);
  239. }
  240. return super.appendChild(newChild);
  241. }
  242. }