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

XMLPos.cs

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.5
00004 //
00005 // XMLPos.cs
00006 //
00007 // Copyright (c) 2010 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.Collections.Generic;
00040 
00041 namespace XMLStorage
00042 {
00043 
00044 /**
00045  * XMLPos is the iterator for XML trees in XMLStorage.
00046  * @author Martin Fuchs
00047  */
00048 public class XMLPos
00049 {
00050     /**
00051      * Construct an XMLPos, which points to the given XMLNode. 
00052      * @param root root node
00053      */
00054     public XMLPos(XMLNode root)
00055     {
00056         _root = root;
00057         _cur = root;
00058     }
00059 
00060     /**
00061      * Construct an XMLPos as copy of another.
00062      * @param other XMLPos original
00063      */
00064     public XMLPos(XMLPos other)
00065     {
00066         _root = other._root;
00067         _cur = other._cur;
00068         // don't copy _stack
00069     }
00070 
00071     /**
00072      * Create XML tree iterator positioned at subnode node_name of current other position.
00073      * @param other position to be copied
00074      * @param nodeName new node name
00075      */
00076     public XMLPos(XMLPos other, string nodeName)
00077     {
00078         _root = other._root;
00079         _cur = other._cur;
00080         // don't copy _stack
00081         smartCreate(nodeName);
00082     }
00083 
00084     /**
00085      * Create XML tree iterator positioned at subnode node_name
00086      * with attribute attrName and attribute value attrValue.
00087      * @see XMLPos.smart_create
00088      * @param other position to be copied
00089      * @param nodeName new node name
00090      * @param attrName attribute name
00091      * @param attrValue attribute value
00092      */
00093     public XMLPos(XMLPos other, string nodeName, string attrName, string attrValue)
00094     {
00095         _root = other._root;
00096         _cur = other._cur;
00097         // don't copy _stack
00098         smartCreate(nodeName, attrName, attrValue);
00099     }
00100 
00101     /**
00102      * Return the current node.
00103      * @return current position
00104      */
00105     public XMLNode cur()
00106     {
00107         return _cur;
00108     }
00109 
00110     /**
00111      * Return the value of an attribute in the current node or "".
00112      * @param attrName attribute name
00113      * @return attribute value
00114      */
00115     public string get(string attrName)
00116     {
00117         return _cur.get(attrName);
00118     }
00119 
00120     /**
00121      * Return the value of an attribute in the current node or a default value.
00122      * @param attrName attribute name
00123      * @param def default value
00124      * @return value
00125      */
00126     public string get(string attrName, string def)
00127     {
00128         return _cur.get(attrName, def);
00129     }
00130 
00131     /**
00132      * Set an attribute value.
00133      * @param attrName attribute name
00134      * @param value new attribute value
00135      */
00136     public void put(string attrName, string value)
00137     {
00138         _cur.put(attrName, value);
00139     }
00140 
00141     /**
00142      * Insert children when building tree.
00143      * @param child new child
00144      */
00145     public void addDown(XMLNode child)
00146     {
00147         _cur.addChild(child);
00148         goTo(child);
00149     }
00150 
00151     /**
00152      * Go back to previous position.
00153      * @return true if successfull
00154      */
00155     public bool back()
00156     {
00157         if (_stack.Count > 0) {
00158             _cur = _stack.Pop();
00159             return true;
00160         } else
00161             return false;
00162     }
00163 
00164     /**
00165      * Go down to first child.
00166      * @return true if successfull
00167      */
00168     public bool goDown()
00169     {
00170         XMLNode node = _cur.getFirstChild();
00171 
00172         if (node != null) {
00173             goTo(node);
00174             return true;
00175         } else
00176             return false;
00177     }
00178 
00179     /**
00180      * Search for child and go down.
00181      * @param child_name node name
00182      * @return true if successfull
00183      */
00184     public bool goDown(string child_name)
00185     {
00186         return goDown(child_name, 0);
00187     }
00188 
00189     /**
00190      * Search for child and go down.
00191      * @param child_name node name
00192      * @param child_idx child index
00193      * @return true if successfull
00194      */
00195     public bool goDown(string child_name, int child_idx)
00196     {
00197         XMLNode node = new XPathElement(child_name, child_idx).find(_cur);
00198 
00199         if (node != null) {
00200             goTo(node);
00201             return true;
00202         } else
00203             return false;
00204     }
00205 
00206     /**
00207      * Move X-Path like to position in XML tree.
00208      * @param path XPath expression
00209      * @return true if successfull
00210      */
00211     public bool go(string path)
00212     {
00213         return go(new XPath(path));
00214     }
00215 
00216     /**
00217      * Move X-Path like to position in XML tree.
00218      * @param path XPath objhect
00219      * @return true if successfull
00220      */
00221     public bool go(XPath xpath)
00222     {
00223         XMLNode node = xpath._absolute ? _root : _cur;
00224 
00225         node = node.findRelative(xpath);
00226 
00227         if (node != null) {
00228             goTo(node);
00229             return true;
00230         } else
00231             return false;
00232     }
00233 
00234     /**
00235      * Create node and move to it.
00236      * @param name name of the new node
00237      */
00238     public void create(string name)
00239     {
00240         addDown(new XMLNode(name));
00241     }
00242 
00243     /**
00244      * Create node if not already existing and move to it.
00245      * @param child_name name of the new node
00246      */
00247     public void smartCreate(string child_name)
00248     {
00249         XMLNode node = new XPathElement(child_name).find(_cur);
00250 
00251         if (node != null)
00252             goTo(node);
00253         else
00254             addDown(new XMLNode(child_name));
00255     }
00256 
00257     /**
00258      * Search matching child node identified by key name and an attribute value.
00259      * It it doesn't exist, create it.
00260      * @param name name of the new node
00261      * @param attrName attribute name
00262      * @param attrValue attribute value
00263      */
00264     public void smartCreate(string name, string attrName, string attrValue)
00265     {
00266         XMLNode node = new XPathElement(name, 0, attrName, attrValue).find(_cur);
00267 
00268         if (node != null)
00269             goTo(node);
00270         else {
00271             node = new XMLNode(name);
00272             addDown(node);
00273             node.put(attrName, attrValue);
00274         }
00275     }
00276 
00277     /**
00278      * @return content string
00279      */
00280     public string getContent()
00281     {
00282         return _cur.getContent();
00283     }
00284 
00285     /**
00286      * @param s new content string
00287      */
00288     public void setContent(string s)
00289     {
00290         setContent(s, false);
00291     }
00292 
00293     /**
00294      * @param s new content string
00295      * @param cdata write in CDATA format if true
00296      */
00297     public void setContent(string s, bool cdata)
00298     {
00299         _cur.setContent(s, cdata);
00300     }
00301 
00302     /**
00303      * Return the name oif the current node.
00304      */
00305     public override string ToString()
00306     {
00307         return _cur.ToString();
00308     }
00309 
00310     protected XMLNode _root;
00311     protected XMLNode _cur;
00312     protected Stack<XMLNode> _stack = new Stack<XMLNode>();
00313 
00314     /**
00315      * Go to the specified node.
00316      * @param child new position
00317      */
00318     protected void goTo(XMLNode child)
00319     {
00320         _stack.Push(_cur);
00321         _cur = child;
00322     }
00323 
00324     /**
00325      * Set an attribute in a subnode.
00326      * @param nodeName node name
00327      * @param attrName attribute name
00328      * @param value attribute value
00329      */
00330     public void putSubattrib(string nodeName, string attrName, string value)
00331     {
00332         _cur.putSubattrib(nodeName, attrName, value);
00333     }
00334 }
00335 
00336 } // namespace XMLStorage

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