jaxp/src/com/sun/org/apache/xalan/internal/xsltc/trax/TrAXFilter.java
author joehw
Tue, 17 Apr 2012 11:17:59 -0700
changeset 12458 d601e4bba306
parent 12457 c348e06f0e82
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: TrAXFilter.java,v 1.2.4.1 2005/09/06 12:23:19 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
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
package com.sun.org.apache.xalan.internal.xsltc.trax;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
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.FactoryConfigurationError;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import javax.xml.parsers.ParserConfigurationException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import javax.xml.parsers.SAXParser;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import javax.xml.parsers.SAXParserFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import javax.xml.transform.ErrorListener;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import javax.xml.transform.Templates;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import javax.xml.transform.Transformer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import javax.xml.transform.TransformerConfigurationException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import javax.xml.transform.sax.SAXResult;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.xml.internal.utils.XMLReaderManager;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import org.xml.sax.ContentHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import org.xml.sax.InputSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
import org.xml.sax.XMLReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
import org.xml.sax.helpers.XMLFilterImpl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
import org.xml.sax.helpers.XMLReaderFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * skeleton extension of XMLFilterImpl for now.
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * @author G. Todd Miller
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
public class TrAXFilter extends XMLFilterImpl {
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    private Templates              _templates;
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
    private TransformerImpl        _transformer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
    private TransformerHandlerImpl _transformerHandler;
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    58
    private boolean _useServicesMechanism = true;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
    public TrAXFilter(Templates templates)  throws
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
        TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
        _templates = templates;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
        _transformer = (TransformerImpl) templates.newTransformer();
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
        _transformerHandler = new TransformerHandlerImpl(_transformer);
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    66
        _useServicesMechanism = _transformer.useServicesMechnism();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    public Transformer getTransformer() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
        return _transformer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    private void createParent() throws SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
        XMLReader parent = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
            SAXParserFactory pfactory = SAXParserFactory.newInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
            pfactory.setNamespaceAware(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
            if (_transformer.isSecureProcessing()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
                    pfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
                catch (SAXException e) {}
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
            SAXParser saxparser = pfactory.newSAXParser();
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
            parent = saxparser.getXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        catch (ParserConfigurationException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
            throw new SAXException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
        catch (FactoryConfigurationError e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
            throw new SAXException(e.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
        if (parent == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
            parent = XMLReaderFactory.createXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
        // make this XMLReader the parent of this filter
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        setParent(parent);
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    public void parse (InputSource input) throws SAXException, IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
        XMLReader managedReader = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
            if (getParent() == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
                try {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   111
                    managedReader = XMLReaderManager.getInstance(_useServicesMechanism)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
                                                    .getXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
                    setParent(managedReader);
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
                } catch (SAXException  e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
                    throw new SAXException(e.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
            // call parse on the parent
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
            getParent().parse(input);
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
        } finally {
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
            if (managedReader != null) {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   123
                XMLReaderManager.getInstance(_useServicesMechanism).releaseXMLReader(managedReader);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
    public void parse (String systemId) throws SAXException, IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
        parse(new InputSource(systemId));
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    public void setContentHandler (ContentHandler handler)
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        _transformerHandler.setResult(new SAXResult(handler));
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
        if (getParent() == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
                    createParent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
                catch (SAXException  e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
                   return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
        getParent().setContentHandler(_transformerHandler);
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    public void setErrorListener (ErrorListener handler) { }
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
}