net.sf.jmatchparser.template
Class Parser

java.lang.Object
  extended by net.sf.jmatchparser.template.Parser

public class Parser
extends Object

A parser for MatchTemplates. Each parser can be used for parsing exactly one file/input stream; the templates can be reused for parsing more than one file.


Constructor Summary
Parser(Class<?> clazz, String resourceName, String encoding)
          Create a new parser using a new MatchTemplate.
Parser(File file, String encoding)
          Create a new parser using a new MatchTemplate.
Parser(MatchTemplate.MatchTemplateResolver resolver, String templateName, BufferedReader br)
          Create a new parser using a new MatchTemplate.
Parser(MatchTemplate template)
          Create a new parser using an existing MatchTemplate.
Parser(String filename, String encoding)
          Create a new parser using a new MatchTemplate.
 
Method Summary
 void addCallback(String name, CallbackFunction function)
          Add a callback function to this parser.
static void convertDOMToCSV(Document doc, AbstractCSVWriter[] writers)
          Convert a DOM Document to one or more CSV files.
 void parse(BufferedReader reader, Element target)
          Parse a buffered reader and add the resulting XML to a DOM Element.
 Document parse(BufferedReader reader, String toplevelTag)
          Parse a buffered reader and create a new DOM Document from the resulting XML.
<T> T
parse(BufferedReader reader, Unmarshaller unmarshaller, Class<T> clazz)
          Parse a buffered reader using a JAXB Unmarshaller for parsing the resulting XML.
 void parse(CharSequence file, Element target)
          Parse a String or other char sequence and add the resulting XML to a DOM Element.
 Document parse(CharSequence file, String toplevelTag)
          Parse a String or other char sequence and create a new DOM Document from the resulting XML.
<T> T
parse(CharSequence file, Unmarshaller unmarshaller, Class<T> clazz)
          Parse a String or other char sequence using a JAXB Unmarshaller for parsing the resulting XML.
 void parse(InputStream stream, String encoding, Element target)
          Parse an input stream and add the resulting XML to a DOM Element.
 Document parse(InputStream stream, String encoding, String toplevelTag)
          Parse an input stream and create a new DOM Document from the resulting XML.
<T> T
parse(InputStream stream, String encoding, Unmarshaller unmarshaller, Class<T> clazz)
          Parse an input stream using a JAXB Unmarshaller for parsing the resulting XML.
 void parseToCSV(BufferedReader reader, AbstractCSVWriter writer)
          Parse a buffered reader to a CSV file.
 void parseToCSV(CharSequence file, AbstractCSVWriter writer)
          Parse a String or other char sequence to a CSV file.
 void parseToCSV(InputStream stream, String encoding, AbstractCSVWriter writer)
          Parse an input stream to a CSV file.
 void setDebugStream(PrintStream debugStream)
          Set the debug stream where DEBUGWRITE and DUMPLINE commands write to.
 void setLocal(String name, String value)
          Set a local variable in this parser.
 void setParserName(String parserName)
          Set the name of this parser.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Parser

public Parser(String filename,
              String encoding)
       throws IOException
Create a new parser using a new MatchTemplate.

Parameters:
filename - Name of the file
encoding - File encoding
Throws:
IOException
See Also:
MatchTemplate.MatchTemplate(String, String, Formatter...)

Parser

public Parser(File file,
              String encoding)
       throws IOException
Create a new parser using a new MatchTemplate.

Parameters:
file - The file
encoding - File encoding
Throws:
IOException
See Also:
MatchTemplate.MatchTemplate(File, String, Formatter...)

Parser

public Parser(Class<?> clazz,
              String resourceName,
              String encoding)
       throws IOException
Create a new parser using a new MatchTemplate.

Parameters:
clazz - Class to use as a base for finding the resource
resourceName - Name of the resource
encoding - File encoding
Throws:
IOException
See Also:
MatchTemplate.MatchTemplate(Class, String, String, Formatter...)

Parser

public Parser(MatchTemplate.MatchTemplateResolver resolver,
              String templateName,
              BufferedReader br)
       throws IOException
Create a new parser using a new MatchTemplate.

Parameters:
resolver - Match template resolver for resolving additional template names ($INCLUDE), optional
templateName - Name of the template
br - Reader to read the template from
Throws:
IOException
See Also:
MatchTemplate.MatchTemplate(MatchTemplate.MatchTemplateResolver, String, BufferedReader, Formatter...)

Parser

public Parser(MatchTemplate template)
Create a new parser using an existing MatchTemplate.

Parameters:
template - The match template to use
Method Detail

setLocal

public void setLocal(String name,
                     String value)
Set a local variable in this parser. This method can only be called before parsing started.

Parameters:
name - Name of the variable
value - Value of the variable

setParserName

public void setParserName(String parserName)
Set the name of this parser. This name is used in error messages and can be used to distinguish multiple parsers easily.


parse

public <T> T parse(InputStream stream,
                   String encoding,
                   Unmarshaller unmarshaller,
                   Class<T> clazz)
        throws ParserException,
               ParserConfigurationException,
               IOException,
               JAXBException
Parse an input stream using a JAXB Unmarshaller for parsing the resulting XML.

Type Parameters:
T - Type of the unmarshalled object
Parameters:
stream - Input stream to parse
encoding - Encoding of the stream
unmarshaller - Unmarshaller to use
clazz - Class of the unmarshalled object
Returns:
Unmarshalled object
Throws:
ParserException
ParserConfigurationException
IOException
JAXBException

parse

public Document parse(InputStream stream,
                      String encoding,
                      String toplevelTag)
               throws ParserException,
                      ParserConfigurationException,
                      IOException
Parse an input stream and create a new DOM Document from the resulting XML.

Parameters:
stream - Input stream to parse
encoding - Encoding of the stream
toplevelTag - Tag of the document element to use in the new document
Returns:
Document
Throws:
ParserException
ParserConfigurationException
IOException

parse

public void parse(InputStream stream,
                  String encoding,
                  Element target)
           throws ParserException,
                  IOException
Parse an input stream and add the resulting XML to a DOM Element.

Parameters:
stream - Input stream to parse
encoding - Encoding of the stream
target - Element to add the XML to
Throws:
ParserException
IOException

parse

public <T> T parse(BufferedReader reader,
                   Unmarshaller unmarshaller,
                   Class<T> clazz)
        throws ParserException,
               ParserConfigurationException,
               IOException,
               JAXBException
Parse a buffered reader using a JAXB Unmarshaller for parsing the resulting XML.

Type Parameters:
T - Type of the unmarshalled object
Parameters:
reader - BufferedReader to parse
unmarshaller - Unmarshaller to use
clazz - Class of the unmarshalled object
Returns:
Unmarshalled object
Throws:
ParserException
ParserConfigurationException
IOException
JAXBException

parse

public Document parse(BufferedReader reader,
                      String toplevelTag)
               throws ParserException,
                      ParserConfigurationException,
                      IOException
Parse a buffered reader and create a new DOM Document from the resulting XML.

Parameters:
reader - BufferedReader to parse
toplevelTag - Tag of the document element to use in the new document
Returns:
Document
Throws:
ParserException
ParserConfigurationException
IOException

parse

public void parse(BufferedReader reader,
                  Element target)
           throws ParserException,
                  IOException
Parse a buffered reader and add the resulting XML to a DOM Element.

Parameters:
reader - BufferedReader to parse
target - Element to add the XML to
Throws:
ParserException
IOException

parse

public <T> T parse(CharSequence file,
                   Unmarshaller unmarshaller,
                   Class<T> clazz)
        throws ParserException,
               ParserConfigurationException,
               IOException,
               JAXBException
Parse a String or other char sequence using a JAXB Unmarshaller for parsing the resulting XML.

Type Parameters:
T - Type of the unmarshalled object
Parameters:
file - CharSequence to parse
unmarshaller - Unmarshaller to use
clazz - Class of the unmarshalled object
Returns:
Unmarshalled object
Throws:
ParserException
ParserConfigurationException
IOException
JAXBException

parse

public Document parse(CharSequence file,
                      String toplevelTag)
               throws ParserException,
                      ParserConfigurationException,
                      IOException
Parse a String or other char sequence and create a new DOM Document from the resulting XML.

Parameters:
file - CharSequence to parse
toplevelTag - Tag of the document element to use in the new document
Returns:
Document
Throws:
ParserException
ParserConfigurationException
IOException

parse

public void parse(CharSequence file,
                  Element target)
           throws ParserException,
                  IOException
Parse a String or other char sequence and add the resulting XML to a DOM Element.

Parameters:
file - CharSequence to parse
target - Element to add the XML to
Throws:
ParserException
IOException

parseToCSV

public void parseToCSV(InputStream stream,
                       String encoding,
                       AbstractCSVWriter writer)
                throws ParserConfigurationException,
                       ParserException,
                       IOException
Parse an input stream to a CSV file.

Parameters:
stream - Input stream to parse
encoding - Encoding of the stream
writer - AbstractCSVWriter to write the file to
Throws:
ParserConfigurationException
ParserException
IOException

parseToCSV

public void parseToCSV(BufferedReader reader,
                       AbstractCSVWriter writer)
                throws ParserConfigurationException,
                       ParserException,
                       IOException
Parse a buffered reader to a CSV file.

Parameters:
reader - BufferedReader to parse
writer - AbstractCSVWriter to write the file to
Throws:
ParserConfigurationException
ParserException
IOException

parseToCSV

public void parseToCSV(CharSequence file,
                       AbstractCSVWriter writer)
                throws ParserConfigurationException,
                       ParserException,
                       IOException
Parse a String or other char sequence to a CSV file.

Parameters:
file - CharSequence to parse
writer - AbstractCSVWriter to write the file to
Throws:
ParserConfigurationException
ParserException
IOException

convertDOMToCSV

public static void convertDOMToCSV(Document doc,
                                   AbstractCSVWriter[] writers)
                            throws IOException
Convert a DOM Document to one or more CSV files. Each child element of the document element is a row, each child element of a row is a cell. Rows may have an attribute @file which contains the zero-based index of the writer to save this row to (zero by default); all other attributes are ignored.

Parameters:
doc - Document to parse
writers - AbstractCSVWriter(s) to write the file(s) to
Throws:
IOException

addCallback

public void addCallback(String name,
                        CallbackFunction function)
Add a callback function to this parser.

Parameters:
name - Name of the function
function - Function to add

setDebugStream

public void setDebugStream(PrintStream debugStream)
Set the debug stream where DEBUGWRITE and DUMPLINE commands write to.

Parameters:
debugStream - New debug stream to use


Copyright © 2011. All Rights Reserved.