[java-support] branch master updated: JSPT-55: Consider adding BasicParserPool setters for EntityResolver and ErrorHandler
Brent Putman
putmanb at georgetown.edu
Fri Nov 6 20:07:11 EST 2015
This is an automated email from the git hooks/post-receive script.
putmanb pushed a commit to branch master
in repository java-support.
The following commit(s) were added to refs/heads/master by this push:
new f41b515 JSPT-55: Consider adding BasicParserPool setters for EntityResolver and ErrorHandler
f41b515 is described below
commit f41b51535169f15b8544f241678f17a41d731fdc
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Fri Nov 6 20:07:22 2015 -0500
JSPT-55: Consider adding BasicParserPool setters for EntityResolver and
ErrorHandler
---
.../java/support/xml/BasicParserPool.java | 40 +++++++++++-
.../java/support/xml/BasicParserPoolTest.java | 71 +++++++++++++++++++++-
2 files changed, 109 insertions(+), 2 deletions(-)
diff --git a/src/main/java/net/shibboleth/utilities/java/support/xml/BasicParserPool.java b/src/main/java/net/shibboleth/utilities/java/support/xml/BasicParserPool.java
index 7011f7f..155a6e7 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/xml/BasicParserPool.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/xml/BasicParserPool.java
@@ -124,7 +124,7 @@ public class BasicParserPool extends AbstractInitializableComponent implements P
private EntityResolver entityResolver;
/** Error handler used by builders. */
- private final ErrorHandler errorHandler;
+ private ErrorHandler errorHandler;
/** Constructor. */
public BasicParserPool() {
@@ -468,6 +468,44 @@ public class BasicParserPool extends AbstractInitializableComponent implements P
builderAttributes.remove("http://java.sun.com/xml/jaxp/properties/schemaLanguage");
}
}
+
+ /**
+ * Gets the {@link EntityResolver}.
+ *
+ * @return the configured entity resolver, may be null
+ */
+ @Nullable public EntityResolver getEntityResolver() {
+ return entityResolver;
+ }
+
+ /**
+ * Sets the {@link EntityResolver}.
+ *
+ * @param resolver the new entity resolver, may be null
+ */
+ public void setEntityResolver(@Nullable final EntityResolver resolver) {
+ checkNotInitializedNotDestroyed();
+ entityResolver = resolver;
+ }
+
+ /**
+ * Gets the {@link ErrorHandler}.
+ *
+ * @return the configured entity resolver, may be null
+ */
+ @Nonnull public ErrorHandler getErrorHandler() {
+ return errorHandler;
+ }
+
+ /**
+ * Sets the {@link ErrorHandler}.
+ *
+ * @param handler the new error handler
+ */
+ public void setErrorHandler(@Nonnull final ErrorHandler handler) {
+ checkNotInitializedNotDestroyed();
+ errorHandler = Constraint.isNotNull(handler, "ErrorHandler may not be null");
+ }
/**
* Gets whether the builders are validating.
diff --git a/src/test/java/net/shibboleth/utilities/java/support/xml/BasicParserPoolTest.java b/src/test/java/net/shibboleth/utilities/java/support/xml/BasicParserPoolTest.java
index 797099c..468da4f 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/xml/BasicParserPoolTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/xml/BasicParserPoolTest.java
@@ -37,6 +37,7 @@ import net.shibboleth.utilities.java.support.component.ComponentInitializationEx
import net.shibboleth.utilities.java.support.component.DestroyedComponentException;
import net.shibboleth.utilities.java.support.component.UninitializedComponentException;
import net.shibboleth.utilities.java.support.component.UnmodifiableComponentException;
+import net.shibboleth.utilities.java.support.logic.ConstraintViolationException;
import net.shibboleth.utilities.java.support.xml.BasicParserPool.DocumentBuilderProxy;
import org.springframework.core.io.ClassPathResource;
@@ -46,8 +47,11 @@ import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
/**
* Tests for {@link NamespaceSupport}
@@ -97,6 +101,11 @@ public class BasicParserPoolTest {
Schema schema = schemaBuilder.buildSchema();
basicParserPool.setSchema(schema);
basicParserPool.setXincludeAware(true);
+ EntityResolver entityResolver = new MockEntityResolver();
+ basicParserPool.setEntityResolver(entityResolver);
+ ErrorHandler errorHandler = new MockErrorHandler();
+ basicParserPool.setErrorHandler(errorHandler);
+
basicParserPool.initialize();
@@ -131,6 +140,10 @@ public class BasicParserPoolTest {
Assert.assertTrue(builder.isXIncludeAware(), "builder isXIncludeAware");
Assert.assertTrue(basicParserPool.isXincludeAware(), "pool isXIncludeAware");
+
+ Assert.assertSame(basicParserPool.getEntityResolver(), entityResolver);
+
+ Assert.assertSame(basicParserPool.getErrorHandler(), errorHandler);
basicParserPool = new BasicParserPool();
@@ -142,6 +155,13 @@ public class BasicParserPoolTest {
basicParserPool.setNamespaceAware(false);
basicParserPool.setSchema(null);
basicParserPool.setXincludeAware(false);
+ basicParserPool.setEntityResolver(null);
+ try {
+ basicParserPool.setErrorHandler(null);
+ Assert.fail("Null ErrorHandler should have been rejected");
+ } catch (ConstraintViolationException e) {
+ //Expected
+ }
basicParserPool.initialize();
@@ -168,6 +188,10 @@ public class BasicParserPoolTest {
Assert.assertFalse(builder.isXIncludeAware(), "builder isXIncludeAware");
Assert.assertFalse(basicParserPool.isXincludeAware(), "pool isXIncludeAware");
+
+ Assert.assertNull(basicParserPool.getEntityResolver(), "EntityResolver is non-null");
+
+ Assert.assertNotNull(basicParserPool.getErrorHandler(), "ErrorHandler was null");
}
@Test public void testInit() throws ComponentInitializationException, SAXException, XMLParserException, IOException {
@@ -288,6 +312,22 @@ public class BasicParserPoolTest {
thrown = false;
try {
+ basicParserPool.setEntityResolver(new MockEntityResolver());
+ } catch (UnmodifiableComponentException e) {
+ thrown = true;
+ }
+ Assert.assertTrue(thrown, "setEntityResolver after init");
+
+ thrown = false;
+ try {
+ basicParserPool.setErrorHandler(new MockErrorHandler());
+ } catch (UnmodifiableComponentException e) {
+ thrown = true;
+ }
+ Assert.assertTrue(thrown, "setErrorHandler after init");
+
+ thrown = false;
+ try {
basicParserPool.initialize();
} catch (ComponentInitializationException e) {
thrown = true;
@@ -398,6 +438,22 @@ public class BasicParserPoolTest {
thrown = false;
try {
+ pool.setEntityResolver(new MockEntityResolver());
+ } catch (DestroyedComponentException e) {
+ thrown = true;
+ }
+ Assert.assertTrue(thrown, "setEntityResolver after destroy");
+
+ thrown = false;
+ try {
+ pool.setErrorHandler(new MockErrorHandler());
+ } catch (DestroyedComponentException e) {
+ thrown = true;
+ }
+ Assert.assertTrue(thrown, "setErrorHandler after destroy");
+
+ thrown = false;
+ try {
pool.initialize();
} catch (DestroyedComponentException e) {
thrown = true;
@@ -695,6 +751,19 @@ pool.initialize();
}
-
+
+ // Helpers
+
+ public static class MockEntityResolver implements EntityResolver {
+ public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
+ return null;
+ }
+ }
+
+ public static class MockErrorHandler implements ErrorHandler {
+ public void warning(SAXParseException exception) throws SAXException { }
+ public void error(SAXParseException exception) throws SAXException { }
+ public void fatalError(SAXParseException exception) throws SAXException { }
+ }
}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list