Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a convenient handler class for elements that consist only of attributes. Affects: #2
- Loading branch information
Showing
8 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
com.io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/BTLeafElementHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...io7m.blackthorne.api/src/main/java/com/io7m/blackthorne/api/BTLeafElementHandlerType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters