Java XML Parse Stream parse(final InputStream is)

Here you can find the source of parse(final InputStream is)

Description

Grab the Document loaded from the specified InputStream

License

Open Source License

Parameter

Parameter Description
is a parameter

Exception

Parameter Description
SAXException an exception
IOException an exception
ParserConfigurationException an exception

Return

a Document loaded from the specified InputStream.

Declaration

public static Document parse(final InputStream is)
        throws SAXException, IOException, ParserConfigurationException 

Method Source Code

//package com.java2s;
/**//from   w w  w.j a v a 2s  . co  m
 * Copyright 2006 OCLC Online Computer Library Center Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
 * agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    private static DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    /**
     * Grab the Document found at the specified URL.
     * 
     * @param ref the URL location of an XML document.
     * @return a Document loaded from the specified URL.
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    public static Document parse(final String ref) throws SAXException, IOException, ParserConfigurationException {
        final String protocol = ref.split(":", 2)[0];
        return parse(protocol, new InputSource(ref));
    }

    /**
     * Grab the Document found at the specified URL.
     * 
     * @param protocol
     * @param is
     * @return a Document loaded from the specified URL.
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    public static Document parse(final String protocol, final InputSource is)
            throws SAXException, IOException, ParserConfigurationException {
        if ("http".equals(protocol)) {
            return getThreadedDocumentBuilder().parse(is);
        }
        throw new IOException("Protocol handler not implemented yet: " + protocol);
    }

    /**
     * Grab the Document loaded from the specified InputSource.
     * 
     * @param is
     * @return a Document loaded from the specified InputSource
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    public static Document parse(final InputSource is)
            throws SAXException, IOException, ParserConfigurationException {
        return getThreadedDocumentBuilder().parse(is);
    }

    /**
     * Grab the Document loaded from the specified InputStream
     * 
     * @param is
     * @return a Document loaded from the specified InputStream.
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    public static Document parse(final InputStream is)
            throws SAXException, IOException, ParserConfigurationException {
        return getThreadedDocumentBuilder().parse(is);
    }

    /**
     * Get a thread-safe DocumentBuilder
     * 
     * @return a namespaceAware DocumentBuilder assigned to the current thread
     * @throws ParserConfigurationException
     */
    public static DocumentBuilder getThreadedDocumentBuilder() throws ParserConfigurationException {
        // Thread currentThread = Thread.currentThread();
        // DocumentBuilder builder =
        // (DocumentBuilder) builderMap.get(currentThread);
        // if (builder == null) {
        // builder = dbFactory.newDocumentBuilder();
        // builderMap.put(currentThread, builder);
        // }
        final DocumentBuilder builder = dbFactory.newDocumentBuilder();
        return builder;
    }
}

Related

  1. getNextStartElement(XMLStreamReader parser)
  2. getParamStream(XMLOutputFactory outputFactory, XMLEventFactory eventFactory, XMLEventReader parser, XMLEvent paramEvent)
  3. parse(DefaultHandler handler, InputStream in)
  4. parse(final InputStream in)
  5. parse(final InputStream inputStream)
  6. parse(InputStream anInputStream)
  7. parse(InputStream byteStream)
  8. parse(InputStream in)
  9. parse(InputStream in)