~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlSchemaDefinition.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Xml;
 
6
using System.Xml.Schema;
 
7
 
 
8
namespace ICSharpCode.XmlEditor
 
9
{
 
10
        public class XmlSchemaDefinition
 
11
        {
 
12
                public const string XmlSchemaNamespace = "http://www.w3.org/2001/XMLSchema";
 
13
 
 
14
                XmlSchemaCompletionCollection schemas;
 
15
                XmlSchemaCompletion currentSchema;
 
16
                SelectedXmlElement selectedElement;
 
17
                
 
18
                public XmlSchemaDefinition(XmlSchemaCompletionCollection schemas, XmlSchemaCompletion currentSchema)
 
19
                {
 
20
                        this.schemas = schemas;
 
21
                        this.currentSchema = currentSchema;
 
22
                }
 
23
                
 
24
                /// <summary>
 
25
                /// Determines whether the specified namespace is actually the W3C namespace for
 
26
                /// XSD files.
 
27
                /// </summary>
 
28
                public static bool IsXmlSchemaNamespace(string schemaNamespace)
 
29
                {
 
30
                        return schemaNamespace == XmlSchemaNamespace;
 
31
                }
 
32
                
 
33
                public XmlSchemaObjectLocation GetSelectedSchemaObjectLocation(string xml, int index)
 
34
                {
 
35
                        XmlSchemaObject schemaObject = GetSelectedSchemaObject(xml, index);
 
36
                        return new XmlSchemaObjectLocation(schemaObject);
 
37
                }
 
38
                
 
39
                /// <summary>
 
40
                /// Gets the XmlSchemaObject that defines the currently selected xml element or
 
41
                /// attribute.
 
42
                /// </summary>
 
43
                /// <param name="text">The complete xml text.</param>
 
44
                /// <param name="index">The current cursor index.</param>
 
45
                /// <param name="currentSchemaCompletionData">This is the schema completion data for the
 
46
                /// schema currently being displayed. This can be null if the document is
 
47
                /// not a schema.</param>
 
48
                public XmlSchemaObject GetSelectedSchemaObject(string xml, int index)
 
49
                {
 
50
                        FindSelectedElement(xml, index);
 
51
                        return GetSelectedSchemaObject();
 
52
                }
 
53
                
 
54
                XmlSchemaObject GetSelectedSchemaObject()
 
55
                {
 
56
                        XmlSchemaCompletion schemaForSelectedElement = FindSchemaForSelectedElement();
 
57
                        if (schemaForSelectedElement == null) {
 
58
                                return null;
 
59
                        }
 
60
                        
 
61
                        XmlSchemaElement selectedSchemaElement = FindSchemaObjectForSelectedElement(schemaForSelectedElement);
 
62
                        if (selectedSchemaElement == null) {
 
63
                                return null;
 
64
                        }
 
65
 
 
66
                        if (selectedElement.HasSelectedAttribute) {
 
67
                                XmlSchemaAttribute attribute = FindSchemaObjectForSelectedAttribute(schemaForSelectedElement, selectedSchemaElement);
 
68
                                if (attribute == null) {
 
69
                                        return selectedSchemaElement;
 
70
                                }
 
71
                                
 
72
                                if (selectedElement.HasSelectedAttributeValue) {
 
73
                                        XmlSchemaObject schemaObject = FindSchemaObjectReferencedByAttributeValue(selectedSchemaElement, attribute);
 
74
                                        if (schemaObject != null) {
 
75
                                                return schemaObject;
 
76
                                        }
 
77
                                }
 
78
                                return attribute;
 
79
                        }
 
80
                        return selectedSchemaElement;
 
81
                }
 
82
                
 
83
                void FindSelectedElement(string xml, int index)
 
84
                {
 
85
                        selectedElement = new SelectedXmlElement(xml, index);
 
86
                }
 
87
                
 
88
                XmlSchemaCompletion FindSchemaForSelectedElement()
 
89
                {
 
90
                        return schemas[selectedElement.Path.GetRootNamespace()];
 
91
                }
 
92
                
 
93
                XmlSchemaElement FindSchemaObjectForSelectedElement(XmlSchemaCompletion schemaForSelectedElement)
 
94
                {
 
95
                        return schemaForSelectedElement.FindElement(selectedElement.Path);
 
96
                }
 
97
                
 
98
                XmlSchemaAttribute FindSchemaObjectForSelectedAttribute(XmlSchemaCompletion schemaForSelectedElement, XmlSchemaElement selectedSchemaElement)
 
99
                {
 
100
                        return schemaForSelectedElement.FindAttribute(selectedSchemaElement, selectedElement.SelectedAttribute);
 
101
                }
 
102
                
 
103
                /// <summary>
 
104
                /// If the attribute value found references another item in the schema
 
105
                /// return this instead of the attribute schema object. For example, if the
 
106
                /// user can select the attribute value and the code will work out the schema object pointed to by the ref
 
107
                /// or type attribute:
 
108
                ///
 
109
                /// xs:element ref="ref-name"
 
110
                /// xs:attribute type="type-name"
 
111
                /// </summary>
 
112
                /// <returns>
 
113
                /// The <paramref name="attribute"/> if no schema object was referenced.
 
114
                /// </returns>
 
115
                XmlSchemaObject FindSchemaObjectReferencedByAttributeValue(XmlSchemaElement element, XmlSchemaAttribute attribute)
 
116
                {
 
117
                        if ((currentSchema != null) && IsXmlSchemaNamespaceElement(element)) {
 
118
                                return GetSchemaObjectReferencedByAttributeValue(element.Name, attribute.Name);
 
119
                        }
 
120
                        return null;
 
121
                }
 
122
                
 
123
                XmlSchemaObject GetSchemaObjectReferencedByAttributeValue(string elementName, string attributeName)
 
124
                {
 
125
                        if (attributeName == "ref") {
 
126
                                return FindSchemaObjectReference(selectedElement.SelectedAttributeValue, elementName);
 
127
                        } else if (attributeName == "type") {
 
128
                                return FindSchemaObjectType(selectedElement.SelectedAttributeValue, elementName);
 
129
                        }
 
130
                        return null;
 
131
                }
 
132
                
 
133
                /// <summary>
 
134
                /// Attempts to locate the reference name in the specified schema.
 
135
                /// </summary>
 
136
                /// <param name="name">The reference to look up.</param>
 
137
                /// <param name="schemaCompletionData">The schema completion data to use to
 
138
                /// find the reference.</param>
 
139
                /// <param name="elementName">The element to determine what sort of reference it is
 
140
                /// (e.g. group, attribute, element).</param>
 
141
                /// <returns><see langword="null"/> if no match can be found.</returns>
 
142
                XmlSchemaObject FindSchemaObjectReference(string name, string elementName)
 
143
                {
 
144
                        QualifiedName qualifiedName = currentSchema.CreateQualifiedName(name);
 
145
                        XmlSchemaCompletion schema = GetSchemaForQualifiedName(qualifiedName);
 
146
                        return FindSchemaObjectReference(qualifiedName, elementName, schema);
 
147
                }
 
148
                
 
149
                XmlSchemaObject FindSchemaObjectReference(QualifiedName qualifiedName, string elementName, XmlSchemaCompletion schema)
 
150
                {
 
151
                        switch (elementName) {
 
152
                                case "element":
 
153
                                        return schema.FindRootElement(qualifiedName);
 
154
                                case "attribute":
 
155
                                        return schema.FindAttribute(qualifiedName.Name);
 
156
                                case "group":
 
157
                                        return schema.FindGroup(qualifiedName.Name);
 
158
                                case "attributeGroup":
 
159
                                        return schema.FindAttributeGroup(qualifiedName.Name);
 
160
                        }
 
161
                        return null;
 
162
                }
 
163
                
 
164
                XmlSchemaCompletion GetSchemaForQualifiedName(QualifiedName name)
 
165
                {
 
166
                        XmlSchemaCompletion schema = schemas[name.Namespace];
 
167
                        if (schema != null) {
 
168
                                return schema;
 
169
                        }
 
170
                        return currentSchema;
 
171
                }
 
172
                
 
173
                /// <summary>
 
174
                /// Attempts to locate the type name in the specified schema.
 
175
                /// </summary>
 
176
                /// <param name="name">The type to look up.</param>
 
177
                /// <param name="elementName">The element to determine what sort of type it is
 
178
                /// (e.g. group, attribute, element).</param>
 
179
                /// <returns><see langword="null"/> if no match can be found.</returns>
 
180
                XmlSchemaObject FindSchemaObjectType(string name, string elementName)
 
181
                {
 
182
                        QualifiedName qualifiedName = currentSchema.CreateQualifiedName(name);
 
183
                        XmlSchemaCompletion schema = GetSchemaForQualifiedName(qualifiedName);
 
184
                        return FindSchemaObjectType(qualifiedName, elementName, schema);
 
185
                }
 
186
                
 
187
                XmlSchemaObject FindSchemaObjectType(QualifiedName qualifiedName, string elementName, XmlSchemaCompletion schema)
 
188
                {
 
189
                        switch (elementName) {
 
190
                                case "element":
 
191
                                        return schema.FindComplexType(qualifiedName);
 
192
                                case "attribute":
 
193
                                        return schema.FindSimpleType(qualifiedName.Name);
 
194
                        }
 
195
                        return null;
 
196
                }
 
197
                
 
198
                /// <summary>
 
199
                /// Checks whether the element belongs to the XSD namespace.
 
200
                /// </summary>
 
201
                static bool IsXmlSchemaNamespaceElement(XmlSchemaElement element)
 
202
                {
 
203
                        XmlQualifiedName qualifiedName = element.QualifiedName;
 
204
                        if (qualifiedName != null) {
 
205
                                return IsXmlSchemaNamespace(qualifiedName.Namespace);
 
206
                        }
 
207
                        return false;
 
208
                }
 
209
        }
 
210
}