📄 xmlschemacompletiondata.cs
字号:
data.AddRange(GetChildElementCompletionData(sequence.Items, prefix));
} else if (choice != null) {
data.AddRange(GetChildElementCompletionData(choice.Items, prefix));
} else if (groupRef != null) {
data.AddRange(GetChildElementCompletionData(groupRef, prefix));
}
}
return data;
}
XmlCompletionDataCollection GetChildElementCompletionData(XmlSchemaGroupRef groupRef, string prefix)
{
XmlCompletionDataCollection data = new XmlCompletionDataCollection();
XmlSchemaGroup group = FindNamedGroup(groupRef.RefName.Name);
if (group != null) {
XmlSchemaSequence sequence = group.Particle as XmlSchemaSequence;
XmlSchemaChoice choice = group.Particle as XmlSchemaChoice;
if(sequence != null) {
data = GetChildElementCompletionData(sequence.Items, prefix);
} else if (choice != null) {
data = GetChildElementCompletionData(choice.Items, prefix);
}
}
return data;
}
XmlCompletionDataCollection GetChildElementCompletionData(XmlSchemaComplexContentRestriction restriction, string prefix)
{
XmlCompletionDataCollection data = new XmlCompletionDataCollection();
// Add any elements.
if (restriction.Particle != null) {
XmlSchemaSequence sequence = restriction.Particle as XmlSchemaSequence;
XmlSchemaChoice choice = restriction.Particle as XmlSchemaChoice;
if(sequence != null) {
data = GetChildElementCompletionData(sequence.Items, prefix);
} else if (choice != null) {
data = GetChildElementCompletionData(choice.Items, prefix);
}
}
return data;
}
/// <summary>
/// Adds an element completion data to the collection if it does not
/// already exist.
/// </summary>
void AddElement(XmlCompletionDataCollection data, string name, string prefix, string documentation)
{
if (!data.Contains(name)) {
if (prefix.Length > 0) {
name = String.Concat(prefix, ":", name);
}
XmlCompletionData completionData = new XmlCompletionData(name, documentation);
data.Add(completionData);
}
}
/// <summary>
/// Adds an element completion data to the collection if it does not
/// already exist.
/// </summary>
void AddElement(XmlCompletionDataCollection data, string name, string prefix, XmlSchemaAnnotation annotation)
{
// Get any annotation documentation.
string documentation = GetDocumentation(annotation);
AddElement(data, name, prefix, documentation);
}
/// <summary>
/// Adds elements to the collection if it does not already exist.
/// </summary>
void AddElements(XmlCompletionDataCollection lhs, XmlCompletionDataCollection rhs)
{
foreach (XmlCompletionData data in rhs) {
if (!lhs.Contains(data)) {
lhs.Add(data);
}
}
}
/// <summary>
/// Gets the documentation from the annotation element.
/// </summary>
/// <remarks>
/// All documentation elements are added. All text nodes inside
/// the documentation element are added.
/// </remarks>
string GetDocumentation(XmlSchemaAnnotation annotation)
{
string documentation = String.Empty;
if (annotation != null) {
StringBuilder documentationBuilder = new StringBuilder();
foreach (XmlSchemaObject schemaObject in annotation.Items) {
XmlSchemaDocumentation schemaDocumentation = schemaObject as XmlSchemaDocumentation;
if (schemaDocumentation != null) {
foreach (XmlNode node in schemaDocumentation.Markup) {
XmlText textNode = node as XmlText;
if (textNode != null) {
if (textNode.Data != null) {
if (textNode.Data.Length > 0) {
documentationBuilder.Append(textNode.Data);
}
}
}
}
}
}
documentation = documentationBuilder.ToString();
}
return documentation;
}
/// <summary>
/// Finds the element that exists at the specified path.
/// </summary>
XmlSchemaElement FindElement(XmlElementPath path)
{
XmlSchemaElement element = null;
for (int i = 0; i < path.Elements.Count; ++i) {
QualifiedName name = path.Elements[i];
if (i == 0) {
// Look for root element.
element = FindElement(name);
if (element == null) {
break;
}
} else {
element = FindChildElement(element, name);
if (element == null) {
break;
}
}
}
return element;
}
XmlCompletionDataCollection GetAttributeCompletionData(XmlSchemaElement element)
{
XmlCompletionDataCollection data = new XmlCompletionDataCollection();
XmlSchemaComplexType complexType = GetElementAsComplexType(element);
if (complexType != null) {
data.AddRange(GetAttributeCompletionData(complexType));
}
return data;
}
XmlCompletionDataCollection GetAttributeCompletionData(XmlSchemaComplexContentRestriction restriction)
{
XmlCompletionDataCollection data = new XmlCompletionDataCollection();
data.AddRange(GetAttributeCompletionData(restriction.Attributes));
XmlSchemaComplexType baseComplexType = FindNamedType(schema, restriction.BaseTypeName);
if (baseComplexType != null) {
data.AddRange(GetAttributeCompletionData(baseComplexType));
}
return data;
}
XmlCompletionDataCollection GetAttributeCompletionData(XmlSchemaComplexType complexType)
{
XmlCompletionDataCollection data = new XmlCompletionDataCollection();
data = GetAttributeCompletionData(complexType.Attributes);
// Add any complex content attributes.
XmlSchemaComplexContent complexContent = complexType.ContentModel as XmlSchemaComplexContent;
if (complexContent != null) {
XmlSchemaComplexContentExtension extension = complexContent.Content as XmlSchemaComplexContentExtension;
XmlSchemaComplexContentRestriction restriction = complexContent.Content as XmlSchemaComplexContentRestriction;
if (extension != null) {
data.AddRange(GetAttributeCompletionData(extension));
} else if (restriction != null) {
data.AddRange(GetAttributeCompletionData(restriction));
}
} else {
XmlSchemaSimpleContent simpleContent = complexType.ContentModel as XmlSchemaSimpleContent;
if (simpleContent != null) {
data.AddRange(GetAttributeCompletionData(simpleContent));
}
}
return data;
}
XmlCompletionDataCollection GetAttributeCompletionData(XmlSchemaComplexContentExtension extension)
{
XmlCompletionDataCollection data = new XmlCompletionDataCollection();
data.AddRange(GetAttributeCompletionData(extension.Attributes));
XmlSchemaComplexType baseComplexType = FindNamedType(schema, extension.BaseTypeName);
if (baseComplexType != null) {
data.AddRange(GetAttributeCompletionData(baseComplexType));
}
return data;
}
XmlCompletionDataCollection GetAttributeCompletionData(XmlSchemaSimpleContent simpleContent)
{
XmlCompletionDataCollection data = new XmlCompletionDataCollection();
XmlSchemaSimpleContentExtension extension = simpleContent.Content as XmlSchemaSimpleContentExtension;
if (extension != null) {
data.AddRange(GetAttributeCompletionData(extension));
}
return data;
}
XmlCompletionDataCollection GetAttributeCompletionData(XmlSchemaSimpleContentExtension extension)
{
XmlCompletionDataCollection data = new XmlCompletionDataCollection();
data.AddRange(GetAttributeCompletionData(extension.Attributes));
return data;
}
/// <summary>
/// Converts the element to a complex type if possible.
/// </summary>
XmlSchemaComplexType GetElementAsComplexType(XmlSchemaElement element)
{
XmlSchemaComplexType complexType = element.SchemaType as XmlSchemaComplexType;
if (complexType == null) {
complexType = FindNamedType(schema, element.SchemaTypeName);
}
return complexType;
}
XmlCompletionDataCollection GetAttributeCompletionData(XmlSchemaObjectCollection attributes)
{
XmlCompletionDataCollection data = new XmlCompletionDataCollection();
foreach (XmlSchemaObject schemaObject in attributes) {
XmlSchemaAttribute attribute = schemaObject as XmlSchemaAttribute;
XmlSchemaAttributeGroupRef attributeGroupRef = schemaObject as XmlSchemaAttributeGroupRef;
if (attribute != null) {
if (!IsProhibitedAttribute(attribute)) {
AddAttribute(data, attribute);
} else {
prohibitedAttributes.Add(attribute);
}
} else if (attributeGroupRef != null) {
data.AddRange(GetAttributeCompletionData(attributeGroupRef));
}
}
return data;
}
/// <summary>
/// Checks that the attribute is prohibited or has been flagged
/// as prohibited previously.
/// </summary>
bool IsProhibitedAttribute(XmlSchemaAttribute attribute)
{
bool prohibited = false;
if (attribute.Use == XmlSchemaUse.Prohibited) {
prohibited = true;
} else {
foreach (XmlSchemaAttribute prohibitedAttribute in prohibitedAttributes) {
if (prohibitedAttribute.QualifiedName == attribute.QualifiedName) {
prohibited = true;
break;
}
}
}
return prohibited;
}
/// <summary>
/// Adds an attribute to the completion data collection.
/// </summary>
/// <remarks>
/// Note the special handling of xml:lang attributes.
/// </remarks>
void AddAttribute(XmlCompletionDataCollection data, XmlSchemaAttribute attribute)
{
string name = attribute.Name;
if (name == null) {
if (attribute.RefName.Namespace == "http://www.w3.org/XML/1998/namespace") {
name = String.Concat("xml:", attribute.RefName.Name);
}
}
if (name != null) {
string documentation = GetDocumentation(attribute.Annotation);
XmlCompletionData completionData = new XmlCompletionData(name, documentation, XmlCompletionData.DataType.XmlAttribute);
data.Add(completionData);
}
}
/// <summary>
/// Gets attribute completion data from a group ref.
/// </summary>
XmlCompletionDataCollection GetAttributeCompletionData(XmlSchemaAttributeGroupRef groupRef)
{
XmlCompletionDataCollection data = new XmlCompletionDataCollection();
XmlSchemaAttributeGroup group = FindAttributeGroup(schema, groupRef.RefName.Name);
if (group != null) {
data = GetAttributeCompletionData(group.Attributes);
}
return data;
}
static XmlSchemaComplexType FindNamedType(XmlSchema schema, XmlQualifiedName name)
{
XmlSchemaComplexType matchedComplexType = null;
if (name != null) {
foreach (XmlSchemaObject schemaObject in schema.Items) {
XmlSchemaComplexType complexType = schemaObject as XmlSchemaComplexType;
if (complexType != null) {
if (complexType.QualifiedName == name) {
matchedComplexType = complexType;
break;
}
}
}
// Try included schemas.
if (matchedComplexType == null) {
foreach (XmlSchemaExternal external in schema.Includes) {
XmlSchemaInclude include = external as XmlSchemaInclude;
if (include != null) {
if (include.Schema != null) {
matchedComplexType = FindNamedType(include.Schema, name);
}
}
}
}
}
return matchedComplexType;
}
/// <summary>
/// Finds an element that matches the specified <paramref name="name"/>
/// from the children of the given <paramref name="element"/>.
/// </summary>
XmlSchemaElement FindChildElement(XmlSchemaElement element, QualifiedName name)
{
XmlSchemaElement matchedElement = null;
XmlSchemaComplexType complexType = GetElementAsComplexType(element);
if (complexType != null) {
matchedElement = FindChildElement(complexType, name);
}
return matchedElement;
}
XmlSchemaElement FindChildElement(XmlSchemaComplexType complexType, QualifiedName name)
{
XmlSchemaElement matchedElement = null;
XmlSchemaSequence sequence = complexType.Particle as XmlSchemaSequence;
XmlSchemaChoice choice = complexType.Particle as XmlSchemaChoice;
XmlSchemaGroupRef groupRef = complexType.Particle as XmlSchemaGroupRef;
XmlSchemaComplexContent complexContent = complexType.ContentModel as XmlSchemaComplexContent;
if (sequence != null) {
matchedElement = FindElement(sequence.Items, name);
} else if (choice != null) {
matchedElement = FindElement(choice.Items, name);
} else if (complexContent != null) {
XmlSchemaComplexContentExtension extension = complexContent.Content as XmlSchemaComplexContentExtension;
XmlSchemaComplexContentRestriction restriction = complexContent.Content as XmlSchemaComplexContentRestriction;
if (extension != null) {
matchedElement = FindChildElement(extension, name);
} else if (restriction != null) {
matchedElement = FindChildElement(restriction, name);
}
} else if (groupRef != null) {
matchedElement = FindElement(groupRef, name);
}
return matchedElement;
}
/// <summary>
/// Finds the named child element contained in the extension element.
/// </summary>
XmlSchemaElement FindChildElement(XmlSchemaComplexContentExtension extension, QualifiedName name)
{
XmlSchemaElement matchedElement = null;
XmlSchemaComplexType complexType = FindNamedType(schema, extension.BaseTypeName);
if (complexType != null) {
matchedElement = FindChildElement(complexType, name);
if (matchedElement == null) {
XmlSchemaSequence sequence = extension.Particle as XmlSchemaSequence;
XmlSchemaChoice choice = extension.Particle as XmlSchemaChoice;
XmlSchemaGroupRef groupRef = extension.Particle as XmlSchemaGroupRef;
if (sequence != null) {
matchedElement = FindElement(sequence.Items, name);
} else if (choice != null) {
matchedElement = FindElement(choice.Items, name);
} else if (groupRef != null) {
matchedElement = FindElement(groupRef, name);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -