org.purl.sword.base
Class XmlElement

java.lang.Object
  extended by org.purl.sword.base.XmlElement
Direct Known Subclasses:
Author, Collection, Content, Entry, Generator, Link, Service, Source, TextConstruct, Workspace

public class XmlElement
extends Object

Parent class for all classes that represent an XML element. This provides some common utility methods that are useful for marshalling and unmarshalling data.

Author:
Neil Taylor

Field Summary
protected static String DATE_FORMAT
          The Date format that is used to parse dates to and from the ISO format in the XML data.
protected  String localName
           
protected  String prefix
          The name to use for the prefix.
 
Constructor Summary
XmlElement(String localName)
           
XmlElement(String prefix, String localName)
          Create a new instance.
 
Method Summary
protected  String dateToString(Date date)
          Convert the date to a string.
 String getQualifiedName()
          Retrieve the qualified name for this object.
 String getQualifiedName(String name)
          Retrieve the qualified name.
protected  boolean isInstanceOf(nu.xom.Element element, String localName, String namespaceURI)
          Determines if the specified element is an instance of the element name.
protected  Date stringToDate(String date)
          Convert the string into a Date object.
protected  boolean unmarshallBoolean(nu.xom.Element element)
          Extract a boolean value from the specified element.
protected  Date unmarshallDate(nu.xom.Element element)
          Extract an date value from the specified element.
protected  int unmarshallInteger(nu.xom.Element element)
          Extract an integer value from the specified element.
protected  String unmarshallString(nu.xom.Element element)
          Extract a string value from the specified element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

prefix

protected String prefix
The name to use for the prefix.


localName

protected String localName

DATE_FORMAT

protected static final String DATE_FORMAT
The Date format that is used to parse dates to and from the ISO format in the XML data.

See Also:
Constant Field Values
Constructor Detail

XmlElement

public XmlElement(String localName)
Parameters:
localName -

XmlElement

public XmlElement(String prefix,
                  String localName)
Create a new instance.

Method Detail

unmarshallBoolean

protected boolean unmarshallBoolean(nu.xom.Element element)
                             throws UnmarshallException
Extract a boolean value from the specified element. The boolean value is represented as the string 'true' or 'false' as the only child of the specified element.

Parameters:
element - The element that contains the boolean value.
Returns:
True or false, based on the string in the element's content.
Throws:
UnmarshallException - If the element does not contain a single child, or if the child does not contain the value 'true' or 'false'.

unmarshallString

protected String unmarshallString(nu.xom.Element element)
                           throws UnmarshallException
Extract a string value from the specified element. The value is the only child of the specified element.

Parameters:
element - The element that contains the string value.
Returns:
The string.
Throws:
UnmarshallException - If the element does not contain a single child.

unmarshallInteger

protected int unmarshallInteger(nu.xom.Element element)
                         throws UnmarshallException
Extract an integer value from the specified element. The integer value is represented as a string in the only child of the specified element.

Parameters:
element - The element that contains the integer.
Returns:
The integer.
Throws:
UnmarshallException - If the element does not contain a single child, or if the child does not contain the valid integer.

unmarshallDate

protected Date unmarshallDate(nu.xom.Element element)
                       throws UnmarshallException
Extract an date value from the specified element. The date value is represented as a string in the only child of the element.

Parameters:
element - The element that contains the date.
Returns:
The date.
Throws:
UnmarshallException - If the element does not contain a single child, or if the child does not contain the valid date.

dateToString

protected String dateToString(Date date)
Convert the date to a string. If the date is null, the result will result to a default date of 1st January 1970. FIXME - is this sensible?

Parameters:
date - The Date object.
Returns:
The Date, expressed as a string in the format yyyy-MM-ddTHH:mm:ssZ.

stringToDate

protected Date stringToDate(String date)
                     throws ParseException
Convert the string into a Date object.

Parameters:
date - The date, represented as a string.
Returns:
A Date.
Throws:
ParseException - If the string does not match the format of yyyy-MM-ddTHH:mm:ssZ.

isInstanceOf

protected boolean isInstanceOf(nu.xom.Element element,
                               String localName,
                               String namespaceURI)
Determines if the specified element is an instance of the element name. If you are checking the name title in the ATOM namespace, then the local name should be 'title' and the namespaceURI is the URI for the ATOM namespace.

Parameters:
element - The specified element.
localName - The local name for the element.
namespaceURI - The namespace for the element.
Returns:
True if the element matches the localname and namespace. Otherwise, false.

getQualifiedName

public String getQualifiedName()
Retrieve the qualified name for this object. This uses the prefix and local name stored in this object.

Returns:
A string of the format 'prefix:localName'

getQualifiedName

public String getQualifiedName(String name)
Retrieve the qualified name. The prefix for this object is prepended onto the specified local name.

Parameters:
name - the specified local name.
Returns:
A string of the format 'prefix:name'


Copyright © 2008 The DSpace Foundation. All Rights Reserved.