KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > 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.1.2.2 2007/03/12 18:46:04 joehw Exp $
18  */

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

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

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

94
95                     if (reader == null) {
96                        try {
97                            reader= XMLReaderFactory.createXMLReader();
98                        } catch (Exception JavaDoc e ) {
99                            try {
100
101                                //Incase there is an exception thrown
102
// resort to JAXP
103
SAXParserFactory JavaDoc parserFactory =
104                                       SAXParserFactory.newInstance();
105                                parserFactory.setNamespaceAware(true);
106                                
107                                if (xsltc.isSecureProcessing()) {
108                                   try {
109                                       parserFactory.setFeature(
110                                           XMLConstants.FEATURE_SECURE_PROCESSING, true);
111                                   }
112                                   catch (org.xml.sax.SAXException JavaDoc se) {}
113                                }
114                                
115                                reader = parserFactory.newSAXParser()
116                                      .getXMLReader();
117
118                                
119                            } catch (ParserConfigurationException JavaDoc pce ) {
120                                throw new TransformerConfigurationException JavaDoc
121                                  ("ParserConfigurationException" ,pce);
122                            }
123                        }
124                     }
125                     reader.setFeature
126                         ("http://xml.org/sax/features/namespaces",true);
127                     reader.setFeature
128                         ("http://xml.org/sax/features/namespace-prefixes",false);
129
130                     xsltc.setXMLReader(reader);
131                 }catch (SAXNotRecognizedException JavaDoc snre ) {
132                   throw new TransformerConfigurationException JavaDoc
133                        ("SAXNotRecognizedException ",snre);
134                 }catch (SAXNotSupportedException JavaDoc snse ) {
135                   throw new TransformerConfigurationException JavaDoc
136                        ("SAXNotSupportedException ",snse);
137                 }catch (SAXException JavaDoc se ) {
138                   throw new TransformerConfigurationException JavaDoc
139                        ("SAXException ",se);
140                 }
141
142         }
143         // handle DOMSource
144
else if (source instanceof DOMSource JavaDoc) {
145         final DOMSource JavaDoc domsrc = (DOMSource JavaDoc)source;
146         final Document JavaDoc dom = (Document JavaDoc)domsrc.getNode();
147         final DOM2SAX dom2sax = new DOM2SAX(dom);
148         xsltc.setXMLReader(dom2sax);
149
150             // Try to get SAX InputSource from DOM Source.
151
input = SAXSource.sourceToInputSource(source);
152         if (input == null){
153             input = new InputSource JavaDoc(domsrc.getSystemId());
154         }
155         }
156         // Try to get InputStream or Reader from StreamSource
157
else if (source instanceof StreamSource JavaDoc) {
158         final StreamSource JavaDoc stream = (StreamSource JavaDoc)source;
159         final InputStream JavaDoc istream = stream.getInputStream();
160         final Reader JavaDoc reader = stream.getReader();
161                 xsltc.setXMLReader(null); // Clear old XML reader
162

163         // Create InputSource from Reader or InputStream in Source
164
if (istream != null) {
165             input = new InputSource JavaDoc(istream);
166         }
167         else if (reader != null) {
168             input = new InputSource JavaDoc(reader);
169         }
170         else {
171             input = new InputSource JavaDoc(systemId);
172         }
173         }
174         else {
175         ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR);
176         throw new TransformerConfigurationException JavaDoc(err.toString());
177         }
178         input.setSystemId(systemId);
179     }
180     catch (NullPointerException JavaDoc e) {
181         ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR,
182                     "TransformerFactory.newTemplates()");
183         throw new TransformerConfigurationException JavaDoc(err.toString());
184     }
185     catch (SecurityException JavaDoc e) {
186         ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId);
187         throw new TransformerConfigurationException JavaDoc(err.toString());
188     }
189     return input;
190     }
191
192 }
193
194
Popular Tags