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)
6
using System.Xml.Schema;
8
namespace ICSharpCode.XmlEditor
10
public class XmlSchemaDefinition
12
public const string XmlSchemaNamespace = "http://www.w3.org/2001/XMLSchema";
14
XmlSchemaCompletionCollection schemas;
15
XmlSchemaCompletion currentSchema;
16
SelectedXmlElement selectedElement;
18
public XmlSchemaDefinition(XmlSchemaCompletionCollection schemas, XmlSchemaCompletion currentSchema)
20
this.schemas = schemas;
21
this.currentSchema = currentSchema;
25
/// Determines whether the specified namespace is actually the W3C namespace for
28
public static bool IsXmlSchemaNamespace(string schemaNamespace)
30
return schemaNamespace == XmlSchemaNamespace;
33
public XmlSchemaObjectLocation GetSelectedSchemaObjectLocation(string xml, int index)
35
XmlSchemaObject schemaObject = GetSelectedSchemaObject(xml, index);
36
return new XmlSchemaObjectLocation(schemaObject);
40
/// Gets the XmlSchemaObject that defines the currently selected xml element or
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)
50
FindSelectedElement(xml, index);
51
return GetSelectedSchemaObject();
54
XmlSchemaObject GetSelectedSchemaObject()
56
XmlSchemaCompletion schemaForSelectedElement = FindSchemaForSelectedElement();
57
if (schemaForSelectedElement == null) {
61
XmlSchemaElement selectedSchemaElement = FindSchemaObjectForSelectedElement(schemaForSelectedElement);
62
if (selectedSchemaElement == null) {
66
if (selectedElement.HasSelectedAttribute) {
67
XmlSchemaAttribute attribute = FindSchemaObjectForSelectedAttribute(schemaForSelectedElement, selectedSchemaElement);
68
if (attribute == null) {
69
return selectedSchemaElement;
72
if (selectedElement.HasSelectedAttributeValue) {
73
XmlSchemaObject schemaObject = FindSchemaObjectReferencedByAttributeValue(selectedSchemaElement, attribute);
74
if (schemaObject != null) {
80
return selectedSchemaElement;
83
void FindSelectedElement(string xml, int index)
85
selectedElement = new SelectedXmlElement(xml, index);
88
XmlSchemaCompletion FindSchemaForSelectedElement()
90
return schemas[selectedElement.Path.GetRootNamespace()];
93
XmlSchemaElement FindSchemaObjectForSelectedElement(XmlSchemaCompletion schemaForSelectedElement)
95
return schemaForSelectedElement.FindElement(selectedElement.Path);
98
XmlSchemaAttribute FindSchemaObjectForSelectedAttribute(XmlSchemaCompletion schemaForSelectedElement, XmlSchemaElement selectedSchemaElement)
100
return schemaForSelectedElement.FindAttribute(selectedSchemaElement, selectedElement.SelectedAttribute);
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:
109
/// xs:element ref="ref-name"
110
/// xs:attribute type="type-name"
113
/// The <paramref name="attribute"/> if no schema object was referenced.
115
XmlSchemaObject FindSchemaObjectReferencedByAttributeValue(XmlSchemaElement element, XmlSchemaAttribute attribute)
117
if ((currentSchema != null) && IsXmlSchemaNamespaceElement(element)) {
118
return GetSchemaObjectReferencedByAttributeValue(element.Name, attribute.Name);
123
XmlSchemaObject GetSchemaObjectReferencedByAttributeValue(string elementName, string attributeName)
125
if (attributeName == "ref") {
126
return FindSchemaObjectReference(selectedElement.SelectedAttributeValue, elementName);
127
} else if (attributeName == "type") {
128
return FindSchemaObjectType(selectedElement.SelectedAttributeValue, elementName);
134
/// Attempts to locate the reference name in the specified schema.
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)
144
QualifiedName qualifiedName = currentSchema.CreateQualifiedName(name);
145
XmlSchemaCompletion schema = GetSchemaForQualifiedName(qualifiedName);
146
return FindSchemaObjectReference(qualifiedName, elementName, schema);
149
XmlSchemaObject FindSchemaObjectReference(QualifiedName qualifiedName, string elementName, XmlSchemaCompletion schema)
151
switch (elementName) {
153
return schema.FindRootElement(qualifiedName);
155
return schema.FindAttribute(qualifiedName.Name);
157
return schema.FindGroup(qualifiedName.Name);
158
case "attributeGroup":
159
return schema.FindAttributeGroup(qualifiedName.Name);
164
XmlSchemaCompletion GetSchemaForQualifiedName(QualifiedName name)
166
XmlSchemaCompletion schema = schemas[name.Namespace];
167
if (schema != null) {
170
return currentSchema;
174
/// Attempts to locate the type name in the specified schema.
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)
182
QualifiedName qualifiedName = currentSchema.CreateQualifiedName(name);
183
XmlSchemaCompletion schema = GetSchemaForQualifiedName(qualifiedName);
184
return FindSchemaObjectType(qualifiedName, elementName, schema);
187
XmlSchemaObject FindSchemaObjectType(QualifiedName qualifiedName, string elementName, XmlSchemaCompletion schema)
189
switch (elementName) {
191
return schema.FindComplexType(qualifiedName);
193
return schema.FindSimpleType(qualifiedName.Name);
199
/// Checks whether the element belongs to the XSD namespace.
201
static bool IsXmlSchemaNamespaceElement(XmlSchemaElement element)
203
XmlQualifiedName qualifiedName = element.QualifiedName;
204
if (qualifiedName != null) {
205
return IsXmlSchemaNamespace(qualifiedName.Namespace);