Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
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
00083 int l = f!=-1? f-idx: path.length()-idx;
00084 String comp = path.substring(idx, idx+l);
00085 idx += l;
00086
00087
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));
00101
00102 if (n != 0)
00103 _childIdx = n - 1;
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
00112
00113 int idxDel1 = idxEqual+1;
00114 char del1 = expr.charAt(idxDel1);
00115 int idxDel2 = del1=='\'' || del1=='"'? expr.indexOf(del1, idxDel1+1);
00116
00117
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
00131
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};
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("*"))
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 }