[java-opensaml COMMIT] /trunk/opensaml-core/src/main/java/org/opensaml/core/xml/XMLObjectBuilderFactory.java

noreply at shibboleth.net noreply at shibboleth.net
Wed Dec 18 17:07:10 EST 2013


Author: scantor
Date: Wed Dec 18 17:07:10 2013
New Revision: 3519

URL: http://svn.shibboleth.net/view/java-opensaml?rev=3519&view=rev
Log:
Add null-checking builder accessors.

Modified:
    trunk/opensaml-core/src/main/java/org/opensaml/core/xml/XMLObjectBuilderFactory.java

Modified: trunk/opensaml-core/src/main/java/org/opensaml/core/xml/XMLObjectBuilderFactory.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-core/src/main/java/org/opensaml/core/xml/XMLObjectBuilderFactory.java?rev=3519&r1=3518&r2=3519&view=diff
==============================================================================
--- trunk/opensaml-core/src/main/java/org/opensaml/core/xml/XMLObjectBuilderFactory.java (original)
+++ trunk/opensaml-core/src/main/java/org/opensaml/core/xml/XMLObjectBuilderFactory.java Wed Dec 18 17:07:10 2013
@@ -25,6 +25,8 @@
 import javax.annotation.Nullable;
 import javax.xml.namespace.QName;
 
+import net.shibboleth.utilities.java.support.annotation.constraint.NotLive;
+import net.shibboleth.utilities.java.support.annotation.constraint.Unmodifiable;
 import net.shibboleth.utilities.java.support.logic.Constraint;
 import net.shibboleth.utilities.java.support.xml.DomTypeSupport;
 import net.shibboleth.utilities.java.support.xml.QNameSupport;
@@ -59,7 +61,7 @@
      * @return the builder, or null
      */
     @Nullable public XMLObjectBuilder<?> getBuilder(@Nullable final QName key) {
-        if(key == null){
+        if (key == null){
             return null;
         }
         return builders.get(key);
@@ -74,22 +76,65 @@
      * @return the builder for the XMLObject the given element can be unmarshalled into, or null
      */
     @Nullable public XMLObjectBuilder<?> getBuilder(@Nullable final Element domElement) {
-
+    
         XMLObjectBuilder<?> builder = getBuilder(DomTypeSupport.getXSIType(domElement));
-
+    
         if (builder == null) {
             builder = getBuilder(QNameSupport.getNodeQName(domElement));
         }
-
+    
         return builder;
     }
 
+    /**
+     * Retrieves an {@link XMLObjectBuilder} using the key it was registered with, or throws a runtime
+     * error if unable to locate one.
+     * 
+     * @param <XMLObjectType> the type of object the builder is assumed to support
+     * @param key the key used to register the builder
+     * 
+     * @return the builder
+     * @throws XMLRuntimeException  if the builder can't be obtained
+     */
+    @Nonnull public <XMLObjectType extends XMLObject> XMLObjectBuilder<XMLObjectType> getBuilderOrThrow(
+            @Nonnull final QName key) {
+
+        XMLObjectBuilder<?> builder = getBuilder(key);
+        if (builder == null) {
+            throw new XMLRuntimeException("Unable to locate a builder for " + key);
+        }
+        
+        return (XMLObjectBuilder<XMLObjectType>) builder;
+    }
+    
+    /**
+     * Retrieves the {@link XMLObjectBuilder} for the given element. The schema type, if present, is tried first
+     * as the key with the element QName used if no schema type is present or does not have a builder registered
+     * under it.
+     * 
+     * @param <XMLObjectType> the type of object the builder is assumed to support
+     * @param domElement the element to retrieve the builder for
+     * 
+     * @return the builder for the XMLObject the given element can be unmarshalled into
+     * @throws XMLRuntimeException  if the builder can't be obtained
+     */
+    @Nonnull public <XMLObjectType extends XMLObject> XMLObjectBuilder<XMLObjectType> getBuilderOrThrow(
+            @Nonnull final Element domElement) {
+        
+        XMLObjectBuilder<?> builder = getBuilder(domElement);
+        if (builder == null) {
+            throw new XMLRuntimeException("Unable to locate a builder for " + domElement.getLocalName());
+        }
+        
+        return (XMLObjectBuilder<XMLObjectType>) builder;
+    }
+    
     /**
      * Gets an immutable list of all the builders currently registered.
      * 
      * @return list of all the builders currently registered
      */
-    @Nonnull public Map<QName, XMLObjectBuilder<?>> getBuilders() {
+    @Nonnull @NotLive @Unmodifiable public Map<QName, XMLObjectBuilder<?>> getBuilders() {
         return Collections.unmodifiableMap(builders);
     }
 



More information about the commits mailing list