public class SurfParser extends Object
This parser is meant to be used once for parsing a single SURF document. It should not be used to parse multiple documents, as it maintains parsing state.
The parser should be released after use so as not to leak memory of parsed resources when resources are present with tags/IDs/aliases.
This implementation is not thread safe.
| Modifier and Type | Field and Description |
|---|---|
protected static com.globalmentor.java.Characters |
WHITESPACE_EOL_CHARACTERS
Whitespace and end-of-line characters.
|
| Constructor and Description |
|---|
SurfParser() |
| Modifier and Type | Method and Description |
|---|---|
Optional<SurfObject> |
findObjectById(String typeHandle,
String id)
Returns a parsed object by its type handle and ID.
|
Optional<SurfObject> |
findObjectByTag(URI tag)
Returns a parsed object by its tag.
|
Optional<Object> |
findResourceByAlias(String alias)
Returns a parsed resource by its alias.
|
protected SurfObject |
getObjectById(String typeHandle,
String id)
Returns a parsed object by its type handle and ID.
|
Optional<Object> |
parse(InputStream inputStream)
Parses a SURF resource from an input stream.
|
Optional<Object> |
parse(Reader reader)
Parses a SURF resource from a reader.
|
Optional<Object> |
parse(String string)
Parses a SURF resource from a string.
|
static byte[] |
parseBinary(Reader reader)
Parses a binary literal.
|
static Boolean |
parseBoolean(Reader reader)
Parses a Boolean value.
|
static com.globalmentor.java.CodePointCharacter |
parseCharacter(Reader reader)
Parses a character literal.
|
static int |
parseCharacterCodePoint(Reader reader,
char delimiter)
Parses a character as content, without any delimiters.
|
protected SurfObject |
parseDescription(Reader reader,
SurfObject resource)
Parses the description properties of a resource.
|
static com.globalmentor.net.EmailAddress |
parseEmailAddress(Reader reader)
Parses an email address.
|
static String |
parseHandle(Reader reader)
Parses a handle composed of a name token followed by zero or more name tokens, separated by handle segment delimiters.
|
static URI |
parseIRI(Reader reader)
Parses an IRI.
|
static Object |
parseLabel(Reader reader)
Parses a label, surrounded by label delimiters, and which is returned as one of the following:
tag
The tag IRI as an absolute
URI.
ID
Some object that is neither a URI nor a String and which will provide the ID using Object.toString().
alias
An Alias containing the alias string.
|
List<Object> |
parseList(io.urf.surf.SurfParser.Alias alias,
Reader reader)
Parses an a list.
|
Map<Object,Object> |
parseMap(io.urf.surf.SurfParser.Alias alias,
Reader reader)
Parses a map.
|
protected static String |
parseNameToken(Reader reader)
Parses a name token composed of a name token beginning character followed by zero or more name token characters.
|
static Number |
parseNumber(Reader reader)
Parses a number.
|
protected SurfObject |
parseObject(Object label,
Reader reader)
Parses an object indicated by .
|
SurfObject |
parseObject(URI tag,
Reader reader)
Parses an object, a described resource instance indicated by .
|
static Pattern |
parseRegularExpression(Reader reader)
Parses a regular expression surrounded by regular expression delimiters and optionally ending with flags.
|
Object |
parseResource(Reader reader)
Parses a resource; either a tag or a resource representation with an optional description.
|
Object |
parseResource(Reader reader,
boolean allowDescription)
Parses a resource; either a tag or a resource representation.
|
protected static int |
parseSequence(Reader reader,
char sequenceEnd,
com.globalmentor.io.function.IOConsumer<Reader> itemParser)
Parses a general SURF sequence (such as a list).
|
Set<Object> |
parseSet(io.urf.surf.SurfParser.Alias alias,
Reader reader)
Parses a set.
|
static String |
parseString(Reader reader)
Parses a string surrounded by string delimiters.
|
static com.globalmentor.itu.TelephoneNumber |
parseTelephoneNumber(Reader reader)
Parses a telephone number.
|
static TemporalAccessor |
parseTemporal(Reader reader)
Parses a temporal.
|
static UUID |
parseUuid(Reader reader)
Parses a UUID.
|
protected static int |
skipFiller(Reader reader)
Skips over SURF filler in a reader, including whitespace and line comments.The new position will either be the that of the first non-whitespace character
or the end of the input stream.
|
protected static int |
skipLineBreaks(Reader reader)
Skips over SURF line breaks in a reader, including whitespace and line comments.
|
protected static Optional<Boolean> |
skipSequenceDelimiters(Reader reader)
Skips over SURF sequence delimiters in a reader.
|
protected static final com.globalmentor.java.Characters WHITESPACE_EOL_CHARACTERS
public Optional<Object> findResourceByAlias(@Nonnull String alias)
alias - The alias used by the resource in the document.public Optional<SurfObject> findObjectByTag(@Nonnull URI tag)
tag - The global IRI identifier tag of the resource.public Optional<SurfObject> findObjectById(@Nonnull String typeHandle, @Nonnull String id)
typeHandle - The handle of the object's type.id - The object ID for the indicated type.protected SurfObject getObjectById(@Nonnull String typeHandle, @Nonnull String id)
typeHandle - The handle of the object's type.id - The object ID for the indicated type.null if no such object was found.public Optional<Object> parse(@Nonnull String string) throws IOException, com.globalmentor.io.ParseIOException
This is a convenience method that delegates to parse(Reader).
string - The string containing SURF data.IOException - If there was an error reading the SURF data.com.globalmentor.io.ParseIOException - if the SURF data was invalid.public Optional<Object> parse(@Nonnull InputStream inputStream) throws IOException, com.globalmentor.io.ParseIOException
inputStream - The input stream containing SURF data.IOException - If there was an error reading the SURF data.com.globalmentor.io.ParseIOException - if the SURF data was invalid.public Optional<Object> parse(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
reader - The reader containing SURF data.IOException - If there was an error reading the SURF data.com.globalmentor.io.ParseIOException - if the SURF data was invalid.public static Object parseLabel(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
URI.URI nor a String and which will provide the ID using Object.toString().Alias containing the alias string.The current position must be that of the beginning label delimiter character. The new position will be that immediately after the last label delimiter.
reader - The reader the contents of which to be parsed.Alias, an absolute URI for a tag, or String for an ID.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if the label is not valid.SURF.LABEL_DELIMITER,
parseHandle(Reader),
parseIRI(Reader)protected static String parseNameToken(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
reader - The reader the contents of which to be parsed.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if there are are no name characters.SURF.Name.isTokenBeginCharacter(int),
SURF.Name.isTokenCharacter(int)public static String parseHandle(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
reader - The reader the contents of which to be parsed.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if there are are no handle characters.public Object parseResource(@Nonnull Reader reader) throws IOException
reader - The reader containing SURF data.IOException - If there was an error reading the SURF data.com.globalmentor.io.ParseIOException - if the SURF data was invalid.public Object parseResource(@Nonnull Reader reader, boolean allowDescription) throws IOException
reader - The reader containing SURF data.allowDescription - Whether a description is allowed; if false, an following description delimiter will not be considered part of the
resource.IOException - If there was an error reading the SURF data.com.globalmentor.io.ParseIOException - if the SURF data was invalid.public SurfObject parseObject(@Nullable URI tag, @Nonnull Reader reader) throws IOException
tag - The object resource's globally identifying tag, or null if the object has no tag.reader - The reader containing SURF data.IOException - If there was an error reading the SURF data.SURF.OBJECT_BEGINprotected SurfObject parseObject(@Nullable Object label, @Nonnull Reader reader) throws IOException
If the given label indicates an ID, after parsing the object type if an object already exists with the given ID for that type, it will be immediately returned.
label - The object label; either an Alias, a URI representing a tag IRI, or a String representing an ID.reader - The reader containing SURF data.IOException - If there was an error reading the SURF data.SURF.OBJECT_BEGINprotected SurfObject parseDescription(@Nonnull Reader reader, @Nonnull SurfObject resource) throws IOException
reader - The reader containing SURF data.resource - The resource to which properties should be added; SURF only supports object descriptions.IOException - If there was an error reading the SURF data.SURF.DESCRIPTION_BEGINpublic static byte[] parseBinary(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
reader - The reader the contents of which to be parsed.URI containing the IRI parsed from the reader.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if the IRI is not in the correct format.SURF.BINARY_BEGINpublic static Boolean parseBoolean(@Nonnull Reader reader) throws IOException
SURF.BOOLEAN_FALSE_LEXICAL_FORM or
SURF.BOOLEAN_TRUE_LEXICAL_FORM.reader - The reader containing SURF data.Boolean representing the SURF boolean literal read from the reader.IOException - If there was an error reading the SURF data.public static com.globalmentor.java.CodePointCharacter parseCharacter(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
reader - The reader the contents of which to be parsed.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if the character literal is empty, if a control character was present, if the character is not escaped correctly, or the reader
has no more characters before the current character is completely parsed.parseCharacterCodePoint(Reader, char)public static int parseCharacterCodePoint(@Nonnull Reader reader, char delimiter) throws IOException, com.globalmentor.io.ParseIOException
This method always allows the delimiter to be escaped.
reader - The reader the contents of which to be parsed.delimiter - The delimiter that surrounds the character and which should be escaped.-1 if the unescaped delimiter was encountered.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if a control character was represented, if the character is not escaped correctly, or the reader has no more characters before the
current character is completely parsed.public static com.globalmentor.net.EmailAddress parseEmailAddress(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
reader - The reader the contents of which to be parsed.EmailAddress representing the SURF email addresses parsed from the reader.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if the email address is not in the correct format.SURF.EMAIL_ADDRESS_BEGINpublic static URI parseIRI(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
SURF IRI short forms are accepted.
reader - The reader the contents of which to be parsed.URI containing the IRI parsed from the reader.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if the IRI is not in the correct format.SURF.IRI_BEGIN,
SURF.IRI_ENDpublic static Number parseNumber(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
This implementation will return one of the following types:
Integerint.Longint but within the range of long.Doubledouble.BigIntegerBigDecimalreader - The reader the contents of which to be parsed.Number containing the number parsed from the reader.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if the number is not in the correct format, or if the number is outside the range that can be represented by this parser.public static Pattern parseRegularExpression(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
reader - The reader the contents of which to be parsed.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if the regular expressions is not escaped correctly or the reader has no more characters before the current regular expression is
completely parsed.SURF.REGULAR_EXPRESSION_DELIMITER,
SURF.REGULAR_EXPRESSION_ESCAPEpublic static String parseString(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
reader - The reader the contents of which to be parsed.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if a control character was present, if the string is not escaped correctly, or the reader has no more characters before the
current string is completely parsed.SURF.STRING_DELIMITER,
parseCharacterCodePoint(Reader, char)public static com.globalmentor.itu.TelephoneNumber parseTelephoneNumber(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
reader - The reader the contents of which to be parsed.TelephoneNumber representing the SURF telephone number parsed from the reader.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if the telephone number is not in the correct format.SURF.TELEPHONE_NUMBER_BEGINpublic static TemporalAccessor parseTemporal(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
reader - The reader the contents of which to be parsed.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if the temporal is not in the correct format, or if the temporal is outside the range that can be represented by this parser.SURF.TEMPORAL_BEGINpublic static UUID parseUuid(@Nonnull Reader reader) throws IOException, com.globalmentor.io.ParseIOException
reader - The reader the contents of which to be parsed.UUID representing the SURF UUID parsed from the reader.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.com.globalmentor.io.ParseIOException - if the UUID is not in the correct format.SURF.UUID_BEGINpublic List<Object> parseList(@Nullable io.urf.surf.SurfParser.Alias alias, @Nonnull Reader reader) throws IOException
Once the list is created, it will be associated with the given alias if any.
alias - The resource alias, or null if the resource has no alias.reader - The reader containing SURF data.IOException - If there was an error reading the SURF data.public Map<Object,Object> parseMap(@Nullable io.urf.surf.SurfParser.Alias alias, @Nonnull Reader reader) throws IOException
Once the map is created, it will be associated with the given alias if any.
alias - The resource alias, or null if the resource has no alias.reader - The reader containing SURF data.IOException - If there was an error reading the SURF data.public Set<Object> parseSet(@Nullable io.urf.surf.SurfParser.Alias alias, @Nonnull Reader reader) throws IOException
Once the set is created, it will be associated with the given alias if any.
alias - The resource alias, or null if the resource has no alias.reader - The reader containing SURF data.IOException - If there was an error reading the SURF data.protected static int parseSequence(@Nonnull Reader reader, char sequenceEnd, @Nonnull com.globalmentor.io.function.IOConsumer<Reader> itemParser) throws IOException
IOConsumer.accept(Object) is called, passing the Reader, for the item to be parsed.reader - The reader containing the sequence to parse.sequenceEnd - The character expected to end the sequence.itemParser - The parser strategy, which is passed the Reader to use for parsing.Reader.read() operation, or -1 if the end of the reader has been
reached without encountering the end of the sequence.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.protected static Optional<Boolean> skipSequenceDelimiters(@Nonnull Reader reader) throws IOException
reader - The reader the contents of which to be parsed.Boolean.TRUE if a line delimiter was encountered that requires a following item, Boolean.FALSE if a line delimiter was encountered
for which a following item is optional, or Optional.empty() if no line ending was encountered.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.protected static int skipFiller(@Nonnull Reader reader) throws IOException
reader - The reader the contents of which to be parsed.Reader.read() operation, or -1 if the end of the reader has been
reached.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.protected static int skipLineBreaks(@Nonnull Reader reader) throws IOException
reader - The reader the contents of which to be parsed.Reader.read() operation, or -1 if the end of the reader has been
reached.NullPointerException - if the given reader is null.IOException - if there is an error reading from the reader.Copyright © 2007–2017 GlobalMentor, Inc.. All rights reserved.