Skip to content

Commit

Permalink
Add a convenient leaf handler
Browse files Browse the repository at this point in the history
This adds a convenient handler class for elements that consist only
of attributes.

Affects: #2
  • Loading branch information
io7m committed Oct 24, 2020
1 parent f70ba21 commit 0ca01a2
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 6 deletions.
2 changes: 1 addition & 1 deletion com.io7m.blackthorne.api/pom.xml
Expand Up @@ -8,7 +8,7 @@
<parent>
<artifactId>com.io7m.blackthorne</artifactId>
<groupId>com.io7m.blackthorne</groupId>
<version>1.0.1-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.blackthorne.api</artifactId>
Expand Down
@@ -0,0 +1,80 @@
/*
* Copyright © 2019 Mark Raynsford <code@io7m.com> 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 <S> The type of returned values
*/

public final class BTLeafElementHandler<S>
implements BTElementHandlerType<Object, S>
{
private final BTQualifiedName name;
private final BTLeafElementHandlerType<S> 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<S> 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;
}
}
@@ -0,0 +1,45 @@
/*
* Copyright © 2020 Mark Raynsford <code@io7m.com> 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 <S> The type of returned values
*/

public interface BTLeafElementHandlerType<S>
{
/**
* 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;
}
Expand Up @@ -19,7 +19,7 @@
*/

@Export
@Version("1.0.0")
@Version("1.1.0")
package com.io7m.blackthorne.api;

import org.osgi.annotation.bundle.Export;
Expand Down
2 changes: 1 addition & 1 deletion com.io7m.blackthorne.jxe/pom.xml
Expand Up @@ -8,7 +8,7 @@
<parent>
<artifactId>com.io7m.blackthorne</artifactId>
<groupId>com.io7m.blackthorne</groupId>
<version>1.0.1-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.blackthorne.jxe</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion com.io7m.blackthorne.tests/pom.xml
Expand Up @@ -8,7 +8,7 @@
<parent>
<artifactId>com.io7m.blackthorne</artifactId>
<groupId>com.io7m.blackthorne</groupId>
<version>1.0.1-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.blackthorne.tests</artifactId>
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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.<Number>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());
}
}
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -13,7 +13,7 @@

<groupId>com.io7m.blackthorne</groupId>
<artifactId>com.io7m.blackthorne</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>com.io7m.blackthorne</name>
Expand All @@ -33,7 +33,7 @@
<com.io7m.immutables.version>2.7.5</com.io7m.immutables.version>
<com.io7m.junit.version>5.7.0</com.io7m.junit.version>
<com.io7m.jxe.version>1.0.0</com.io7m.jxe.version>
<io7m.api.previousVersion>0.0.1-SNAPSHOT</io7m.api.previousVersion>
<io7m.api.previousVersion>1.0.0</io7m.api.previousVersion>
</properties>

<licenses>
Expand Down

0 comments on commit 0ca01a2

Please sign in to comment.