Skip to content

Commit

Permalink
Ensure null hostility
Browse files Browse the repository at this point in the history
If an empty element was presented to a scalar element handler, the
element handler's onCharacters method was never called. This allowed
scalar element handlers to return `null`. Now, if the handler has
not been called for an element, it will be called explicitly with an
empty string before returning.

Fix: #4
  • Loading branch information
io7m committed Feb 6, 2021
1 parent 4863b02 commit 2c73f9e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 8 deletions.
11 changes: 10 additions & 1 deletion README-CHANGES.xml
Expand Up @@ -13,7 +13,7 @@
</c:change>
</c:changes>
</c:release>
<c:release date="2021-01-24T13:01:32+00:00" is-open="false" ticket-system="com.github.io7m.blackthorne" version="1.2.0">
<c:release date="2021-01-24T00:00:00+00:00" is-open="false" ticket-system="com.github.io7m.blackthorne" version="1.2.0">
<c:changes>
<c:change date="2021-01-24T00:00:00+00:00" summary="Add logging for content handlers">
<c:tickets>
Expand All @@ -27,6 +27,15 @@
</c:change>
</c:changes>
</c:release>
<c:release date="2021-02-06T10:37:44+00:00" is-open="true" ticket-system="com.github.io7m.blackthorne" version="1.2.1">
<c:changes>
<c:change date="2021-02-06T10:37:44+00:00" summary="Ensure that scalar element handlers never return null for empty elements">
<c:tickets>
<c:ticket id="4"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
</c:releases>
<c:ticket-systems>
<c:ticket-system default="true" id="com.github.io7m.blackthorne" url="https://www.github.com/io7m/blackthorne/issues/"/>
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.3.0-SNAPSHOT</version>
<version>1.2.1-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.blackthorne.api</artifactId>
Expand Down
Expand Up @@ -27,6 +27,7 @@

public final class BTScalarElementHandler<S> implements BTElementHandlerType<Object, S>
{
private static final char[] CHARACTERS = new char[0];
private final BTCharacterHandlerType<S> handler;
private final BTQualifiedName name;
private S result;
Expand All @@ -42,8 +43,10 @@ public BTScalarElementHandler(
final BTQualifiedName inName,
final BTCharacterHandlerType<S> inHandler)
{
this.name = Objects.requireNonNull(inName, "name");
this.handler = Objects.requireNonNull(inHandler, "handler");
this.name =
Objects.requireNonNull(inName, "name");
this.handler =
Objects.requireNonNull(inHandler, "handler");
}

@Override
Expand All @@ -60,13 +63,23 @@ public void onCharacters(
final int length)
throws Exception
{
this.result = this.handler.parse(context, data, offset, length);
final var parsed =
this.handler.parse(context, data, offset, length);
this.result =
Objects.requireNonNull(parsed, "parsed");
}

@Override
public S onElementFinished(
final BTElementParsingContextType context)
throws Exception
{
if (this.result == null) {
final var parsed =
this.handler.parse(context, CHARACTERS, 0, 0);
this.result =
Objects.requireNonNull(parsed, "parsed");
}
return this.result;
}
}
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.3.0-SNAPSHOT</version>
<version>1.2.1-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.3.0-SNAPSHOT</version>
<version>1.2.1-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.blackthorne.tests</artifactId>
Expand Down
Expand Up @@ -1397,4 +1397,34 @@ public void testText3()
"This is some text.",
handler.result().get().getMessage());
}

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

@Test
public void testText4()
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("stringEmpty.xml"));

Assertions.assertFalse(handler.failed());
Assertions.assertEquals(0, this.errors.size());
Assertions.assertEquals(
"",
handler.result().get().getMessage());
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8" ?>

<ts:string xmlns:ts="urn:tests"/>
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -13,7 +13,7 @@

<groupId>com.io7m.blackthorne</groupId>
<artifactId>com.io7m.blackthorne</artifactId>
<version>1.3.0-SNAPSHOT</version>
<version>1.2.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>com.io7m.blackthorne</name>
Expand Down

0 comments on commit 2c73f9e

Please sign in to comment.