Public Member Functions | Public Attributes | Package Functions | Package Attributes | Private Attributes

de.mfuchs.xmlstorage.XPathElement Class Reference

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

List of all members.

Public Member Functions

 XPathElement ()
 XPathElement (final String child_name)
 XPathElement (final String child_name, int child_idx)
 XPathElement (final String child_name, int child_idx, final String attr_name, final String attr_value)
XMLNode find (final XMLNode node)
boolean matches (final AbstractXMLNode node, int[] n)

Public Attributes

String _childName

Package Functions

int parse (final String path, int idx)

Package Attributes

Map< String, String > _mAttrAndAttr = new HashMap<String, String>()

Private Attributes

int _childIdx

Detailed Description

Definition at line 44 of file XPathElement.java.


Constructor & Destructor Documentation

de.mfuchs.xmlstorage.XPathElement.XPathElement (  )

Definition at line 46 of file XPathElement.java.

References de.mfuchs.xmlstorage.XPathElement._childIdx.

    {
        _childIdx = -1;
    }
de.mfuchs.xmlstorage.XPathElement.XPathElement ( final String  child_name )
de.mfuchs.xmlstorage.XPathElement.XPathElement ( final String  child_name,
int  child_idx 
)
de.mfuchs.xmlstorage.XPathElement.XPathElement ( final String  child_name,
int  child_idx,
final String  attr_name,
final String  attr_value 
)

Definition at line 63 of file XPathElement.java.

References de.mfuchs.xmlstorage.XPathElement._mAttrAndAttr.

    {
        this(child_name, child_idx);

        _mAttrAndAttr.put(attr_name, attr_value);
    }

Member Function Documentation

XMLNode de.mfuchs.xmlstorage.XPathElement.find ( final XMLNode  node )

Definition at line 144 of file XPathElement.java.

References de.mfuchs.xmlstorage.XMLNode._children, and de.mfuchs.xmlstorage.XPathElement.matches().

    {
        int[] n = new int[] {0};    // index in array for call-by-reference matches()

        for(XMLNode it : node._children)
            if (matches(it, n))
                return it;

        return null;
    }

Here is the call graph for this function:

boolean de.mfuchs.xmlstorage.XPathElement.matches ( final AbstractXMLNode  node,
int[]  n 
)

Definition at line 155 of file XPathElement.java.

References de.mfuchs.xmlstorage.XPathElement._childIdx, de.mfuchs.xmlstorage.XPathElement._childName, de.mfuchs.xmlstorage.XPathElement._mAttrAndAttr, de.mfuchs.xmlstorage.AbstractXMLNode.get(), and de.mfuchs.xmlstorage.AbstractXMLNode.toString().

Referenced by de.mfuchs.xmlstorage.XPathElement.find().

    {
        if (!node.toString().equals(_childName))
            if (!_childName.equals("*"))    // use asterisk as wildcard
                return false;

        for(Map.Entry<String, String> it : _mAttrAndAttr.entrySet())
            if (!node.get(it.getKey()).equals(it.getValue()))
                return false;

        if (_childIdx == -1)
            return true;
        else if (n[0]++ == _childIdx)
            return true;
        else
            return false;
    }

Here is the call graph for this function:

int de.mfuchs.xmlstorage.XPathElement.parse ( final String  path,
int  idx 
) [package]

Definition at line 76 of file XPathElement.java.

References de.mfuchs.xmlstorage.XPathElement._childIdx, de.mfuchs.xmlstorage.XPathElement._childName, de.mfuchs.xmlstorage.XPathElement._mAttrAndAttr, and de.mfuchs.xmlstorage.XMLHelper.unescape().

Referenced by de.mfuchs.xmlstorage.XPath.init().

    {
        int f = path.indexOf('/', idx);
        if (f == idx)
            return -1;

         // parse relative path
        int l = f!=-1? f-idx: path.length()-idx;
        String comp = path.substring(idx, idx+l);
        idx += l;

         // look for [n] and [@attr_name="attr_value"] expressions in path components
        int idxBracket = comp.indexOf('[');
        l = idxBracket!=-1? idxBracket: comp.length();
        _childName = comp.substring(0, l);

        if (idxBracket != -1) {
            String expr = XMLHelper.unescape(comp.substring(idxBracket), '[', ']');
            int i = 0;

            while(i<expr.length() && Character.isWhitespace(expr.charAt(i))) ++i;

            if (i<expr.length() && Character.isDigit(expr.charAt(i))) {
                try {
                    int n = Integer.parseInt(expr.substring(i));    // read index number
    
                    if (n != 0)
                        _childIdx = n - 1;  // convert into zero based index
                } catch(NumberFormatException e) {
                }
            } else {
                while(i<expr.length() && expr.charAt(i)=='@') {
                    int idxEqual = expr.indexOf('=', ++i);

                    if (idxEqual != -1) {
                         // look for delimiters
                        //TODO If there is neither an opening nor a closing delimiter, we should look for white space as delimiter.
                        int idxDel1 = idxEqual+1;
                        char del1 = expr.charAt(idxDel1);
                        int idxDel2 = del1=='\'' || del1=='"'? expr.indexOf(del1, idxDel1+1);

                         // read attribute name and value
                        String attrName = XMLHelper.unescape(expr.substring(i, idxEqual));
                        String attrValue = idxDel2!=-1? XMLHelper.unescape(expr.substring(idxDel1, ++idxDel2)):
                                                        XMLHelper.unescape(expr.substring(idxDel1));

                        _mAttrAndAttr.put(attrName, attrValue);

                        if (idxDel2 == -1)
                            break;

                        i = idxDel2 + 1;
                        while(i<expr.length() && Character.isWhitespace(expr.charAt(i))) ++i;

                         // handle [@attr1="value1" and @attr2="value2"] expressions
                         // handle expressions like [@name="1.2" and @profil="IPE" and @date="2006_07"]
                        if (expr.regionMatches(true, 0, "and ", i, 4))
                            i += 4;
                    }

                    while(i<expr.length() && Character.isWhitespace(expr.charAt(i))) ++i;
                }
            }
        }

        return idx;
    }

Here is the call graph for this function:


Member Data Documentation

Map<String, String> de.mfuchs.xmlstorage.XPathElement._mAttrAndAttr = new HashMap<String, String>() [package]

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