Util.java revision 573
0N/A/*
2362N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * Copyright 2001-2004 The Apache Software Foundation.
2362N/A *
0N/A * Licensed under the Apache License, Version 2.0 (the "License");
2362N/A * you may not use this file except in compliance with the License.
0N/A * You may obtain a copy of the License at
0N/A *
0N/A * http://www.apache.org/licenses/LICENSE-2.0
0N/A *
0N/A * Unless required by applicable law or agreed to in writing, software
0N/A * distributed under the License is distributed on an "AS IS" BASIS,
0N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0N/A * See the License for the specific language governing permissions and
0N/A * limitations under the License.
0N/A */
0N/A/*
2362N/A * $Id: Util.java,v 1.2.4.1 2005/09/14 09:37:34 pvedula Exp $
2362N/A */
2362N/A
0N/Apackage com.sun.org.apache.xalan.internal.xsltc.trax;
0N/A
0N/Aimport java.io.InputStream;
0N/Aimport java.io.Reader;
0N/A
0N/Aimport javax.xml.XMLConstants;
0N/Aimport javax.xml.parsers.ParserConfigurationException;
0N/Aimport javax.xml.parsers.SAXParser;
0N/Aimport javax.xml.parsers.SAXParserFactory;
0N/A
0N/Aimport javax.xml.stream.XMLEventReader;
0N/Aimport javax.xml.stream.XMLStreamReader;
0N/A
0N/Aimport javax.xml.transform.Source;
0N/Aimport javax.xml.transform.TransformerConfigurationException;
0N/Aimport javax.xml.transform.dom.DOMSource;
0N/Aimport javax.xml.transform.sax.SAXSource;
0N/Aimport javax.xml.transform.stax.StAXResult;
0N/Aimport javax.xml.transform.stax.StAXSource;
0N/Aimport javax.xml.transform.stream.StreamSource;
0N/A
0N/Aimport com.sun.org.apache.xalan.internal.utils.FactoryImpl;
0N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
0N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
0N/A
0N/Aimport org.w3c.dom.Document;
0N/A
0N/Aimport org.xml.sax.InputSource;
0N/Aimport org.xml.sax.SAXException;
0N/Aimport org.xml.sax.SAXNotRecognizedException;
0N/Aimport org.xml.sax.SAXNotSupportedException;
0N/Aimport org.xml.sax.XMLReader;
0N/Aimport org.xml.sax.helpers.XMLReaderFactory;
0N/A
0N/A/**
0N/A * @author Santiago Pericas-Geertsen
0N/A */
0N/Apublic final class Util {
0N/A
0N/A public static String baseName(String name) {
0N/A return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.baseName(name);
0N/A }
0N/A
0N/A public static String noExtName(String name) {
0N/A return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.noExtName(name);
0N/A }
0N/A
0N/A public static String toJavaName(String name) {
0N/A return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.toJavaName(name);
0N/A }
0N/A
0N/A
0N/A
0N/A
0N/A /**
0N/A * Creates a SAX2 InputSource object from a TrAX Source object
0N/A */
0N/A public static InputSource getInputSource(XSLTC xsltc, Source source)
0N/A throws TransformerConfigurationException
0N/A {
0N/A InputSource input = null;
0N/A
0N/A String systemId = source.getSystemId();
0N/A
0N/A try {
0N/A // Try to get InputSource from SAXSource input
0N/A if (source instanceof SAXSource) {
0N/A final SAXSource sax = (SAXSource)source;
0N/A input = sax.getInputSource();
0N/A // Pass the SAX parser to the compiler
0N/A try {
0N/A XMLReader reader = sax.getXMLReader();
0N/A
0N/A /*
0N/A * Fix for bug 24695
0N/A * According to JAXP 1.2 specification if a SAXSource
0N/A * is created using a SAX InputSource the Transformer or
0N/A * TransformerFactory creates a reader via the
0N/A * XMLReaderFactory if setXMLReader is not used
0N/A */
0N/A
0N/A if (reader == null) {
0N/A try {
0N/A reader= XMLReaderFactory.createXMLReader();
0N/A } catch (Exception e ) {
0N/A try {
0N/A
0N/A //Incase there is an exception thrown
0N/A // resort to JAXP
0N/A SAXParserFactory parserFactory = FactoryImpl.getSAXFactory(xsltc.useServicesMechnism());
0N/A parserFactory.setNamespaceAware(true);
0N/A
0N/A if (xsltc.isSecureProcessing()) {
0N/A try {
0N/A parserFactory.setFeature(
0N/A XMLConstants.FEATURE_SECURE_PROCESSING, true);
0N/A }
0N/A catch (org.xml.sax.SAXException se) {}
0N/A }
0N/A
0N/A reader = parserFactory.newSAXParser()
0N/A .getXMLReader();
0N/A
0N/A
0N/A } catch (ParserConfigurationException pce ) {
0N/A throw new TransformerConfigurationException
0N/A ("ParserConfigurationException" ,pce);
0N/A }
0N/A }
0N/A }
0N/A reader.setFeature
0N/A ("http://xml.org/sax/features/namespaces",true);
0N/A reader.setFeature
0N/A ("http://xml.org/sax/features/namespace-prefixes",false);
0N/A
0N/A try {
0N/A reader.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD,
0N/A xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_DTD));
0N/A } catch (SAXNotRecognizedException e) {
0N/A System.err.println("Warning: " + reader.getClass().getName() + ": "
0N/A + e.getMessage());
0N/A }
0N/A
0N/A xsltc.setXMLReader(reader);
0N/A }catch (SAXNotRecognizedException snre ) {
0N/A throw new TransformerConfigurationException
0N/A ("SAXNotRecognizedException ",snre);
0N/A }catch (SAXNotSupportedException snse ) {
0N/A throw new TransformerConfigurationException
0N/A ("SAXNotSupportedException ",snse);
0N/A }catch (SAXException se ) {
0N/A throw new TransformerConfigurationException
0N/A ("SAXException ",se);
0N/A }
0N/A
0N/A }
0N/A // handle DOMSource
0N/A else if (source instanceof DOMSource) {
0N/A final DOMSource domsrc = (DOMSource)source;
0N/A final Document dom = (Document)domsrc.getNode();
0N/A final DOM2SAX dom2sax = new DOM2SAX(dom);
0N/A xsltc.setXMLReader(dom2sax);
0N/A
0N/A // Try to get SAX InputSource from DOM Source.
0N/A input = SAXSource.sourceToInputSource(source);
0N/A if (input == null){
0N/A input = new InputSource(domsrc.getSystemId());
0N/A }
0N/A }
0N/A
0N/A // handle StAXSource
0N/A else if (source instanceof StAXSource) {
0N/A final StAXSource staxSource = (StAXSource)source;
0N/A StAXEvent2SAX staxevent2sax = null;
0N/A StAXStream2SAX staxStream2SAX = null;
0N/A if (staxSource.getXMLEventReader() != null) {
0N/A final XMLEventReader xmlEventReader = staxSource.getXMLEventReader();
0N/A staxevent2sax = new StAXEvent2SAX(xmlEventReader);
0N/A xsltc.setXMLReader(staxevent2sax);
0N/A } else if (staxSource.getXMLStreamReader() != null) {
0N/A final XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader();
0N/A staxStream2SAX = new StAXStream2SAX(xmlStreamReader);
0N/A xsltc.setXMLReader(staxStream2SAX);
0N/A }
0N/A
0N/A // get sax InputSource from StAXSource
0N/A input = SAXSource.sourceToInputSource(source);
0N/A if (input == null){
0N/A input = new InputSource(staxSource.getSystemId());
0N/A }
0N/A }
0N/A
0N/A // Try to get InputStream or Reader from StreamSource
0N/A else if (source instanceof StreamSource) {
0N/A final StreamSource stream = (StreamSource)source;
0N/A final InputStream istream = stream.getInputStream();
0N/A final Reader reader = stream.getReader();
0N/A xsltc.setXMLReader(null); // Clear old XML reader
0N/A
0N/A // Create InputSource from Reader or InputStream in Source
0N/A if (istream != null) {
0N/A input = new InputSource(istream);
0N/A }
0N/A else if (reader != null) {
0N/A input = new InputSource(reader);
0N/A }
0N/A else {
0N/A input = new InputSource(systemId);
0N/A }
0N/A }
0N/A else {
0N/A ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR);
0N/A throw new TransformerConfigurationException(err.toString());
0N/A }
0N/A input.setSystemId(systemId);
0N/A }
0N/A catch (NullPointerException e) {
0N/A ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR,
0N/A "TransformerFactory.newTemplates()");
0N/A throw new TransformerConfigurationException(err.toString());
0N/A }
0N/A catch (SecurityException e) {
0N/A ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId);
0N/A throw new TransformerConfigurationException(err.toString());
0N/A }
0N/A return input;
0N/A }
0N/A
0N/A}
0N/A