• Main Page
  • Namespaces
  • Classes
  • Files
  • Directories
  • File List

XPathElement.cs

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.5
00004 //
00005 // XPathElement.cs
00006 //
00007 // Copyright (c) 2010, 1014 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 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     // map to handle AND conditions, maps attribute names to its value
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          // parse relative path
00085         int l = f!=-1? f-idx: path.Length-idx;
00086         string comp = path.Substring(idx, l);
00087         idx += l;
00088 
00089          // look for [n] and [@attr_name="attr_value"] expressions in path components
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))    // read index number
00104                     if (n != 0)
00105                         _childIdx = n - 1;  // convert into zero based index
00106             } else {
00107                 while(i<expr.Length && expr[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[idxDel1];
00115                         int idxDel2 = del1=='\'' || del1=='"'? expr.IndexOf(del1, idxDel1+1): -1;
00116 
00117                          // read attribute name and value
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                          // handle [@attr1="value1" and @attr2="value2"] expressions
00131                          // handle expressions like [@name="1.2" and @profil="IPE" and @date="2006_07"]
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};    // index in array for call-by-reference matches()
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 != "*")  // use asterisk as wildcard
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 } // namespace XMLStorage

Generated on Tue Oct 28 2014 00:05:07 for XMLStorage/C# by  doxygen 1.7.2