org.jaffa.util
Class XmlHelper

java.lang.Object
  extended byorg.jaffa.util.XmlHelper

public class XmlHelper
extends Object

Utility Class for Common XML Manipulation routines.


Constructor Summary
XmlHelper()
           
 
Method Summary
static String getTextTrim(Node node)
          This returns the textual content directly held under this element.
static boolean hasChildElements(Node node)
          Test whether the input element has a child element.
static InputStream stripDoctypeDeclaration(InputStream in)
          This routine will return an InputStream which will strip off the DOCTYPE declaration from the source InputStream.
static InputStream stripDoctypeDeclaration(URL in)
          This routine will return an InputStream which will strip off the DOCTYPE declaration from the source URL.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlHelper

public XmlHelper()
Method Detail

hasChildElements

public static boolean hasChildElements(Node node)
Test whether the input element has a child element.

Parameters:
node - The Element to be tested.
Returns:
true if this element has at least one child element.

getTextTrim

public static String getTextTrim(Node node)
This returns the textual content directly held under this element. This will include all text within this single element, including whitespace and CDATA sections if they exist. It's essentially the concatenation of all Text and CDATA nodes. The call does not recurse into child elements. If no textual value exists for the element, an empty String ("") is returned.

Parameters:
node - The Element for which the Text is to be returned
Returns:
text content for the element, or empty string if none.

stripDoctypeDeclaration

public static InputStream stripDoctypeDeclaration(URL in)
                                           throws IOException
This routine will return an InputStream which will strip off the DOCTYPE declaration from the source URL.

Parameters:
in - The source URL from which the DOCTYPE will be stripped off.
Returns:
The InputStream with no DOCTYPE.
Throws:
IOException - if an I/O error occurs.
See Also:
stripDoctypeDeclaration(InputStream)

stripDoctypeDeclaration

public static InputStream stripDoctypeDeclaration(InputStream in)
                                           throws IOException
This routine will return an InputStream which will strip off the DOCTYPE declaration from the source InputStream. Example declaration: ''

This is mostly used on incomming JAXB unmarshalling where there is no way specify a default entity resolver. Here is an example...
        URL xmlFile = "testfile.xml";

        try {
            xmlFile = URLHelper.newExtendedURL(name);
        } catch (MalformedURLException e) {
            log.fatal("Can't Find Components Definition File. Bad URL - " + name, e);
            return null;
        }

        try {
            // create a JAXBContext capable of handling classes generated into the package
            JAXBContext jc = JAXBContext.newInstance("org.jaffa.presentation.portlet.component.componentdomain");

            // create an Unmarshaller
            Unmarshaller u = jc.createUnmarshaller();

            // enable validation
            u.setValidating(true);

            // unmarshal a document into a tree of Java content objects composed of classes from the package.
            compList = (Components) u.unmarshal(XmlHelper.stripDoctypeDeclaration(xmlFile));
        } catch (JAXBException e) {
            log.fatal("XML Formatting Error Reading Components Definition File", e);
            return null;
        } catch (IOException e) {
            log.fatal("Error in Reading Components Definition File", e);
            return null;
        }
 

Parameters:
in - The source InputStream from which the DOCTYPE will be stripped off.
Returns:
The InputStream with no DOCTYPE.
Throws:
IOException - if an I/O error occurs.


Copyright © 2002-2004 JAFFA Project.