[java-opensaml] branch main updated: Add additional helper methods to factories, fix some warnings.
Scott Cantor
cantor.2 at osu.edu
Tue Mar 14 23:54:28 UTC 2023
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch main
in repository java-opensaml.
View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=9a0c83454cbd2c3342235608d8062ded94c4bc61
The following commit(s) were added to refs/heads/main by this push:
new 9a0c83454 Add additional helper methods to factories, fix some warnings.
9a0c83454 is described below
commit 9a0c83454cbd2c3342235608d8062ded94c4bc61
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Mar 14 19:54:25 2023 -0400
Add additional helper methods to factories, fix some warnings.
---
.../core/xml/io/AbstractXMLObjectMarshaller.java | 1 +
.../core/xml/io/AbstractXMLObjectUnmarshaller.java | 3 +-
.../opensaml/core/xml/io/MarshallerFactory.java | 68 ++++++++++++++++------
.../opensaml/core/xml/io/UnmarshallerFactory.java | 53 ++++++++++++++---
.../core/testing/XMLObjectBaseTestCase.java | 24 +++-----
5 files changed, 107 insertions(+), 42 deletions(-)
diff --git a/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/AbstractXMLObjectMarshaller.java b/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/AbstractXMLObjectMarshaller.java
index 1ce50ac68..f8e13bb7d 100644
--- a/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/AbstractXMLObjectMarshaller.java
+++ b/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/AbstractXMLObjectMarshaller.java
@@ -158,6 +158,7 @@ public abstract class AbstractXMLObjectMarshaller implements Marshaller {
log.trace("{} does not contain a cached DOM representation. Creating Element to marshall into.", xmlObject
.getElementQName());
final Document owningDocument = parentElement.getOwnerDocument();
+ assert owningDocument != null;
domElement = ElementSupport.constructElement(owningDocument, xmlObject.getElementQName());
log.trace("Appending newly created element to given parent element");
diff --git a/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/AbstractXMLObjectUnmarshaller.java b/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/AbstractXMLObjectUnmarshaller.java
index 4326ab4eb..e3dec6380 100644
--- a/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/AbstractXMLObjectUnmarshaller.java
+++ b/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/AbstractXMLObjectUnmarshaller.java
@@ -23,6 +23,7 @@ import javax.annotation.Nonnull;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
+import net.shibboleth.shared.primitive.LoggerFactory;
import net.shibboleth.shared.primitive.StringSupport;
import net.shibboleth.shared.xml.ParserPool;
import net.shibboleth.shared.xml.QNameSupport;
@@ -37,7 +38,7 @@ import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
import org.opensaml.core.xml.schema.XSBooleanValue;
import org.opensaml.core.xml.util.XMLObjectSupport;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
diff --git a/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/MarshallerFactory.java b/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/MarshallerFactory.java
index 3f14f4110..fdcbcdb5e 100644
--- a/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/MarshallerFactory.java
+++ b/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/MarshallerFactory.java
@@ -17,7 +17,6 @@
package org.opensaml.core.xml.io;
-import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -26,10 +25,12 @@ import javax.annotation.Nullable;
import javax.xml.namespace.QName;
import org.opensaml.core.xml.XMLObject;
+import org.opensaml.core.xml.XMLRuntimeException;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import net.shibboleth.shared.collection.CollectionSupport;
import net.shibboleth.shared.logic.Constraint;
+import net.shibboleth.shared.primitive.LoggerFactory;
/**
* This thread-safe factory creates {@link org.opensaml.core.xml.io.Marshaller}s that can be used to convert
@@ -40,10 +41,10 @@ import net.shibboleth.shared.logic.Constraint;
public class MarshallerFactory {
/** Class logger. */
- private final Logger log = LoggerFactory.getLogger(MarshallerFactory.class);
+ @Nonnull private final Logger log = LoggerFactory.getLogger(MarshallerFactory.class);
/** Map of marshallers to the elements they are for. */
- private final Map<QName, Marshaller> marshallers;
+ @Nonnull private final Map<QName, Marshaller> marshallers;
/**
* Constructor.
@@ -59,11 +60,7 @@ public class MarshallerFactory {
*
* @return the Marshaller or null
*/
- @Nullable public Marshaller getMarshaller(@Nullable final QName key) {
- if (key == null) {
- return null;
- }
-
+ @Nullable public Marshaller getMarshaller(@Nonnull final QName key) {
return marshallers.get(key);
}
@@ -76,9 +73,12 @@ public class MarshallerFactory {
* @return the marshaller that can be used for the given XMLObject
*/
@Nullable public Marshaller getMarshaller(@Nonnull final XMLObject xmlObject) {
- Marshaller marshaller;
+ Marshaller marshaller = null;
- marshaller = getMarshaller(xmlObject.getSchemaType());
+ final QName xsitype = xmlObject.getSchemaType();
+ if (xsitype != null) {
+ marshaller = getMarshaller(xsitype);
+ }
if (marshaller == null) {
marshaller = getMarshaller(xmlObject.getElementQName());
@@ -87,13 +87,51 @@ public class MarshallerFactory {
return marshaller;
}
+ /**
+ * Call {@link #getMarshaller(QName)} and raise an exception if no marshaller is registered.
+ *
+ * @param key type of marshaller to fetch
+ *
+ * @return the registered marshaller
+ *
+ * @throw XMLRuntimeException if no marshaller is registered
+ *
+ * @since 5.0.0
+ */
+ @Nonnull public Marshaller ensureMarshaller(@Nonnull final QName key) {
+ final Marshaller m = getMarshaller(key);
+ if (m != null) {
+ return m;
+ }
+ throw new XMLRuntimeException("Unable to obtain marshaller for " + key.toString());
+ }
+
+ /**
+ * Call {@link #getMarshaller(XMLObject)} and raise an exception if no marshaller is registered.
+ *
+ * @param xmlObject type of marshaller to fetch
+ *
+ * @return the registered marshaller
+ *
+ * @throw XMLRuntimeException if no marshaller is registered
+ *
+ * @since 5.0.0
+ */
+ @Nonnull public Marshaller ensureMarshaller(@Nonnull final XMLObject xmlObject) {
+ final Marshaller m = getMarshaller(xmlObject);
+ if (m != null) {
+ return m;
+ }
+ throw new XMLRuntimeException("Unable to obtain marshaller for " + xmlObject.getClass().getName());
+ }
+
/**
* Gets an immutable listing of all the Marshallers currently registered.
*
* @return a listing of all the Marshallers currently registered
*/
@Nonnull public Map<QName, Marshaller> getMarshallers() {
- return Collections.unmodifiableMap(marshallers);
+ return CollectionSupport.copyToMap(marshallers);
}
/**
@@ -120,10 +158,6 @@ public class MarshallerFactory {
*/
@Nullable public Marshaller deregisterMarshaller(@Nonnull final QName key) {
log.debug("Deregistering marshaller for object type {}", key);
- if(key != null){
- return marshallers.remove(key);
- }
-
- return null;
+ return marshallers.remove(key);
}
}
\ No newline at end of file
diff --git a/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/UnmarshallerFactory.java b/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/UnmarshallerFactory.java
index f3e6867a2..8bbcbd275 100644
--- a/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/UnmarshallerFactory.java
+++ b/opensaml-core-api/src/main/java/org/opensaml/core/xml/io/UnmarshallerFactory.java
@@ -26,11 +26,12 @@ import javax.xml.namespace.QName;
import net.shibboleth.shared.collection.CollectionSupport;
import net.shibboleth.shared.logic.Constraint;
+import net.shibboleth.shared.primitive.LoggerFactory;
import net.shibboleth.shared.xml.DOMTypeSupport;
import net.shibboleth.shared.xml.QNameSupport;
+import org.opensaml.core.xml.XMLRuntimeException;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
/**
@@ -42,10 +43,10 @@ import org.w3c.dom.Element;
public class UnmarshallerFactory {
/** Class logger. */
- private final Logger log = LoggerFactory.getLogger(UnmarshallerFactory.class);
+ @Nonnull private final Logger log = LoggerFactory.getLogger(UnmarshallerFactory.class);
/** Map of unmarshallers to the elements they are for. */
- private final Map<QName, Unmarshaller> unmarshallers;
+ @Nonnull private final Map<QName, Unmarshaller> unmarshallers;
/**
* Constructor.
@@ -87,6 +88,45 @@ public class UnmarshallerFactory {
return unmarshaller;
}
+
+ /**
+ * Call {@link #getUnmarshaller(QName)} and raise an exception if no unmarshaller is registered.
+ *
+ * @param key type of unmarshaller to fetch
+ *
+ * @return the registered unmarshaller
+ *
+ * @throw XMLRuntimeException if no unmarshaller is registered
+ *
+ * @since 5.0.0
+ */
+ @Nonnull public Unmarshaller ensureUnmarshaller(@Nonnull final QName key) {
+ final Unmarshaller m = getUnmarshaller(key);
+ if (m != null) {
+ return m;
+ }
+ throw new XMLRuntimeException("Unable to obtain unmarshaller for " + key.toString());
+ }
+
+ /**
+ * Call {@link #getUnmarshaller(Element)} and raise an exception if no unmarshaller is registered.
+ *
+ * @param domElement element to find unmarshaller for
+ *
+ * @return the registered unmarshaller
+ *
+ * @throw XMLRuntimeException if no unmarshaller is registered
+ *
+ * @since 5.0.0
+ */
+ @Nonnull public Unmarshaller ensureUnmarshaller(@Nonnull final Element domElement) {
+ final Unmarshaller m = getUnmarshaller(domElement);
+ if (m != null) {
+ return m;
+ }
+ throw new XMLRuntimeException("Unable to obtain unmarshaller for " +
+ QNameSupport.getNodeQName(domElement).toString());
+ }
/**
* Gets an immutable listing of all the Unarshallers currently registered.
@@ -121,10 +161,7 @@ public class UnmarshallerFactory {
*/
@Nullable public Unmarshaller deregisterUnmarshaller(@Nonnull final QName key) {
log.debug("Deregistering marshaller for object type {}", key);
- if (key != null) {
- return unmarshallers.remove(key);
- }
-
- return null;
+ return unmarshallers.remove(key);
}
+
}
\ No newline at end of file
diff --git a/opensaml-testing/src/main/java/org/opensaml/core/testing/XMLObjectBaseTestCase.java b/opensaml-testing/src/main/java/org/opensaml/core/testing/XMLObjectBaseTestCase.java
index 43bff224a..dfa5597b9 100644
--- a/opensaml-testing/src/main/java/org/opensaml/core/testing/XMLObjectBaseTestCase.java
+++ b/opensaml-testing/src/main/java/org/opensaml/core/testing/XMLObjectBaseTestCase.java
@@ -241,7 +241,7 @@ public abstract class XMLObjectBaseTestCase extends OpenSAMLInitBaseTestCase {
*
* @return the XMLObjectBuilder
*/
- protected <T extends XMLObject> XMLObjectBuilder<T> getBuilder(@Nonnull final QName qname) {
+ @Nonnull protected <T extends XMLObject> XMLObjectBuilder<T> getBuilder(@Nonnull final QName qname) {
return builderFactory.ensureBuilder(qname);
}
@@ -251,12 +251,8 @@ public abstract class XMLObjectBaseTestCase extends OpenSAMLInitBaseTestCase {
* @param qname the QName for which to find the marshaller
* @return the marshaller
*/
- protected Marshaller getMarshaller(@Nonnull final QName qname) {
- Marshaller marshaller = marshallerFactory.getMarshaller(qname);
- if (marshaller == null) {
- Assert.fail("no marshaller registered for " + qname);
- }
- return marshaller;
+ @Nonnull protected Marshaller getMarshaller(@Nonnull final QName qname) {
+ return marshallerFactory.ensureMarshaller(qname);
}
/**
@@ -265,12 +261,8 @@ public abstract class XMLObjectBaseTestCase extends OpenSAMLInitBaseTestCase {
* @param xmlObject the XMLObject for which to find the marshaller
* @return the marshaller
*/
- protected Marshaller getMarshaller(@Nonnull final XMLObject xmlObject) {
- Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
- if (marshaller == null) {
- Assert.fail("no marshaller registered for " + xmlObject.getClass().getName());
- }
- return marshaller;
+ @Nonnull protected Marshaller getMarshaller(@Nonnull final XMLObject xmlObject) {
+ return marshallerFactory.ensureMarshaller(xmlObject);
}
/**
@@ -293,7 +285,7 @@ public abstract class XMLObjectBaseTestCase extends OpenSAMLInitBaseTestCase {
* @param xmlObject the XMLObject for which to find the unmarshaller
* @return the unmarshaller
*/
- protected Unmarshaller getUnmarshaller(@Nonnull final XMLObject xmlObject) {
+ @Nonnull protected Unmarshaller getUnmarshaller(@Nonnull final XMLObject xmlObject) {
return getUnmarshaller(xmlObject.getElementQName());
}
@@ -303,7 +295,7 @@ public abstract class XMLObjectBaseTestCase extends OpenSAMLInitBaseTestCase {
* @param element the Element for which to find the unmarshaller
* @return the unmarshaller
*/
- protected Unmarshaller getUnmarshaller(@Nonnull final Element element) {
+ @Nonnull protected Unmarshaller getUnmarshaller(@Nonnull final Element element) {
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
if (unmarshaller == null) {
Assert.fail("no unmarshaller registered for " + QNameSupport.getNodeQName(element));
@@ -318,7 +310,7 @@ public abstract class XMLObjectBaseTestCase extends OpenSAMLInitBaseTestCase {
* @return the parsed Document
* @throws XMLParserException if parsing did not succeed
*/
- protected Document parseXMLDocument(@Nonnull final String xmlFilename) throws XMLParserException {
+ @Nonnull protected Document parseXMLDocument(@Nonnull final String xmlFilename) throws XMLParserException {
InputStream is = getClass().getResourceAsStream(xmlFilename);
Document doc = parserPool.parse(is);
return doc;
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list