jaxp/src/com/sun/org/apache/xalan/internal/xsltc/trax/Util.java
author joehw
Tue, 17 Apr 2012 11:17:59 -0700
changeset 12458 d601e4bba306
parent 12457 c348e06f0e82
child 17534 21dc0b2762da
permissions -rw-r--r--
7160380: Sync JDK8 with JAXP 1.4.5 Summary: bring JDK8 up to date to what we have in 7u4 Reviewed-by: lancea, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 2001-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: Util.java,v 1.2.4.1 2005/09/14 09:37:34 pvedula Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
package com.sun.org.apache.xalan.internal.xsltc.trax;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.io.InputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.io.Reader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import javax.xml.XMLConstants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import javax.xml.parsers.ParserConfigurationException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import javax.xml.parsers.SAXParser;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import javax.xml.parsers.SAXParserFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import javax.xml.stream.XMLEventReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import javax.xml.stream.XMLStreamReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import javax.xml.transform.Source;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import javax.xml.transform.TransformerConfigurationException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import javax.xml.transform.dom.DOMSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import javax.xml.transform.sax.SAXSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import javax.xml.transform.stax.StAXResult;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import javax.xml.transform.stax.StAXSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import javax.xml.transform.stream.StreamSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    45
import com.sun.org.apache.xalan.internal.utils.FactoryImpl;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
import org.w3c.dom.Document;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
import org.xml.sax.InputSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
import org.xml.sax.SAXNotRecognizedException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
import org.xml.sax.SAXNotSupportedException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
import org.xml.sax.XMLReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
import org.xml.sax.helpers.XMLReaderFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
public final class Util {
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    public static String baseName(String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
        return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.baseName(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    public static String noExtName(String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
        return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.noExtName(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    public static String toJavaName(String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
        return com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util.toJavaName(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
     * Creates a SAX2 InputSource object from a TrAX Source object
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    public static InputSource getInputSource(XSLTC xsltc, Source source)
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
        throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
        InputSource input = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
        String systemId = source.getSystemId();
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
            // Try to get InputSource from SAXSource input
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
            if (source instanceof SAXSource) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
                final SAXSource sax = (SAXSource)source;
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
                input = sax.getInputSource();
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
                // Pass the SAX parser to the compiler
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
                    XMLReader reader = sax.getXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
                     /*
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
                      * Fix for bug 24695
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
                      * According to JAXP 1.2 specification if a SAXSource
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
                      * is created using a SAX InputSource the Transformer or
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
                      * TransformerFactory creates a reader via the
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
                      * XMLReaderFactory if setXMLReader is not used
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
                      */
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
                    if (reader == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
                       try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
                           reader= XMLReaderFactory.createXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
                       } catch (Exception e ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
                           try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
                               //Incase there is an exception thrown
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
                               // resort to JAXP
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   113
                               SAXParserFactory parserFactory = FactoryImpl.getSAXFactory(xsltc.useServicesMechnism());
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
                               parserFactory.setNamespaceAware(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
                               if (xsltc.isSecureProcessing()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
                                  try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
                                      parserFactory.setFeature(
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
                                          XMLConstants.FEATURE_SECURE_PROCESSING, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
                                  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
                                  catch (org.xml.sax.SAXException se) {}
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
                               }
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
                               reader = parserFactory.newSAXParser()
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
                                     .getXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
                           } catch (ParserConfigurationException pce ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
                               throw new TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
                                 ("ParserConfigurationException" ,pce);
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
                           }
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
                       }
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
                    reader.setFeature
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
                        ("http://xml.org/sax/features/namespaces",true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
                    reader.setFeature
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
                        ("http://xml.org/sax/features/namespace-prefixes",false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
                    xsltc.setXMLReader(reader);
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
                }catch (SAXNotRecognizedException snre ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
                  throw new TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
                       ("SAXNotRecognizedException ",snre);
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
                }catch (SAXNotSupportedException snse ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
                  throw new TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
                       ("SAXNotSupportedException ",snse);
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
                }catch (SAXException se ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
                  throw new TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
                       ("SAXException ",se);
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
            // handle  DOMSource
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
            else if (source instanceof DOMSource) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
                final DOMSource domsrc = (DOMSource)source;
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
                final Document dom = (Document)domsrc.getNode();
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
                final DOM2SAX dom2sax = new DOM2SAX(dom);
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
                xsltc.setXMLReader(dom2sax);
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
                // Try to get SAX InputSource from DOM Source.
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
                input = SAXSource.sourceToInputSource(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
                if (input == null){
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
                    input = new InputSource(domsrc.getSystemId());
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
            // handle StAXSource
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
            else if (source instanceof StAXSource) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
                final StAXSource staxSource = (StAXSource)source;
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
                StAXEvent2SAX staxevent2sax = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
                StAXStream2SAX staxStream2SAX = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
                if (staxSource.getXMLEventReader() != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
                    final XMLEventReader xmlEventReader = staxSource.getXMLEventReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
                    staxevent2sax = new StAXEvent2SAX(xmlEventReader);
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
                    xsltc.setXMLReader(staxevent2sax);
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
                } else if (staxSource.getXMLStreamReader() != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
                    final XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
                    staxStream2SAX = new StAXStream2SAX(xmlStreamReader);
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
                    xsltc.setXMLReader(staxStream2SAX);
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
                // get sax InputSource from StAXSource
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
                input = SAXSource.sourceToInputSource(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
                if (input == null){
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
                    input = new InputSource(staxSource.getSystemId());
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
            // Try to get InputStream or Reader from StreamSource
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
            else if (source instanceof StreamSource) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
                final StreamSource stream = (StreamSource)source;
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
                final InputStream istream = stream.getInputStream();
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
                final Reader reader = stream.getReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
                xsltc.setXMLReader(null);     // Clear old XML reader
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
                // Create InputSource from Reader or InputStream in Source
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
                if (istream != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
                    input = new InputSource(istream);
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
                else if (reader != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
                    input = new InputSource(reader);
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
                else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
                    input = new InputSource(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
                ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_SOURCE_ERR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
                throw new TransformerConfigurationException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
            input.setSystemId(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
        catch (NullPointerException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
            ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
                                        "TransformerFactory.newTemplates()");
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
            throw new TransformerConfigurationException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
        catch (SecurityException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
            ErrorMsg err = new ErrorMsg(ErrorMsg.FILE_ACCESS_ERR, systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
            throw new TransformerConfigurationException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
        return input;
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
}