Class SimpleMarkupParser
- java.lang.Object
-
- org.attoparser.simple.SimpleMarkupParser
-
- All Implemented Interfaces:
ISimpleMarkupParser
public final class SimpleMarkupParser extends java.lang.Object implements ISimpleMarkupParser
Default implementation of
ISimpleMarkupParser.AttoParser simple markup parsers work as SAX-style parsers that need a markup handler object for handling parsing events. These handlers implement the
ISimpleMarkupHandlerinterface, and are normally developed by users in order to perform the operations they require for their applications.See the documentation of the
ISimpleMarkupHandlerinterface for more information on the event handler methods.Note that this parser class and its corresponding handlers are actually a simplified version of the full-blown
IMarkupParserinfrastructure.Sample usage:
// Obtain a java.io.Reader on the document to be parsed final Reader documentReader = ...; // Create the handler instance. Extending the no-op AbstractSimpleMarkupHandler is a good start final ISimpleMarkupHandler handler = new AbstractSimpleMarkupHandler() { ... // some events implemented }; // Create or obtain the parser instance (can be reused). Example uses the default configuration for HTML final ISimpleMarkupParser parser = new SimpleMarkupParser(ParseConfiguration.htmlConfiguration()); // Parse it! parser.parse(documentReader, handler);This parser class uses an instance of the
MarkupParserclass underneath (configured with the default values for its buffer pool), and applies to it an instance of theSimplifierMarkupHandlerhandler class in order to make it able to report handling events by means of anISimpleMarkupHandlerimplementation instead of using the defaultIMarkupHandlerinterface.In fact, using the
SimpleMarkupParserclass as shown above is completely equivalent to:// Obtain a java.io.Reader on the document to be parsed final Reader documentReader = ...; // Create the handler instance. Extending the no-op AbstractSimpleMarkupHandler is a good start final ISimpleMarkupHandler simpleHandler = new AbstractSimpleMarkupHandler() { ... // some events implemented }; // Create a handler chain with the 'simplifier' handler, which will convert events from 'normal' to 'simple'. final IMarkupHandler handler = new SimplifierMarkupHandler(simpleHandler); // Create or obtain the parser instance (note this is not the 'simple' one!) final IMarkupParser parser = new MarkupParser(ParseConfiguration.htmlConfiguration()); // Parse it! parser.parse(documentReader, handler);This parser class is thread-safe. However, take into account that, normally,
ISimpleMarkupHandlerimplementations are not. So, even if parsers can be reused, handler objects usually cannot.- Since:
- 2.0.0
-
-
Field Summary
Fields Modifier and Type Field Description private MarkupParsermarkupParser
-
Constructor Summary
Constructors Constructor Description SimpleMarkupParser(ParseConfiguration configuration)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidparse(char[] document, int offset, int len, ISimpleMarkupHandler handler)Parse a document using the specifiedISimpleMarkupHandler.voidparse(char[] document, ISimpleMarkupHandler handler)Parse a document using the specifiedISimpleMarkupHandler.voidparse(java.io.Reader reader, ISimpleMarkupHandler handler)Parse a document using the specifiedISimpleMarkupHandler.voidparse(java.lang.String document, ISimpleMarkupHandler handler)Parse a document using the specifiedISimpleMarkupHandler.
-
-
-
Field Detail
-
markupParser
private final MarkupParser markupParser
-
-
Constructor Detail
-
SimpleMarkupParser
public SimpleMarkupParser(ParseConfiguration configuration)
-
-
Method Detail
-
parse
public void parse(java.lang.String document, ISimpleMarkupHandler handler) throws ParseExceptionDescription copied from interface:ISimpleMarkupParserParse a document using the specified
ISimpleMarkupHandler.- Specified by:
parsein interfaceISimpleMarkupParser- Parameters:
document- the document to be parsed, as a String.handler- the handler to be used, anISimpleMarkupHandlerimplementation.- Throws:
ParseException- if the document cannot be parsed.
-
parse
public void parse(char[] document, ISimpleMarkupHandler handler) throws ParseExceptionDescription copied from interface:ISimpleMarkupParserParse a document using the specified
ISimpleMarkupHandler.- Specified by:
parsein interfaceISimpleMarkupParser- Parameters:
document- the document to be parsed, as a char[].handler- the handler to be used, anISimpleMarkupHandlerimplementation.- Throws:
ParseException- if the document cannot be parsed.
-
parse
public void parse(char[] document, int offset, int len, ISimpleMarkupHandler handler) throws ParseExceptionDescription copied from interface:ISimpleMarkupParserParse a document using the specified
ISimpleMarkupHandler.- Specified by:
parsein interfaceISimpleMarkupParser- Parameters:
document- the document to be parsed, as a char[].offset- the offset to be applied on the char[] document to determine the start of the document contents.len- the length (in chars) of the document stored in the char[].handler- the handler to be used, anISimpleMarkupHandlerimplementation.- Throws:
ParseException- if the document cannot be parsed.
-
parse
public void parse(java.io.Reader reader, ISimpleMarkupHandler handler) throws ParseExceptionDescription copied from interface:ISimpleMarkupParserParse a document using the specified
ISimpleMarkupHandler.Implementations of this interface must close the provided
Readerobject after parsing.- Specified by:
parsein interfaceISimpleMarkupParser- Parameters:
reader- a Reader on the document.handler- the handler to be used, anISimpleMarkupHandlerimplementation.- Throws:
ParseException- if the document cannot be parsed.
-
-