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.Serializable;
  59. //import org.w3c.dom.Node;
  60. import javax.xml.transform.TransformerException;
  61. import org.apache.xpath.XPath;
  62. import org.apache.xpath.XPathContext;
  63. import org.apache.xpath.patterns.StepPattern;
  64. import org.apache.xml.utils.QName;
  65. /**
  66. * A class to contain a match pattern and it's corresponding template.
  67. * This class also defines a node in a match pattern linked list.
  68. */
  69. class TemplateSubPatternAssociation implements Serializable, Cloneable
  70. {
  71. /** Step pattern */
  72. StepPattern m_stepPattern;
  73. /** Template pattern */
  74. private String m_pattern;
  75. /** The template element */
  76. private ElemTemplate m_template;
  77. /** Next pattern */
  78. private TemplateSubPatternAssociation m_next = null;
  79. /** Flag indicating whether this is wild card pattern */
  80. private boolean m_wild;
  81. /** Target string for this match pattern */
  82. private String m_targetString;
  83. /**
  84. * Construct a match pattern from a pattern and template.
  85. * @param template The node that contains the template for this pattern.
  86. * @param pattern An executable XSLT StepPattern.
  87. * @param pat For now a Nodelist that contains old-style element patterns.
  88. */
  89. TemplateSubPatternAssociation(ElemTemplate template, StepPattern pattern, String pat)
  90. {
  91. m_pattern = pat;
  92. m_template = template;
  93. m_stepPattern = pattern;
  94. m_targetString = m_stepPattern.getTargetString();
  95. m_wild = m_targetString.equals("*");
  96. }
  97. /**
  98. * Clone this object.
  99. *
  100. * @return The cloned object.
  101. *
  102. * @throws CloneNotSupportedException
  103. */
  104. public Object clone() throws CloneNotSupportedException
  105. {
  106. TemplateSubPatternAssociation tspa =
  107. (TemplateSubPatternAssociation) super.clone();
  108. tspa.m_next = null;
  109. return tspa;
  110. }
  111. /**
  112. * Get the target string of the pattern. For instance, if the pattern is
  113. * "foo/baz/boo[@daba]", this string will be "boo".
  114. *
  115. * @return The "target" string.
  116. */
  117. public final String getTargetString()
  118. {
  119. return m_targetString;
  120. }
  121. /**
  122. * Set Target String for this template pattern
  123. *
  124. *
  125. * @param key Target string to set
  126. */
  127. public void setTargetString(String key)
  128. {
  129. m_targetString = key;
  130. }
  131. /**
  132. * Tell if two modes match according to the rules of XSLT.
  133. *
  134. * @param m1 mode to match
  135. *
  136. * @return True if the given mode matches this template's mode
  137. */
  138. boolean matchMode(QName m1)
  139. {
  140. return matchModes(m1, m_template.getMode());
  141. }
  142. /**
  143. * Tell if two modes match according to the rules of XSLT.
  144. *
  145. * @param m1 First mode to match
  146. * @param m2 Second mode to match
  147. *
  148. * @return True if the two given modes match
  149. */
  150. private boolean matchModes(QName m1, QName m2)
  151. {
  152. return (((null == m1) && (null == m2))
  153. || ((null != m1) && (null != m2) && m1.equals(m2)));
  154. }
  155. /**
  156. * Return the mode associated with the template.
  157. *
  158. *
  159. * @param xctxt XPath context to use with this template
  160. * @param targetNode Target node
  161. * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
  162. * @return The mode associated with the template.
  163. *
  164. * @throws TransformerException
  165. */
  166. public boolean matches(XPathContext xctxt, int targetNode, QName mode)
  167. throws TransformerException
  168. {
  169. double score = m_stepPattern.getMatchScore(xctxt, targetNode);
  170. return (XPath.MATCH_SCORE_NONE != score)
  171. && matchModes(mode, m_template.getMode());
  172. }
  173. /**
  174. * Tell if the pattern for this association is a wildcard.
  175. *
  176. * @return true if this pattern is considered to be a wild match.
  177. */
  178. public final boolean isWild()
  179. {
  180. return m_wild;
  181. }
  182. /**
  183. * Get associated XSLT StepPattern.
  184. *
  185. * @return An executable StepPattern object, never null.
  186. *
  187. */
  188. public final StepPattern getStepPattern()
  189. {
  190. return m_stepPattern;
  191. }
  192. /**
  193. * Get the pattern string for diagnostic purposes.
  194. *
  195. * @return The pattern string for diagnostic purposes.
  196. *
  197. */
  198. public final String getPattern()
  199. {
  200. return m_pattern;
  201. }
  202. /**
  203. * Return the position of the template in document
  204. * order in the stylesheet.
  205. *
  206. * @return The position of the template in the overall template order.
  207. */
  208. public int getDocOrderPos()
  209. {
  210. return m_template.getUid();
  211. }
  212. /**
  213. * Return the import level associated with the stylesheet into which
  214. * this template is composed.
  215. *
  216. * @return The import level of this template.
  217. */
  218. public final int getImportLevel()
  219. {
  220. return m_template.getStylesheetComposed().getImportCountComposed();
  221. }
  222. /**
  223. * Get the assocated xsl:template.
  224. *
  225. * @return An ElemTemplate, never null.
  226. *
  227. */
  228. public final ElemTemplate getTemplate()
  229. {
  230. return m_template;
  231. }
  232. /**
  233. * Get the next association.
  234. *
  235. * @return A valid TemplateSubPatternAssociation, or null.
  236. */
  237. public final TemplateSubPatternAssociation getNext()
  238. {
  239. return m_next;
  240. }
  241. /**
  242. * Set the next element on this association
  243. * list, which should be equal or less in priority to
  244. * this association, and, if equal priority, should occur
  245. * before this template in document order.
  246. *
  247. * @param mp The next association to score if this one fails.
  248. *
  249. */
  250. public void setNext(TemplateSubPatternAssociation mp)
  251. {
  252. m_next = mp;
  253. }
  254. }