2005-08-16 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaImport.cs
1 // Author: Dwivedi, Ajay kumar\r
2 //            Adwiv@Yahoo.com\r
3
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 using System;\r
25 using System.Xml;\r
26 using System.Xml.Serialization;\r
27 \r
28 namespace System.Xml.Schema\r
29 {\r
30         /// <summary>\r
31         /// Summary description for XmlSchemaImport.\r
32         /// </summary>\r
33         public class XmlSchemaImport : XmlSchemaExternal\r
34         {\r
35                 private XmlSchemaAnnotation annotation;\r
36                 private string nameSpace;\r
37                 const string xmlname = "import";\r
38 \r
39                 public XmlSchemaImport()\r
40                 {\r
41                 }\r
42                 \r
43                 [System.Xml.Serialization.XmlAttribute("namespace", DataType="anyURI")]\r
44                 public string Namespace \r
45                 {\r
46                         get{ return  nameSpace; } \r
47                         set{ nameSpace = value; }\r
48                 }\r
49 \r
50                 [XmlElement("annotation", Type=typeof (XmlSchemaAnnotation))]\r
51                 public XmlSchemaAnnotation Annotation \r
52                 {\r
53                         get{ return  annotation; } \r
54                         set{ annotation = value; }\r
55                 }\r
56                 //<import \r
57                 //  id = ID \r
58                 //  namespace = anyURI \r
59                 //  schemaLocation = anyURI \r
60                 //  {any attributes with non-schema namespace . . .}>\r
61                 //  Content: (annotation?)\r
62                 //</import>\r
63                 internal static XmlSchemaImport Read(XmlSchemaReader reader, ValidationEventHandler h)\r
64                 {\r
65                         XmlSchemaImport import = new XmlSchemaImport();\r
66                         reader.MoveToElement();\r
67 \r
68                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != "import")\r
69                         {\r
70                                 error(h,"Should not happen :1: XmlSchemaImport.Read, name="+reader.Name,null);\r
71                                 reader.SkipToEnd();\r
72                                 return null;\r
73                         }\r
74 \r
75                         import.LineNumber = reader.LineNumber;\r
76                         import.LinePosition = reader.LinePosition;\r
77                         import.SourceUri = reader.BaseURI;\r
78 \r
79                         while(reader.MoveToNextAttribute())\r
80                         {\r
81                                 if(reader.Name == "id")\r
82                                 {\r
83                                         import.Id = reader.Value;\r
84                                 }\r
85                                 else if(reader.Name == "namespace")\r
86                                 {\r
87                                         import.nameSpace = reader.Value;\r
88                                 }\r
89                                 else if(reader.Name == "schemaLocation")\r
90                                 {\r
91                                         import.SchemaLocation = reader.Value;\r
92                                 }\r
93                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
94                                 {\r
95                                         error(h,reader.Name + " is not a valid attribute for import",null);\r
96                                 }\r
97                                 else\r
98                                 {\r
99                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,import);\r
100                                 }\r
101                         }\r
102 \r
103                         reader.MoveToElement(); \r
104                         if(reader.IsEmptyElement)\r
105                                 return import;\r
106 \r
107                         //  Content: (annotation?)\r
108                         int level = 1;\r
109                         while(reader.ReadNextElement())\r
110                         {\r
111                                 if(reader.NodeType == XmlNodeType.EndElement)\r
112                                 {\r
113                                         if(reader.LocalName != xmlname)\r
114                                                 error(h,"Should not happen :2: XmlSchemaImport.Read, name="+reader.Name,null);\r
115                                         break;\r
116                                 }\r
117                                 if(level <= 1 && reader.LocalName == "annotation")\r
118                                 {\r
119                                         level = 2;      //Only one annotation\r
120                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
121                                         if(annotation != null)\r
122                                                 import.Annotation = annotation;\r
123                                         continue;\r
124                                 }\r
125                                 reader.RaiseInvalidElementError();\r
126                         }\r
127                         return import;\r
128                 }\r
129         }\r
130 }\r