Example usage for javax.xml.parsers DocumentBuilderFactory getClass

List of usage examples for javax.xml.parsers DocumentBuilderFactory getClass

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

/**
 * Used to optimize performance by avoiding expensive file access every time
 * a DocumentBuilderFactory is constructed as a result of constructing a
 * Xalan document factory./* w ww.  j a  v  a  2  s . c  o m*/
 */
private static void speedUpDcoumentBuilderFactory() {
    if (System.getProperty(DOCUMENT_BUILDER_FACTORY_PROP_NAME) == null) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        if (DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME.equals(factory.getClass().getName())) {
            // This would avoid the file system to be accessed every time
            // the internal DocumentBuilderFactory is instantiated.
            System.setProperty(DOCUMENT_BUILDER_FACTORY_PROP_NAME, DOCUMENT_BUILDER_FACTORY_IMPL_CLASS_NAME);
        }
    }
}

From source file:com.microsoft.tfs.util.xml.JAXPUtils.java

/**
 * A convenience method to create a new {@link DocumentBuilderFactory}. If
 * the given {@link ClassLoader} is not <code>null</code>, it is temporarily
 * set as the calling thread's context classloader while calling into JAXP
 * to get a {@link DocumentBuilderFactory} instance. The factory is
 * configured to be namespace aware (/*from   w  ww .j a v a  2s.c  o m*/
 * {@link DocumentBuilderFactory#setNamespaceAware(boolean)} is called with
 * <code>true</code>).
 *
 * @throws XMLException
 *         if a new factory can't be created
 *
 * @param contextClassloader
 *        the context classloader or <code>null</code> if none
 * @return a new {@link DocumentBuilderFactory} (never <code>null</code>)
 */
public static DocumentBuilderFactory newDocumentBuilderFactory(final ClassLoader contextClassloader) {
    final boolean setTCL = contextClassloader != null;
    ClassLoader currentTCL = null;

    if (setTCL) {
        currentTCL = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(contextClassloader);
    }

    try {
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        if (log.isTraceEnabled()) {
            final String messageFormat = "Created a new DocumentBuilderFactory: {0}(loaded from: {1})"; //$NON-NLS-1$
            final String message = MessageFormat.format(messageFormat, factory.getClass().getName(),
                    factory.getClass().getClassLoader());
            log.trace(message);
        }

        factory.setNamespaceAware(true);

        return factory;
    } catch (final FactoryConfigurationError e) {
        throw new XMLException(e);
    } finally {
        if (setTCL) {
            Thread.currentThread().setContextClassLoader(currentTCL);
        }
    }
}

From source file:com.microsoft.tfs.util.xml.JAXPUtils.java

/**
 * A convenience method to create a new {@link DocumentBuilder} using the
 * given {@link DocumentBuilderFactory}. The new {@link DocumentBuilder} is
 * not configured in any way by this method.
 *
 * @throws XMLException/*w  w  w .  j a  va2  s.  co  m*/
 *         if a new {@link DocumentBuilder} can't be created
 *
 * @param factory
 *        the {@link DocumentBuilderFactory} to use or <code>null</code> to
 *        create a new factory using the
 *        {@link #newDocumentBuilderFactory()} method
 * @return a new {@link DocumentBuilder} created from the given
 *         {@link DocumentBuilderFactory} (never <code>null</code>)
 */
public static DocumentBuilder newDocumentBuilder(DocumentBuilderFactory factory) {
    if (factory == null) {
        factory = newDocumentBuilderFactory();
    }

    try {
        final DocumentBuilder builder = factory.newDocumentBuilder();

        if (log.isTraceEnabled()) {
            final String messageFormat = "Created a new DocumentBuilder: {0} (loaded from: {1}) (factory: {2})"; //$NON-NLS-1$
            final String message = MessageFormat.format(messageFormat, builder.getClass().getName(),
                    builder.getClass().getClassLoader(), factory.getClass().getName());
            log.trace(message);
        }

        return builder;
    } catch (final ParserConfigurationException e) {
        throw new XMLException(e);
    }
}

From source file:com.microsoft.tfs.util.xml.JAXPUtils.java

/**
 * <p>/*  w w w .ja  v  a2s  .  com*/
 * Creates a new (or configures an existing) {@link DocumentBuilderFactory}
 * that will perform XML Schema validation when parsing. This method is
 * called before parsing to obtain a configured
 * {@link DocumentBuilderFactory} that produces {@link DocumentBuilder}s
 * that will be used for XML Schema for validation.
 * </p>
 *
 * <p>
 * The supplied <code>schemaSource</code> object must be one of the
 * following:
 * <ul>
 * <li>A {@link String} that points to the URI of the schema</li>
 *
 * <li>An {@link InputStream} with the schema contents (will not be closed
 * by this method)</li>
 *
 * <li>A SAX {@link InputSource} that indicates the schema</li>
 *
 * <li>A {@link File} that indicates the schema</li>
 *
 * <li>An array of objects, each one of which is one of the above</li>
 * </ul>
 * </p>
 *
 * @throws XMLException
 *         if the {@link DocumentBuilderFactory} can't be created or
 *         properly configured
 *
 * @param factory
 *        the {@link DocumentBuilderFactory} to configure, or
 *        <code>null</code> to create a {@link DocumentBuilderFactory} using
 *        the {@link #newDocumentBuilderFactory()} method
 * @param schemaSource
 *        the schema source as described above
 * @return a configured {@link DocumentBuilderFactory} (never
 *         <code>null</code>)
 */
public static DocumentBuilderFactory newDocumentBuilderFactoryForXSValidation(DocumentBuilderFactory factory,
        final Object schemaSource) {
    if (factory == null) {
        factory = newDocumentBuilderFactory();
    }

    factory.setNamespaceAware(true);
    factory.setValidating(true);

    try {
        factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
    } catch (final IllegalArgumentException e) {
        final String messageFormat = "The DocumentBuilderFactory [{0}] loaded from ClassLoader [{1}] does not support JAXP 1.2"; //$NON-NLS-1$
        final String message = MessageFormat.format(messageFormat, factory.getClass().getName(),
                factory.getClass().getClassLoader());
        throw new XMLException(message, e);
    }

    if (schemaSource != null) {
        factory.setAttribute(JAXP_SCHEMA_SOURCE, schemaSource);
    }

    return factory;
}

From source file:nl.nn.adapterframework.util.XmlUtils.java

public static String getVersionInfo() {
    StringBuilder sb = new StringBuilder();
    sb.append(AppConstants.getInstance().getProperty("application.name") + " "
            + AppConstants.getInstance().getProperty("application.version")).append(SystemUtils.LINE_SEPARATOR);
    sb.append("XML tool version info:").append(SystemUtils.LINE_SEPARATOR);

    SAXParserFactory spFactory = getSAXParserFactory();
    sb.append("SAXParserFactory-class =").append(spFactory.getClass().getName())
            .append(SystemUtils.LINE_SEPARATOR);
    DocumentBuilderFactory domFactory1 = getDocumentBuilderFactory(false);
    sb.append("DocumentBuilderFactory1-class =").append(domFactory1.getClass().getName())
            .append(SystemUtils.LINE_SEPARATOR);
    DocumentBuilderFactory domFactory2 = getDocumentBuilderFactory(true);
    sb.append("DocumentBuilderFactory2-class =").append(domFactory2.getClass().getName())
            .append(SystemUtils.LINE_SEPARATOR);

    TransformerFactory tFactory1 = getTransformerFactory(false);
    sb.append("TransformerFactory1-class =").append(tFactory1.getClass().getName())
            .append(SystemUtils.LINE_SEPARATOR);
    TransformerFactory tFactory2 = getTransformerFactory(true);
    sb.append("TransformerFactory2-class =").append(tFactory2.getClass().getName())
            .append(SystemUtils.LINE_SEPARATOR);

    sb.append("Apache-XML tool version info:").append(SystemUtils.LINE_SEPARATOR);

    try {/*from   w w  w . j a  va  2 s  .  c o m*/
        sb.append("Xerces-Version=").append(org.apache.xerces.impl.Version.getVersion())
                .append(SystemUtils.LINE_SEPARATOR);
    } catch (Throwable t) {
        sb.append("Xerces-Version not found (").append(t.getClass().getName()).append(": ")
                .append(t.getMessage()).append(")").append(SystemUtils.LINE_SEPARATOR);
    }

    try {
        String xalanVersion;
        if (IbisContext.getApplicationServerType().startsWith("WAS")) {
            xalanVersion = nl.nn.org.apache.xalan.Version.getVersion();
        } else {
            xalanVersion = org.apache.xalan.Version.getVersion();
        }
        sb.append("Xalan-Version=" + xalanVersion + SystemUtils.LINE_SEPARATOR);
    } catch (Throwable t) {
        sb.append("Xalan-Version not found (").append(t.getClass().getName()).append(": ")
                .append(t.getMessage()).append(")").append(SystemUtils.LINE_SEPARATOR);
    }

    try {
        //         sb.append("XmlCommons-Version="+org.apache.xmlcommons.Version.getVersion()+SystemUtils.LINE_SEPARATOR);
    } catch (Throwable t) {
        sb.append("XmlCommons-Version not found (").append(t.getClass().getName()).append(": ")
                .append(t.getMessage()).append(")").append(SystemUtils.LINE_SEPARATOR);
    }

    return sb.toString();
}

From source file:org.apache.woden.internal.DOMWSDLReader.java

/**
 * Create the JAXP DocumentBuilderFactory instance.Use JAXP 1.2 API for validation.     
 * @param namespaceAware whether the returned factory is to provide support for XML namespaces
 * @return the JAXP DocumentBuilderFactory
 * @throws ParserConfigurationException if we failed to build a proper DocumentBuilderFactory
 */// w  w  w . java 2s . c  om
protected DocumentBuilderFactory createDocumentBuilderFactory(boolean namespaceAware)
        throws ParserConfigurationException, WSDLException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(namespaceAware);

    // Enable validation on the XML parser if it has been enabled 
    // for the Woden parser.
    if (features.getValue(WSDLReader.FEATURE_VALIDATION)) {
        factory.setValidating(true);
        // Enforce namespace aware for XSD...
        factory.setNamespaceAware(true);
        try {
            factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
            factory.setAttribute(JAXP_SCHEMA_SOURCE, schemas);

        } catch (IllegalArgumentException e) {
            getErrorReporter().reportError(new ErrorLocatorImpl(), //TODO line&col nos.
                    "WSDL515", new Object[] { factory.getClass().getName() },
                    ErrorReporter.SEVERITY_FATAL_ERROR, e);
        }
    } else {
        factory.setValidating(false);
    }

    return factory;
}

From source file:org.settings4j.config.DOMConfigurator.java

private void doConfigure(final ParseAction action) throws FactoryConfigurationError {
    DocumentBuilderFactory dbf = null;
    try {/* w  w  w  . j  a va 2s  . co  m*/
        LOG.debug("System property is: {}", System.getProperty(DOCUMENT_BUILDER_FACTORY_KEY));
        dbf = DocumentBuilderFactory.newInstance();
        LOG.debug("Standard DocumentBuilderFactory search succeded.");
        LOG.debug("DocumentBuilderFactory is: {}", dbf.getClass().getName());
    } catch (final FactoryConfigurationError fce) {
        final Exception e = fce.getException();
        LOG.debug("Could not instantiate a DocumentBuilderFactory.", e);
        throw fce;
    }

    try {
        dbf.setValidating(true);

        final DocumentBuilder docBuilder = dbf.newDocumentBuilder();

        docBuilder.setErrorHandler(new SAXErrorHandler());
        docBuilder.setEntityResolver(new Settings4jEntityResolver());

        final Document doc = action.parse(docBuilder);
        parse(doc.getDocumentElement());
    } catch (final Exception e) {
        // I know this is miserable...
        LOG.error("Could not parse {}.", action.toString(), e);
    }
}

From source file:org.springframework.beans.factory.xml.DefaultDocumentLoader.java

/**
 * Load the {@link Document} at the supplied {@link InputSource} using the standard JAXP-configured
 * XML parser.//  ww  w  .  ja  v a  2s  .  c o m
 */
@Override
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler,
        int validationMode, boolean namespaceAware) throws Exception {

    DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
    if (logger.isDebugEnabled()) {
        logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
    }
    DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
    return builder.parse(inputSource);
}