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

XMLString.cs

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.5
00004 //
00005 // XMLString.cs
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 
00040 namespace XMLStorage
00041 {
00042 
00043 /**
00044  * XMLString is used convenient attribute data access in XMLNodes.
00045  */
00046 public class XMLString
00047 {
00048     public XMLString(string value)
00049     {
00050         _value = value;
00051     }
00052 
00053     public XMLString(string value, string def/*=""*/)
00054     {
00055         if (value!=null && value.Length>0)
00056             _value = value;
00057         else
00058             _value = def;
00059     }
00060 
00061     public XMLString(AbstractXMLNode node, string attrName)
00062     {
00063         init(node, attrName, "");
00064     }
00065 
00066     public XMLString(XMLPos pos, string attrName)
00067     {
00068         init(pos.cur(), attrName, "");
00069     }
00070 
00071     public XMLString(XMLPos pos, string attrName, string def/*=""*/)
00072     {
00073         init(pos.cur(), attrName, def);
00074     }
00075 
00076     public XMLString(XMLStreamReader reader, string attrName)
00077     {
00078         init(reader.getNode(), attrName, "");
00079     }
00080 
00081     public XMLString(XMLStreamReader reader, string attrName, string def)
00082     {
00083         init(reader.getNode(), attrName, def);
00084     }
00085 
00086     public XMLString(AbstractXMLNode node, string attrName, string def/*=""*/)
00087     {
00088         init(node, attrName, def);
00089     }
00090 
00091     private void init(AbstractXMLNode node, string attrName, string def)
00092     {
00093         string value = node.get(attrName);
00094 
00095         if (value.Length > 0)
00096             _value = value;
00097         else
00098             _value = def;
00099     }
00100 
00101 
00102     public override string ToString()
00103     {
00104         return _value;
00105     }
00106 
00107     protected string _value;
00108 }
00109 
00110 
00111 /**
00112  * XMLStringRef is used to provide write access to XML attribute values.
00113  */
00114 public class XMLStringRef
00115 {
00116     public XMLStringRef(XMLNode node, string attrName)
00117     {
00118         init(node, attrName, "");
00119     }
00120 
00121     public XMLStringRef(XMLPos pos, string attrName)
00122     {
00123         init(pos.cur(), attrName, "");
00124     }
00125 
00126     public XMLStringRef(XMLNode node, string attrName, string def/*=""*/)
00127     {
00128         init(node, attrName, def);
00129     }
00130 
00131     public XMLStringRef(XMLPos pos, string attrName, string def/*=""*/)
00132     {
00133         init(pos.cur(), attrName, def);
00134     }
00135 
00136     private void init(XMLNode node, string attrName, string def)
00137     {
00138         _node = node;
00139         _attrName = attrName;
00140 
00141         if (node != null)
00142             if (node.get(attrName).Length == 0)
00143                 assign(def);
00144     }
00145 
00146     public override string ToString()
00147     {
00148         if (_node != null)
00149             return _node.get(_attrName);
00150         else
00151             return "";
00152     }
00153 
00154     public void assign(string value)
00155     {
00156         if (_node != null)
00157             _node.put(_attrName, value);
00158     }
00159 
00160     protected XMLNode _node;
00161     protected string _attrName;
00162 }
00163 
00164 } // namespace XMLStorage

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