Classes | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Package Functions

de.mfuchs.xmlstorage.XMLNode Class Reference

XMLNode is the in memory representation of an XML node in the XMLStorage library. More...

Inheritance diagram for de.mfuchs.xmlstorage.XMLNode:
Inheritance graph
[legend]
Collaboration diagram for de.mfuchs.xmlstorage.XMLNode:
Collaboration graph
[legend]

List of all members.

Classes

class  AttributeMap
 AttributeMap is a sorted map of attributes, used to prevent type cast warning in assign(). More...
class  Children
 Children contains the list of immediate child nodes. More...

Public Member Functions

 XMLNode (final String name)
 XMLNode (final String name, final String leading)
 XMLNode (final XMLNode other)
String getName ()
String toString ()
void clear ()
void assign (final XMLNode other)
void addChild (XMLNode child)
 add a new child node
String get (final String attrName)
 read only access to an attribute
String get (final String attrName, final String def)
 read only access to an attribute with default value
void put (final String attrName, final String value)
 write access to an attribute
String getSubattrib (final String nodeName, final String attrName)
 convenient value access in children node
String getSubattrib (final String nodeName, final String attrName, int n)
void putSubattrib (final String nodeName, final String attrName, final String value)
 convenient storage of distinct values in children node
void putSubattrib (final String nodeName, final String attrName, final String value, int n)
Children getChildren ()
String getContent ()
void setContent (final String s)
void setContent (final String s, boolean cdata)
void write (Writer out) throws Exception
 pretty print node to stream without preserving original white space
void write (Writer out, int mode) throws Exception
void write (Writer out, int mode, int indent) throws IOException
 write node with children tree
XMLNode find (final String nodeName)
 Find a children node by name.
XMLNode find (final String nodeName, int n)
 Find the n'th children node with tag nodeName.
XMLNode find (final String nodeName, final String attrName, final String attrValue)
 Search matching child node identified by key name and an attribute value.
XMLNode find (final String nodeName, final String attrName, final String attrValue, int n)
 Search matching child node identified by key name and an attribute value.
XMLInt getInt (final String attrName)
XMLInt getInt (final String attrName, int def)
XMLNumber getNumber (final String attrName)
XMLNumber getNumber (final String attrName, BigDecimal def)
XMLBool getBool (final String attrName)
XMLBool getBool (final String attrName, boolean def)
XMLString getString (final String attrName)
XMLString getString (final String attrName, final String def)

Static Public Attributes

static final int FORMAT_SMART = 0
static final int FORMAT_ORIGINAL = 1
 preserve original white space and comments if present; pretty print otherwise
static final int FORMAT_PRETTY = 2
 write XML stream preserving original white space and comments
static final String XML_INDENT_SPACE = " "

Protected Member Functions

XMLNode getFirstChild ()
void writeWorker (Writer out, int indent) throws IOException
 Write node with children tree using original white space.
void prettyWriteWorker (Writer out, int indent) throws IOException
void smartWriteWorker (Writer out, int indent) throws IOException

Protected Attributes

String _name
Children _children = new Children()
AttributeMap _attributes = new AttributeMap()
String _leading = ""
String _content = ""
String _endLeading = ""
String _trailing = ""

Package Functions

XMLNode findRelative (XPath xpath)
 XPath find function.
AbstractXMLNode createRelative (XPath xpath)
 relative XPath create function

Detailed Description

XMLNode is the in memory representation of an XML node in the XMLStorage library.

Definition at line 53 of file XMLNode.java.


Constructor & Destructor Documentation

de.mfuchs.xmlstorage.XMLNode.XMLNode ( final String  name )
de.mfuchs.xmlstorage.XMLNode.XMLNode ( final String  name,
final String  leading 
)

Definition at line 60 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._leading, and de.mfuchs.xmlstorage.XMLNode._name.

    {
         _name = name;
         _leading = leading;
    }
de.mfuchs.xmlstorage.XMLNode.XMLNode ( final XMLNode  other )

Definition at line 66 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode.assign().

    {
        assign(other);
    }

Here is the call graph for this function:


Member Function Documentation

void de.mfuchs.xmlstorage.XMLNode.addChild ( XMLNode  child )
void de.mfuchs.xmlstorage.XMLNode.assign ( final XMLNode  other )

Definition at line 139 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._attributes, de.mfuchs.xmlstorage.XMLNode._children, de.mfuchs.xmlstorage.XMLNode._content, de.mfuchs.xmlstorage.XMLNode._endLeading, de.mfuchs.xmlstorage.XMLNode._leading, de.mfuchs.xmlstorage.XMLNode._trailing, and de.mfuchs.xmlstorage.XMLNode.Children.assign().

Referenced by de.mfuchs.xmlstorage.XMLNode.XMLNode().

    {
        _children.assign(other._children);

        _attributes = (AttributeMap) other._attributes.clone();

        _leading = other._leading;
        _content = other._content;
        _endLeading = other._endLeading;
        _trailing = other._trailing;
    }

Here is the call graph for this function:

void de.mfuchs.xmlstorage.XMLNode.clear (  )
AbstractXMLNode de.mfuchs.xmlstorage.XMLNode.createRelative ( XPath  xpath ) [package]

relative XPath create function

Definition at line 372 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode.addChild(), de.mfuchs.xmlstorage.XMLNode.find(), de.mfuchs.xmlstorage.XMLNode.put(), and de.mfuchs.xmlstorage.XMLNode.XMLNode().

    {
        XMLNode node = this;

        for(XPathElement elem : xpath) {
            XMLNode child = elem.find(node);

            if (child == null) {
                child = new XMLNode(elem._childName);
                node.addChild(child);

                for(Map.Entry<String, String> it : elem._mAttrAndAttr.entrySet())
                    put(it.getKey(), it.getValue());
            }

            node = child;
        }

        return node;
    }

Here is the call graph for this function:

XMLNode de.mfuchs.xmlstorage.XMLNode.find ( final String  nodeName )

Find a children node by name.

Parameters:
nodeName
Returns:

Definition at line 297 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._children.

Referenced by de.mfuchs.xmlstorage.XMLNode.createRelative(), de.mfuchs.xmlstorage.XMLNode.findRelative(), de.mfuchs.xmlstorage.XMLNode.getSubattrib(), de.mfuchs.xmlstorage.XMLPos.goDown(), de.mfuchs.xmlstorage.XMLNode.putSubattrib(), and de.mfuchs.xmlstorage.XMLPos.smartCreate().

    {
        for(XMLNode child : _children)
            if (child._name.equals(nodeName))
                return child;

        return null;
    }
XMLNode de.mfuchs.xmlstorage.XMLNode.find ( final String  nodeName,
int  n 
)

Find the n'th children node with tag nodeName.

Parameters:
nodeName
n
Returns:

Definition at line 312 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._children.

    {
        for(XMLNode child : _children)
            if (child._name.equals(nodeName))
                if (n-- == 0)
                    return child;

        return null;
    }
XMLNode de.mfuchs.xmlstorage.XMLNode.find ( final String  nodeName,
final String  attrName,
final String  attrValue 
)

Search matching child node identified by key name and an attribute value.

Parameters:
nodeName
attrName
attrValue
Returns:
XMLNode

Definition at line 329 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._children.

    {
        for(XMLNode child : _children)
            if (child._name.equals(nodeName) && child.get(attrName).equals(attrValue))
                return child;

        return null;
    }
XMLNode de.mfuchs.xmlstorage.XMLNode.find ( final String  nodeName,
final String  attrName,
final String  attrValue,
int  n 
)

Search matching child node identified by key name and an attribute value.

Parameters:
nodeName
attrName
attrValue
n
Returns:
XMLNode

Definition at line 346 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._children.

    {
        for(XMLNode node : _children)
            if (node._name.equals(nodeName) && node.get(attrName).equals(attrValue))
                if (n-- == 0)
                    return node;

        return null;
    }
XMLNode de.mfuchs.xmlstorage.XMLNode.findRelative ( XPath  xpath ) [package]

XPath find function.

Definition at line 357 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode.find().

Referenced by de.mfuchs.xmlstorage.XMLPos.go().

    {
        XMLNode node = this;

        for(XPathElement xelem : xpath) {
            node = xelem.find(node);

            if (node == null)
                break;
        }

        return node;
    }

Here is the call graph for this function:

String de.mfuchs.xmlstorage.XMLNode.get ( final String  attrName ) [virtual]
String de.mfuchs.xmlstorage.XMLNode.get ( final String  attrName,
final String  def 
) [virtual]

read only access to an attribute with default value

Implements de.mfuchs.xmlstorage.AbstractXMLNode.

Definition at line 169 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._attributes.

    {
        String found = _attributes.get(attrName);

        if (found != null)
            return found;
        else
            return def;
    }
XMLBool de.mfuchs.xmlstorage.AbstractXMLNode.getBool ( final String  attrName ) [inherited]

Definition at line 42 of file AbstractXMLNode.java.

    {
        return new XMLBool(this, attrName);
    }
XMLBool de.mfuchs.xmlstorage.AbstractXMLNode.getBool ( final String  attrName,
boolean  def 
) [inherited]

Definition at line 46 of file AbstractXMLNode.java.

    {
        return new XMLBool(this, attrName, def);
    }
Children de.mfuchs.xmlstorage.XMLNode.getChildren (  )
String de.mfuchs.xmlstorage.XMLNode.getContent (  ) [virtual]

Implements de.mfuchs.xmlstorage.AbstractXMLNode.

Definition at line 236 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._content, and de.mfuchs.xmlstorage.XMLHelper.decodeXMLString().

Referenced by de.mfuchs.xmlstorage.XMLPos.getContent().

    {
        return XMLHelper.decodeXMLString(_content);
    }

Here is the call graph for this function:

XMLNode de.mfuchs.xmlstorage.XMLNode.getFirstChild (  ) [protected]

Definition at line 284 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._children.

Referenced by de.mfuchs.xmlstorage.XMLPos.goDown().

    {
        if (!_children.isEmpty())
            return _children.getFirst();
        else
            return null;
    }
XMLInt de.mfuchs.xmlstorage.AbstractXMLNode.getInt ( final String  attrName,
int  def 
) [inherited]

Definition at line 28 of file AbstractXMLNode.java.

    {
        return new XMLInt(this, attrName, def);
    }
XMLInt de.mfuchs.xmlstorage.AbstractXMLNode.getInt ( final String  attrName ) [inherited]

Definition at line 24 of file AbstractXMLNode.java.

    {
        return new XMLInt(this, attrName);
    }
String de.mfuchs.xmlstorage.XMLNode.getName (  ) [virtual]

Implements de.mfuchs.xmlstorage.AbstractXMLNode.

Definition at line 73 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._name.

    {
        return _name;
    }
XMLNumber de.mfuchs.xmlstorage.AbstractXMLNode.getNumber ( final String  attrName ) [inherited]

Definition at line 33 of file AbstractXMLNode.java.

    {
        return new XMLNumber(this, attrName);
    }
XMLNumber de.mfuchs.xmlstorage.AbstractXMLNode.getNumber ( final String  attrName,
BigDecimal  def 
) [inherited]

Definition at line 37 of file AbstractXMLNode.java.

    {
        return new XMLNumber(this, attrName, def);
    }
XMLString de.mfuchs.xmlstorage.AbstractXMLNode.getString ( final String  attrName ) [inherited]

Definition at line 51 of file AbstractXMLNode.java.

    {
        return new XMLString(this, attrName);
    }
XMLString de.mfuchs.xmlstorage.AbstractXMLNode.getString ( final String  attrName,
final String  def 
) [inherited]

Definition at line 55 of file AbstractXMLNode.java.

    {
        return new XMLString(this, attrName, def);
    }
String de.mfuchs.xmlstorage.XMLNode.getSubattrib ( final String  nodeName,
final String  attrName 
)

convenient value access in children node

Definition at line 186 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode.find(), and de.mfuchs.xmlstorage.AbstractXMLNode.get().

    {
        final AbstractXMLNode node = find(nodeName);

        if (node != null)
            return node.get(attrName);
        else
            return "";
    }

Here is the call graph for this function:

String de.mfuchs.xmlstorage.XMLNode.getSubattrib ( final String  nodeName,
final String  attrName,
int  n 
)

Definition at line 196 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode.find(), and de.mfuchs.xmlstorage.AbstractXMLNode.get().

    {
        final AbstractXMLNode node = find(nodeName, n);

        if (node != null)
            return node.get(attrName);
        else
            return "";
    }

Here is the call graph for this function:

void de.mfuchs.xmlstorage.XMLNode.prettyWriteWorker ( Writer  out,
int  indent 
) throws IOException [protected]

Definition at line 438 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._attributes, de.mfuchs.xmlstorage.XMLNode._children, de.mfuchs.xmlstorage.XMLNode._content, de.mfuchs.xmlstorage.XMLNode._name, de.mfuchs.xmlstorage.XMLHelper.encodeXMLString(), and de.mfuchs.xmlstorage.XMLNode.XML_INDENT_SPACE.

Referenced by de.mfuchs.xmlstorage.XMLNode.write().

    {
        for(int i=indent; i-->0; )
            out.write(XML_INDENT_SPACE);

        out.write('<');
        out.write(XMLHelper.encodeXMLString(_name));

        for(Map.Entry<String,String> it : _attributes.entrySet()) {
            String attrValue = it.getValue();

            if (attrValue != null) {
                out.write(' ');
                out.write(XMLHelper.encodeXMLString(it.getKey()));
                out.write("=\"");
                out.write(XMLHelper.encodeXMLString(attrValue));
                out.write("\"");
            }
        }

        if (!_children.isEmpty() || _content.length()>0) {
            out.write(">\n");

            for(XMLNode child : _children)
                child.prettyWriteWorker(out, indent+1);

            for(int i=indent; i-->0; )
                out.write(XML_INDENT_SPACE);

            out.write("</");
            out.write(XMLHelper.encodeXMLString(_name));
            out.write(">\n");
        } else
            out.write("/>\n");
    }

Here is the call graph for this function:

void de.mfuchs.xmlstorage.XMLNode.put ( final String  attrName,
final String  value 
)
void de.mfuchs.xmlstorage.XMLNode.putSubattrib ( final String  nodeName,
final String  attrName,
final String  value 
)

convenient storage of distinct values in children node

Definition at line 207 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode.addChild(), de.mfuchs.xmlstorage.XMLNode.find(), de.mfuchs.xmlstorage.XMLNode.put(), and de.mfuchs.xmlstorage.XMLNode.XMLNode().

Referenced by de.mfuchs.xmlstorage.XMLPos.putSubattrib().

    {
        XMLNode node = find(nodeName);

        if (node == null) {
            node = new XMLNode(nodeName);
            addChild(node);
        }

        node.put(attrName, value);
    }

Here is the call graph for this function:

void de.mfuchs.xmlstorage.XMLNode.putSubattrib ( final String  nodeName,
final String  attrName,
final String  value,
int  n 
)

Definition at line 219 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode.addChild(), de.mfuchs.xmlstorage.XMLNode.find(), de.mfuchs.xmlstorage.XMLNode.put(), and de.mfuchs.xmlstorage.XMLNode.XMLNode().

    {
        XMLNode node = find(nodeName, n);

        if (node == null) {
            node = new XMLNode(nodeName);
            addChild(node);
        }

        node.put(attrName, value);
    }

Here is the call graph for this function:

void de.mfuchs.xmlstorage.XMLNode.setContent ( final String  s )

Definition at line 241 of file XMLNode.java.

Referenced by de.mfuchs.xmlstorage.XMLPos.setContent().

    {
        setContent(s, false);
    }
void de.mfuchs.xmlstorage.XMLNode.setContent ( final String  s,
boolean  cdata 
)

Definition at line 246 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._content, and de.mfuchs.xmlstorage.XMLHelper.encodeXMLString().

    {
        _content = XMLHelper.encodeXMLString(s, cdata);
    }

Here is the call graph for this function:

void de.mfuchs.xmlstorage.XMLNode.smartWriteWorker ( Writer  out,
int  indent 
) throws IOException [protected]

Definition at line 474 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._attributes, de.mfuchs.xmlstorage.XMLNode._children, de.mfuchs.xmlstorage.XMLNode._content, de.mfuchs.xmlstorage.XMLNode._endLeading, de.mfuchs.xmlstorage.XMLNode._leading, de.mfuchs.xmlstorage.XMLNode._name, de.mfuchs.xmlstorage.XMLNode._trailing, de.mfuchs.xmlstorage.XMLHelper.encodeXMLString(), and de.mfuchs.xmlstorage.XMLNode.XML_INDENT_SPACE.

Referenced by de.mfuchs.xmlstorage.XMLNode.write().

    {
        if (_leading.length() == 0)
            for(int i=indent; i-->0; )
                out.write(XML_INDENT_SPACE);
        else
            out.write(_leading);

        out.write('<');
        out.write(XMLHelper.encodeXMLString(_name));

        for(Map.Entry<String,String> it : _attributes.entrySet()) {
            String attrValue = it.getValue();

            if (attrValue != null) {
                out.write(' ');
                out.write(XMLHelper.encodeXMLString(it.getKey()));
                out.write("=\"");
                out.write(XMLHelper.encodeXMLString(attrValue));
                out.write("\"");
            }
        }

        if (_children.isEmpty() && _content.length()==0)
            out.write("/>");
        else {
            out.write('>');

            if (_content.length() == 0)
                out.write('\n');
            else
                out.write(_content);

            Iterator<XMLNode> it = _children.iterator();

            if (it.hasNext()) {
                while(it.hasNext())
                    it.next().smartWriteWorker(out, indent+1);

                if (_endLeading.length() == 0)
                    for(int i=indent; i-->0; )
                        out.write(XML_INDENT_SPACE);
                else
                    out.write(_endLeading);
            } else
                out.write(_endLeading);

            out.write("</");
            out.write(XMLHelper.encodeXMLString(_name));
            out.write('>');
        }

        if (_trailing.length() == 0)
            out.write('\n');
        else
            out.write(_trailing);
    }

Here is the call graph for this function:

String de.mfuchs.xmlstorage.XMLNode.toString (  ) [virtual]

Implements de.mfuchs.xmlstorage.AbstractXMLNode.

Definition at line 78 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._name.

Referenced by de.mfuchs.xmlstorage.XMLPos.toString().

    {
        return _name;
    }
void de.mfuchs.xmlstorage.XMLNode.write ( Writer  out ) throws Exception

pretty print node to stream without preserving original white space

Reimplemented in de.mfuchs.xmlstorage.XMLDoc.

Definition at line 257 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode.FORMAT_SMART.

Referenced by de.mfuchs.xmlstorage.XMLNode.write().

    {
        write(out, FORMAT_SMART, 0);
    }
void de.mfuchs.xmlstorage.XMLNode.write ( Writer  out,
int  mode 
) throws Exception

Reimplemented in de.mfuchs.xmlstorage.XMLDoc.

Definition at line 262 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode.write().

    {
        write(out, mode, 0);
    }

Here is the call graph for this function:

void de.mfuchs.xmlstorage.XMLNode.write ( Writer  out,
int  mode,
int  indent 
) throws IOException

write node with children tree

Definition at line 268 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode.FORMAT_ORIGINAL, de.mfuchs.xmlstorage.XMLNode.FORMAT_PRETTY, de.mfuchs.xmlstorage.XMLNode.prettyWriteWorker(), de.mfuchs.xmlstorage.XMLNode.smartWriteWorker(), and de.mfuchs.xmlstorage.XMLNode.writeWorker().

    {
        switch(mode) {
          case FORMAT_PRETTY:
            prettyWriteWorker(out, indent);
            break;

          case FORMAT_ORIGINAL:
            writeWorker(out, indent);
            break;

        default:     // FORMAT_SMART
            smartWriteWorker(out, indent);
        }
    }

Here is the call graph for this function:

void de.mfuchs.xmlstorage.XMLNode.writeWorker ( Writer  out,
int  indent 
) throws IOException [protected]

Write node with children tree using original white space.

Parameters:
out
indent
Exceptions:
IOException

Definition at line 400 of file XMLNode.java.

References de.mfuchs.xmlstorage.XMLNode._attributes, de.mfuchs.xmlstorage.XMLNode._children, de.mfuchs.xmlstorage.XMLNode._content, de.mfuchs.xmlstorage.XMLNode._endLeading, de.mfuchs.xmlstorage.XMLNode._leading, de.mfuchs.xmlstorage.XMLNode._name, de.mfuchs.xmlstorage.XMLNode._trailing, and de.mfuchs.xmlstorage.XMLHelper.encodeXMLString().

Referenced by de.mfuchs.xmlstorage.XMLNode.write().

    {
        out.write(_leading);
        out.write('<');
        out.write(XMLHelper.encodeXMLString(_name));

        for(Map.Entry<String,String> it : _attributes.entrySet()) {
            String attrValue = it.getValue();

            if (attrValue != null) {
                out.write(' ');
                out.write(XMLHelper.encodeXMLString(it.getKey()));
                out.write("=\"");
                out.write(XMLHelper.encodeXMLString(attrValue));
                out.write("\"");
            }
        }


        if (!_children.isEmpty() || _content.length()>0) {
            out.write('>');
            out.write(_content);

            for(XMLNode child: _children)
                child.writeWorker(out, indent+1);

            out.write(_endLeading);
            out.write("</");
            out.write(XMLHelper.encodeXMLString(_name));
            out.write('>');
        } else
            out.write("/>");

        out.write(_trailing);
    }

Here is the call graph for this function:


Member Data Documentation

String de.mfuchs.xmlstorage.XMLNode._content = "" [protected]
String de.mfuchs.xmlstorage.XMLNode._leading = "" [protected]
String de.mfuchs.xmlstorage.XMLNode._trailing = "" [protected]

preserve original white space and comments if present; pretty print otherwise

Definition at line 254 of file XMLNode.java.

Referenced by de.mfuchs.xmlstorage.XMLNode.write().

write XML stream preserving original white space and comments

Definition at line 255 of file XMLNode.java.

Referenced by de.mfuchs.xmlstorage.XMLNode.write(), and de.mfuchs.xmlstorage.XMLDoc.writeFormating().

final String de.mfuchs.xmlstorage.XMLNode.XML_INDENT_SPACE = " " [static]

The documentation for this class was generated from the following file: