< prev index next >

src/java.xml/share/classes/javax/xml/parsers/DocumentBuilderFactory.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -37,11 +37,12 @@
  *
  * @since 1.4
  */
 
 public abstract class DocumentBuilderFactory {
-
+    private static final String DEFAULT_IMPL =
+            "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
     private boolean validating = false;
     private boolean namespaceAware = false;
     private boolean whitespace = false;
     private boolean expandEntityRef = true;
     private boolean ignoreComments = false;

@@ -53,10 +54,80 @@
      */
     protected DocumentBuilderFactory () {
     }
 
     /**
+     * Creates a new NamespaceAware instance of the {@code DocumentBuilderFactory}
+     * builtin system-default implementation. Parsers produced by the factory
+     * instance provides support for XML namespaces by default.
+     *
+     * @implSpec
+     * In addition to creating a factory instance using the same process as
+     * {@link #newDefaultInstance()}, this method must set NamespaceAware to true.
+     *
+     * @return a new instance of the {@code DocumentBuilderFactory} builtin
+     *         system-default implementation.
+     *
+     * @since 13
+     */
+    public static DocumentBuilderFactory newDefaultNSInstance() {
+        return makeNSAware(new DocumentBuilderFactoryImpl());
+    }
+
+    /**
+     * Creates a new NamespaceAware instance of a {@code DocumentBuilderFactory}.
+     * Parsers produced by the factory instance provides support for XML namespaces
+     * by default.
+     *
+     * @implSpec
+     * In addition to creating a factory instance using the same process as
+     * {@link #newInstance()}, this method must set NamespaceAware to true.
+     *
+     * @return a new instance of a {@code DocumentBuilderFactory}
+     *
+     * @throws FactoryConfigurationError in case of {@linkplain
+     *         java.util.ServiceConfigurationError service configuration error}
+     *         or if the implementation is not available or cannot be instantiated.
+     *
+     * @since 13
+     */
+    public static DocumentBuilderFactory newNSInstance() {
+        return makeNSAware(FactoryFinder.find(DocumentBuilderFactory.class, DEFAULT_IMPL));
+    }
+
+    /**
+     * Creates a new NamespaceAware instance of a {@code DocumentBuilderFactory}
+     * from the class name. Parsers produced by the factory instance provides
+     * support for XML namespaces by default.
+     *
+     * @implSpec
+     * In addition to creating a factory instance using the same process as
+     * {@link #newInstance(java.lang.String, java.lang.ClassLoader)}, this method
+     * must set NamespaceAware to true.
+     *
+     * @param factoryClassName a fully qualified factory class name that provides
+     *                         implementation of
+     *                         {@code javax.xml.parsers.DocumentBuilderFactory}.
+     *
+     * @param classLoader the {@code ClassLoader} used to load the factory class.
+     *                    If it is {@code null}, the current {@code Thread}'s
+     *                    context classLoader is used to load the factory class.
+     *
+     * @return a new instance of a {@code DocumentBuilderFactory}
+     *
+     * @throws FactoryConfigurationError if {@code factoryClassName} is {@code null}, or
+     *                                   the factory class cannot be loaded, instantiated.
+     *
+     * @since 13
+     */
+    public static DocumentBuilderFactory newNSInstance(String factoryClassName,
+            ClassLoader classLoader) {
+            return makeNSAware(FactoryFinder.newInstance(
+                    DocumentBuilderFactory.class, factoryClassName, classLoader, false));
+    }
+
+    /**
      * Creates a new instance of the {@code DocumentBuilderFactory} builtin
      * system-default implementation.
      *
      * @return A new instance of the {@code DocumentBuilderFactory} builtin
      *         system-default implementation.

@@ -139,11 +210,11 @@
     public static DocumentBuilderFactory newInstance() {
         return FactoryFinder.find(
                 /* The default property name according to the JAXP spec */
                 DocumentBuilderFactory.class, // "javax.xml.parsers.DocumentBuilderFactory"
                 /* The fallback implementation class name */
-                "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
+                DEFAULT_IMPL);
     }
 
     /**
      * Obtain a new instance of a {@code DocumentBuilderFactory} from class name.
      * This function is useful when there are multiple providers in the classpath.

@@ -183,10 +254,15 @@
             //do not fallback if given classloader can't find the class, throw exception
             return FactoryFinder.newInstance(DocumentBuilderFactory.class,
                         factoryClassName, classLoader, false);
     }
 
+    private static DocumentBuilderFactory makeNSAware(DocumentBuilderFactory dbf) {
+        dbf.setNamespaceAware(true);
+        return dbf;
+    }
+
     /**
      * Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}
      * using the currently configured parameters.
      *
      * @return A new instance of a DocumentBuilder.
< prev index next >