KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > processor > XSLTElementProcessor


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: XSLTElementProcessor.java,v 1.18 2004/02/11 18:15:50 minchau Exp $
18  */

19 package org.apache.xalan.processor;
20
21 import java.util.Vector JavaDoc;
22
23 import org.apache.xalan.res.XSLMessages;
24 import org.apache.xalan.res.XSLTErrorResources;
25 import org.apache.xalan.templates.ElemTemplateElement;
26 import org.apache.xml.utils.IntStack;
27
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.helpers.AttributesImpl JavaDoc;
31
32 /**
33  * This class acts as the superclass for all stylesheet element
34  * processors, and deals with things that are common to all elements.
35  * @see <a HREF="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
36  */

37 public class XSLTElementProcessor extends ElemTemplateElement
38 {
39
40   /**
41    * Construct a processor for top-level elements.
42    * @see <a HREF="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
43    */

44   XSLTElementProcessor(){}
45     
46     private IntStack m_savedLastOrder;
47
48   /**
49    * The element definition that this processor conforms to.
50    */

51   private XSLTElementDef m_elemDef;
52
53   /**
54    * Get the element definition that belongs to this element.
55    *
56    * @return The element definition object that produced and constrains this element.
57    */

58   XSLTElementDef getElemDef()
59   {
60     return m_elemDef;
61   }
62
63   /**
64    * Set the element definition that belongs to this element.
65    *
66    * @param def The element definition object that produced and constrains this element.
67    */

68   void setElemDef(XSLTElementDef def)
69   {
70     m_elemDef = def;
71   }
72
73   /**
74    * Resolve an external entity.
75    *
76    *
77    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
78    * @param publicId The public identifer, or null if none is
79    * available.
80    * @param systemId The system identifier provided in the XML
81    * document.
82    * @return The new input source, or null to require the
83    * default behaviour.
84    */

85   public InputSource JavaDoc resolveEntity(
86           StylesheetHandler handler, String JavaDoc publicId, String JavaDoc systemId)
87             throws org.xml.sax.SAXException JavaDoc
88   {
89     return null;
90   }
91
92   /**
93    * Receive notification of a notation declaration.
94    *
95    *
96    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
97    * @param name The notation name.
98    * @param publicId The notation public identifier, or null if not
99    * available.
100    * @param systemId The notation system identifier.
101    * @see org.xml.sax.DTDHandler#notationDecl
102    */

103   public void notationDecl(StylesheetHandler handler, String JavaDoc name,
104                            String JavaDoc publicId, String JavaDoc systemId)
105   {
106
107     // no op
108
}
109
110   /**
111    * Receive notification of an unparsed entity declaration.
112    *
113    *
114    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
115    * @param name The entity name.
116    * @param publicId The entity public identifier, or null if not
117    * available.
118    * @param systemId The entity system identifier.
119    * @param notationName The name of the associated notation.
120    * @see org.xml.sax.DTDHandler#unparsedEntityDecl
121    */

122   public void unparsedEntityDecl(StylesheetHandler handler, String JavaDoc name,
123                                  String JavaDoc publicId, String JavaDoc systemId,
124                                  String JavaDoc notationName)
125   {
126
127     // no op
128
}
129
130   /**
131    * Receive notification of the start of the non-text event. This
132    * is sent to the current processor when any non-text event occurs.
133    *
134    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
135    */

136   public void startNonText(StylesheetHandler handler) throws org.xml.sax.SAXException JavaDoc
137   {
138
139     // no op
140
}
141
142   /**
143    * Receive notification of the start of an element.
144    *
145    * @param name The element type name.
146    *
147    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
148    * @param uri The Namespace URI, or an empty string.
149    * @param localName The local name (without prefix), or empty string if not namespace processing.
150    * @param rawName The qualified name (with prefix).
151    * @param attributes The specified or defaulted attributes.
152    */

153   public void startElement(
154           StylesheetHandler handler, String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes JavaDoc attributes)
155             throws org.xml.sax.SAXException JavaDoc
156   {
157
158     if (m_savedLastOrder == null)
159                 m_savedLastOrder = new IntStack();
160             m_savedLastOrder.push(getElemDef().getLastOrder());
161             getElemDef().setLastOrder(-1);
162   }
163
164   /**
165    * Receive notification of the end of an element.
166    *
167    * @param name The element type name.
168    * @param attributes The specified or defaulted attributes.
169    *
170    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
171    * @param uri The Namespace URI, or an empty string.
172    * @param localName The local name (without prefix), or empty string if not namespace processing.
173    * @param rawName The qualified name (with prefix).
174    */

175   public void endElement(
176           StylesheetHandler handler, String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName)
177             throws org.xml.sax.SAXException JavaDoc
178   {
179         if (m_savedLastOrder != null && !m_savedLastOrder.empty())
180             getElemDef().setLastOrder(m_savedLastOrder.pop());
181         
182         if (!getElemDef().getRequiredFound())
183             handler.error(XSLTErrorResources.ER_REQUIRED_ELEM_NOT_FOUND, new Object JavaDoc[]{getElemDef().getRequiredElem()}, null);
184   }
185
186   /**
187    * Receive notification of character data inside an element.
188    *
189    *
190    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
191    * @param ch The characters.
192    * @param start The start position in the character array.
193    * @param length The number of characters to use from the
194    * character array.
195    */

196   public void characters(
197           StylesheetHandler handler, char ch[], int start, int length)
198             throws org.xml.sax.SAXException JavaDoc
199   {
200     handler.error(XSLTErrorResources.ER_CHARS_NOT_ALLOWED, null, null);//"Characters are not allowed at this point in the document!",
201
//null);
202
}
203
204   /**
205    * Receive notification of ignorable whitespace in element content.
206    *
207    *
208    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
209    * @param ch The whitespace characters.
210    * @param start The start position in the character array.
211    * @param length The number of characters to use from the
212    * character array.
213    */

214   public void ignorableWhitespace(
215           StylesheetHandler handler, char ch[], int start, int length)
216             throws org.xml.sax.SAXException JavaDoc
217   {
218
219     // no op
220
}
221
222   /**
223    * Receive notification of a processing instruction.
224    *
225    *
226    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
227    * @param target The processing instruction target.
228    * @param data The processing instruction data, or null if
229    * none is supplied.
230    */

231   public void processingInstruction(
232           StylesheetHandler handler, String JavaDoc target, String JavaDoc data)
233             throws org.xml.sax.SAXException JavaDoc
234   {
235
236     // no op
237
}
238
239   /**
240    * Receive notification of a skipped entity.
241    *
242    *
243    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
244    * @param name The name of the skipped entity.
245    */

246   public void skippedEntity(StylesheetHandler handler, String JavaDoc name)
247           throws org.xml.sax.SAXException JavaDoc
248   {
249
250     // no op
251
}
252
253   /**
254    * Set the properties of an object from the given attribute list.
255    * @param handler The stylesheet's Content handler, needed for
256    * error reporting.
257    * @param rawName The raw name of the owner element, needed for
258    * error reporting.
259    * @param attributes The list of attributes.
260    * @param target The target element where the properties will be set.
261    */

262   void setPropertiesFromAttributes(
263           StylesheetHandler handler, String JavaDoc rawName, Attributes JavaDoc attributes,
264           ElemTemplateElement target)
265             throws org.xml.sax.SAXException JavaDoc
266   {
267     setPropertiesFromAttributes(handler, rawName, attributes, target, true);
268   }
269
270   /**
271    * Set the properties of an object from the given attribute list.
272    * @param handler The stylesheet's Content handler, needed for
273    * error reporting.
274    * @param rawName The raw name of the owner element, needed for
275    * error reporting.
276    * @param attributes The list of attributes.
277    * @param target The target element where the properties will be set.
278    * @param throwError True if it should throw an error if an
279    * attribute is not defined.
280    * @return the attributes not allowed on this element.
281    *
282    * @throws TransformerException
283    */

284   Attributes JavaDoc setPropertiesFromAttributes(
285           StylesheetHandler handler, String JavaDoc rawName, Attributes JavaDoc attributes,
286           ElemTemplateElement target, boolean throwError)
287             throws org.xml.sax.SAXException JavaDoc
288   {
289
290     XSLTElementDef def = getElemDef();
291     AttributesImpl JavaDoc undefines = null;
292     boolean isCompatibleMode = ((null != handler.getStylesheet()
293                                  && handler.getStylesheet().getCompatibleMode())
294                                 || !throwError);
295     if (isCompatibleMode)
296       undefines = new AttributesImpl JavaDoc();
297
298
299     // Keep track of which XSLTAttributeDefs have been processed, so
300
// I can see which default values need to be set.
301
Vector JavaDoc processedDefs = new Vector JavaDoc();
302
303     // Keep track of XSLTAttributeDefs that were invalid
304
Vector JavaDoc errorDefs = new Vector JavaDoc();
305     int nAttrs = attributes.getLength();
306
307     for (int i = 0; i < nAttrs; i++)
308     {
309       String JavaDoc attrUri = attributes.getURI(i);
310       // Hack for Crimson. -sb
311
if((null != attrUri) && (attrUri.length() == 0)
312                            && (attributes.getQName(i).startsWith("xmlns:") ||
313                                attributes.getQName(i).equals("xmlns")))
314       {
315         attrUri = org.apache.xalan.templates.Constants.S_XMLNAMESPACEURI;
316       }
317       String JavaDoc attrLocalName = attributes.getLocalName(i);
318       XSLTAttributeDef attrDef = def.getAttributeDef(attrUri, attrLocalName);
319
320       if (null == attrDef)
321       {
322         if (!isCompatibleMode)
323         {
324
325           // Then barf, because this element does not allow this attribute.
326
handler.error(XSLTErrorResources.ER_ATTR_NOT_ALLOWED, new Object JavaDoc[]{attributes.getQName(i), rawName}, null);//"\""+attributes.getQName(i)+"\""
327
//+ " attribute is not allowed on the " + rawName
328
// + " element!", null);
329
}
330         else
331         {
332           undefines.addAttribute(attrUri, attrLocalName,
333                                  attributes.getQName(i),
334                                  attributes.getType(i),
335                                  attributes.getValue(i));
336         }
337       }
338       else
339       {
340         // Can we switch the order here:
341

342         boolean success = attrDef.setAttrValue(handler, attrUri, attrLocalName,
343                              attributes.getQName(i), attributes.getValue(i),
344                              target);
345                              
346         // Now we only add the element if it passed a validation check
347
if (success)
348             processedDefs.addElement(attrDef);
349         else
350             errorDefs.addElement(attrDef);
351       }
352     }
353
354     XSLTAttributeDef[] attrDefs = def.getAttributes();
355     int nAttrDefs = attrDefs.length;
356
357     for (int i = 0; i < nAttrDefs; i++)
358     {
359       XSLTAttributeDef attrDef = attrDefs[i];
360       String JavaDoc defVal = attrDef.getDefault();
361
362       if (null != defVal)
363       {
364         if (!processedDefs.contains(attrDef))
365         {
366           attrDef.setDefAttrValue(handler, target);
367         }
368       }
369
370       if (attrDef.getRequired())
371       {
372         if ((!processedDefs.contains(attrDef)) && (!errorDefs.contains(attrDef)))
373           handler.error(
374             XSLMessages.createMessage(
375               XSLTErrorResources.ER_REQUIRES_ATTRIB, new Object JavaDoc[]{ rawName,
376                                                                    attrDef.getName() }), null);
377       }
378     }
379
380     return undefines;
381   }
382 }
383
Popular Tags