ehsbe CommonsTM
Version 1.1.0rc3

com.ehsbe.commons.util
Class Stax

java.lang.Object
  extended by com.ehsbe.commons.util.Stax

public class Stax
extends Object

Stax utilities.

Version:
$Revision: 456 $
Author:
Jens Elkner

Field Summary
protected static SimpleDateFormat dateFormatter
          formatter to be used for date formatting
protected static SimpleDateFormat dateTimeTimezoneFormatter
          formatter to be used for datetime formatting
protected static TimeZone gmtTimeZone
          timezone to be used for date formatting/parsing
protected static Locale locale
          locale to be used for date formatting/parsing
protected static SimpleDateFormat stdDateTimeFormat
          formatter to be used for datetime formatting without timezone
 
Constructor Summary
Stax()
           
 
Method Summary
static String asString(Calendar cal)
          Get the date the given calendar represents as an XML Schema Part 2: Datatypes Second Edition GMT formatted dateTime string (e.g., 2004-08-04T19:09:02.768Z).
static String asString(Date date)
          Format the given date as date string 'yyyy-MM-dd'.
static void fastForwardToEndOfElement(XMLStreamReader reader)
          Forward the cursor of the stream to the end of the current start element.
static void forwardToNextChildOrEOE(XMLStreamReader reader)
          Forward the cursor of the stream to the start of the next element.
static IdentityHashMap<String,String> getAttributes(XMLStreamReader in)
          Get all all attributes of the element, where the cursor currently points to.
static Calendar getCalendar(String dateTimeStr)
          Convert the given XML Schema Part 2: Datatypes Second Edition formatted dateTime string (i.e.
static Date getDate(String dateStr)
          Convert the given GMT date string ('yyyy-MM-dd') into a date.
static XMLResolver getIgnoringXMLResolver()
          Get an XMLResolver, which always returns an empty InputStream, to make braindead Stax Implementations (e.g. from SUN) happy.
static StreamSource getInputSourceByFile(File path, boolean gzip)
          Boilerplate: Get a stream source from the given file.
static StreamSource getInputSourceByResource(String resourceName)
          Get a StreamSource from the given resource.
static InputStream getNullInputStream()
          Get an InputStream whoms read method always returns -1.
static XMLStreamReader getReader(StreamSource src, String root)
          Create a none-validating XMLStreamReader for the given InputStream and position the cursor at the first root element with the given name.
static XMLStreamReader getReader(StreamSource src, String root, boolean namespaceAware)
          Create an none-validating XMLStreamReader for the given InputStream and position the cursor at the first root element with the given name.
static String location2string(Location loc)
          Helper to map a Stream Location into a String in a standardized way.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

locale

protected static final Locale locale
locale to be used for date formatting/parsing


gmtTimeZone

protected static final TimeZone gmtTimeZone
timezone to be used for date formatting/parsing


dateFormatter

protected static final SimpleDateFormat dateFormatter
formatter to be used for date formatting


dateTimeTimezoneFormatter

protected static final SimpleDateFormat dateTimeTimezoneFormatter
formatter to be used for datetime formatting


stdDateTimeFormat

protected static final SimpleDateFormat stdDateTimeFormat
formatter to be used for datetime formatting without timezone

Constructor Detail

Stax

public Stax()
Method Detail

getInputSourceByResource

public static StreamSource getInputSourceByResource(String resourceName)
Get a StreamSource from the given resource.

Parameters:
resourceName - name of the resource.
Returns:
null if the resource cannot be found, the StreamSource otherwise.
Throws:
NullPointerException - if resourceName is null.

getInputSourceByFile

public static StreamSource getInputSourceByFile(File path,
                                                boolean gzip)
                                         throws IOException
Boilerplate: Get a stream source from the given file.

The underlying stream is an InputStream. So to finally free the resources allocated by the source one should call close on StreamSource.getInputStream().

Parameters:
path - file to read.
gzip - if true the file is gzipped.
Returns:
a StreamSource for the given path
Throws:
IOException - if path does not exist or is not readable or the is not in gzipped format when gzip option is set.

fastForwardToEndOfElement

public static void fastForwardToEndOfElement(XMLStreamReader reader)
                                      throws XMLStreamException
Forward the cursor of the stream to the end of the current start element.

Parameters:
reader - a reader, whoms cursor points to an start element
Throws:
XMLStreamException

location2string

public static String location2string(Location loc)
Helper to map a Stream Location into a String in a standardized way.

Parameters:
loc - stream location to convert
Returns:
??? if location is null, a String which describes the location otherwise.

getReader

public static final XMLStreamReader getReader(StreamSource src,
                                              String root)
Create a none-validating XMLStreamReader for the given InputStream and position the cursor at the first root element with the given name. The reader is XML Namespace unaware.

Parameters:
src - xml stream source to read from
root - name of the document root element
Returns:
null if one of the parameters are null or the given root element could not be found, the reader otherwise.

getNullInputStream

public static final InputStream getNullInputStream()
Get an InputStream whoms read method always returns -1.

Returns:
a reusable InputStream.
See Also:
getIgnoringXMLResolver()

getIgnoringXMLResolver

public static final XMLResolver getIgnoringXMLResolver()
Get an XMLResolver, which always returns an empty InputStream, to make braindead Stax Implementations (e.g. from SUN) happy.

Returns:
a resolver, which resolves everything to an empty entity.
See Also:
getNullInputStream()

getReader

public static final XMLStreamReader getReader(StreamSource src,
                                              String root,
                                              boolean namespaceAware)
Create an none-validating XMLStreamReader for the given InputStream and position the cursor at the first root element with the given name. The reader ignores external entities.

Parameters:
src - xml stream source to read from
root - name of the document root element. If null, the cursor gets positioned at the start of the first element encountered in the stream.
namespaceAware - if true the returned reader is XML Namespace aware, otherwise not.
Returns:
null if one of the parameters are null or the given root element could not be found, the reader otherwise.

asString

public static final String asString(Calendar cal)
Get the date the given calendar represents as an XML Schema Part 2: Datatypes Second Edition GMT formatted dateTime string (e.g., 2004-08-04T19:09:02.768Z). The time value obtained from the given calendar is automatically adjusted to GMT aka Zulu (i.e. timezone offsets are properly added).

Parameters:
cal - calendar to use to retrieve the time
Returns:
an ISO 8601 formatted string

getCalendar

public static final Calendar getCalendar(String dateTimeStr)
                                  throws ParseException
Convert the given XML Schema Part 2: Datatypes Second Edition formatted dateTime string (i.e. YYYY+-MM-DD'T'HH:MM:SS(.s+)?(((+|-)hh:mm)|'Z')?) to a Calendar instance with an appropriate timezone and the default locale of the system.

Parameters:
dateTimeStr - string to convert
Returns:
a Calendar instance
Throws:
ParseException - if the dateTimeStr is not correctly formatted
IndexOutOfBoundsException - same as for parse exception

asString

public static final String asString(Date date)
Format the given date as date string 'yyyy-MM-dd'. The time value obtained from the given date is adjusted to GMT automatically before the formatting is done.

Parameters:
date - date to format
Returns:
the formatted date

getDate

public static final Date getDate(String dateStr)
                          throws ParseException
Convert the given GMT date string ('yyyy-MM-dd') into a date. The underlying calender uses the GMT timezone for convertion.

Parameters:
dateStr - string to convert
Returns:
a date
Throws:
ParseException - if the given parameter has not the appropriate format.

forwardToNextChildOrEOE

public static final void forwardToNextChildOrEOE(XMLStreamReader reader)
                                          throws XMLStreamException
Forward the cursor of the stream to the start of the next element. Stops immediately, if the end of an element (EOE) is encountered. So the the cursor finally points either to the start of the next child or the end of the current element.

Parameters:
reader - a reader, whoms cursor points to an start element
Throws:
XMLStreamException

getAttributes

public static final IdentityHashMap<String,String> getAttributes(XMLStreamReader in)
Get all all attributes of the element, where the cursor currently points to.

Parameters:
in - reader, whoms cursor is at the start of an element
Returns:
a hash map with interned keys, which might be empty.

ehsbe CommonsTM
Version 1.1.0rc3

Copyright ©2005-2009 EHSBE, Walther-Rathenau-Str. 58, 39104 Magdeburg, Sachsen-Anhalt, Germany. All Rights Reserved.