com.marringtons.xml
Class XMLparser

java.lang.Object
  extended bycom.marringtons.xml.XMLparser
Direct Known Subclasses:
XMLtoMap

public abstract class XMLparser
extends Object

Process a given a stream of XML. This is an abstract class. Concrete examples will need to create methods to be called on start and end elements and for text elements. The start and end methods are passed the current tag name - but they also have all the elements in the tree path in elementStack. Once the process is complete, the return code can be used to see if errors occurred. Errors can be read directly or turned into an exception thrown. You can add errors returned to the thread or system messages list with

Author:
Paul Marrington
See Also:
 XMLprocessor xmlProcessor = new XMLprocessor( "sampleXML");
 boolean ok = xmlProcessor.parse( sampleXML));
 Messages.getSystemInstance().add( xmlProcessor.errors());
 
 xmlProcessor.parse( new FileReader( "test.xml"));
 if (xmlProcessor.hasErrors())
   System.out.println( "XML Errors: "+errors());
 
 ///////////////////////////
 private static class XMLprocessor extends XMLparser
   {
     public void startElement(String tagName, HashMap attributes)
       {
         if (elementStack.peek().equals( "IgnoreMe")) return;
         if (elementStack.peek().equals( "Unexpected"))
           reportProblem( Format.pattern( "unexpected tag '{0}'")
             .with( elementStack.peek());
         ...
       }
     public void endElement(String tagName)
       { ... }
     public void text(String str)
       { ... }
   }
 

Field Summary
 StringStack elementStack
          elementStack lists the element path to the current point.
 int lineNumber
          lineNumber records the current line being processed (for errors mainly)
 Messages problems
          List of problems that arise during XML parsing.
 
Constructor Summary
XMLparser()
          Default constructor - does nothing but wait.
XMLparser(Reader reader)
          Create a parser and immediately parse a source.
XMLparser(String key)
          Default constructor - does nothing but wait.
XMLparser(String key, Reader reader)
          Create a parser and immediately parse a source.
 
Method Summary
 Messages parse(BufferedReader reader)
          Walk through an XML input stream calling processor methods on tags start, content and end.
 Messages parse(Reader reader)
          Buffer the reader and walk through an XML input stream calling processor methods on tag start, content and end.
 Messages parse(String xml)
          Walk through an XML input stream calling processor methods on tag start, content and end.
 void reportProblem(Object message)
          Messages recorded during the process of parsing the XML.
 void reportProblem(Throwable throwable)
          Report an problem caused by an exception.
 void reportProblem(Throwable throwable, Object message)
          Report an problem caused by an exception.
 void throwException()
          Normally returns OK and uses hasErrors() to indicate problems.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

problems

public Messages problems
List of problems that arise during XML parsing. Add to in when you want to record problems in subclasses.


elementStack

public StringStack elementStack
elementStack lists the element path to the current point.


lineNumber

public int lineNumber
lineNumber records the current line being processed (for errors mainly)

Constructor Detail

XMLparser

public XMLparser()
Default constructor - does nothing but wait.


XMLparser

public XMLparser(String key)
Default constructor - does nothing but wait.

Parameters:
key - to record problems under. Should be unique - include file name if there is one.

XMLparser

public XMLparser(Reader reader)
Create a parser and immediately parse a source.

Parameters:
reader - for a steam of XML text.

XMLparser

public XMLparser(String key,
                 Reader reader)
Create a parser and immediately parse a source.

Parameters:
key - to record problems under. Should be unique - include file name if there is one.
reader - for a steam of XML text.
Method Detail

parse

public Messages parse(String xml)
Walk through an XML input stream calling processor methods on tag start, content and end.

Parameters:
xml - XML in a string - only first outer element parsed
Returns:
list of problems encountered - problems.none() if none.

parse

public Messages parse(Reader reader)
Buffer the reader and walk through an XML input stream calling processor methods on tag start, content and end.

Parameters:
reader - souce for XML to parse
Returns:
list of problems encountered - problems.none() if none.

reportProblem

public void reportProblem(Object message)
Messages recorded during the process of parsing the XML. As a side-effect it sets the problems key to the current line number.
 problems().add( "My problem with {0}", tagName);
 

Parameters:
message - Message to add to the list of problems encountered.

reportProblem

public void reportProblem(Throwable throwable)
Report an problem caused by an exception. Dumps stack trace to log as well as reporting message.

Parameters:
throwable - exception to report

reportProblem

public void reportProblem(Throwable throwable,
                          Object message)
Report an problem caused by an exception. Dumps stack trace to log as well as reporting message.

Parameters:
throwable - exception to report
message - to show as title of problem.

parse

public Messages parse(BufferedReader reader)
Walk through an XML input stream calling processor methods on tags start, content and end.

Parameters:
reader - souce for XML to parse
Returns:
list of problems encountered - problems.none() if none.

throwException

public void throwException()
                    throws XMLexception
Normally returns OK and uses hasErrors() to indicate problems. In environments where an exception is preferable, us this method after parsing.
 if (! xmlParser.parse())
   xmlParser.exeption();
 

Throws:
XMLexception - wrapping text errors found during parsing.


Copyright © 2005 Paul Marrington http://library.marringtons.com