de.jstacs.io
Class XMLParser

java.lang.Object
  extended by de.jstacs.io.XMLParser

public final class XMLParser
extends Object

Class for parsing standard data types and arrays in and out of an XML File. The methods with prefix append or add are for encoding, while methods with prefix extract are for decoding. Supported types include:

Author:
Jan Grau, Jens Keilwagen

Constructor Summary
XMLParser()
           
 
Method Summary
static void addTags(StringBuffer source, String tag)
          Frames the StringBuffer source with equal tags "< tag>" and "</tag >".
static void addTagsAndAttributes(StringBuffer source, String tag, String attributes)
          Frames the StringBuffer source with "< tag attributes>" and "</tag >".
static void appendObjectWithTags(StringBuffer xml, Object s, String tag)
          Appends an Object with the tags to the StringBuffer xml.
static void appendObjectWithTagsAndAttributes(StringBuffer xml, Object s, String tag, String attributes)
          Appends an Object with the tags and attributes to the StringBuffer xml.
static void appendObjectWithTagsAndAttributes(StringBuffer xml, Object s, String tag, String attributes, boolean writeClassInfo)
          Appends an Object with the tags and attributes to the StringBuffer xml.
static StringBuffer extractForTag(StringBuffer source, String tag)
          Extracts the contents of source between tag start and end tags.
static StringBuffer extractForTag(StringBuffer source, String tag, Map<String,String> attributes, Map<String,String> filterAttributes)
          Extracts the contents of source between tag start and end tags.
static Object extractObjectAndAttributesForTags(StringBuffer xml, String tag, Map<String,String> attributes, Map<String,String> filterAttributes)
          Returns the parsed value between the tags.
static
<T> T
extractObjectAndAttributesForTags(StringBuffer xml, String tag, Map<String,String> attributes, Map<String,String> filterAttributes, Class<T> k)
          Returns the parsed value between the tags.
static
<T,S> T
extractObjectAndAttributesForTags(StringBuffer xml, String tag, Map<String,String> attributes, Map<String,String> filterAttributes, Class<T> k, Class<S> outerClass, S outerInstance)
          Returns the parsed value between the tags as an inner instance of the object outerInstance.
static Object extractObjectForTags(StringBuffer xml, String tag)
          Returns the parsed value between the tags.
static
<T> T
extractObjectForTags(StringBuffer xml, String tag, Class<T> k)
          Returns the parsed value between the tags.
static boolean hasTag(StringBuffer source, String tag, Map<String,String> attributes, Map<String,String> filterAttributes)
          This method allows to check whether an XML contains a tagged entry.
static String parseAttributes(Map<String,String> map)
          This method parses a map of attribute, i.e.
static String parseString(String original)
          This method parses the original String to null if original equals "null".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLParser

public XMLParser()
Method Detail

addTags

public static void addTags(StringBuffer source,
                           String tag)
Frames the StringBuffer source with equal tags "< tag>" and "</tag >".

Parameters:
source - the source StringBuffer that should be encoded in XML
tag - the tags by which the StringBuffer should be framed
See Also:
addTagsAndAttributes(StringBuffer, String, String)

addTagsAndAttributes

public static void addTagsAndAttributes(StringBuffer source,
                                        String tag,
                                        String attributes)
Frames the StringBuffer source with "< tag attributes>" and "</tag >".

Parameters:
source - the source StringBuffer that should be encoded in XML
tag - the tags by which the StringBuffer should be framed
attributes - null or some attributes, i.e. value="100" confidence="0"
See Also:
parseAttributes(Map)

appendObjectWithTags

public static void appendObjectWithTags(StringBuffer xml,
                                        Object s,
                                        String tag)
Appends an Object with the tags to the StringBuffer xml.

Parameters:
xml - the source StringBuffer that should be encoded in XML
s - the object that should be framed by the tags and appended to the source, s might be null, Storable, String, primitive type, wrapper class of a primitive type, or any array thereof
tag - the tags by which the value should be framed
See Also:
parseAttributes(Map), appendObjectWithTagsAndAttributes(StringBuffer, Object, String, String, boolean)

appendObjectWithTagsAndAttributes

public static void appendObjectWithTagsAndAttributes(StringBuffer xml,
                                                     Object s,
                                                     String tag,
                                                     String attributes)
Appends an Object with the tags and attributes to the StringBuffer xml.

Parameters:
xml - the source StringBuffer that should be encoded in XML
s - the object that should be framed by the tags and appended to the source, s might be null, Storable, String, primitive type, wrapper class of a primitive type, or any array thereof
tag - the tags by which the value should be framed
attributes - null or some attributes, e.g., value="100" confidence="0"
See Also:
parseAttributes(Map), appendObjectWithTagsAndAttributes(StringBuffer, Object, String, String, boolean)

appendObjectWithTagsAndAttributes

public static void appendObjectWithTagsAndAttributes(StringBuffer xml,
                                                     Object s,
                                                     String tag,
                                                     String attributes,
                                                     boolean writeClassInfo)
Appends an Object with the tags and attributes to the StringBuffer xml.

Parameters:
xml - the source StringBuffer that should be encoded in XML
s - the object that should be framed by the tags and appended to the source, s might be null, Storable, String, primitive type, wrapper class of a primitive type, or any array thereof
tag - the tags by which the value should be framed
attributes - null or some attributes, e.g., value="100" confidence="0"
writeClassInfo - a boolean to enable to write class information (e.g. Class.getSimpleName()) of the Object s into the XML.
See Also:
parseAttributes(Map), extractObjectAndAttributesForTags(StringBuffer, String, Map, Map, Class)

parseAttributes

public static String parseAttributes(Map<String,String> map)
This method parses a map of attribute, i.e. key-value-pairs, to a String representation that is used for encoding XML.

Parameters:
map - the Map of attribute names and values
Returns:
the String representation of the attributes
See Also:
parseAttributes(String)

extractForTag

public static StringBuffer extractForTag(StringBuffer source,
                                         String tag)
                                  throws NonParsableException
Extracts the contents of source between tag start and end tags.

Parameters:
source - the XML-code containing start and end tag
tag - the tag (without angle brackets)
Returns:
the contents of start and end tags, without these tags, as a StringBuffer
Throws:
NonParsableException - if start or end tag could not be found

extractForTag

public static StringBuffer extractForTag(StringBuffer source,
                                         String tag,
                                         Map<String,String> attributes,
                                         Map<String,String> filterAttributes)
                                  throws NonParsableException
Extracts the contents of source between tag start and end tags. If attributes is not null, the attributes of the start tag are added to this Map. If filterAttributes is not null, the start tag is accepted only if its attributes and associated values contain those defined in filterAttributed.

Parameters:
source - the XML-code containing start and end tag
tag - the tag (without angle brackets)
attributes - a Map for attributes and values, or null if no attributes should be parsed.
filterAttributes - a Map of attributes and associated values, which must be present in the attributes of the start tag, or null for no filtering
Returns:
the contents of start and end tags, without these tags, as a StringBuffer, if the XML does not contain such a tagged entry, the method returns null
Throws:
NonParsableException - if the XML is malformed

hasTag

public static boolean hasTag(StringBuffer source,
                             String tag,
                             Map<String,String> attributes,
                             Map<String,String> filterAttributes)
                      throws NonParsableException
This method allows to check whether an XML contains a tagged entry.

Parameters:
source - the XML-code containing start and end tag
tag - the tag (without angle brackets)
attributes - a Map for attributes and values, or null if no attributes should be parsed.
filterAttributes - a Map of attributes and associated values, which must be present in the attributes of the start tag, or null for no filtering
Returns:
true if the XML contains such a tag, otherwise false
Throws:
NonParsableException - if the XML is malformed

extractObjectForTags

public static Object extractObjectForTags(StringBuffer xml,
                                          String tag)
                                   throws NonParsableException
Returns the parsed value between the tags.

Parameters:
xml - the source StringBuffer that should be decoded from XML
tag - the tags between which the value shall be taken
Returns:
the value between the tags
Throws:
NonParsableException - if the value could not be parsed
See Also:
extractObjectAndAttributesForTags(StringBuffer, String, Map, Map, Class)

extractObjectForTags

public static <T> T extractObjectForTags(StringBuffer xml,
                                         String tag,
                                         Class<T> k)
                              throws NonParsableException
Returns the parsed value between the tags.

Type Parameters:
T - the type of the parsed object
Parameters:
xml - the source StringBuffer that should be decoded from XML
tag - the tags between which the value shall be taken
k - the class used to parse the value between the tag, if null the class will be inferred from the XML
Returns:
the value between the tags
Throws:
NonParsableException - if the value could not be parsed
See Also:
extractObjectAndAttributesForTags(StringBuffer, String, Map, Map, Class)

extractObjectAndAttributesForTags

public static Object extractObjectAndAttributesForTags(StringBuffer xml,
                                                       String tag,
                                                       Map<String,String> attributes,
                                                       Map<String,String> filterAttributes)
                                                throws NonParsableException
Returns the parsed value between the tags.

Parameters:
xml - the source StringBuffer that should be decoded from XML
tag - the tags between which the value shall be taken
attributes - a Map which can be used to obtain the attribute of the tag
filterAttributes - a Map which defines a filter for the tags
Returns:
the value between the tags
Throws:
NonParsableException - if the value could not be parsed
See Also:
extractObjectAndAttributesForTags(StringBuffer, String, Map, Map, Class)

extractObjectAndAttributesForTags

public static <T> T extractObjectAndAttributesForTags(StringBuffer xml,
                                                      String tag,
                                                      Map<String,String> attributes,
                                                      Map<String,String> filterAttributes,
                                                      Class<T> k)
                                           throws NonParsableException
Returns the parsed value between the tags.

Type Parameters:
T - the type of the parsed object
Parameters:
xml - the source StringBuffer that should be decoded from XML
tag - the tags between which the value shall be taken
attributes - a Map which can be used to obtain the attribute of the tag
filterAttributes - a Map which defines a filter for the tags
k - the class used to parse the value between the tag, if null the class will be inferred from the XML
Returns:
the value between the tags
Throws:
NonParsableException - if the value could not be parsed
See Also:
appendObjectWithTagsAndAttributes(StringBuffer, Object, String, String, boolean)

extractObjectAndAttributesForTags

public static <T,S> T extractObjectAndAttributesForTags(StringBuffer xml,
                                                        String tag,
                                                        Map<String,String> attributes,
                                                        Map<String,String> filterAttributes,
                                                        Class<T> k,
                                                        Class<S> outerClass,
                                                        S outerInstance)
                                           throws NonParsableException
Returns the parsed value between the tags as an inner instance of the object outerInstance.

Type Parameters:
T - the type of the parsed object
S - the type of the outer object
Parameters:
xml - the source StringBuffer that should be decoded from XML
tag - the tags between which the value shall be taken
attributes - a Map which can be used to obtain the attribute of the tag
filterAttributes - a Map which defines a filter for the tags
k - the class used to parse the value between the tag, if null the class will be inferred from the XML
outerClass - the class of the outer instance of the parsed object
outerInstance - the outer instance of the parsed object
Returns:
the value between the tags
Throws:
NonParsableException - if the value could not be parsed
See Also:
appendObjectWithTagsAndAttributes(StringBuffer, Object, String, String, boolean)

parseString

public static String parseString(String original)
This method parses the original String to null if original equals "null".

Parameters:
original - the original String
Returns:
null or original