KickJava   Java API By Example, From Geeks To Geeks.

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


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: StylesheetComposed.java,v 1.29 2004/02/16 20:32:32 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import java.util.Vector JavaDoc;
22
23 import javax.xml.transform.TransformerException JavaDoc;
24
25 /**
26  * Represents a stylesheet that has methods that resolve includes and
27  * imports. It has methods on it that
28  * return "composed" properties, which mean that:
29  * <ol>
30  * <li>Properties that are aggregates, like OutputProperties, will
31  * be composed of properties declared in this stylsheet and all
32  * included stylesheets.</li>
33  * <li>Properties that aren't found, will be searched for first in
34  * the includes, and, if none are located, will be searched for in
35  * the imports.</li>
36  * <li>Properties in that are not atomic on a stylesheet will
37  * have the form getXXXComposed. Some properties, like version and id,
38  * are not inherited, and so won't have getXXXComposed methods.</li>
39  * </ol>
40  * <p>In some cases getXXXComposed methods may calculate the composed
41  * values dynamically, while in other cases they may store the composed
42  * values.</p>
43  */

44 public class StylesheetComposed extends Stylesheet
45 {
46
47   /**
48    * Uses an XSL stylesheet document.
49    * @param parent The including or importing stylesheet.
50    */

51   public StylesheetComposed(Stylesheet parent)
52   {
53     super(parent);
54   }
55
56   /**
57    * Tell if this can be cast to a StylesheetComposed, meaning, you
58    * can ask questions from getXXXComposed functions.
59    *
60    * @return True since this is a StylesheetComposed
61    */

62   public boolean isAggregatedType()
63   {
64     return true;
65   }
66
67   /**
68    * Adds all recomposable values for this precedence level into the recomposableElements Vector
69    * that was passed in as the first parameter. All elements added to the
70    * recomposableElements vector should extend ElemTemplateElement.
71    * @param recomposableElements a Vector of ElemTemplateElement objects that we will add all of
72    * our recomposable objects to.
73    */

74   public void recompose(Vector JavaDoc recomposableElements) throws TransformerException JavaDoc
75   {
76
77     //recomposeImports(); // Calculate the number of this import.
78
//recomposeIncludes(this); // Build the global include list for this stylesheet.
79

80     // Now add in all of the recomposable elements at this precedence level
81

82     int n = getIncludeCountComposed();
83
84     for (int i = -1; i < n; i++)
85     {
86       Stylesheet included = getIncludeComposed(i);
87
88       // Add in the output elements
89

90       int s = included.getOutputCount();
91       for (int j = 0; j < s; j++)
92       {
93         recomposableElements.addElement(included.getOutput(j));
94       }
95
96       // Next, add in the attribute-set elements
97

98       s = included.getAttributeSetCount();
99       for (int j = 0; j < s; j++)
100       {
101         recomposableElements.addElement(included.getAttributeSet(j));
102       }
103
104       // Now the decimal-formats
105

106       s = included.getDecimalFormatCount();
107       for (int j = 0; j < s; j++)
108       {
109         recomposableElements.addElement(included.getDecimalFormat(j));
110       }
111
112       // Now the keys
113

114       s = included.getKeyCount();
115       for (int j = 0; j < s; j++)
116       {
117         recomposableElements.addElement(included.getKey(j));
118       }
119
120       // And the namespace aliases
121

122       s = included.getNamespaceAliasCount();
123       for (int j = 0; j < s; j++)
124       {
125         recomposableElements.addElement(included.getNamespaceAlias(j));
126       }
127
128       // Next comes the templates
129

130       s = included.getTemplateCount();
131       for (int j = 0; j < s; j++)
132       {
133         recomposableElements.addElement(included.getTemplate(j));
134       }
135
136       // Then, the variables
137

138       s = included.getVariableOrParamCount();
139       for (int j = 0; j < s; j++)
140       {
141         recomposableElements.addElement(included.getVariableOrParam(j));
142       }
143
144       // And lastly the whitespace preserving and stripping elements
145

146       s = included.getStripSpaceCount();
147       for (int j = 0; j < s; j++)
148       {
149         recomposableElements.addElement(included.getStripSpace(j));
150       }
151
152       s = included.getPreserveSpaceCount();
153       for (int j = 0; j < s; j++)
154       {
155         recomposableElements.addElement(included.getPreserveSpace(j));
156       }
157     }
158   }
159
160   /** Order in import chain.
161    * @serial */

162   private int m_importNumber = -1;
163
164   /** The precedence of this stylesheet in the global import list.
165    * The lowest precedence stylesheet is 0. A higher
166    * number has a higher precedence.
167    * @serial
168    */

169   private int m_importCountComposed;
170   
171   /* The count of imports composed for this stylesheet */
172   private int m_endImportCountComposed;
173
174   /**
175    * Recalculate the precedence of this stylesheet in the global
176    * import list. The lowest precedence stylesheet is 0. A higher
177    * number has a higher precedence.
178    */

179   void recomposeImports()
180   {
181
182     m_importNumber = getStylesheetRoot().getImportNumber(this);
183
184     StylesheetRoot root = getStylesheetRoot();
185     int globalImportCount = root.getGlobalImportCount();
186
187     m_importCountComposed = (globalImportCount - m_importNumber) - 1;
188     
189     // Now get the count of composed imports from this stylesheet's imports
190
int count = getImportCount();
191     if ( count > 0)
192     {
193       m_endImportCountComposed += count;
194       while (count > 0)
195         m_endImportCountComposed += this.getImport(--count).getEndImportCountComposed();
196     }
197     
198     // Now get the count of composed imports from this stylesheet's
199
// composed includes.
200
count = getIncludeCountComposed();
201     while (count>0)
202     {
203       int imports = getIncludeComposed(--count).getImportCount();
204       m_endImportCountComposed += imports;
205       while (imports > 0)
206         m_endImportCountComposed +=getIncludeComposed(count).getImport(--imports).getEndImportCountComposed();
207      
208     }
209   }
210
211   /**
212    * Get a stylesheet from the "import" list.
213    * @see <a HREF="http://www.w3.org/TR/xslt#import">import in XSLT Specification</a>
214    *
215    * @param i Index of stylesheet in import list
216    *
217    * @return The stylesheet at the given index
218    *
219    * @throws ArrayIndexOutOfBoundsException
220    */

221   public StylesheetComposed getImportComposed(int i)
222           throws ArrayIndexOutOfBoundsException JavaDoc
223   {
224
225     StylesheetRoot root = getStylesheetRoot();
226
227     // Get the stylesheet that is offset past this stylesheet.
228
// Thus, if the index of this stylesheet is 3, an argument
229
// to getImportComposed of 0 will return the 4th stylesheet
230
// in the global import list.
231
return root.getGlobalImport(1 + m_importNumber + i);
232   }
233
234   /**
235    * Get the precedence of this stylesheet in the global import list.
236    * The lowest precedence is 0. A higher number has a higher precedence.
237    * @see <a HREF="http://www.w3.org/TR/xslt#import">import in XSLT Specification</a>
238    *
239    * @return the precedence of this stylesheet in the global import list.
240    */

241   public int getImportCountComposed()
242   {
243     return m_importCountComposed;
244   }
245   
246   /**
247    * Get the number of import in this stylesheet's composed list.
248    *
249    * @return the number of imports in this stylesheet's composed list.
250    */

251   public int getEndImportCountComposed()
252   {
253     return m_endImportCountComposed;
254   }
255   
256
257   /**
258    * The combined list of includes.
259    * @serial
260    */

261   private transient Vector JavaDoc m_includesComposed;
262
263   /**
264    * Recompose the value of the composed include list. Builds a composite
265    * list of all stylesheets included by this stylesheet to any depth.
266    *
267    * @param including Stylesheet to recompose
268    */

269   void recomposeIncludes(Stylesheet including)
270   {
271
272     int n = including.getIncludeCount();
273
274     if (n > 0)
275     {
276       if (null == m_includesComposed)
277         m_includesComposed = new Vector JavaDoc();
278
279       for (int i = 0; i < n; i++)
280       {
281         Stylesheet included = including.getInclude(i);
282         m_includesComposed.addElement(included);
283         recomposeIncludes(included);
284       }
285     }
286   }
287
288   /**
289    * Get an "xsl:include" property.
290    * @see <a HREF="http://www.w3.org/TR/xslt#include">include in XSLT Specification</a>
291    *
292    * @param i Index of stylesheet in "include" list
293    *
294    * @return The stylesheet at the given index in the "include" list
295    *
296    * @throws ArrayIndexOutOfBoundsException
297    */

298   public Stylesheet getIncludeComposed(int i)
299           throws ArrayIndexOutOfBoundsException JavaDoc
300   {
301
302     if (-1 == i)
303       return this;
304
305     if (null == m_includesComposed)
306       throw new ArrayIndexOutOfBoundsException JavaDoc();
307
308     return (Stylesheet) m_includesComposed.elementAt(i);
309   }
310
311   /**
312    * Get the number of included stylesheets.
313    * @see <a HREF="http://www.w3.org/TR/xslt#import">import in XSLT Specification</a>
314    *
315    * @return the number of included stylesheets.
316    */

317   public int getIncludeCountComposed()
318   {
319     return (null != m_includesComposed) ? m_includesComposed.size() : 0;
320   }
321
322   /**
323    * For compilation support, we need the option of overwriting
324    * (rather than appending to) previous composition.
325    * We could phase out the old API in favor of this one, but I'm
326    * holding off until we've made up our minds about compilation.
327    * ADDED 9/5/2000 to support compilation experiment.
328    * NOTE: GLP 29-Nov-00 I've left this method in so that CompilingStylesheetHandler will compile. However,
329    * I'm not sure why it's needed or what it does and I've commented out the body.
330    *
331    * @see <a HREF="http://www.w3.org/TR/xslt#section-Defining-Template-Rules">section-Defining-Template-Rules in XSLT Specification</a>
332    * @param flushFirst Flag indicating the option of overwriting
333    * (rather than appending to) previous composition.
334    *
335    * @throws TransformerException
336    */

337   public void recomposeTemplates(boolean flushFirst) throws TransformerException JavaDoc
338   {
339 /*************************************** KEEP METHOD IN FOR COMPILATION
340     if (flushFirst)
341       m_templateList = new TemplateList(this);
342
343     recomposeTemplates();
344 *****************************************/

345   }
346 }
347
Popular Tags