Java XML Parse InputSource parse(InputSource source)

Here you can find the source of parse(InputSource source)

Description

Parse the given input source and return the root Element

License

Open Source License

Declaration

public static Element parse(InputSource source) throws IOException 

Method Source Code


//package com.java2s;
/*// w  ww.  j  a va2  s . co m
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import org.w3c.dom.*;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

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

import java.io.*;

public class Main {
    private static ThreadLocal<DocumentBuilder> builderThreadLocal = new ThreadLocal<DocumentBuilder>() {
        protected DocumentBuilder initialValue() {
            try {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setValidating(false);
                factory.setNamespaceAware(true);
                DocumentBuilder builder = factory.newDocumentBuilder();
                setEntityResolver(builder);
                return builder;
            } catch (ParserConfigurationException e) {
                throw new RuntimeException("Failed to create DocumentBuilder", e);
            }
        }

        private void setEntityResolver(DocumentBuilder builder) {
            String[] resolvers = new String[] { "org.jboss.ws.core.utils.JBossWSEntityResolver",
                    "org.jboss.util.xml.JBossEntityResolver" };

            EntityResolver entityResolver = null;
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            for (String resolver : resolvers) {
                try {
                    Class<?> resolverClass = loader.loadClass(resolver);
                    entityResolver = (EntityResolver) resolverClass.newInstance();
                } catch (Exception ex) {
                    //
                }
            }

            if (entityResolver != null)
                builder.setEntityResolver(entityResolver);
        }
    };

    /** Parse the given XML string and return the root Element
     */
    public static Element parse(String xmlString) throws IOException {
        try {
            return parse(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
        } catch (IOException e) {
            throw e;
        }
    }

    /** Parse the given XML stream and return the root Element
     */
    public static Element parse(InputStream xmlStream) throws IOException {
        try {
            return getDocumentBuilder().parse(xmlStream).getDocumentElement();
        } catch (SAXException se) {
            throw new IOException(se.toString());
        } finally {
            xmlStream.close();
        }
    }

    /** Parse the given input source and return the root Element
     */
    public static Element parse(InputSource source) throws IOException {
        try {
            return getDocumentBuilder().parse(source).getDocumentElement();
        } catch (SAXException se) {
            throw new IOException(se.toString());
        } finally {
            InputStream is = source.getByteStream();
            if (is != null) {
                is.close();
            }
            Reader r = source.getCharacterStream();
            if (r != null) {
                r.close();
            }
        }
    }

    /** Initialize the DocumentBuilder
     */
    public static DocumentBuilder getDocumentBuilder() {
        return builderThreadLocal.get();
    }
}

Related

  1. parse(final InputSource is, final Class loader)
  2. parse(InputSource in)
  3. parse(InputSource input, boolean validate, boolean namespaceAware, ErrorHandler errorHandler, EntityResolver entityResolver)
  4. parseXML(InputSource is)