com.arsdigita.xml
Class Element

java.lang.Object
  extended bycom.arsdigita.xml.Element
Direct Known Subclasses:
ExtendedElement

public class Element
extends Object

A wrapper class that implements some functionality of org.jdom.Element using org.w3c.dom.Element.

Since:
ACS 4.5a
Version:
$Revision: #16 $ $Date: 2004/04/07 $
Author:
Patrick McNeill

Field Summary
protected  org.w3c.dom.Element m_element
           
static String versionId
           
 
Constructor Summary
protected Element()
          Protected constructor to set up factories, etc.
  Element(String name)
          Creates a new element with the given name and no assigned namespace.
  Element(String name, String uri)
          Creates a new element with the given name, and assigns it to the namespace defined at uri.
 
Method Summary
 Element addAttribute(String name, String value)
          Adds an attribute to the element.
 Element addAttribute(String name, String value, String ns)
           
 Element addContent(Element newContent)
          Adds a child element to this element.
 String getAttribute(String name)
          Retrieves an attribute value by name.
 Map getAttributes()
           
 String getCDATASection()
           
 List getChildren()
          Returns a List of all the child elements nested directly (one level deep) within this element, as Element objects.
protected  org.w3c.dom.Element getInternalElement()
          Functions to allow this class to interact appropriately with the Document class (for example, allows nodes to be moved around, and so on).
 String getName()
           
 String getText()
          Returns the concatenation of all the text in all child nodes of the current element.
 boolean hasAttribute(String name)
           
protected  void importInto(org.w3c.dom.Document doc)
          Imports the internal node into another document.
 Element newChildElement(Element copyFrom)
          Copies the passed in element and all of its children to a new Element.
 Element newChildElement(String name)
          Creates a new element and adds it as a child to this element.
 Element newChildElement(String name, Element copyFrom)
          Copies the passed in element and all of its children to a new Element using the passed-in name
 Element newChildElement(String name, String uri)
          Creates a new element.
 Element newChildElement(String name, String uri, Element copyFrom)
          Copies the passed in element and all of its children to a new Element using the passed-in name
 Element setCDATASection(String cdata)
           
 Element setText(String text)
          Sets the text value of the current element (the part between the tags).
static void visitAllAttributes(org.w3c.dom.Node node)
          Deprecated. with no replacement, 1 May 2003
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

versionId

public static final String versionId
See Also:
Constant Field Values

m_element

protected org.w3c.dom.Element m_element
Constructor Detail

Element

protected Element()
Protected constructor to set up factories, etc. Does not actually create a new element. Used if we are programatically setting the m_element field later.


Element

public Element(String name)
Creates a new element with the given name and no assigned namespace.

Parameters:
name - the name of the element

Element

public Element(String name,
               String uri)
Creates a new element with the given name, and assigns it to the namespace defined at uri. The namespace prefix is automatically determined.

Parameters:
name - the name of the element
uri - the URI for the namespace definition
Method Detail

newChildElement

public Element newChildElement(String name)
Creates a new element and adds it as a child to this element. elt.newChildElement("newElt") is equivalent to
 Element newElt = new Element("newElt");
 elt.addChild(newElt);
 

Parameters:
name - the name of the element
Returns:
the created child element.

newChildElement

public Element newChildElement(String name,
                               String uri)
Creates a new element. Adds it as a child to this element element and assigns it to the namespace defined at uri. elt.newChildElement("newElt", namespace) is equivalent to
 Element newElt = new Element("newElt", namespace);
 elt.addChild(newElt);
 

Parameters:
name - the name of the Element
uri - the URI for the namespace definition
Returns:
the created child element.

newChildElement

public Element newChildElement(Element copyFrom)
Copies the passed in element and all of its children to a new Element.


newChildElement

public Element newChildElement(String name,
                               Element copyFrom)
Copies the passed in element and all of its children to a new Element using the passed-in name


newChildElement

public Element newChildElement(String name,
                               String uri,
                               Element copyFrom)
Copies the passed in element and all of its children to a new Element using the passed-in name


addAttribute

public Element addAttribute(String name,
                            String value)
Adds an attribute to the element.

Parameters:
name - the name of the attribute
value - the value of the attribute
Returns:
this element.

addAttribute

public Element addAttribute(String name,
                            String value,
                            String ns)

addContent

public Element addContent(Element newContent)
Adds a child element to this element.

Parameters:
newContent - the new child element
Returns:
this element.

setText

public Element setText(String text)
Sets the text value of the current element (the part between the tags). If the passed in text is null then it is converted to the empty string.

Parameters:
text - the text to include
Returns:
this element.

getText

public String getText()
Returns the concatenation of all the text in all child nodes of the current element.


setCDATASection

public Element setCDATASection(String cdata)

getCDATASection

public String getCDATASection()

getChildren

public List getChildren()
Returns a List of all the child elements nested directly (one level deep) within this element, as Element objects. If this target element has no nested elements, an empty List is returned. The returned list is "live", so changes to it affect the element's actual contents.

This performs no recursion, so elements nested two levels deep would have to be obtained with:

 Iterator itr = currentElement.getChildren().iterator();
 while (itr.hasNext()) {
    Element oneLevelDeep = (Element)nestedElements.next();
    List twoLevelsDeep = oneLevelDeep.getChildren();
      // Do something with these children
    }
 

Returns:
list of child Element objects for this element.

getAttributes

public Map getAttributes()

getAttribute

public String getAttribute(String name)
Retrieves an attribute value by name.

Parameters:
name - The name of the attribute to retrieve
Returns:
The Attr value as a string, or the empty string if that attribute does not have a specified or default value.

hasAttribute

public boolean hasAttribute(String name)

getName

public String getName()

getInternalElement

protected final org.w3c.dom.Element getInternalElement()
Functions to allow this class to interact appropriately with the Document class (for example, allows nodes to be moved around, and so on).

Returns:
the internal DOM Element.

importInto

protected void importInto(org.w3c.dom.Document doc)
Imports the internal node into another document. This could also be done with a combination of getInternalElement and a setInternalElement function.

Parameters:
doc - the org.w3c.dom.Document to import into

visitAllAttributes

public static void visitAllAttributes(org.w3c.dom.Node node)
Deprecated. with no replacement, 1 May 2003

Workaround for bug in some versions of Xerces. For some reason, importNode doesn't also copy attribute values unless you call getValue() on them first. This may be fixed in a later version of Xerces. In the meantime, calling visitAllAttributes(node) before importNode should help.

Parameters:
node - the org.w3c.dom.Node about to be imported


Copyright (c) 2004 Red Hat, Inc. Corporation. All Rights Reserved. Generated at July 20 2004:2337 UTC