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

XMLBool.cs

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

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