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.util.Vector;
  59. import org.apache.xml.dtm.DTM;
  60. import javax.xml.transform.TransformerException;
  61. import org.apache.xpath.*;
  62. import org.apache.xml.utils.QName;
  63. import java.util.StringTokenizer;
  64. import org.apache.xalan.transformer.TransformerImpl;
  65. /**
  66. * <meta name="usage" content="advanced"/>
  67. * Implement xsl:use.
  68. * This acts as a superclass for ElemCopy, ElemAttributeSet,
  69. * ElemElement, and ElemLiteralResult, on order to implement
  70. * shared behavior the use-attribute-sets attribute.
  71. * @see <a href="http://www.w3.org/TR/xslt#attribute-sets">attribute-sets in XSLT Specification</a>
  72. */
  73. public class ElemUse extends ElemTemplateElement
  74. {
  75. /**
  76. * The value of the "use-attribute-sets" attribute.
  77. * @serial
  78. */
  79. private QName m_attributeSetsNames[] = null;
  80. /**
  81. * Set the "use-attribute-sets" attribute.
  82. * Attribute sets are used by specifying a use-attribute-sets
  83. * attribute on xsl:element, xsl:copy (see [7.5 Copying]) or
  84. * xsl:attribute-set elements. The value of the use-attribute-sets
  85. * attribute is a whitespace-separated list of names of attribute
  86. * sets. Each name is specified as a QName, which is expanded as
  87. * described in [2.4 Qualified Names].
  88. *
  89. * @param v The value to set for the "use-attribute-sets" attribute.
  90. */
  91. public void setUseAttributeSets(Vector v)
  92. {
  93. int n = v.size();
  94. m_attributeSetsNames = new QName[n];
  95. for (int i = 0; i < n; i++)
  96. {
  97. m_attributeSetsNames[i] = (QName) v.elementAt(i);
  98. }
  99. }
  100. /**
  101. * Set the "use-attribute-sets" attribute.
  102. * Attribute sets are used by specifying a use-attribute-sets
  103. * attribute on xsl:element, xsl:copy (see [7.5 Copying]) or
  104. * xsl:attribute-set elements. The value of the use-attribute-sets
  105. * attribute is a whitespace-separated list of names of attribute
  106. * sets. Each name is specified as a QName, which is expanded as
  107. * described in [2.4 Qualified Names].
  108. *
  109. * @param v The value to set for the "use-attribute-sets" attribute.
  110. */
  111. public void setUseAttributeSets(QName[] v)
  112. {
  113. m_attributeSetsNames = v;
  114. }
  115. /**
  116. * Get the "use-attribute-sets" attribute.
  117. * Attribute sets are used by specifying a use-attribute-sets
  118. * attribute on xsl:element, xsl:copy (see [7.5 Copying]) or
  119. * xsl:attribute-set elements, or a xsl:use-attribute-sets attribute on
  120. * Literal Result Elements.
  121. * The value of the use-attribute-sets
  122. * attribute is a whitespace-separated list of names of attribute
  123. * sets. Each name is specified as a QName, which is expanded as
  124. * described in [2.4 Qualified Names].
  125. *
  126. * @return The value of the "use-attribute-sets" attribute.
  127. */
  128. public QName[] getUseAttributeSets()
  129. {
  130. return m_attributeSetsNames;
  131. }
  132. /**
  133. * Add the attributes from the named attribute sets to the attribute list.
  134. * TODO: Error handling for: "It is an error if there are two attribute sets
  135. * with the same expanded-name and with equal import precedence and that both
  136. * contain the same attribute unless there is a definition of the attribute
  137. * set with higher import precedence that also contains the attribute."
  138. *
  139. * @param transformer non-null reference to the the current transform-time state.
  140. * @param stylesheet The owning root stylesheet
  141. * @param attributeSetsNames List of attribute sets names to apply
  142. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  143. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  144. *
  145. * @throws TransformerException
  146. */
  147. public void applyAttrSets(
  148. TransformerImpl transformer, StylesheetRoot stylesheet)
  149. throws TransformerException
  150. {
  151. applyAttrSets(transformer, stylesheet, m_attributeSetsNames);
  152. }
  153. /**
  154. * Add the attributes from the named attribute sets to the attribute list.
  155. * TODO: Error handling for: "It is an error if there are two attribute sets
  156. * with the same expanded-name and with equal import precedence and that both
  157. * contain the same attribute unless there is a definition of the attribute
  158. * set with higher import precedence that also contains the attribute."
  159. *
  160. * @param transformer non-null reference to the the current transform-time state.
  161. * @param stylesheet The owning root stylesheet
  162. * @param attributeSetsNames List of attribute sets names to apply
  163. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  164. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  165. *
  166. * @throws TransformerException
  167. */
  168. private void applyAttrSets(
  169. TransformerImpl transformer, StylesheetRoot stylesheet, QName attributeSetsNames[])
  170. throws TransformerException
  171. {
  172. if (null != attributeSetsNames)
  173. {
  174. int nNames = attributeSetsNames.length;
  175. for (int i = 0; i < nNames; i++)
  176. {
  177. QName qname = attributeSetsNames[i];
  178. Vector attrSets = stylesheet.getAttributeSetComposed(qname);
  179. if (null != attrSets)
  180. {
  181. int nSets = attrSets.size();
  182. // Highest priority attribute set will be at the top,
  183. // so process it last.
  184. for (int k = nSets-1; k >= 0 ; k--)
  185. {
  186. ElemAttributeSet attrSet =
  187. (ElemAttributeSet) attrSets.elementAt(k);
  188. attrSet.execute(transformer);
  189. }
  190. }
  191. }
  192. }
  193. }
  194. /**
  195. * Copy attributes specified by use-attribute-sets to the result tree.
  196. * Specifying a use-attribute-sets attribute is equivalent to adding
  197. * xsl:attribute elements for each of the attributes in each of the
  198. * named attribute sets to the beginning of the content of the element
  199. * with the use-attribute-sets attribute, in the same order in which
  200. * the names of the attribute sets are specified in the use-attribute-sets
  201. * attribute. It is an error if use of use-attribute-sets attributes
  202. * on xsl:attribute-set elements causes an attribute set to directly
  203. * or indirectly use itself.
  204. *
  205. * @param transformer non-null reference to the the current transform-time state.
  206. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
  207. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  208. *
  209. * @throws TransformerException
  210. */
  211. public void execute(
  212. TransformerImpl transformer)
  213. throws TransformerException
  214. {
  215. if (TransformerImpl.S_DEBUG)
  216. transformer.getTraceManager().fireTraceEvent(this);
  217. if (null != m_attributeSetsNames)
  218. {
  219. applyAttrSets(transformer, getStylesheetRoot(),
  220. m_attributeSetsNames);
  221. }
  222. if (TransformerImpl.S_DEBUG)
  223. transformer.getTraceManager().fireTraceEndEvent(this);
  224. }
  225. }