KickJava   Java API By Example, From Geeks To Geeks.

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


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: ElemUse.java,v 1.19 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 import org.apache.xalan.res.XSLMessages;
26 import org.apache.xalan.res.XSLTErrorResources;
27 import org.apache.xalan.transformer.TransformerImpl;
28 import org.apache.xml.utils.QName;
29
30 /**
31  * Implement xsl:use.
32  * This acts as a superclass for ElemCopy, ElemAttributeSet,
33  * ElemElement, and ElemLiteralResult, on order to implement
34  * shared behavior the use-attribute-sets attribute.
35  * @see <a HREF="http://www.w3.org/TR/xslt#attribute-sets">attribute-sets in XSLT Specification</a>
36  * @xsl.usage advanced
37  */

38 public class ElemUse extends ElemTemplateElement
39 {
40
41   /**
42    * The value of the "use-attribute-sets" attribute.
43    * @serial
44    */

45   private QName m_attributeSetsNames[] = null;
46
47   /**
48    * Set the "use-attribute-sets" attribute.
49    * Attribute sets are used by specifying a use-attribute-sets
50    * attribute on xsl:element, xsl:copy (see [7.5 Copying]) or
51    * xsl:attribute-set elements. The value of the use-attribute-sets
52    * attribute is a whitespace-separated list of names of attribute
53    * sets. Each name is specified as a QName, which is expanded as
54    * described in [2.4 Qualified Names].
55    *
56    * @param v The value to set for the "use-attribute-sets" attribute.
57    */

58   public void setUseAttributeSets(Vector JavaDoc v)
59   {
60
61     int n = v.size();
62
63     m_attributeSetsNames = new QName[n];
64
65     for (int i = 0; i < n; i++)
66     {
67       m_attributeSetsNames[i] = (QName) v.elementAt(i);
68     }
69   }
70
71   /**
72    * Set the "use-attribute-sets" attribute.
73    * Attribute sets are used by specifying a use-attribute-sets
74    * attribute on xsl:element, xsl:copy (see [7.5 Copying]) or
75    * xsl:attribute-set elements. The value of the use-attribute-sets
76    * attribute is a whitespace-separated list of names of attribute
77    * sets. Each name is specified as a QName, which is expanded as
78    * described in [2.4 Qualified Names].
79    *
80    * @param v The value to set for the "use-attribute-sets" attribute.
81    */

82   public void setUseAttributeSets(QName[] v)
83   {
84     m_attributeSetsNames = v;
85   }
86
87   /**
88    * Get the "use-attribute-sets" attribute.
89    * Attribute sets are used by specifying a use-attribute-sets
90    * attribute on xsl:element, xsl:copy (see [7.5 Copying]) or
91    * xsl:attribute-set elements, or a xsl:use-attribute-sets attribute on
92    * Literal Result Elements.
93    * The value of the use-attribute-sets
94    * attribute is a whitespace-separated list of names of attribute
95    * sets. Each name is specified as a QName, which is expanded as
96    * described in [2.4 Qualified Names].
97    *
98    * @return The value of the "use-attribute-sets" attribute.
99    */

100   public QName[] getUseAttributeSets()
101   {
102     return m_attributeSetsNames;
103   }
104   
105   /**
106    * Add the attributes from the named attribute sets to the attribute list.
107    * TODO: Error handling for: "It is an error if there are two attribute sets
108    * with the same expanded-name and with equal import precedence and that both
109    * contain the same attribute unless there is a definition of the attribute
110    * set with higher import precedence that also contains the attribute."
111    *
112    * @param transformer non-null reference to the the current transform-time state.
113    * @param stylesheet The owning root stylesheet
114    * @param attributeSetsNames List of attribute sets names to apply
115    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
116    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
117    *
118    * @throws TransformerException
119    */

120   public void applyAttrSets(
121           TransformerImpl transformer, StylesheetRoot stylesheet)
122             throws TransformerException JavaDoc
123   {
124     applyAttrSets(transformer, stylesheet, m_attributeSetsNames);
125   }
126
127   /**
128    * Add the attributes from the named attribute sets to the attribute list.
129    * TODO: Error handling for: "It is an error if there are two attribute sets
130    * with the same expanded-name and with equal import precedence and that both
131    * contain the same attribute unless there is a definition of the attribute
132    * set with higher import precedence that also contains the attribute."
133    *
134    * @param transformer non-null reference to the the current transform-time state.
135    * @param stylesheet The owning root stylesheet
136    * @param attributeSetsNames List of attribute sets names to apply
137    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
138    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
139    *
140    * @throws TransformerException
141    */

142   private void applyAttrSets(
143           TransformerImpl transformer, StylesheetRoot stylesheet, QName attributeSetsNames[])
144             throws TransformerException JavaDoc
145   {
146
147     if (null != attributeSetsNames)
148     {
149       int nNames = attributeSetsNames.length;
150
151       for (int i = 0; i < nNames; i++)
152       {
153         QName qname = attributeSetsNames[i];
154         Vector JavaDoc attrSets = stylesheet.getAttributeSetComposed(qname);
155
156         if (null != attrSets)
157         {
158           int nSets = attrSets.size();
159
160           // Highest priority attribute set will be at the top,
161
// so process it last.
162
for (int k = nSets-1; k >= 0 ; k--)
163           {
164             ElemAttributeSet attrSet =
165               (ElemAttributeSet) attrSets.elementAt(k);
166
167             attrSet.execute(transformer);
168           }
169         }
170         else
171         {
172           throw new TransformerException JavaDoc(
173               XSLMessages.createMessage(XSLTErrorResources.ER_NO_ATTRIB_SET,
174                   new Object JavaDoc[] {qname}),this);
175         }
176       }
177     }
178   }
179
180   /**
181    * Copy attributes specified by use-attribute-sets to the result tree.
182    * Specifying a use-attribute-sets attribute is equivalent to adding
183    * xsl:attribute elements for each of the attributes in each of the
184    * named attribute sets to the beginning of the content of the element
185    * with the use-attribute-sets attribute, in the same order in which
186    * the names of the attribute sets are specified in the use-attribute-sets
187    * attribute. It is an error if use of use-attribute-sets attributes
188    * on xsl:attribute-set elements causes an attribute set to directly
189    * or indirectly use itself.
190    *
191    * @param transformer non-null reference to the the current transform-time state.
192    * @param sourceNode non-null reference to the <a HREF="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
193    * @param mode reference, which may be null, to the <a HREF="http://www.w3.org/TR/xslt#modes">current mode</a>.
194    *
195    * @throws TransformerException
196    */

197   public void execute(
198           TransformerImpl transformer)
199             throws TransformerException JavaDoc
200   {
201
202     if (null != m_attributeSetsNames)
203     {
204       applyAttrSets(transformer, getStylesheetRoot(),
205                     m_attributeSetsNames);
206     }
207  
208   }
209 }
210
Popular Tags