Skip to content

Commit

Permalink
Add more scalar element handlers
Browse files Browse the repository at this point in the history
This adds more scalar element handlers to the Blackthorne API to make
it more convenient to parse text values from elements.

Affects: #2
  • Loading branch information
io7m committed Jan 24, 2021
1 parent cfd2d98 commit 0cc1b8b
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 8 deletions.
9 changes: 7 additions & 2 deletions README-CHANGES.xml
Expand Up @@ -13,13 +13,18 @@
</c:change>
</c:changes>
</c:release>
<c:release date="2021-01-24T12:38:06+00:00" is-open="true" ticket-system="com.github.io7m.blackthorne" version="1.2.0">
<c:release date="2021-01-24T12:55:01+00:00" is-open="true" ticket-system="com.github.io7m.blackthorne" version="1.2.0">
<c:changes>
<c:change date="2021-01-24T12:38:06+00:00" summary="Add logging for content handlers">
<c:change date="2021-01-24T00:00:00+00:00" summary="Add logging for content handlers">
<c:tickets>
<c:ticket id="3"/>
</c:tickets>
</c:change>
<c:change date="2021-01-24T12:55:01+00:00" summary="Add more convenience methods for scalar elements">
<c:tickets>
<c:ticket id="2"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
</c:releases>
Expand Down
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.1.1-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.blackthorne.api</artifactId>
Expand Down
Expand Up @@ -276,6 +276,87 @@ public static <S> BTElementHandlerConstructorType<S, List<S>> forListPoly(
ignoreUnrecognized);
}

/**
* A convenience function for constructing content handlers that produce a scalar value from the
* text content of a single XML element.
*
* @param elementName The name of the element
*
* @return A content handler constructor
*/

public static BTElementHandlerConstructorType<?, String> forScalarString(
final BTQualifiedName elementName)
{
return forScalar(elementName, (context, characters, offset, length) -> {
// CHECKSTYLE:OFF
return new String(characters, offset, length);
// CHECKSTYLE:ON
});
}

/**
* A convenience function for constructing content handlers that produce a scalar value from the
* text content of a single XML element.
*
* @param namespaceURI The namespace of the element
* @param localName The local element name
*
* @return A content handler constructor
*/

public static BTElementHandlerConstructorType<?, String> forScalarString(
final String namespaceURI,
final String localName)
{
return forScalar(
namespaceURI,
localName,
(context, characters, offset, length) -> {
// CHECKSTYLE:OFF
return new String(characters, offset, length);
// CHECKSTYLE:ON
});
}

/**
* A convenience function for constructing content handlers that produce a scalar value from the
* text content of a single XML element.
*
* @param elementName The name of the element
* @param parser A function from strings to values of type {@code S}
* @param <S> The type of returned values
*
* @return A content handler constructor
*/

public static <S> BTElementHandlerConstructorType<?, S> forScalarFromString(
final BTQualifiedName elementName,
final Function<String, S> parser)
{
return mapConstructor(forScalarString(elementName), parser);
}

/**
* A convenience function for constructing content handlers that produce a scalar value from the
* text content of a single XML element.
*
* @param namespaceURI The namespace of the element
* @param localName The local element name
* @param parser A function from strings to values of type {@code S}
* @param <S> The type of returned values
*
* @return A content handler constructor
*/

public static <S> BTElementHandlerConstructorType<?, S> forScalarFromString(
final String namespaceURI,
final String localName,
final Function<String, S> parser)
{
return mapConstructor(forScalarString(namespaceURI, localName), parser);
}

/**
* A convenience method to configure and execute a parser.
*
Expand Down
Expand Up @@ -19,7 +19,7 @@
*/

@Export
@Version("1.1.0")
@Version("1.2.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.1.1-SNAPSHOT</version>
<version>1.2.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.1.1-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.blackthorne.tests</artifactId>
Expand Down
Expand Up @@ -1278,4 +1278,123 @@ public void testLeafIntegerAttr0()
Assertions.assertEquals(0, this.errors.size());
Assertions.assertEquals(BigInteger.valueOf(23L), handler.result().get());
}

/**
* Scalar element handlers work.
*
* @throws Exception On errors
*/

@Test
public void testText0()
throws Exception
{
final var handler =
BTContentHandler.<String>builder()
.addHandler(
"urn:tests",
"string",
Blackthorne.forScalarString("urn:tests", "string"))
.build(URI.create("urn:text"), this::logError);

final var reader = createReader();
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
reader.parse(resource("string.xml"));

Assertions.assertFalse(handler.failed());
Assertions.assertEquals(0, this.errors.size());
Assertions.assertEquals("This is some text.", handler.result().get());
}

/**
* Scalar element handlers work.
*
* @throws Exception On errors
*/

@Test
public void testText1()
throws Exception
{
final var name =
BTQualifiedName.of("urn:tests", "string");

final var handler =
BTContentHandler.<String>builder()
.addHandler(name, Blackthorne.forScalarString(name))
.build(URI.create("urn:text"), this::logError);

final var reader = createReader();
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
reader.parse(resource("string.xml"));

Assertions.assertFalse(handler.failed());
Assertions.assertEquals(0, this.errors.size());
Assertions.assertEquals("This is some text.", handler.result().get());
}

/**
* Scalar element handlers work.
*
* @throws Exception On errors
*/

@Test
public void testText2()
throws Exception
{
final var handler =
BTContentHandler.<Exception>builder()
.addHandler(
"urn:tests",
"string",
Blackthorne.forScalarFromString(
"urn:tests",
"string",
Exception::new))
.build(URI.create("urn:text"), this::logError);

final var reader = createReader();
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
reader.parse(resource("string.xml"));

Assertions.assertFalse(handler.failed());
Assertions.assertEquals(0, this.errors.size());
Assertions.assertEquals(
"This is some text.",
handler.result().get().getMessage());
}

/**
* Scalar element handlers work.
*
* @throws Exception On errors
*/

@Test
public void testText3()
throws Exception
{
final var name =
BTQualifiedName.of("urn:tests", "string");

final var handler =
BTContentHandler.<Exception>builder()
.addHandler(name, Blackthorne.forScalarFromString(name, Exception::new))
.build(URI.create("urn:text"), this::logError);

final var reader = createReader();
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
reader.parse(resource("string.xml"));

Assertions.assertFalse(handler.failed());
Assertions.assertEquals(0, this.errors.size());
Assertions.assertEquals(
"This is some text.",
handler.result().get().getMessage());
}
}
Expand Up @@ -5,6 +5,12 @@
attributeFormDefault="qualified"
targetNamespace="urn:tests">

<xsd:element name="string">
<xsd:simpleType>
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:element>

<xsd:element name="int">
<xsd:simpleType>
<xsd:restriction base="xsd:integer"/>
Expand Down
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8" ?>

<ts:string xmlns:ts="urn:tests">This is some text.</ts:string>
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.1.1-SNAPSHOT</version>
<version>1.2.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>1.0.0</io7m.api.previousVersion>
<io7m.api.previousVersion>1.1.0</io7m.api.previousVersion>
</properties>

<licenses>
Expand Down

0 comments on commit 0cc1b8b

Please sign in to comment.