KickJava   Java API By Example, From Geeks To Geeks.

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


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: ProcessorLRE.java,v 1.24 2004/02/11 18:15:51 minchau Exp $
18  */

19 package org.apache.xalan.processor;
20
21 import java.util.Vector JavaDoc;
22
23 import javax.xml.transform.TransformerConfigurationException JavaDoc;
24 import javax.xml.transform.TransformerException JavaDoc;
25
26 import org.apache.xalan.res.XSLMessages;
27 import org.apache.xalan.res.XSLTErrorResources;
28 import org.apache.xalan.templates.Constants;
29 import org.apache.xalan.templates.ElemExtensionCall;
30 import org.apache.xalan.templates.ElemLiteralResult;
31 import org.apache.xalan.templates.ElemTemplate;
32 import org.apache.xalan.templates.ElemTemplateElement;
33 import org.apache.xalan.templates.Stylesheet;
34 import org.apache.xalan.templates.StylesheetRoot;
35 import org.apache.xalan.templates.XMLNSDecl;
36 import org.apache.xml.utils.SAXSourceLocator;
37 import org.apache.xpath.XPath;
38
39 import org.xml.sax.Attributes JavaDoc;
40 import org.xml.sax.Locator JavaDoc;
41 import org.xml.sax.helpers.AttributesImpl JavaDoc;
42
43 /**
44  * Processes an XSLT literal-result-element, or something that looks
45  * like one. The actual {@link org.apache.xalan.templates.ElemTemplateElement}
46  * produced may be a {@link org.apache.xalan.templates.ElemLiteralResult},
47  * a {@link org.apache.xalan.templates.StylesheetRoot}, or a
48  * {@link org.apache.xalan.templates.ElemExtensionCall}.
49  *
50  * @see <a HREF="http://www.w3.org/TR/xslt#literal-result-element">literal-result-element in XSLT Specification</a>
51  * @see org.apache.xalan.templates.ElemLiteralResult
52  * @xsl.usage internal
53  */

54 public class ProcessorLRE extends ProcessorTemplateElem
55 {
56
57   /**
58    * Receive notification of the start of an element.
59    *
60    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
61    * @param uri The Namespace URI, or an empty string.
62    * @param localName The local name (without prefix), or empty string if not namespace processing.
63    * @param rawName The qualified name (with prefix).
64    * @param attributes The specified or defaulted attributes.
65    */

66   public void startElement(
67           StylesheetHandler handler, String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes JavaDoc attributes)
68             throws org.xml.sax.SAXException JavaDoc
69   {
70
71     try
72     {
73       ElemTemplateElement p = handler.getElemTemplateElement();
74       boolean excludeXSLDecl = false;
75       boolean isLREAsStyleSheet = false;
76
77       if (null == p)
78       {
79
80         // Literal Result Template as stylesheet.
81
XSLTElementProcessor lreProcessor = handler.popProcessor();
82         XSLTElementProcessor stylesheetProcessor =
83                                                   handler.getProcessorFor(Constants.S_XSLNAMESPACEURL, "stylesheet",
84                                                                           "xsl:stylesheet");
85
86         handler.pushProcessor(lreProcessor);
87
88         Stylesheet stylesheet;
89         try
90         {
91           stylesheet = new StylesheetRoot(handler.getSchema(), handler.getStylesheetProcessor().getErrorListener());
92         }
93         catch(TransformerConfigurationException JavaDoc tfe)
94         {
95           throw new TransformerException JavaDoc(tfe);
96         }
97
98         // stylesheet.setDOMBackPointer(handler.getOriginatingNode());
99
// ***** Note that we're assigning an empty locator. Is this necessary?
100
SAXSourceLocator slocator = new SAXSourceLocator();
101         Locator locator = handler.getLocator();
102         if(null != locator)
103         {
104           slocator.setLineNumber(locator.getLineNumber());
105           slocator.setColumnNumber(locator.getColumnNumber());
106           slocator.setPublicId(locator.getPublicId());
107           slocator.setSystemId(locator.getSystemId());
108         }
109         stylesheet.setLocaterInfo(slocator);
110         stylesheet.setPrefixes(handler.getNamespaceSupport());
111         handler.pushStylesheet(stylesheet);
112
113         isLREAsStyleSheet = true;
114
115         AttributesImpl JavaDoc stylesheetAttrs = new AttributesImpl JavaDoc();
116         AttributesImpl JavaDoc lreAttrs = new AttributesImpl JavaDoc();
117         int n = attributes.getLength();
118
119         for (int i = 0; i < n; i++)
120         {
121           String JavaDoc attrLocalName = attributes.getLocalName(i);
122           String JavaDoc attrUri = attributes.getURI(i);
123           String JavaDoc value = attributes.getValue(i);
124
125           if ((null != attrUri) && attrUri.equals(Constants.S_XSLNAMESPACEURL))
126           {
127             stylesheetAttrs.addAttribute(null, attrLocalName, attrLocalName,
128                                          attributes.getType(i),
129                                          attributes.getValue(i));
130           }
131           else if ((attrLocalName.startsWith("xmlns:") || attrLocalName.equals(
132                                                                                "xmlns")) && value.equals(Constants.S_XSLNAMESPACEURL))
133           {
134
135             // ignore
136
}
137           else
138           {
139             lreAttrs.addAttribute(attrUri, attrLocalName,
140                                   attributes.getQName(i),
141                                   attributes.getType(i),
142                                   attributes.getValue(i));
143           }
144         }
145
146         attributes = lreAttrs;
147
148         // Set properties from the attributes, but don't throw
149
// an error if there is an attribute defined that is not
150
// allowed on a stylesheet.
151
try{
152         stylesheetProcessor.setPropertiesFromAttributes(handler, "stylesheet",
153                                                         stylesheetAttrs, stylesheet);
154                 }
155                 catch (Exception JavaDoc e)
156                 {
157                     // This is pretty ugly, but it will have to do for now.
158
// This is just trying to append some text specifying that
159
// this error came from a missing or invalid XSLT namespace
160
// declaration.
161
// If someone comes up with a better solution, please feel
162
// free to contribute it. -mm
163

164                     if (stylesheet.getDeclaredPrefixes() == null ||
165                         !declaredXSLNS(stylesheet))
166                     {
167                         throw new org.xml.sax.SAXException JavaDoc(XSLMessages.createWarning(XSLTErrorResources.WG_OLD_XSLT_NS, null));
168                     }
169                     else
170                     {
171                         throw new org.xml.sax.SAXException JavaDoc(e);
172                     }
173                 }
174         handler.pushElemTemplateElement(stylesheet);
175
176         ElemTemplate template = new ElemTemplate();
177
178         appendAndPush(handler, template);
179
180         XPath rootMatch = new XPath("/", stylesheet, stylesheet, XPath.MATCH,
181              handler.getStylesheetProcessor().getErrorListener());
182
183         template.setMatch(rootMatch);
184
185         // template.setDOMBackPointer(handler.getOriginatingNode());
186
stylesheet.setTemplate(template);
187
188         p = handler.getElemTemplateElement();
189         excludeXSLDecl = true;
190       }
191
192       XSLTElementDef def = getElemDef();
193       Class JavaDoc classObject = def.getClassObject();
194       boolean isExtension = false;
195       boolean isComponentDecl = false;
196       boolean isUnknownTopLevel = false;
197
198       while (null != p)
199       {
200
201         // System.out.println("Checking: "+p);
202
if (p instanceof ElemLiteralResult)
203         {
204           ElemLiteralResult parentElem = (ElemLiteralResult) p;
205
206           isExtension = parentElem.containsExtensionElementURI(uri);
207         }
208         else if (p instanceof Stylesheet)
209         {
210           Stylesheet parentElem = (Stylesheet) p;
211
212           isExtension = parentElem.containsExtensionElementURI(uri);
213
214           if ((false == isExtension) && (null != uri)
215               && (uri.equals(Constants.S_BUILTIN_EXTENSIONS_URL)
216                   || uri.equals(Constants.S_BUILTIN_OLD_EXTENSIONS_URL)))
217           {
218             isComponentDecl = true;
219           }
220           else
221           {
222             isUnknownTopLevel = true;
223           }
224         }
225
226         if (isExtension)
227           break;
228
229         p = p.getParentElem();
230       }
231
232       ElemTemplateElement elem = null;
233
234       try
235       {
236         if (isExtension)
237         {
238
239           // System.out.println("Creating extension(1): "+uri);
240
elem = new ElemExtensionCall();
241         }
242         else if (isComponentDecl)
243         {
244           elem = (ElemTemplateElement) classObject.newInstance();
245         }
246         else if (isUnknownTopLevel)
247         {
248
249           // TBD: Investigate, not sure about this. -sb
250
elem = (ElemTemplateElement) classObject.newInstance();
251         }
252         else
253         {
254           elem = (ElemTemplateElement) classObject.newInstance();
255         }
256
257         elem.setDOMBackPointer(handler.getOriginatingNode());
258         elem.setLocaterInfo(handler.getLocator());
259         elem.setPrefixes(handler.getNamespaceSupport(), excludeXSLDecl);
260
261         if (elem instanceof ElemLiteralResult)
262         {
263           ((ElemLiteralResult) elem).setNamespace(uri);
264           ((ElemLiteralResult) elem).setLocalName(localName);
265           ((ElemLiteralResult) elem).setRawName(rawName);
266           ((ElemLiteralResult) elem).setIsLiteralResultAsStylesheet(
267                                                                     isLREAsStyleSheet);
268         }
269       }
270       catch (InstantiationException JavaDoc ie)
271       {
272         handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMLITRSLT, null, ie);//"Failed creating ElemLiteralResult instance!", ie);
273
}
274       catch (IllegalAccessException JavaDoc iae)
275       {
276         handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMLITRSLT, null, iae);//"Failed creating ElemLiteralResult instance!", iae);
277
}
278
279       setPropertiesFromAttributes(handler, rawName, attributes, elem);
280
281       // bit of a hack here...
282
if (!isExtension && (elem instanceof ElemLiteralResult))
283       {
284         isExtension =
285                      ((ElemLiteralResult) elem).containsExtensionElementURI(uri);
286
287         if (isExtension)
288         {
289
290           // System.out.println("Creating extension(2): "+uri);
291
elem = new ElemExtensionCall();
292
293           elem.setLocaterInfo(handler.getLocator());
294           elem.setPrefixes(handler.getNamespaceSupport());
295           ((ElemLiteralResult) elem).setNamespace(uri);
296           ((ElemLiteralResult) elem).setLocalName(localName);
297           ((ElemLiteralResult) elem).setRawName(rawName);
298           setPropertiesFromAttributes(handler, rawName, attributes, elem);
299         }
300       }
301
302       appendAndPush(handler, elem);
303     }
304     catch(TransformerException JavaDoc te)
305     {
306       throw new org.xml.sax.SAXException JavaDoc(te);
307     }
308   }
309
310   /**
311    * Receive notification of the end of an element.
312    *
313    * @param name The element type name.
314    * @param attributes The specified or defaulted attributes.
315    *
316    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
317    * @param uri The Namespace URI, or an empty string.
318    * @param localName The local name (without prefix), or empty string if not namespace processing.
319    * @param rawName The qualified name (with prefix).
320    */

321   public void endElement(
322           StylesheetHandler handler, String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName)
323             throws org.xml.sax.SAXException JavaDoc
324   {
325
326     ElemTemplateElement elem = handler.getElemTemplateElement();
327
328     if (elem instanceof ElemLiteralResult)
329     {
330       if (((ElemLiteralResult) elem).getIsLiteralResultAsStylesheet())
331       {
332         handler.popStylesheet();
333       }
334     }
335
336     super.endElement(handler, uri, localName, rawName);
337   }
338     
339     private boolean declaredXSLNS(Stylesheet stylesheet)
340     {
341         Vector JavaDoc declaredPrefixes = stylesheet.getDeclaredPrefixes();
342         int n = declaredPrefixes.size();
343
344         for (int i = 0; i < n; i++)
345         {
346             XMLNSDecl decl = (XMLNSDecl) declaredPrefixes.elementAt(i);
347             if(decl.getURI().equals(Constants.S_XSLNAMESPACEURL))
348                 return true;
349         }
350         return false;
351     }
352 }
353
Popular Tags