KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > trax > Util


1 /*
2  * Copyright 2001-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: Util.java,v 1.10 2004/02/16 22:57:21 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.trax;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.Reader JavaDoc;
24
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26 import javax.xml.parsers.SAXParser JavaDoc;
27 import javax.xml.parsers.SAXParserFactory JavaDoc;
28
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.TransformerConfigurationException JavaDoc;
31 import javax.xml.transform.dom.DOMSource JavaDoc;
32 import javax.xml.transform.sax.SAXSource JavaDoc;
33 import javax.xml.transform.stream.StreamSource JavaDoc;
34
35 import org.apache.xalan.xsltc.compiler.XSLTC;
36 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
37
38 import org.w3c.dom.Document JavaDoc;
39
40 import org.xml.sax.InputSource JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42 import org.xml.sax.SAXNotRecognizedException JavaDoc;
43 import org.xml.sax.SAXNotSupportedException JavaDoc;
44 import org.xml.sax.XMLReader JavaDoc;
45 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
46
47 /**
48  * @author Santiago Pericas-Geertsen
49  */

50 public final class Util {
51
52     public static String JavaDoc baseName(String JavaDoc name) {
53     return org.apache.xalan.xsltc.compiler.util.Util.baseName(name);
54     }
55     
56     public static String JavaDoc noExtName(String JavaDoc name) {
57     return org.apache.xalan.xsltc.compiler.util.Util.noExtName(name);
58     }
59
60     public static String JavaDoc toJavaName(String JavaDoc name) {
61     return org.apache.xalan.xsltc.compiler.util.Util.toJavaName(name);
62     }
63
64      
65
66     
67     /**
68      * Creates a SAX2 InputSource object from a TrAX Source object
69      */

70     public static InputSource JavaDoc getInputSource(XSLTC xsltc, Source JavaDoc source)
71     throws TransformerConfigurationException JavaDoc
72     {
73     InputSource JavaDoc input = null;
74
75     String JavaDoc systemId = source.getSystemId();
76
77     try {
78         // Try to get InputSource from SAXSource input
79
if (source instanceof SAXSource JavaDoc) {
80         final SAXSource JavaDoc sax = (SAXSource JavaDoc)source;
81         input = sax.getInputSource();
82         // Pass the SAX parser to the compiler
83
try {
84                     XMLReader JavaDoc reader = sax.getXMLReader();
85
86                      /*
87                       * Fix for bug 24695
88                       * According to JAXP 1.2 specification if a SAXSource
89                       * is created using a SAX InputSource the Transformer or
90                       * TransformerFactory creates a reader via the
91                       * XMLReaderFactory if setXMLReader is not used
92                       */

93
94                     if (reader == null) {
95                        try {
96                            reader= XMLReaderFactory.createXMLReader();
97                        } catch (Exception JavaDoc e ) {
98                            try {
99
100                                //Incase there is an exception thrown
101
// resort to JAXP
102
SAXParserFactory JavaDoc parserFactory =
103                                       SAXParserFactory.newInstance();
104                                parserFactory.setNamespaceAware(true);
105                                reader = parserFactory.newSAXParser()
106                                      .getXMLReader();
107
108                                
109                            } catch (ParserConfigurationException JavaDoc pce ) {
110                                throw new TransformerConfigurationException JavaDoc
111                                  ("ParserConfigurationException" ,pce);
112                            }
113                        }
114                     }
115                     reader.setFeature
116                         ("http://xml.org/sax/features/namespaces",true);
117                     reader.setFeature
118                         ("http://xml.org/sax/features/namespace-prefixes",false);
119
120                     xsltc.setXMLReader(reader);
121                 }catch (SAXNotRecognizedException JavaDoc snre ) {
122                   throw new TransformerConfigurationException JavaDoc
123                        ("SAXNotRecognizedException ",snre);
124                 }catch (SAXNotSupportedException JavaDoc snse ) {
125                   throw new TransformerConfigurationException JavaDoc
126                        ("SAXNotSupportedException ",snse);
127                 }catch (SAXException JavaDoc se ) {
128                   throw new TransformerConfigurationException JavaDoc
129                        ("SAXException ",se);
130                 }
131
132         }
133         // handle DOMSource
134
else if (source instanceof DOMSource JavaDoc) {
135         final DOMSource JavaDoc domsrc = (DOMSource JavaDoc)source;
136         final Document JavaDoc dom = (Document JavaDoc)domsrc.getNode();
137         final DOM2SAX dom2sax = new DOM2SAX(dom);
138         xsltc.setXMLReader(dom2sax);
139
140             // Try to get SAX InputSource from DOM Source.
141
input = SAXSource.sourceToInputSource(source);
142         if (input == null){
143             input = new InputSource JavaDoc(domsrc.getSystemId());
144         }
145         }
146         // Try to get InputStream or Reader from StreamSource
147
else if (source instanceof StreamSource JavaDoc) {
148         final StreamSource JavaDoc stream = (StreamSource JavaDoc)source;
149         final InputStream JavaDoc istream = stream.getInputStream();
150         final Reader JavaDoc reader = stream.getReader();
151
152         // Create InputSource from Reader or InputStream in Source
153
if (istream != null) {
154             input = new InputSource JavaDoc(istream);
155         }
156         else if (reader != null) {
157             input = new InputSource JavaDoc(reader);
158         }
159         else {
160             input = new InputSource JavaDoc(systemId);
161         }
162         }
163         else {
164         ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR);
165         throw new TransformerConfigurationException JavaDoc(err.toString());
166         }
167         input.setSystemId(systemId);
168     }
169     catch (NullPointerException JavaDoc e) {
170         ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR,
171                     "TransformerFactory.newTemplates()");
172         throw new TransformerConfigurationException JavaDoc(err.toString());
173     }
174     catch (SecurityException JavaDoc e) {
175         ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId);
176         throw new TransformerConfigurationException JavaDoc(err.toString());
177     }
178     return input;
179     }
180
181 }
182
183
Popular Tags