Skip to content

Commit

Permalink
Handlers may throw raw exceptions
Browse files Browse the repository at this point in the history
This adjusts the API to allow handlers to throw raw Exception
values. The Exception values will be caught and a useful parse error
will result (with the correct line/column numbers).

This also removes the recent leaf element handler as similar
functionality already existed.

Affects: #2
  • Loading branch information
io7m committed Oct 24, 2020
1 parent 0ca01a2 commit 43a61da
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 197 deletions.
Expand Up @@ -18,7 +18,6 @@
package com.io7m.blackthorne.api;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
* A function that, given a set of attributes, returns a {@code T}.
Expand All @@ -36,11 +35,11 @@ public interface BTAttributesHandlerType<T>
*
* @return A value of {@code T}
*
* @throws SAXException On errors
* @throws Exception On errors
*/

T parse(
BTElementParsingContextType context,
Attributes attributes)
throws SAXException;
throws Exception;
}
Expand Up @@ -17,8 +17,6 @@

package com.io7m.blackthorne.api;

import org.xml.sax.SAXException;

/**
* A function that, given a string, returns a {@code T}.
*
Expand All @@ -37,13 +35,13 @@ public interface BTCharacterHandlerType<T>
*
* @return A value of {@code T}
*
* @throws SAXException On errors
* @throws Exception On errors
*/

T parse(
BTElementParsingContextType context,
char[] characters,
int offset,
int length)
throws SAXException;
throws Exception;
}
Expand Up @@ -17,19 +17,20 @@
package com.io7m.blackthorne.api;

import com.io7m.jlexing.core.LexicalPosition;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ext.DefaultHandler2;
import org.xml.sax.ext.Locator2;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;

/**
* A dispatching handler that produces values of type {@code T}. The handler is responsible for
* instantiating a content handler based on the received document namespace URI.
Expand Down Expand Up @@ -95,6 +96,18 @@ public static <U> BTContentHandlerBuilderType<U> builder(
return new Builder<>();
}

private static String messageOrException(
final Exception e)
{
final var messageOrNull = e.getMessage();
if (messageOrNull == null) {
return String.format(
"No error message provided for exception %s",
e.getClass().getName());
}
return messageOrNull;
}

@Override
public void setDocumentLocator(
final Locator in_locator)
Expand All @@ -109,12 +122,13 @@ public void startElement(
final String localName,
final String qualifiedName,
final Attributes attributes)
throws SAXException
{
try {
this.stackHandler.onElementStarted(namespaceURI, localName, attributes);
} catch (final SAXParseException e) {
this.error(e);
} catch (final Exception e) {
this.error(this.saxParseExceptionOf(e));
}
}

Expand All @@ -123,12 +137,13 @@ public void endElement(
final String namespaceURI,
final String localName,
final String qualifiedName)
throws SAXException
{
try {
this.stackHandler.onElementFinished(namespaceURI, localName);
} catch (final SAXParseException e) {
this.error(e);
} catch (final Exception e) {
this.error(this.saxParseExceptionOf(e));
}
}

Expand All @@ -137,12 +152,13 @@ public void characters(
final char[] ch,
final int start,
final int length)
throws SAXException
{
try {
this.stackHandler.onCharacters(ch, start, length);
} catch (final SAXParseException e) {
this.error(e);
} catch (final Exception e) {
this.error(this.saxParseExceptionOf(e));
}
}

Expand Down Expand Up @@ -189,18 +205,6 @@ public void fatalError(
throw e;
}

private static String messageOrException(
final Exception e)
{
final var messageOrNull = e.getMessage();
if (messageOrNull == null) {
return String.format(
"No error message provided for exception %s",
e.getClass().getName());
}
return messageOrNull;
}

private LexicalPosition<URI> currentLexical()
{
final LexicalPosition<URI> lexicalPosition;
Expand All @@ -223,6 +227,12 @@ private LexicalPosition<URI> currentLexical()
return lexicalPosition;
}

private SAXParseException saxParseExceptionOf(
final Exception e)
{
return new SAXParseException(e.getLocalizedMessage(), this.locator, e);
}

/**
* @return The parsed value
*/
Expand Down
Expand Up @@ -55,7 +55,9 @@ default BTContentHandlerBuilderType<T> addHandler(
final String localName,
final BTElementHandlerConstructorType<?, T> constructor)
{
return this.addHandler(BTQualifiedName.of(namespaceURI, localName), constructor);
return this.addHandler(
BTQualifiedName.of(namespaceURI, localName),
constructor);
}

/**
Expand Down
Expand Up @@ -16,8 +16,6 @@

package com.io7m.blackthorne.api;

import org.xml.sax.SAXException;

/**
* A content handler constructor that produces handlers that produce values of type {@code A}
*
Expand All @@ -34,11 +32,11 @@ public interface BTElementHandlerConstructorType<CT, RT>
*
* @return A new content handler
*
* @throws SAXException If required
* @throws Exception If required
* @see BTElementParsingContextType#parseException(Exception)
*/

BTElementHandlerType<? extends CT, ? extends RT> create(
BTElementParsingContextType context)
throws SAXException;
throws Exception;
}
Expand Up @@ -18,7 +18,6 @@
package com.io7m.blackthorne.api;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -82,13 +81,13 @@ default BTIgnoreUnrecognizedElements onShouldIgnoreUnrecognizedElements(
* @param context The parsing context
* @param attributes The element's attributes
*
* @throws SAXException On parse errors
* @throws Exception On parse errors
*/

default void onElementStart(
final BTElementParsingContextType context,
final Attributes attributes)
throws SAXException
throws Exception
{

}
Expand All @@ -101,25 +100,25 @@ default void onElementStart(
*
* @return The value produced by all of the data received up to this point
*
* @throws SAXException On parse errors
* @throws Exception On parse errors
*/

RT onElementFinished(BTElementParsingContextType context)
throws SAXException;
throws Exception;

/**
* A child element has been parsed successfully and produced a result.
*
* @param context The parsing context
* @param result The value produced by the child element
*
* @throws SAXException On parse errors
* @throws Exception On parse errors
*/

default void onChildValueProduced(
final BTElementParsingContextType context,
final CT result)
throws SAXException
throws Exception
{

}
Expand All @@ -132,15 +131,15 @@ default void onChildValueProduced(
* @param offset The start of the data within {@code data}
* @param length The length of the data within {@code data}
*
* @throws SAXException On parse errors
* @throws Exception On parse errors
*/

default void onCharacters(
final BTElementParsingContextType context,
final char[] data,
final int offset,
final int length)
throws SAXException
throws Exception
{

}
Expand Down
Expand Up @@ -27,15 +27,6 @@ public final class BTException extends Exception
{
private final List<BTParseError> errors;

/**
* @return The parse errors encountered during parsing
*/

public List<BTParseError> errors()
{
return this.errors;
}

/**
* Construct an exception.
*
Expand Down Expand Up @@ -84,4 +75,13 @@ public BTException(
super(Objects.requireNonNull(inCause, "cause"));
this.errors = Objects.requireNonNull(inErrors, "errors");
}

/**
* @return The parse errors encountered during parsing
*/

public List<BTParseError> errors()
{
return this.errors;
}
}
Expand Up @@ -18,7 +18,6 @@
package com.io7m.blackthorne.api;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -76,7 +75,7 @@ public BTIgnoreUnrecognizedElements onShouldIgnoreUnrecognizedElements(
public void onElementStart(
final BTElementParsingContextType context,
final Attributes attributes)
throws SAXException
throws Exception
{
this.handler.onElementStart(context, attributes);
}
Expand All @@ -85,7 +84,7 @@ public void onElementStart(
public void onChildValueProduced(
final BTElementParsingContextType context,
final A result)
throws SAXException
throws Exception
{
this.handler.onChildValueProduced(context, result);
}
Expand All @@ -96,14 +95,14 @@ public void onCharacters(
final char[] data,
final int offset,
final int length)
throws SAXException
throws Exception
{
this.handler.onCharacters(context, data, offset, length);
}

@Override
public C onElementFinished(final BTElementParsingContextType context)
throws SAXException
throws Exception
{
return this.function.apply(this.handler.onElementFinished(context));
}
Expand Down

0 comments on commit 43a61da

Please sign in to comment.