| |||||||||||||||||
| Source File | Conditionals | Statements | Methods | TOTAL | |||||||||||||
| Model\XmlModel\XmlValidateMap.cs | - | 68,8 % | 53,3 % | 61,3 % |
|
||||||||||||
| 1 | #region Copyright | |
| 2 | /* | |
| 3 | ShowX. Maps business objects into presentation layer. | |
| 4 | Copyright (C) 2005 Jesus Diaz. | |
| 5 | ||
| 6 | This library is free software; you can redistribute it and/or | |
| 7 | modify it under the terms of the GNU Lesser General Public | |
| 8 | License as published by the Free Software Foundation; either | |
| 9 | version 2.1 of the License, or (at your option) any later version. | |
| 10 | ||
| 11 | This library is distributed in the hope that it will be useful, | |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 | Lesser General Public License for more details. | |
| 15 | ||
| 16 | You should have received a copy of the GNU Lesser General Public | |
| 17 | License along with this library; if not, write to the Free Software | |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
| 19 | ||
| 20 | For project details see: http://showx.sourceforge.net | |
| 21 | */ | |
| 22 | #endregion | |
| 23 | ||
| 24 | using System; | |
| 25 | using System.Reflection; | |
| 26 | using System.Xml; | |
| 27 | ||
| 28 | namespace ShowX.Model.XmlModel | |
| 29 | { | |
| 30 | /// <summary> | |
| 31 | /// Class used to map validation to make over a certain property. | |
| 32 | /// </summary> | |
| 33 | public class XmlValidateMap : IValidationMap | |
| 34 | { | |
| 35 | #region Protected fields | |
| 36 | ||
| 37 | /// <summary> | |
| 38 | /// Name of the property associated to this mapping. | |
| 39 | /// </summary> | |
| 40 | protected PropertyInfo propertyInfo; | |
| 41 | ||
| 42 | /// <summary> | |
| 43 | /// Type of validation to do over this instance. | |
| 44 | /// </summary> | |
| 45 | ValidateFor validateType; | |
| 46 | ||
| 47 | /// <summary> | |
| 48 | /// Regular expression string to validate this instance. | |
| 49 | /// </summary> | |
| 50 | string regularExpression; | |
| 51 | ||
| 52 | /// <summary> | |
| 53 | /// Message to show if the validation fails. | |
| 54 | /// </summary> | |
| 55 | string message; | |
| 56 | ||
| 57 | #endregion | |
| 58 | ||
| 59 | 118 | private string loadValueFromAttribute(XmlAttribute attribute, |
| 60 | string defaultValue) | |
| 61 | { | |
| 62 | 118 | return (attribute == null) ? defaultValue : attribute.Value; |
| 63 | } | |
| 64 | ||
| 65 | /// <summary> | |
| 66 | /// Constructor. | |
| 67 | /// </summary> | |
| 68 | 118 | public XmlValidateMap(PropertyInfo pInfo, XmlNode validationNode) |
| 69 | { | |
| 70 | //Name attribute is mandatory | |
| 71 | 118 | this.validateType = (ValidateFor)Enum.Parse(ValidateFor.GetType(), |
| 72 | validationNode.Attributes[ | |
| 73 | XmlMappingFileConst.validationValidateForAttr].Value,true); | |
| 74 | ||
| 75 | //Message attribute is mandatory | |
| 76 | 118 | this.message = validationNode.Attributes[ |
| 77 | XmlMappingFileConst.validationMessageAttr].Value; | |
| 78 | ||
| 79 | 118 | this.regularExpression = loadValueFromAttribute( |
| 80 | validationNode.Attributes[XmlMappingFileConst.validationRegExpAttr] | |
| 81 | ,""); | |
| 82 | ||
| 83 | 118 | this.propertyInfo = pInfo; |
| 84 | } | |
| 85 | ||
| 86 | ||
| 87 | #region IValidationMap Members | |
| 88 | ||
| 89 | /// <summary> | |
| 90 | /// Reflex the validateType attribute. | |
| 91 | /// </summary> | |
| 92 | public ValidateFor ValidateFor | |
| 93 | { | |
| 94 | 156 | get { return this.validateType; } |
| 95 | 0 | set { this.validateType = value; } |
| 96 | } | |
| 97 | ||
| 98 | /// <summary> | |
| 99 | /// Reflex the message attribute. | |
| 100 | /// </summary> | |
| 101 | public string Message | |
| 102 | { | |
| 103 | 10 | get { return this.message; } |
| 104 | 0 | set { this.message = value; } |
| 105 | } | |
| 106 | ||
| 107 | /// <summary> | |
| 108 | /// Reflex the regularExpression attribute. | |
| 109 | /// </summary> | |
| 110 | public string RegularExpression | |
| 111 | { | |
| 112 | 6 | get { return this.regularExpression; } |
| 113 | 0 | set { this.regularExpression = value; } |
| 114 | } | |
| 115 | ||
| 116 | ||
| 117 | /// <summary> | |
| 118 | /// Reflex the propertyName attribute. | |
| 119 | /// </summary> | |
| 120 | 0 | public PropertyInfo PropertyInfo |
| 121 | { | |
| 122 | get { return this.propertyInfo; } | |
| 123 | set { this.propertyInfo = value; } | |
| 124 | } | |
| 125 | ||
| 126 | /// <summary> | |
| 127 | /// Type in wich this validation is attached. | |
| 128 | /// </summary> | |
| 129 | public Type Type | |
| 130 | { | |
| 131 | 50 | get { return propertyInfo.DeclaringType; } |
| 132 | 0 | set {} |
| 133 | } | |
| 134 | ||
| 135 | /// <summary> | |
| 136 | /// Name of the property in wich this validation is attached. | |
| 137 | /// </summary> | |
| 138 | public string PropertyName | |
| 139 | { | |
| 140 | 46 | get { return propertyInfo.Name; } |
| 141 | 0 | set {} |
| 142 | } | |
| 143 | ||
| 144 | /// <summary> | |
| 145 | /// Validate the input value, over the kind of validation defined here. The | |
| 146 | /// value is input as a string. | |
| 147 | /// </summary> | |
| 148 | /// <param name="context">Context in which occurs the operation (supplied by | |
| 149 | /// user via control properties)</param> | |
| 150 | /// <param name="s">Value to validate.</param> | |
| 151 | /// <returns>True if the value fulfills the requeriments of this validator. | |
| 152 | /// False otherwise.</returns> | |
| 153 | 38 | public bool Validate(object context, string s) |
| 154 | { | |
| 155 | 38 | return new InternalPropertyValidator().Validate(this,context,s); |
| 156 | } | |
| 157 | ||
| 158 | #endregion | |
| 159 | } | |
| 160 | } |
|
||||||||