KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > templates > TemplateSubPatternAssociation


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: TemplateSubPatternAssociation.java,v 1.13 2004/02/16 20:32:33 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import java.io.Serializable JavaDoc;
22
23 import javax.xml.transform.TransformerException JavaDoc;
24
25 import org.apache.xml.utils.QName;
26 import org.apache.xpath.XPath;
27 import org.apache.xpath.XPathContext;
28 import org.apache.xpath.patterns.StepPattern;
29
30 /**
31  * A class to contain a match pattern and it's corresponding template.
32  * This class also defines a node in a match pattern linked list.
33  */

34 class TemplateSubPatternAssociation implements Serializable JavaDoc, Cloneable JavaDoc
35 {
36
37   /** Step pattern */
38   StepPattern m_stepPattern;
39
40   /** Template pattern */
41   private String JavaDoc m_pattern;
42
43   /** The template element */
44   private ElemTemplate m_template;
45
46   /** Next pattern */
47   private TemplateSubPatternAssociation m_next = null;
48
49   /** Flag indicating whether this is wild card pattern */
50   private boolean m_wild;
51
52   /** Target string for this match pattern */
53   private String JavaDoc m_targetString;
54
55   /**
56    * Construct a match pattern from a pattern and template.
57    * @param template The node that contains the template for this pattern.
58    * @param pattern An executable XSLT StepPattern.
59    * @param pat For now a Nodelist that contains old-style element patterns.
60    */

61   TemplateSubPatternAssociation(ElemTemplate template, StepPattern pattern, String JavaDoc pat)
62   {
63
64     m_pattern = pat;
65     m_template = template;
66     m_stepPattern = pattern;
67     m_targetString = m_stepPattern.getTargetString();
68     m_wild = m_targetString.equals("*");
69   }
70
71   /**
72    * Clone this object.
73    *
74    * @return The cloned object.
75    *
76    * @throws CloneNotSupportedException
77    */

78   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
79   {
80
81     TemplateSubPatternAssociation tspa =
82       (TemplateSubPatternAssociation) super.clone();
83
84     tspa.m_next = null;
85
86     return tspa;
87   }
88
89   /**
90    * Get the target string of the pattern. For instance, if the pattern is
91    * "foo/baz/boo[@daba]", this string will be "boo".
92    *
93    * @return The "target" string.
94    */

95   public final String JavaDoc getTargetString()
96   {
97     return m_targetString;
98   }
99
100   /**
101    * Set Target String for this template pattern
102    *
103    *
104    * @param key Target string to set
105    */

106   public void setTargetString(String JavaDoc key)
107   {
108     m_targetString = key;
109   }
110
111   /**
112    * Tell if two modes match according to the rules of XSLT.
113    *
114    * @param m1 mode to match
115    *
116    * @return True if the given mode matches this template's mode
117    */

118   boolean matchMode(QName m1)
119   {
120     return matchModes(m1, m_template.getMode());
121   }
122
123   /**
124    * Tell if two modes match according to the rules of XSLT.
125    *
126    * @param m1 First mode to match
127    * @param m2 Second mode to match
128    *
129    * @return True if the two given modes match
130    */

131   private boolean matchModes(QName m1, QName m2)
132   {
133     return (((null == m1) && (null == m2))
134             || ((null != m1) && (null != m2) && m1.equals(m2)));
135   }
136
137   /**
138    * Return the mode associated with the template.
139    *
140    *
141    * @param xctxt XPath context to use with this template
142    * @param targetNode Target node
143    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
144    * @return The mode associated with the template.
145    *
146    * @throws TransformerException
147    */

148   public boolean matches(XPathContext xctxt, int targetNode, QName mode)
149           throws TransformerException JavaDoc
150   {
151
152     double score = m_stepPattern.getMatchScore(xctxt, targetNode);
153
154     return (XPath.MATCH_SCORE_NONE != score)
155            && matchModes(mode, m_template.getMode());
156   }
157
158   /**
159    * Tell if the pattern for this association is a wildcard.
160    *
161    * @return true if this pattern is considered to be a wild match.
162    */

163   public final boolean isWild()
164   {
165     return m_wild;
166   }
167
168   /**
169    * Get associated XSLT StepPattern.
170    *
171    * @return An executable StepPattern object, never null.
172    *
173    */

174   public final StepPattern getStepPattern()
175   {
176     return m_stepPattern;
177   }
178
179   /**
180    * Get the pattern string for diagnostic purposes.
181    *
182    * @return The pattern string for diagnostic purposes.
183    *
184    */

185   public final String JavaDoc getPattern()
186   {
187     return m_pattern;
188   }
189
190   /**
191    * Return the position of the template in document
192    * order in the stylesheet.
193    *
194    * @return The position of the template in the overall template order.
195    */

196   public int getDocOrderPos()
197   {
198     return m_template.getUid();
199   }
200
201   /**
202    * Return the import level associated with the stylesheet into which
203    * this template is composed.
204    *
205    * @return The import level of this template.
206    */

207   public final int getImportLevel()
208   {
209     return m_template.getStylesheetComposed().getImportCountComposed();
210   }
211
212   /**
213    * Get the assocated xsl:template.
214    *
215    * @return An ElemTemplate, never null.
216    *
217    */

218   public final ElemTemplate getTemplate()
219   {
220     return m_template;
221   }
222
223   /**
224    * Get the next association.
225    *
226    * @return A valid TemplateSubPatternAssociation, or null.
227    */

228   public final TemplateSubPatternAssociation getNext()
229   {
230     return m_next;
231   }
232
233   /**
234    * Set the next element on this association
235    * list, which should be equal or less in priority to
236    * this association, and, if equal priority, should occur
237    * before this template in document order.
238    *
239    * @param mp The next association to score if this one fails.
240    *
241    */

242   public void setNext(TemplateSubPatternAssociation mp)
243   {
244     m_next = mp;
245   }
246 }
247
Popular Tags