/Misc_Microsoft_MPL_Libs/XObjects/Src/CompiledFacets.cs

http://github.com/o2platform/O2.Platform.Projects · C# · 270 lines · 257 code · 12 blank · 1 comment · 63 complexity · dab3a99f8efa4d04ac0c9e3327ff507a MD5 · raw file

  1. //Copyright (c) Microsoft Corporation. All rights reserved.
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using System.Text;
  6. using System.Xml;
  7. using System.Xml.Schema;
  8. using System.Reflection;
  9. using Xml.Schema.Linq.CodeGen;
  10. namespace Xml.Schema.Linq
  11. {
  12. internal class CompiledFacets
  13. {
  14. RestrictionFlags flags;
  15. int length;
  16. int minLength;
  17. int maxLength;
  18. object maxInclusive;
  19. object maxExclusive;
  20. object minInclusive;
  21. object minExclusive;
  22. int totalDigits;
  23. int fractionDigits;
  24. ArrayList patterns;
  25. ArrayList enumerations;
  26. XmlSchemaWhiteSpace whiteSpace;
  27. public CompiledFacets(XmlSchemaDatatype dt)
  28. {
  29. whiteSpace = dt.GetBuiltInWSFacet();
  30. }
  31. public int Length
  32. {
  33. get
  34. {
  35. return length;
  36. }
  37. }
  38. public int MinLength
  39. {
  40. get
  41. {
  42. return minLength;
  43. }
  44. }
  45. public int MaxLength
  46. {
  47. get
  48. {
  49. return maxLength;
  50. }
  51. }
  52. public ArrayList Patterns
  53. {
  54. get
  55. {
  56. return patterns;
  57. }
  58. }
  59. public ArrayList Enumeration
  60. {
  61. get
  62. {
  63. return enumerations;
  64. }
  65. }
  66. public XmlSchemaWhiteSpace WhiteSpace
  67. {
  68. get
  69. {
  70. return whiteSpace;
  71. }
  72. }
  73. public object MaxInclusive
  74. {
  75. get
  76. {
  77. return maxInclusive;
  78. }
  79. }
  80. public object MaxExclusive
  81. {
  82. get
  83. {
  84. return maxExclusive;
  85. }
  86. }
  87. public object MinInclusive
  88. {
  89. get
  90. {
  91. return minInclusive;
  92. }
  93. }
  94. public object MinExclusive
  95. {
  96. get
  97. {
  98. return minExclusive;
  99. }
  100. }
  101. public int TotalDigits
  102. {
  103. get
  104. {
  105. return totalDigits;
  106. }
  107. }
  108. public int FractionDigits
  109. {
  110. get
  111. {
  112. return fractionDigits;
  113. }
  114. }
  115. public RestrictionFlags Flags
  116. {
  117. get
  118. {
  119. return flags;
  120. }
  121. }
  122. public void compileFacets(XmlSchemaSimpleType simpleType)
  123. {
  124. XmlSchemaSimpleType type = simpleType;
  125. XmlSchemaSimpleType enumSimpleType = null; //simpletype that has most restricted enums.
  126. flags = 0;
  127. while (type != null && !String.Equals(type.QualifiedName.Namespace, Constants.XSD, StringComparison.Ordinal))
  128. {
  129. XmlSchemaSimpleTypeRestriction simpleTypeRestriction = type.Content as XmlSchemaSimpleTypeRestriction;
  130. if (simpleTypeRestriction != null)
  131. {
  132. foreach (XmlSchemaFacet facet in simpleTypeRestriction.Facets)
  133. {
  134. if (facet is XmlSchemaMinLengthFacet)
  135. {
  136. if ((flags & RestrictionFlags.MinLength) == 0)
  137. {
  138. minLength = XmlConvert.ToInt32(facet.Value);
  139. flags |= RestrictionFlags.MinLength;
  140. }
  141. }
  142. else if (facet is XmlSchemaMaxLengthFacet)
  143. {
  144. if ((flags & RestrictionFlags.MaxLength) == 0)
  145. {
  146. maxLength = XmlConvert.ToInt32(facet.Value);
  147. flags |= RestrictionFlags.MaxLength;
  148. }
  149. }
  150. else if (facet is XmlSchemaLengthFacet)
  151. {
  152. if ((flags & RestrictionFlags.Length) == 0)
  153. {
  154. length = XmlConvert.ToInt32(facet.Value);
  155. flags |= RestrictionFlags.Length;
  156. }
  157. }
  158. else if (facet is XmlSchemaEnumerationFacet)
  159. {
  160. if (enumSimpleType == null)
  161. {
  162. enumerations = new ArrayList();
  163. flags |= RestrictionFlags.Enumeration;
  164. enumSimpleType = type;
  165. }
  166. else if (enumSimpleType != type)
  167. {
  168. continue;
  169. }
  170. enumerations.Add(type.BaseXmlSchemaType.Datatype.ParseValue(facet.Value, null, null));
  171. }
  172. else if (facet is XmlSchemaPatternFacet)
  173. {
  174. if (patterns == null)
  175. {
  176. patterns = new ArrayList();
  177. flags |= RestrictionFlags.Pattern;
  178. }
  179. patterns.Add(facet.Value);
  180. }
  181. else if (facet is XmlSchemaMaxInclusiveFacet)
  182. {
  183. if ((flags & RestrictionFlags.MaxInclusive) == 0)
  184. {
  185. maxInclusive = type.BaseXmlSchemaType.Datatype.ParseValue(facet.Value, null, null);
  186. flags |= RestrictionFlags.MaxInclusive;
  187. }
  188. }
  189. else if (facet is XmlSchemaMaxExclusiveFacet)
  190. {
  191. if ((flags & RestrictionFlags.MaxExclusive) == 0)
  192. {
  193. maxExclusive = type.BaseXmlSchemaType.Datatype.ParseValue(facet.Value, null, null);
  194. flags |= RestrictionFlags.MaxExclusive;
  195. }
  196. }
  197. else if (facet is XmlSchemaMinExclusiveFacet)
  198. {
  199. if ((flags & RestrictionFlags.MinExclusive) == 0)
  200. {
  201. minExclusive = type.BaseXmlSchemaType.Datatype.ParseValue(facet.Value, null, null);
  202. flags |= RestrictionFlags.MinExclusive;
  203. }
  204. }
  205. else if (facet is XmlSchemaMinInclusiveFacet)
  206. {
  207. if ((flags & RestrictionFlags.MinInclusive) == 0)
  208. {
  209. minInclusive = type.BaseXmlSchemaType.Datatype.ParseValue(facet.Value, null, null);
  210. flags |= RestrictionFlags.MinInclusive;
  211. }
  212. }
  213. else if (facet is XmlSchemaFractionDigitsFacet)
  214. {
  215. if ((flags & RestrictionFlags.FractionDigits) == 0)
  216. {
  217. fractionDigits = XmlConvert.ToInt32(facet.Value);
  218. flags |= RestrictionFlags.FractionDigits;
  219. }
  220. }
  221. else if (facet is XmlSchemaTotalDigitsFacet)
  222. {
  223. if ((flags & RestrictionFlags.TotalDigits) == 0)
  224. {
  225. totalDigits = XmlConvert.ToInt32(facet.Value);
  226. flags |= RestrictionFlags.TotalDigits;
  227. }
  228. }
  229. else if (facet is XmlSchemaWhiteSpaceFacet)
  230. {
  231. if ((flags & RestrictionFlags.WhiteSpace) == 0)
  232. {
  233. if (facet.Value == "preserve")
  234. {
  235. whiteSpace = XmlSchemaWhiteSpace.Preserve;
  236. }
  237. else if (facet.Value == "replace")
  238. {
  239. whiteSpace = XmlSchemaWhiteSpace.Replace;
  240. }
  241. else if (facet.Value == "collapse")
  242. {
  243. whiteSpace = XmlSchemaWhiteSpace.Collapse;
  244. }
  245. flags |= RestrictionFlags.WhiteSpace;
  246. }
  247. }
  248. else
  249. {
  250. continue;
  251. }
  252. }
  253. }
  254. type = type.BaseXmlSchemaType as XmlSchemaSimpleType;
  255. }
  256. }
  257. }
  258. }