Java XML DocumentBuilder Create getDocumentBuilder()

Here you can find the source of getDocumentBuilder()

Description

Initialize the DocumentBuilder

License

Open Source License

Declaration

public static DocumentBuilder getDocumentBuilder() 

Method Source Code

//package com.java2s;
/*/*from   w w  w.j a  v a  2  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.xml.sax.EntityResolver;

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

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);
        }
    };

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

Related

  1. getDocumentBuilder()
  2. getDocumentBuilder()
  3. getDocumentBuilder()
  4. getDocumentBuilder()
  5. getDocumentBuilder()