|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.marringtons.xml.XMLparser
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
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 |
public Messages problems
public StringStack elementStack
elementStack lists the element path to the current point.
public int lineNumber
lineNumber records the current line being processed (for errors mainly)
| Constructor Detail |
public XMLparser()
public XMLparser(String key)
key - to record problems under. Should be unique - include file name
if there is one.public XMLparser(Reader reader)
reader - for a steam of XML text.
public XMLparser(String key,
Reader reader)
key - to record problems under. Should be unique - include file name
if there is one.reader - for a steam of XML text.| Method Detail |
public Messages parse(String xml)
xml - XML in a string - only first outer element parsed
public Messages parse(Reader reader)
reader - souce for XML to parse
public void reportProblem(Object message)
problems().add( "My problem with {0}", tagName);
message - Message to add to the list of problems encountered.public void reportProblem(Throwable throwable)
throwable - exception to report
public void reportProblem(Throwable throwable,
Object message)
throwable - exception to reportmessage - to show as title of problem.public Messages parse(BufferedReader reader)
reader - souce for XML to parse
public void throwException()
throws XMLexception
if (! xmlParser.parse()) xmlParser.exeption();
XMLexception - wrapping text errors found during parsing.
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||