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

XMLDecimal.cs

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

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