XPathElement.java

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.5
00004 //
00005 // XPathElement.java
00006 //
00007 // Copyright (c) 2010, 2014 Martin Fuchs <martin-fuchs@gmx.net>
00008 //
00009 
00010 
00011 /*
00012 
00013  All rights reserved.
00014 
00015  Redistribution and use in source and binary forms, with or without
00016  modification, are permitted provided that the following conditions are met:
00017 
00018  * Redistributions of source code must retain the above copyright
00019     notice, this list of conditions and the following disclaimer.
00020  * Redistributions in binary form must reproduce the above copyright
00021     notice, this list of conditions and the following disclaimer in
00022     the documentation and/or other materials provided with the
00023     distribution.
00024 
00025  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00029  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00030  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00031  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00034  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  POSSIBILITY OF SUCH DAMAGE.
00036 
00037 */
00038 
00039 package de.mfuchs.xmlstorage;
00040 
00041 import java.util.HashMap;
00042 import java.util.Map;
00043 
00044 public class XPathElement
00045 {
00046     public XPathElement()
00047     {
00048         _childIdx = -1;
00049     }
00050 
00051     public XPathElement(final String child_name)
00052     {
00053         _childName = child_name;
00054         _childIdx = -1;
00055     }
00056 
00057     public XPathElement(final String child_name, int child_idx)
00058     {
00059         _childName = child_name;
00060         _childIdx = child_idx;
00061     }
00062 
00063     public XPathElement(final String child_name, int child_idx, final String attr_name, final String attr_value)
00064     {
00065         this(child_name, child_idx);
00066 
00067         _mAttrAndAttr.put(attr_name, attr_value);
00068     }
00069 
00070     public String   _childName;
00071     private int     _childIdx;
00072 
00073     // map to handle AND conditions, maps attribute names to its value
00074     Map<String, String> _mAttrAndAttr = new HashMap<String, String>();
00075 
00076     int parse(final String path, int idx)
00077     {
00078         int f = path.indexOf('/', idx);
00079         if (f == idx)
00080             return -1;
00081 
00082          // parse relative path
00083         int l = f!=-1? f-idx: path.length()-idx;
00084         String comp = path.substring(idx, idx+l);
00085         idx += l;
00086 
00087          // look for [n] and [@attr_name="attr_value"] expressions in path components
00088         int idxBracket = comp.indexOf('[');
00089         l = idxBracket!=-1? idxBracket: comp.length();
00090         _childName = comp.substring(0, l);
00091 
00092         if (idxBracket != -1) {
00093             String expr = XMLHelper.unescape(comp.substring(idxBracket), '[', ']');
00094             int i = 0;
00095 
00096             while(i<expr.length() && Character.isWhitespace(expr.charAt(i))) ++i;
00097 
00098             if (i<expr.length() && Character.isDigit(expr.charAt(i))) {
00099                 try {
00100                     int n = Integer.parseInt(expr.substring(i));    // read index number
00101     
00102                     if (n != 0)
00103                         _childIdx = n - 1;  // convert into zero based index
00104                 } catch(NumberFormatException e) {
00105                 }
00106             } else {
00107                 while(i<expr.length() && expr.charAt(i)=='@') {
00108                     int idxEqual = expr.indexOf('=', ++i);
00109 
00110                     if (idxEqual != -1) {
00111                          // look for delimiters
00112                         //TODO If there is neither an opening nor a closing delimiter, we should look for white space as delimiter.
00113                         int idxDel1 = idxEqual+1;
00114                         char del1 = expr.charAt(idxDel1);
00115                         int idxDel2 = del1=='\'' || del1=='"'? expr.indexOf(del1, idxDel1+1);
00116 
00117                          // read attribute name and value
00118                         String attrName = XMLHelper.unescape(expr.substring(i, idxEqual));
00119                         String attrValue = idxDel2!=-1? XMLHelper.unescape(expr.substring(idxDel1, ++idxDel2)):
00120                                                         XMLHelper.unescape(expr.substring(idxDel1));
00121 
00122                         _mAttrAndAttr.put(attrName, attrValue);
00123 
00124                         if (idxDel2 == -1)
00125                             break;
00126 
00127                         i = idxDel2 + 1;
00128                         while(i<expr.length() && Character.isWhitespace(expr.charAt(i))) ++i;
00129 
00130                          // handle [@attr1="value1" and @attr2="value2"] expressions
00131                          // handle expressions like [@name="1.2" and @profil="IPE" and @date="2006_07"]
00132                         if (expr.regionMatches(true, 0, "and ", i, 4))
00133                             i += 4;
00134                     }
00135 
00136                     while(i<expr.length() && Character.isWhitespace(expr.charAt(i))) ++i;
00137                 }
00138             }
00139         }
00140 
00141         return idx;
00142     }
00143 
00144     public XMLNode find(final XMLNode node)
00145     {
00146         int[] n = new int[] {0};    // index in array for call-by-reference matches()
00147 
00148         for(XMLNode it : node._children)
00149             if (matches(it, n))
00150                 return it;
00151 
00152         return null;
00153     }
00154 
00155     public boolean matches(final AbstractXMLNode node, int[] n)
00156     {
00157         if (!node.toString().equals(_childName))
00158             if (!_childName.equals("*"))    // use asterisk as wildcard
00159                 return false;
00160 
00161         for(Map.Entry<String, String> it : _mAttrAndAttr.entrySet())
00162             if (!node.get(it.getKey()).equals(it.getValue()))
00163                 return false;
00164 
00165         if (_childIdx == -1)
00166             return true;
00167         else if (n[0]++ == _childIdx)
00168             return true;
00169         else
00170             return false;
00171     }
00172 }