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