KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 package org.apache.xalan.processor;
20
21 import java.io.IOException JavaDoc;
22
23 import javax.xml.transform.Source JavaDoc;
24 import javax.xml.transform.TransformerException JavaDoc;
25 import javax.xml.transform.URIResolver JavaDoc;
26 import javax.xml.transform.dom.DOMSource JavaDoc;
27 import javax.xml.transform.sax.SAXSource JavaDoc;
28 import javax.xml.transform.stream.StreamSource JavaDoc;
29
30 import org.apache.xalan.res.XSLMessages;
31 import org.apache.xalan.res.XSLTErrorResources;
32 import org.apache.xml.utils.SystemIDResolver;
33 import org.apache.xml.utils.TreeWalker;
34
35 import org.w3c.dom.Node JavaDoc;
36
37 import org.xml.sax.Attributes JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.XMLReader JavaDoc;
40 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
41
42 /**
43  * TransformerFactory class for xsl:include markup.
44  * @see <a HREF="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
45  * @see <a HREF="http://www.w3.org/TR/xslt#include">include in XSLT Specification</a>
46  */

47 class ProcessorInclude extends XSLTElementProcessor
48 {
49
50   /**
51    * The base URL of the XSL document.
52    * @serial
53    */

54   private String JavaDoc m_href = null;
55
56   /**
57    * Get the base identifier with which this stylesheet is associated.
58    *
59    * @return non-null reference to the href attribute string, or
60    * null if setHref has not been called.
61    */

62   public String JavaDoc getHref()
63   {
64     return m_href;
65   }
66
67   /**
68    * Get the base identifier with which this stylesheet is associated.
69    *
70    * @param baseIdent Should be a non-null reference to a valid URL string.
71    */

72   public void setHref(String JavaDoc baseIdent)
73   {
74     // Validate?
75
m_href = baseIdent;
76   }
77
78   /**
79    * Get the stylesheet type associated with an included stylesheet
80    *
81    * @return the type of the stylesheet
82    */

83   protected int getStylesheetType()
84   {
85     return StylesheetHandler.STYPE_INCLUDE;
86   }
87
88   /**
89    * Get the error number associated with this type of stylesheet including itself
90    *
91    * @return the appropriate error number
92    */

93   protected String JavaDoc getStylesheetInclErr()
94   {
95     return XSLTErrorResources.ER_STYLESHEET_INCLUDES_ITSELF;
96   }
97
98   /**
99    * Receive notification of the start of an xsl:include element.
100    *
101    * @param handler The calling StylesheetHandler/TemplatesBuilder.
102    * @param uri The Namespace URI, or the empty string if the
103    * element has no Namespace URI or if Namespace
104    * processing is not being performed.
105    * @param localName The local name (without prefix), or the
106    * empty string if Namespace processing is not being
107    * performed.
108    * @param rawName The raw XML 1.0 name (with prefix), or the
109    * empty string if raw names are not available.
110    * @param attributes The attributes attached to the element. If
111    * there are no attributes, it shall be an empty
112    * Attributes object.
113    *
114    * @throws org.xml.sax.SAXException Any SAX exception, possibly
115    * wrapping another exception.
116    */

117   public void startElement(
118           StylesheetHandler handler, String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes JavaDoc attributes)
119             throws org.xml.sax.SAXException JavaDoc
120   {
121
122
123     setPropertiesFromAttributes(handler, rawName, attributes, this);
124
125     try
126     {
127       String JavaDoc hrefUrl = SystemIDResolver.getAbsoluteURI(getHref(),
128                            handler.getBaseIdentifier());
129
130       if (handler.importStackContains(hrefUrl))
131       {
132         throw new org.xml.sax.SAXException JavaDoc(
133           XSLMessages.createMessage(
134           getStylesheetInclErr(), new Object JavaDoc[]{ hrefUrl })); //"(StylesheetHandler) "+hrefUrl+" is directly or indirectly importing itself!");
135
}
136
137       handler.pushImportURL(hrefUrl);
138
139       int savedStylesheetType = handler.getStylesheetType();
140
141       handler.setStylesheetType(this.getStylesheetType());
142       handler.pushNewNamespaceSupport();
143
144       try
145       {
146         parse(handler, uri, localName, rawName, attributes);
147       }
148       finally
149       {
150         handler.setStylesheetType(savedStylesheetType);
151         handler.popImportURL();
152         handler.popNamespaceSupport();
153       }
154     }
155     catch(TransformerException JavaDoc te)
156     {
157       handler.error(te.getMessage(), te);
158     }
159   }
160
161   /**
162    * Set off a new parse for an included or imported stylesheet. This will
163    * set the {@link StylesheetHandler} to a new state, and recurse in with
164    * a new set of parse events. Once this function returns, the state of
165    * the StylesheetHandler should be restored.
166    *
167    * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
168    * @param uri The Namespace URI, which should be the XSLT namespace.
169    * @param localName The local name (without prefix), which should be "include" or "import".
170    * @param rawName The qualified name (with prefix).
171    * @param attributes The list of attributes on the xsl:include or xsl:import element.
172    *
173    * @throws org.xml.sax.SAXException Any SAX exception, possibly
174    * wrapping another exception.
175    */

176   protected void parse(
177           StylesheetHandler handler, String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes JavaDoc attributes)
178             throws org.xml.sax.SAXException JavaDoc
179   {
180     TransformerFactoryImpl processor = handler.getStylesheetProcessor();
181     URIResolver JavaDoc uriresolver = processor.getURIResolver();
182
183     try
184     {
185       Source JavaDoc source = null;
186
187       if (null != uriresolver)
188       {
189         source = uriresolver.resolve(getHref(),
190                                      handler.getBaseIdentifier());
191
192         if (null != source && source instanceof DOMSource JavaDoc)
193         {
194           Node JavaDoc node = ((DOMSource JavaDoc)source).getNode();
195           
196           String JavaDoc systemId = source.getSystemId();
197           if (systemId == null)
198           {
199             systemId = SystemIDResolver.getAbsoluteURI(getHref(),
200                          handler.getBaseIdentifier());
201             
202           }
203           
204           TreeWalker walker = new TreeWalker(handler, new org.apache.xml.utils.DOM2Helper(), systemId);
205
206           try
207           {
208             walker.traverse(node);
209           }
210           catch(org.xml.sax.SAXException JavaDoc se)
211           {
212             throw new TransformerException JavaDoc(se);
213           }
214           return;
215         }
216       }
217       
218       if(null == source)
219       {
220         String JavaDoc absURL = SystemIDResolver.getAbsoluteURI(getHref(),
221                           handler.getBaseIdentifier());
222
223         source = new StreamSource JavaDoc(absURL);
224       }
225       
226       XMLReader JavaDoc reader = null;
227       
228       if(source instanceof SAXSource JavaDoc)
229       {
230         SAXSource JavaDoc saxSource = (SAXSource JavaDoc)source;
231         reader = saxSource.getXMLReader(); // may be null
232
}
233       
234       InputSource JavaDoc inputSource = SAXSource.sourceToInputSource(source);
235
236       if (null == reader)
237       {
238         // Use JAXP1.1 ( if possible )
239
try {
240           javax.xml.parsers.SAXParserFactory JavaDoc factory=
241                                                      javax.xml.parsers.SAXParserFactory.newInstance();
242           factory.setNamespaceAware( true );
243           javax.xml.parsers.SAXParser JavaDoc jaxpParser=
244                                                  factory.newSAXParser();
245           reader=jaxpParser.getXMLReader();
246           
247         } catch( javax.xml.parsers.ParserConfigurationException JavaDoc ex ) {
248           throw new org.xml.sax.SAXException JavaDoc( ex );
249         } catch( javax.xml.parsers.FactoryConfigurationError JavaDoc ex1 ) {
250             throw new org.xml.sax.SAXException JavaDoc( ex1.toString() );
251         }
252         catch( NoSuchMethodError JavaDoc ex2 )
253         {
254         }
255         catch (AbstractMethodError JavaDoc ame){}
256       }
257       if (null == reader)
258         reader = XMLReaderFactory.createXMLReader();
259
260       if (null != reader)
261       {
262         reader.setContentHandler(handler);
263         handler.pushBaseIndentifier(inputSource.getSystemId());
264
265         try
266         {
267           reader.parse(inputSource);
268         }
269         finally
270         {
271           handler.popBaseIndentifier();
272         }
273       }
274     }
275     catch (IOException JavaDoc ioe)
276     {
277       handler.error(XSLTErrorResources.ER_IOEXCEPTION,
278                     new Object JavaDoc[]{ getHref() }, ioe);
279     }
280     catch(TransformerException JavaDoc te)
281     {
282       handler.error(te.getMessage(), te);
283     }
284   }
285 }
286
Popular Tags