de.matthiasmann.twl.utils
Class XMLParser

java.lang.Object
  extended by de.matthiasmann.twl.utils.XMLParser
All Implemented Interfaces:
java.io.Closeable

public class XMLParser
extends java.lang.Object
implements java.io.Closeable

A helper class to make parsing of XML files easier It will also warn if a XML tag contains attributes which are not consumed.


Constructor Summary
XMLParser(java.net.URL url)
          Creates a XMLParser for the given URL.
XMLParser(org.xmlpull.v1.XmlPullParser xpp, java.lang.String source)
           
 
Method Summary
 void close()
           
static org.xmlpull.v1.XmlPullParser createParser()
           
 org.xmlpull.v1.XmlPullParserException error(java.lang.String msg)
           
 org.xmlpull.v1.XmlPullParserException error(java.lang.String msg, java.lang.Throwable cause)
           
 int getAttributeCount()
           
 java.lang.String getAttributeName(int index)
           
 java.lang.String getAttributeNamespace(int index)
           
 java.lang.String getAttributeNotNull(java.lang.String attribute)
           
 java.lang.String getAttributeValue(int index)
           
 java.lang.String getAttributeValue(java.lang.String namespace, java.lang.String name)
           
 int getColumnNumber()
           
 java.lang.String getFilePosition()
           
 int getLineNumber()
           
protected  java.util.logging.Logger getLogger()
           
 java.lang.String getName()
           
 java.lang.String getPositionDescription()
           
 java.util.Map<java.lang.String,java.lang.String> getUnusedAttributes()
           
protected  void handleType(int type)
           
 void ignoreOtherAttributes()
           
 boolean isAttributeUnused(int idx)
           
 boolean isEndTag()
           
 boolean isStartTag()
           
protected  void missingAttribute(java.lang.String attribute)
           
 int next()
           
 int nextTag()
           
 java.lang.String nextText()
           
 char[] nextText(int[] startAndLength)
           
 boolean parseBool(java.lang.String value)
           
 boolean parseBoolFromAttribute(java.lang.String attribName)
           
 boolean parseBoolFromAttribute(java.lang.String attribName, boolean defaultValue)
           
 boolean parseBoolFromText()
           
protected
<E extends java.lang.Enum<E>>
E
parseEnum(java.lang.Class<E> enumClazz, java.lang.String value)
           
<E extends java.lang.Enum<E>>
E
parseEnumFromAttribute(java.lang.String attribName, java.lang.Class<E> enumClazz)
           
<E extends java.lang.Enum<E>>
E
parseEnumFromAttribute(java.lang.String attribName, java.lang.Class<E> enumClazz, E defaultValue)
           
<E extends java.lang.Enum<E>>
E
parseEnumFromText(java.lang.Class<E> enumClazz)
           
protected  float parseFloat(java.lang.String value)
           
 float parseFloatFromAttribute(java.lang.String attribName)
           
 float parseFloatFromAttribute(java.lang.String attribName, float defaultValue)
           
protected  int parseInt(java.lang.String value)
           
 int parseIntFromAttribute(java.lang.String attribName)
           
 int parseIntFromAttribute(java.lang.String attribName, int defaultValue)
           
 void require(int type, java.lang.String namespace, java.lang.String name)
           
 void setLoggerName(java.lang.String loggerName)
           
 void skipText()
           
 org.xmlpull.v1.XmlPullParserException unexpected()
           
protected  void warnUnusedAttributes()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLParser

public XMLParser(org.xmlpull.v1.XmlPullParser xpp,
                 java.lang.String source)

XMLParser

public XMLParser(java.net.URL url)
          throws org.xmlpull.v1.XmlPullParserException,
                 java.io.IOException
Creates a XMLParser for the given URL. This method also calls URL.getContent which allows a custom URLStreamHandler to return a class implementing XmlPullParser.

Parameters:
url - the URL to parse
Throws:
org.xmlpull.v1.XmlPullParserException - if the resource is not a valid XML file
java.io.IOException - if the resource could not be read
See Also:
URLStreamHandler, URL.getContent(java.lang.Class[]), XmlPullParser
Method Detail

createParser

public static org.xmlpull.v1.XmlPullParser createParser()
                                                 throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Throws:
java.io.IOException

setLoggerName

public void setLoggerName(java.lang.String loggerName)

next

public int next()
         throws org.xmlpull.v1.XmlPullParserException,
                java.io.IOException
Throws:
org.xmlpull.v1.XmlPullParserException
java.io.IOException
See Also:
XmlPullParser.next()

nextTag

public int nextTag()
            throws org.xmlpull.v1.XmlPullParserException,
                   java.io.IOException
Throws:
org.xmlpull.v1.XmlPullParserException
java.io.IOException
See Also:
XmlPullParser.nextTag()

nextText

public java.lang.String nextText()
                          throws org.xmlpull.v1.XmlPullParserException,
                                 java.io.IOException
Throws:
org.xmlpull.v1.XmlPullParserException
java.io.IOException
See Also:
XmlPullParser.nextText()

nextText

public char[] nextText(int[] startAndLength)
                throws org.xmlpull.v1.XmlPullParserException,
                       java.io.IOException
Throws:
org.xmlpull.v1.XmlPullParserException
java.io.IOException

skipText

public void skipText()
              throws org.xmlpull.v1.XmlPullParserException,
                     java.io.IOException
Throws:
org.xmlpull.v1.XmlPullParserException
java.io.IOException

isStartTag

public boolean isStartTag()
                   throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

isEndTag

public boolean isEndTag()
                 throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

getPositionDescription

public java.lang.String getPositionDescription()

getLineNumber

public int getLineNumber()

getColumnNumber

public int getColumnNumber()

getFilePosition

public java.lang.String getFilePosition()

getName

public java.lang.String getName()

require

public void require(int type,
                    java.lang.String namespace,
                    java.lang.String name)
             throws org.xmlpull.v1.XmlPullParserException,
                    java.io.IOException
Throws:
org.xmlpull.v1.XmlPullParserException
java.io.IOException
See Also:
XmlPullParser.require(int, java.lang.String, java.lang.String)

getAttributeValue

public java.lang.String getAttributeValue(int index)

getAttributeNamespace

public java.lang.String getAttributeNamespace(int index)

getAttributeName

public java.lang.String getAttributeName(int index)

getAttributeCount

public int getAttributeCount()

getAttributeValue

public java.lang.String getAttributeValue(java.lang.String namespace,
                                          java.lang.String name)

getAttributeNotNull

public java.lang.String getAttributeNotNull(java.lang.String attribute)
                                     throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseBoolFromAttribute

public boolean parseBoolFromAttribute(java.lang.String attribName)
                               throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseBoolFromText

public boolean parseBoolFromText()
                          throws org.xmlpull.v1.XmlPullParserException,
                                 java.io.IOException
Throws:
org.xmlpull.v1.XmlPullParserException
java.io.IOException

parseBoolFromAttribute

public boolean parseBoolFromAttribute(java.lang.String attribName,
                                      boolean defaultValue)
                               throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseIntFromAttribute

public int parseIntFromAttribute(java.lang.String attribName)
                          throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseIntFromAttribute

public int parseIntFromAttribute(java.lang.String attribName,
                                 int defaultValue)
                          throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseFloatFromAttribute

public float parseFloatFromAttribute(java.lang.String attribName)
                              throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseFloatFromAttribute

public float parseFloatFromAttribute(java.lang.String attribName,
                                     float defaultValue)
                              throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseEnumFromAttribute

public <E extends java.lang.Enum<E>> E parseEnumFromAttribute(java.lang.String attribName,
                                                              java.lang.Class<E> enumClazz)
                                                   throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseEnumFromAttribute

public <E extends java.lang.Enum<E>> E parseEnumFromAttribute(java.lang.String attribName,
                                                              java.lang.Class<E> enumClazz,
                                                              E defaultValue)
                                                   throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseEnumFromText

public <E extends java.lang.Enum<E>> E parseEnumFromText(java.lang.Class<E> enumClazz)
                                              throws org.xmlpull.v1.XmlPullParserException,
                                                     java.io.IOException
Throws:
org.xmlpull.v1.XmlPullParserException
java.io.IOException

getUnusedAttributes

public java.util.Map<java.lang.String,java.lang.String> getUnusedAttributes()

ignoreOtherAttributes

public void ignoreOtherAttributes()

isAttributeUnused

public boolean isAttributeUnused(int idx)

error

public org.xmlpull.v1.XmlPullParserException error(java.lang.String msg)

error

public org.xmlpull.v1.XmlPullParserException error(java.lang.String msg,
                                                   java.lang.Throwable cause)

unexpected

public org.xmlpull.v1.XmlPullParserException unexpected()

parseEnum

protected <E extends java.lang.Enum<E>> E parseEnum(java.lang.Class<E> enumClazz,
                                                    java.lang.String value)
                                         throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseBool

public boolean parseBool(java.lang.String value)
                  throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseInt

protected int parseInt(java.lang.String value)
                throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

parseFloat

protected float parseFloat(java.lang.String value)
                    throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

missingAttribute

protected void missingAttribute(java.lang.String attribute)
                         throws org.xmlpull.v1.XmlPullParserException
Throws:
org.xmlpull.v1.XmlPullParserException

handleType

protected void handleType(int type)

warnUnusedAttributes

protected void warnUnusedAttributes()

getLogger

protected java.util.logging.Logger getLogger()