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 using System;
00040 using System.IO;
00041 using System.Collections.Generic;
00042
00043 namespace XMLStorage {
00044
00045 public class XPathElement
00046 {
00047 public XPathElement()
00048 {
00049 _childIdx = -1;
00050 }
00051
00052 public XPathElement(string child_name)
00053 {
00054 _childName = child_name;
00055 _childIdx = -1;
00056 }
00057
00058 public XPathElement(string child_name, int child_idx)
00059 {
00060 _childName = child_name;
00061 _childIdx = child_idx;
00062 }
00063
00064 public XPathElement(string child_name, int child_idx, string attr_name, string attr_value)
00065 {
00066 _childName = child_name;
00067 _childIdx = child_idx;
00068
00069 _mAttrAndAttr.Add(attr_name, attr_value);
00070 }
00071
00072 public string _childName;
00073 private int _childIdx;
00074
00075
00076 public Dictionary<string, string> _mAttrAndAttr = new Dictionary<string, string>();
00077
00078 public int parse(string path, int idx)
00079 {
00080 int f = path.IndexOf('/', idx);
00081 if (f == idx)
00082 return -1;
00083
00084
00085 int l = f!=-1? f-idx: path.Length-idx;
00086 string comp = path.Substring(idx, l);
00087 idx += l;
00088
00089
00090 int idxBracket = comp.IndexOf('[');
00091 l = idxBracket!=-1? idxBracket: comp.Length;
00092 _childName = comp.Substring(0, l);
00093
00094 if (idxBracket != -1) {
00095 string expr = XMLHelper.unescape(comp.Substring(idxBracket), '[', ']');
00096 int i = 0;
00097
00098 while(i<expr.Length && char.IsWhiteSpace(expr[i])) ++i;
00099
00100 if (i<expr.Length && char.IsDigit(expr[i])) {
00101 int n;
00102
00103 if (Int32.TryParse(expr.Substring(i), out n))
00104 if (n != 0)
00105 _childIdx = n - 1;
00106 } else {
00107 while(i<expr.Length && expr[i]=='@') {
00108 int idxEqual = expr.IndexOf('=', ++i);
00109
00110 if (idxEqual != -1) {
00111
00112
00113 int idxDel1 = idxEqual + 1;
00114 char del1 = expr[idxDel1];
00115 int idxDel2 = del1=='\'' || del1=='"'? expr.IndexOf(del1, idxDel1+1): -1;
00116
00117
00118 string attrName = XMLHelper.unescape(expr.Substring(i, idxEqual-i));
00119 string attrValue = idxDel2!=-1? XMLHelper.unescape(expr.Substring(idxDel1, ++idxDel2-idxDel1)):
00120 XMLHelper.unescape(expr.Substring(idxDel1));
00121
00122 _mAttrAndAttr.Add(attrName, attrValue);
00123
00124 if (idxDel2 == -1)
00125 break;
00126
00127 i = idxDel2 + 1;
00128 while(i<expr.Length && char.IsWhiteSpace(expr[i])) ++i;
00129
00130
00131
00132 if (string.Compare(expr, 0, "and ", 0, 4, true) == 0)
00133 i += 4;
00134 }
00135
00136 while(i<expr.Length && char.IsWhiteSpace(expr[i])) ++i;
00137 }
00138 }
00139 }
00140
00141 return idx;
00142 }
00143
00144 public XMLNode find(XMLNode node)
00145 {
00146 int[] n = new int[] {0};
00147
00148 foreach(XMLNode it in node.getChildren())
00149 if (matches(it, n))
00150 return it;
00151
00152 return null;
00153 }
00154
00155 public bool matches(AbstractXMLNode node, int[] n)
00156 {
00157 if (node.ToString() != _childName)
00158 if (_childName != "*")
00159 return false;
00160
00161 foreach(KeyValuePair<string, string> it in _mAttrAndAttr)
00162 if (node.get(it.Key) != it.Value)
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 }
00173
00174 }