diff --git a/com.io7m.blackthorne.api/pom.xml b/com.io7m.blackthorne.api/pom.xml index b7fd4fe..bb7bd3a 100644 --- a/com.io7m.blackthorne.api/pom.xml +++ b/com.io7m.blackthorne.api/pom.xml @@ -8,7 +8,7 @@ com.io7m.blackthorne com.io7m.blackthorne - 1.0.1-SNAPSHOT + 1.1.0-SNAPSHOT com.io7m.blackthorne.api diff --git a/com.io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/BTLeafElementHandler.java b/com.io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/BTLeafElementHandler.java new file mode 100644 index 0000000..42265ce --- /dev/null +++ b/com.io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/BTLeafElementHandler.java @@ -0,0 +1,80 @@ +/* + * Copyright © 2019 Mark Raynsford http://io7m.com + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR + * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + + +package com.io7m.blackthorne.api; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +import java.util.Objects; + +/** + * A convenient handler dealing with elements that do not have children. + * + * @param The type of returned values + */ + +public final class BTLeafElementHandler + implements BTElementHandlerType +{ + private final BTQualifiedName name; + private final BTLeafElementHandlerType handler; + private S result; + + /** + * Construct a handler. + * + * @param inName The name of elements handled by this handler + * @param inHandler The element handler + */ + + public BTLeafElementHandler( + final BTQualifiedName inName, + final BTLeafElementHandlerType inHandler) + { + this.name = + Objects.requireNonNull(inName, "name"); + this.handler = + Objects.requireNonNull(inHandler, "handler"); + } + + @Override + public String name() + { + return this.name.localName(); + } + + @Override + public void onElementStart( + final BTElementParsingContextType context, + final Attributes attributes) + throws SAXException + { + try { + this.result = this.handler.onElement(context, attributes); + } catch (final Exception e) { + throw context.parseException(e); + } + } + + @Override + public S onElementFinished( + final BTElementParsingContextType context) + { + return this.result; + } +} diff --git a/com.io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/BTLeafElementHandlerType.java b/com.io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/BTLeafElementHandlerType.java new file mode 100644 index 0000000..c15f070 --- /dev/null +++ b/com.io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/BTLeafElementHandlerType.java @@ -0,0 +1,45 @@ +/* + * Copyright © 2020 Mark Raynsford http://io7m.com + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR + * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +package com.io7m.blackthorne.api; + +import org.xml.sax.Attributes; + +/** + * An element handler that takes the attributes of a single element and + * produces an output value. + * + * @param The type of returned values + */ + +public interface BTLeafElementHandlerType +{ + /** + * Process the attributes of an element. + * + * @param context The parse context + * @param attributes The element attributes + * + * @return A value of {@code S} + * + * @throws Exception On errors + */ + + S onElement( + BTElementParsingContextType context, + Attributes attributes) + throws Exception; +} diff --git a/com.io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/package-info.java b/com.io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/package-info.java index f894c3c..81fbddc 100644 --- a/com.io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/package-info.java +++ b/com.io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/package-info.java @@ -19,7 +19,7 @@ */ @Export -@Version("1.0.0") +@Version("1.1.0") package com.io7m.blackthorne.api; import org.osgi.annotation.bundle.Export; diff --git a/com.io7m.blackthorne.jxe/pom.xml b/com.io7m.blackthorne.jxe/pom.xml index 0020339..71bcf35 100644 --- a/com.io7m.blackthorne.jxe/pom.xml +++ b/com.io7m.blackthorne.jxe/pom.xml @@ -8,7 +8,7 @@ com.io7m.blackthorne com.io7m.blackthorne - 1.0.1-SNAPSHOT + 1.1.0-SNAPSHOT com.io7m.blackthorne.jxe diff --git a/com.io7m.blackthorne.tests/pom.xml b/com.io7m.blackthorne.tests/pom.xml index c5f732b..2719293 100644 --- a/com.io7m.blackthorne.tests/pom.xml +++ b/com.io7m.blackthorne.tests/pom.xml @@ -8,7 +8,7 @@ com.io7m.blackthorne com.io7m.blackthorne - 1.0.1-SNAPSHOT + 1.1.0-SNAPSHOT com.io7m.blackthorne.tests diff --git a/com.io7m.blackthorne.tests/src/test/java/com/io7m/blackthorne/tests/BlackthorneTest.java b/com.io7m.blackthorne.tests/src/test/java/com/io7m/blackthorne/tests/BlackthorneTest.java index 89f7b10..c86a932 100644 --- a/com.io7m.blackthorne.tests/src/test/java/com/io7m/blackthorne/tests/BlackthorneTest.java +++ b/com.io7m.blackthorne.tests/src/test/java/com/io7m/blackthorne/tests/BlackthorneTest.java @@ -22,6 +22,7 @@ import com.io7m.blackthorne.api.BTElementParsingContextType; import com.io7m.blackthorne.api.BTException; import com.io7m.blackthorne.api.BTIgnoreUnrecognizedElements; +import com.io7m.blackthorne.api.BTLeafElementHandler; import com.io7m.blackthorne.api.BTParseError; import com.io7m.blackthorne.api.BTQualifiedName; import com.io7m.blackthorne.api.Blackthorne; @@ -1246,4 +1247,35 @@ public void testConvenience1() Assertions.assertEquals(SAXParseException.class, ex.getCause().getClass()); Assertions.assertEquals(2, ex.errors().size()); } + + /** + * Leaf element handlers work. + * + * @throws Exception On errors + */ + + @Test + public void testLeafIntegerAttr0() + throws Exception + { + final var intAttr = + new BTLeafElementHandler<>( + BTQualifiedName.of("urn:tests", "intA"), + BlackthorneTest::parseIntAttribute + ); + + final var handler = + BTContentHandler.builder() + .addHandler("urn:tests", "intA", context -> intAttr) + .build(URI.create("urn:text"), this::logError); + + final var reader = createReader(); + reader.setContentHandler(handler); + reader.setErrorHandler(handler); + reader.parse(resource("intA.xml")); + + Assertions.assertFalse(handler.failed()); + Assertions.assertEquals(0, this.errors.size()); + Assertions.assertEquals(BigInteger.valueOf(23L), handler.result().get()); + } } diff --git a/pom.xml b/pom.xml index 47f91b3..9304ac8 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.io7m.blackthorne com.io7m.blackthorne - 1.0.1-SNAPSHOT + 1.1.0-SNAPSHOT pom com.io7m.blackthorne @@ -33,7 +33,7 @@ 2.7.5 5.7.0 1.0.0 - 0.0.1-SNAPSHOT + 1.0.0