Package org.eigenbase.xom
Class DOMElementParser
- java.lang.Object
-
- org.eigenbase.xom.DOMElementParser
-
public class DOMElementParser extends java.lang.Object
DOMElementParser is a utility wrapper around DOMWrapper. Implements a parseable stream of child DOMWrappers and also provides validation on an XML document beyond the DTD.
-
-
Constructor Summary
Constructors Constructor Description DOMElementParser(DOMWrapper wrapper, java.lang.String prefix, java.lang.Class enclosure)
Constructs a new ElementParser based on an Element of the XML parse tree wrapped in a DOMWrapper, and a prefix (to be applied to all element tags except the root), and the name of the enclosing class.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description NodeDef[]
classArray(java.lang.Class elemClass, int min, int max)
This function retrieves a collection of elements which are subclasses of the given class, returning them as an array.NodeDef[]
getArray(java.lang.Class elemClass, int min, int max)
This function retrieves a collection of elements which are subclasses of the given class, returning them as an array.java.lang.Object
getAttribute(java.lang.String attrName, java.lang.String attrType, java.lang.String defaultValue, java.lang.String[] values, boolean required)
Retrieve an Attribute from the parser.NodeDef
getElement(java.lang.Class elementClass, boolean required)
This function retrieves an Element from this parser, advancing the parser if the element is found.static java.lang.Class
getPluginClass(java.lang.Class managerClass)
Get a Class object representing a plugin class, given a manager class that implements the static method getXMLDefClass().static java.lang.Class
getPluginClass(java.lang.String packageName, java.lang.String className)
Get a Class object representing a plugin class, identified either directly by a Java package and Java class name, or indirectly by a Java package and Java class which defines a method called getXMLDefClass() to return the appropriate class.java.lang.String
getString(java.lang.String elementName, boolean required)
This function retrieves a String element from this parser, advancing the parser if the element is found.java.lang.String[]
getStringArray(java.lang.String elemName, int min, int max)
This function returns a collection of String elements of the given name, returning them as an array.java.lang.String
getText()
This function is used to return a CDATA section as text.int
lastOptionIndex()
Returns the option index of the element returned through the last requiredOption call.DOMWrapper[]
optionalArray(java.lang.String elemName, int min, int max)
This function retrieves a collection of elements with the given name, returning them as an array.java.lang.String
optionalAttribute(java.lang.String attrName)
This function retrieves an optional Attribute by name from the current Element.java.lang.Boolean
optionalBooleanAttribute(java.lang.String attrName)
This function retrieves an optional Attribute by name from the current Element, converting it to an Boolean.java.lang.Double
optionalDoubleAttribute(java.lang.String attrName)
This function retrieves an optional Attribute by name from the current Element, converting it to a Double.DOMWrapper
optionalElement(java.lang.String elementName)
This function retrieves an optional Element from this parser, advancing the parser if the element is found.java.lang.Integer
optionalIntegerAttribute(java.lang.String attrName)
This function retrieves an optional Attribute by name from the current Element, converting it to an Integer.java.lang.String
optionalString(java.lang.String elementName)
This function retrieves an optional String element from this parser, advancing the parser if the element is found.java.lang.String
requiredAttribute(java.lang.String attrName)
This function retrieves a required Attribute by name from the current Element.java.lang.Boolean
requiredBooleanAttribute(java.lang.String attrName)
This function retrieves an required Attribute by name from the current Element, converting it to a Boolean.NodeDef
requiredClass(java.lang.Class classTemplate)
This function retrieves a required Element of a specific class from this parser, advancing the parser after the read.static java.lang.String
requiredDefAttribute(DOMWrapper wrapper, java.lang.String attrName, java.lang.String defaultVal)
This static version of requiredAttribute uses any element definition as a basis for the attribute.DOMWrapper
requiredElement(java.lang.String elementName)
This function retrieves a required Element from this parser, advancing the parser after the read.java.lang.Integer
requiredIntegerAttribute(java.lang.String attrName)
This function retrieves an required Attribute by name from the current Element, converting it to an Integer.DOMWrapper
requiredOption(java.lang.String[] elementNames)
This function retrieves a required element which may have one of a number of names.java.lang.String
requiredString(java.lang.String elementName)
This function retrieves a required String element from this parser, advancing the parser after the read.
-
-
-
Constructor Detail
-
DOMElementParser
public DOMElementParser(DOMWrapper wrapper, java.lang.String prefix, java.lang.Class enclosure) throws XOMException
Constructs a new ElementParser based on an Element of the XML parse tree wrapped in a DOMWrapper, and a prefix (to be applied to all element tags except the root), and the name of the enclosing class.- Parameters:
wrapper
- a DOMWrapper representing the section of the XML parse tree to traverse.- Throws:
XOMException
-
-
Method Detail
-
requiredString
public java.lang.String requiredString(java.lang.String elementName) throws XOMException
This function retrieves a required String element from this parser, advancing the parser after the read.- Parameters:
elementName
- the name of the element to retrieve.- Returns:
- the String value stored inside the element to retrieve.
- Throws:
XOMException
- if there is no element with the given name.
-
optionalString
public java.lang.String optionalString(java.lang.String elementName) throws XOMException
This function retrieves an optional String element from this parser, advancing the parser if the element is found. If no element of the correct name is found, this function returns null.- Parameters:
elementName
- the name of the element to retrieve.- Returns:
- the String value stored inside the element to retrieve.
- Throws:
XOMException
-
requiredElement
public DOMWrapper requiredElement(java.lang.String elementName) throws XOMException
This function retrieves a required Element from this parser, advancing the parser after the read.- Parameters:
elementName
- the name of the element to retrieve.- Returns:
- the DOMWrapper to retrieve.
- Throws:
XOMException
- if there is no element with the given name.
-
getText
public java.lang.String getText()
This function is used to return a CDATA section as text. It does no parsing.- Returns:
- the contents of the CDATA element as text.
-
optionalElement
public DOMWrapper optionalElement(java.lang.String elementName) throws XOMException
This function retrieves an optional Element from this parser, advancing the parser if the element is found. If no element of the correct name is found, this function returns null.- Parameters:
elementName
- the name of the element to retrieve.- Returns:
- the DOMWrapper to retreive, or null if none found.
- Throws:
XOMException
-
requiredOption
public DOMWrapper requiredOption(java.lang.String[] elementNames) throws XOMException
This function retrieves a required element which may have one of a number of names. The parser is advanced after the read.- Parameters:
elementNames
- an array of allowed names. Names are compared in a case-insensitive fashion.- Returns:
- the first element with one of the given names.
- Throws:
XOMException
- if there are no more elements to read or if the next element's name is not in the elementNames list.
-
requiredClass
public NodeDef requiredClass(java.lang.Class classTemplate) throws XOMException
This function retrieves a required Element of a specific class from this parser, advancing the parser after the read. The class must be derived from ElementDef.- Throws:
XOMException
-
lastOptionIndex
public int lastOptionIndex()
Returns the option index of the element returned through the last requiredOption call.
-
requiredAttribute
public java.lang.String requiredAttribute(java.lang.String attrName) throws XOMException
This function retrieves a required Attribute by name from the current Element.- Parameters:
attrName
- the name of the attribute.- Returns:
- the String value of the attribute.
- Throws:
XOMException
- if no attribute of this name is set.
-
requiredDefAttribute
public static java.lang.String requiredDefAttribute(DOMWrapper wrapper, java.lang.String attrName, java.lang.String defaultVal) throws XOMException
This static version of requiredAttribute uses any element definition as a basis for the attribute. It is used by Plugin definitions to return attributes before the parser is created.- Parameters:
wrapper
- the Element in which to find the attribute.attrName
- the name of the attribute to retrieve.defaultVal
- the default value of the attribute to retrieve.- Throws:
XOMException
- if no attribute of this name is set.
-
optionalAttribute
public java.lang.String optionalAttribute(java.lang.String attrName) throws XOMException
This function retrieves an optional Attribute by name from the current Element.- Parameters:
attrName
- the name of the attribute.- Returns:
- the String value of the attribute, or null if the attribute is not set.
- Throws:
XOMException
-
optionalIntegerAttribute
public java.lang.Integer optionalIntegerAttribute(java.lang.String attrName) throws XOMException
This function retrieves an optional Attribute by name from the current Element, converting it to an Integer.- Parameters:
attrName
- the name of the attribute.- Returns:
- the Integer value of the attribute, or null if the attribute is not set.
- Throws:
XOMException
- if the value is set to an illegal integer value.
-
optionalDoubleAttribute
public java.lang.Double optionalDoubleAttribute(java.lang.String attrName) throws XOMException
This function retrieves an optional Attribute by name from the current Element, converting it to a Double.- Parameters:
attrName
- the name of the attribute.- Returns:
- the Double value of the attribute, or null if the attribute is not set.
- Throws:
XOMException
- if the value is set to an illegal double value.
-
requiredIntegerAttribute
public java.lang.Integer requiredIntegerAttribute(java.lang.String attrName) throws XOMException
This function retrieves an required Attribute by name from the current Element, converting it to an Integer.- Parameters:
attrName
- the name of the attribute.- Returns:
- the Integer value of the attribute.
- Throws:
XOMException
- if the value is not set, or is set to an illegal integer value.
-
optionalBooleanAttribute
public java.lang.Boolean optionalBooleanAttribute(java.lang.String attrName) throws XOMException
This function retrieves an optional Attribute by name from the current Element, converting it to an Boolean. The string value "true" (in any case) is considered TRUE. Any other value is considered false.- Parameters:
attrName
- the name of the attribute.- Returns:
- the Boolean value of the attribute, or null if the attribute is not set.
- Throws:
XOMException
- if the value is set to an illegal integer value.
-
requiredBooleanAttribute
public java.lang.Boolean requiredBooleanAttribute(java.lang.String attrName) throws XOMException
This function retrieves an required Attribute by name from the current Element, converting it to a Boolean. The string value "true" (in any case) is considered TRUE. Any other value is considered false.- Parameters:
attrName
- the name of the attribute.- Returns:
- the Boolean value of the attribute.
- Throws:
XOMException
-
optionalArray
public DOMWrapper[] optionalArray(java.lang.String elemName, int min, int max) throws XOMException
This function retrieves a collection of elements with the given name, returning them as an array.- Parameters:
elemName
- the element name.min
- the minimum number of elements required in the array. Set this parameter to 0 to indicate no minimum.max
- the maximum number of elements allowed in the array. Set this parameter to 0 to indicate no maximum.- Returns:
- an Element array containing the discovered elements.
- Throws:
XOMException
- if there are fewer than min or more than max elements with the name elemName.
-
classArray
public NodeDef[] classArray(java.lang.Class elemClass, int min, int max) throws XOMException
This function retrieves a collection of elements which are subclasses of the given class, returning them as an array. The array will contain ElementDef objects automatically constructed to be of the correct class.- Parameters:
elemClass
- the element class.min
- the minimum number of elements required in the array. Set this parameter to 0 to indicate no minimum.max
- the maximum number of elements allowed in the array. Set this parameter to 0 to indicate no maximum.- Returns:
- an ElementDef array containing the discovered elements.
- Throws:
XOMException
- if there are fewer than min or more than max elements with the name elemName.
-
getElement
public NodeDef getElement(java.lang.Class elementClass, boolean required) throws XOMException
This function retrieves an Element from this parser, advancing the parser if the element is found. The Element's corresponding ElementDef class is looked up and its constructor is called automatically. If the requested Element is not found the function returns null unless required is set to true. In this case, a XOMException is thrown.- Parameters:
elementClass
- the Class of the element to retrieve.required
- true to throw an exception if the element is not found, false to simply return null.- Returns:
- the element, as an ElementDef, or null if it is not found and required is false.
- Throws:
XOMException
- if required is true and the element could not be found.
-
getArray
public NodeDef[] getArray(java.lang.Class elemClass, int min, int max) throws XOMException
This function retrieves a collection of elements which are subclasses of the given class, returning them as an array. The array will contain ElementDef objects automatically constructed to be of the correct class.- Parameters:
elemClass
- the element class.min
- the minimum number of elements required in the array. Set this parameter to 0 to indicate no minimum.max
- the maximum number of elements allowed in the array. Set this parameter to 0 to indicate no maximum.- Returns:
- an ElementDef array containing the discovered elements.
- Throws:
XOMException
- if there are fewer than min or more than max elements with the name elemName.
-
getString
public java.lang.String getString(java.lang.String elementName, boolean required) throws XOMException
This function retrieves a String element from this parser, advancing the parser if the element is found. If no element of the correct name is found, this function returns null, unless required is true, in which case a XOMException is thrown.- Parameters:
elementName
- the name of the element to retrieve.required
- true to throw an exception if the element is not found, false to simply return null.- Returns:
- the String value stored inside the element to retrieve, or null if no element with the given elementName could be found.
- Throws:
XOMException
-
getStringArray
public java.lang.String[] getStringArray(java.lang.String elemName, int min, int max) throws XOMException
This function returns a collection of String elements of the given name, returning them as an array.- Parameters:
elemName
- the element name.min
- the minimum number of elements required in the array. Set this parameter to 0 to indicate no minimum.max
- the maximum number of elements allowed in the array. Set this parameter to 0 to indicate no maximum.- Returns:
- a String array containing the discovered elements.
- Throws:
XOMException
- if there are fewer than min or more than max elements with the name elemName.
-
getPluginClass
public static java.lang.Class getPluginClass(java.lang.String packageName, java.lang.String className) throws XOMException
Get a Class object representing a plugin class, identified either directly by a Java package and Java class name, or indirectly by a Java package and Java class which defines a method called getXMLDefClass() to return the appropriate class.- Parameters:
packageName
- the name of the Java package containing the plugin class.className
- the name of the plugin definition class.- Throws:
XOMException
- if the plugin class cannot be located or if the designated class is not suitable as a plugin class.
-
getPluginClass
public static java.lang.Class getPluginClass(java.lang.Class managerClass) throws XOMException
Get a Class object representing a plugin class, given a manager class that implements the static method getXMLDefClass().- Parameters:
managerClass
- any Class that implements getXMLDefClass.- Returns:
- the plugin Class.
- Throws:
XOMException
-
getAttribute
public java.lang.Object getAttribute(java.lang.String attrName, java.lang.String attrType, java.lang.String defaultValue, java.lang.String[] values, boolean required) throws XOMException
Retrieve an Attribute from the parser. The Attribute may be of any Java class, provided that the class supports a constructor from the String class. The Attribute's value will be returned as an Object, which must then be cast to the appropraite type. If the attribute is not defined and has no default, either null is returned (if required is false), or a XOMException is thrown (if required is true).- Parameters:
attrName
- the name of the attribute to retreive.attrType
- a String naming a Java Class to serve as the type. If attrType contains a "." character, the class is looked up directly from the type name. Otherwise, the class is looked up in the java.lang package. Finally, the class must have a constructor which takes a String as an argument.defaultValue
- the default value for this attribute. If values is set, the defaultValue must also be one of the set of values. defaultValue may be null.values
- an array of possible values for the attribute. If this parameter is not null, then the attribute's value must be one of the listed set of values or an exception will be thrown.required
- if set, then this function will throw an exception if the attribute has no value and defaultValue is null.- Returns:
- the Attribute's value as an Object. The actual class of this object is determined by attrType.
- Throws:
XOMException
-
-