PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.XML/System.Xml.Serialization/XmlSchemas.cs

https://bitbucket.org/danipen/mono
C# | 272 lines | 195 code | 42 blank | 35 comment | 55 complexity | ea5156d5964bea6deb2d59b6aa9369f3 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Xml.Serialization.XmlSchemas
  3. //
  4. // Author:
  5. // Tim Coleman (tim@timcoleman.com)
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. #if NET_2_0
  31. using System.Collections.Generic;
  32. #endif
  33. using System.Xml.Schema;
  34. namespace System.Xml.Serialization
  35. {
  36. #if NET_2_0
  37. public class XmlSchemas : CollectionBase, IEnumerable<XmlSchema>
  38. #else
  39. public class XmlSchemas : CollectionBase
  40. #endif
  41. {
  42. #region Fields
  43. private static string msdataNS = "urn:schemas-microsoft-com:xml-msdata";
  44. Hashtable table = new Hashtable ();
  45. #endregion
  46. #region Constructors
  47. public XmlSchemas ()
  48. {
  49. }
  50. #endregion // Constructors
  51. #region Properties
  52. public XmlSchema this [int index] {
  53. get {
  54. if (index < 0 || index > Count)
  55. throw new ArgumentOutOfRangeException ();
  56. return (XmlSchema) List [index];
  57. }
  58. set { List [index] = value; }
  59. }
  60. public XmlSchema this [string ns] {
  61. get { return (XmlSchema) table[ns!=null?ns:""]; }
  62. }
  63. #if NET_2_0
  64. [MonoTODO]
  65. public bool IsCompiled
  66. {
  67. get { throw new NotImplementedException (); }
  68. }
  69. [MonoTODO]
  70. public void Compile (ValidationEventHandler handler, bool fullCompile)
  71. {
  72. foreach (XmlSchema xs in this)
  73. if (fullCompile || !xs.IsCompiled)
  74. xs.Compile (handler);
  75. }
  76. #endif
  77. #endregion // Properties
  78. #region Methods
  79. public int Add (XmlSchema schema)
  80. {
  81. Insert (Count, schema);
  82. return (Count - 1);
  83. }
  84. public void Add (XmlSchemas schemas)
  85. {
  86. foreach (XmlSchema schema in schemas)
  87. Add (schema);
  88. }
  89. #if NET_2_0
  90. [MonoNotSupported("")]
  91. public int Add (XmlSchema schema, Uri baseUri)
  92. {
  93. throw new NotImplementedException ();
  94. }
  95. [MonoNotSupported("")]
  96. public void AddReference (XmlSchema schema)
  97. {
  98. throw new NotImplementedException ();
  99. }
  100. #endif
  101. public bool Contains (XmlSchema schema)
  102. {
  103. return List.Contains (schema);
  104. }
  105. #if NET_2_0
  106. [MonoNotSupported("")]
  107. public bool Contains (string targetNamespace)
  108. {
  109. throw new NotImplementedException ();
  110. }
  111. #endif
  112. public void CopyTo (XmlSchema[] array, int index)
  113. {
  114. List.CopyTo (array, index);
  115. }
  116. public object Find (XmlQualifiedName name, Type type)
  117. {
  118. XmlSchema schema = table [name.Namespace] as XmlSchema;
  119. if (schema == null)
  120. {
  121. // An schema may import other schemas. An imported schema would
  122. // not be in the table, but its elements (although from another
  123. // namespace) would be in the schema that imported it. So, we
  124. // need know to check for every schema in the table.
  125. foreach (XmlSchema s in this)
  126. {
  127. object ob = Find (s, name, type);
  128. if (ob != null) return ob;
  129. }
  130. return null;
  131. }
  132. else {
  133. object fschema = Find (schema, name, type);
  134. #if NET_2_0
  135. if (fschema == null) {
  136. // still didn't find it
  137. // (possibly table[name.Namespace] was overwritten in table due to duplicate "" keys),
  138. // so look in all schemas (for consistiency with MS behaviour)
  139. foreach (XmlSchema s in this) {
  140. object ob = Find (s, name, type);
  141. if (ob != null) return ob;
  142. }
  143. }
  144. #endif
  145. return fschema;
  146. }
  147. }
  148. object Find (XmlSchema schema, XmlQualifiedName name, Type type)
  149. {
  150. if (!schema.IsCompiled) {
  151. schema.Compile (null);
  152. }
  153. XmlSchemaObjectTable tbl = null;
  154. if (type == typeof (XmlSchemaSimpleType) || type == typeof (XmlSchemaComplexType))
  155. tbl = schema.SchemaTypes;
  156. else if (type == typeof (XmlSchemaAttribute))
  157. tbl = schema.Attributes;
  158. else if (type == typeof (XmlSchemaAttributeGroup))
  159. tbl = schema.AttributeGroups;
  160. else if (type == typeof (XmlSchemaElement))
  161. tbl = schema.Elements;
  162. else if (type == typeof (XmlSchemaGroup))
  163. tbl = schema.Groups;
  164. else if (type == typeof (XmlSchemaNotation))
  165. tbl = schema.Notations;
  166. object res = (tbl != null) ? tbl [name] : null;
  167. if (res != null && res.GetType () != type) return null;
  168. else return res;
  169. }
  170. #if NET_2_0
  171. [MonoNotSupported("")]
  172. public IList GetSchemas (string ns)
  173. {
  174. throw new NotImplementedException ();
  175. }
  176. #endif
  177. public int IndexOf (XmlSchema schema)
  178. {
  179. return List.IndexOf (schema);
  180. }
  181. public void Insert (int index, XmlSchema schema)
  182. {
  183. List.Insert (index, schema);
  184. }
  185. public static bool IsDataSet (XmlSchema schema)
  186. {
  187. XmlSchemaElement el = schema.Items.Count == 1 ?
  188. schema.Items [0] as XmlSchemaElement : null;
  189. if (el != null && el.UnhandledAttributes != null && el.UnhandledAttributes.Length > 0) {
  190. for (int i = 0; i < el.UnhandledAttributes.Length; i++) {
  191. XmlAttribute attr = el.UnhandledAttributes [i];
  192. if (attr.NamespaceURI == msdataNS && attr.LocalName == "IsDataSet")
  193. return (attr.Value.ToLower (System.Globalization.CultureInfo.InvariantCulture) == "true");
  194. }
  195. }
  196. return false;
  197. }
  198. protected override void OnClear ()
  199. {
  200. table.Clear ();
  201. }
  202. protected override void OnInsert (int index, object value)
  203. {
  204. string ns = ((XmlSchema) value).TargetNamespace;
  205. if (ns == null) ns = "";
  206. table [ns] = value;
  207. }
  208. protected override void OnRemove (int index, object value)
  209. {
  210. table.Remove (value);
  211. }
  212. protected override void OnSet (int index, object oldValue, object newValue)
  213. {
  214. string ns = ((XmlSchema) oldValue).TargetNamespace;
  215. if (ns == null) ns = "";
  216. table [ns] = newValue;
  217. }
  218. public void Remove (XmlSchema schema)
  219. {
  220. List.Remove (schema);
  221. }
  222. #if NET_2_0
  223. IEnumerator<XmlSchema> IEnumerable<XmlSchema>.GetEnumerator ()
  224. {
  225. return new XmlSchemaEnumerator (this);
  226. }
  227. #endif
  228. #endregion // Methods
  229. }
  230. }