/// <summary>
 ///   Adds the contents of another <see cref='XmlCompletionDataCollection'/> to the end of the collection.
 /// </summary>
 /// <param name='val'>
 ///    A <see cref='XmlCompletionDataCollection'/> containing the objects to add to the collection.
 /// </param>
 /// <seealso cref='XmlCompletionDataCollection.Add'/>
 public void AddRange(XmlCompletionDataCollection val)
 {
     for (int i = 0; i < val.Count; i++)
     {
         this.Add(val[i]);
     }
 }
		/// <summary>
		///   Initializes a new instance of <see cref='XmlCompletionDataCollection'/> based on another <see cref='XmlCompletionDataCollection'/>.
		/// </summary>
		/// <param name='val'>
		///   A <see cref='XmlCompletionDataCollection'/> from which the contents are copied
		/// </param>
		public XmlCompletionDataCollection(XmlCompletionDataCollection val)
		{
			this.AddRange(val);
		}
		/// <summary>
		///   Adds the contents of another <see cref='XmlCompletionDataCollection'/> to the end of the collection.
		/// </summary>
		/// <param name='val'>
		///    A <see cref='XmlCompletionDataCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <seealso cref='XmlCompletionDataCollection.Add'/>
		public void AddRange(XmlCompletionDataCollection val)
		{
			for (int i = 0; i < val.Count; i++)
			{
				this.Add(val[i]);
			}
		}
    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;
    }
    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(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;
    }
 /// <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>
 /// 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>
    /// Gets the attribute completion data for the xml element that exists
    /// at the end of the specified path.
    /// </summary>
    public ICompletionData[] GetAttributeCompletionData(XmlElementPath path)
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      // Locate matching element.
      XmlSchemaElement element = FindElement(path);

      // Get completion data.
      if (element != null)
      {
        prohibitedAttributes.Clear();
        data = GetAttributeCompletionData(element);
      }

      return data.ToArray();
    }
    ///// <summary>
    ///// Gets the possible root elements for an xml document using this schema.
    ///// </summary>
    //public ICompletionData[] GetElementCompletionData()
    //{
    //  return GetElementCompletionData(String.Empty);
    //}

    /// <summary>
    /// Gets the possible root elements for an xml document using this schema.
    /// </summary>
    public IEnumerable<ICompletionData> GetElementCompletionData(string namespacePrefix)
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      var isDefined = false;
      var filter = ElementFilter ?? (e => e.Name != null);
      if (string.IsNullOrEmpty(namespacePrefix) && _namespaces != null)
      {
        namespacePrefix = _namespaces.FirstOrDefault(k => k.Value == schema.TargetNamespace).Key;
        isDefined = !string.IsNullOrEmpty(namespacePrefix);
      }

      //<soapenv:Stuff xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
      foreach (var element in schema.Elements.Values.OfType<XmlSchemaElement>().Where(filter))
      {
        var insertText = (string.IsNullOrEmpty(namespacePrefix) ? "" : namespacePrefix + ":") + element.Name;
        if (!isDefined)
          insertText += " xmlns:" + namespacePrefix + "=\"" + schema.TargetNamespace + "\"";
        if (NamespaceWriter != null)
          insertText += " " + NamespaceWriter();

        yield return new XmlCompletionData()
        {
          Text = element.Name,
          Image = Icons.XmlTag16.Wpf,
          Action = () => insertText
        };
      }
    }
 /// <summary>
 /// Adds any elements that have the specified substitution group.
 /// </summary>
 void AddSubstitionGroupElements(XmlCompletionDataCollection data, XmlQualifiedName group, string prefix)
 {
   foreach (XmlSchemaElement element in schema.Elements.Values)
   {
     if (element.SubstitutionGroup == group)
     {
       AddElement(data, element.Name, prefix, element.Annotation);
     }
   }
 }
 /// <summary>
 /// Adds an attribute value to the completion data collection.
 /// </summary>
 void AddAttributeValue(XmlCompletionDataCollection data, string valueText, string description)
 {
   XmlCompletionData completionData = new XmlCompletionData(valueText, description, XmlCompletionData.DataType.XmlAttributeValue);
   data.Add(completionData);
 }
 /// <summary>
 /// Adds an attribute value to the completion data collection.
 /// </summary>
 void AddAttributeValue(XmlCompletionDataCollection data, string valueText, XmlSchemaAnnotation annotation)
 {
   string documentation = GetDocumentation(annotation);
   XmlCompletionData completionData = new XmlCompletionData(valueText, documentation, XmlCompletionData.DataType.XmlAttributeValue);
   data.Add(completionData);
 }
    /// <summary>
    /// Gets the set of attribute values for an xs:boolean type.
    /// </summary>
    XmlCompletionDataCollection GetBooleanAttributeValueCompletionData()
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      AddAttributeValue(data, "0");
      AddAttributeValue(data, "1");
      AddAttributeValue(data, "true");
      AddAttributeValue(data, "false");

      return data;
    }
 /// <summary>
 ///   Initializes a new instance of <see cref='XmlCompletionDataEnumerator'/>.
 /// </summary>
 public XmlCompletionDataEnumerator(XmlCompletionDataCollection mappings)
 {
     this.temp           = ((IEnumerable)(mappings));
     this.baseEnumerator = temp.GetEnumerator();
 }
    XmlCompletionDataCollection GetAttributeValueCompletionData(XmlSchemaSimpleTypeList list)
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      if (list.ItemType != null)
      {
        data.AddRange(GetAttributeValueCompletionData(list.ItemType));
      }
      else if (list.ItemTypeName != null)
      {
        XmlSchemaSimpleType simpleType = FindSimpleType(list.ItemTypeName);
        if (simpleType != null)
        {
          data.AddRange(GetAttributeValueCompletionData(simpleType));
        }
      }

      return data;
    }
    /// <summary>
    /// Gets the child element completion data for the xml element that exists
    /// at the end of the specified path.
    /// </summary>
    public ICompletionData[] GetChildElementCompletionData(XmlElementPath path)
    {
      _namespaces = path.Namespaces;
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      // Locate matching element.
      XmlSchemaElement element = FindElement(path);

      // Get completion data.
      if (element != null)
      {
        data = GetChildElementCompletionData(element, path.Elements.LastPrefix);
      }

      return data.ToArray();
    }
    /// <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>
    /// Gets the autocomplete data for the specified attribute value.
    /// </summary>
    public ICompletionData[] GetAttributeValueCompletionData(XmlElementPath path, string name)
    {
      _namespaces = path.Namespaces;
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      // Locate matching element.
      XmlSchemaElement element = FindElement(path);

      // Get completion data.
      if (element != null)
      {
        data = GetAttributeValueCompletionData(element, name);
      }

      return data.ToArray();
    }
    XmlCompletionDataCollection GetAttributeCompletionData(XmlSchemaElement element)
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      XmlSchemaComplexType complexType = GetElementAsComplexType(element);

      if (complexType != null)
      {
        data.AddRange(GetAttributeCompletionData(complexType));
      }

      return data;
    }
    XmlCompletionDataCollection GetChildElementCompletionData(XmlSchemaElement element, string prefix)
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      XmlSchemaComplexType complexType = GetElementAsComplexType(element);

      if (complexType != null)
      {
        data = GetChildElementCompletionData(complexType, prefix);
      }

      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 GetChildElementCompletionData(XmlSchemaComplexType complexType, string prefix)
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      XmlSchemaSequence sequence = complexType.Particle as XmlSchemaSequence;
      XmlSchemaChoice choice = complexType.Particle as XmlSchemaChoice;
      XmlSchemaGroupRef groupRef = complexType.Particle as XmlSchemaGroupRef;
      XmlSchemaComplexContent complexContent = complexType.ContentModel as XmlSchemaComplexContent;
      XmlSchemaAll all = complexType.Particle as XmlSchemaAll;

      if (sequence != null)
      {
        data = GetChildElementCompletionData(sequence.Items, prefix);
      }
      else if (choice != null)
      {
        data = GetChildElementCompletionData(choice.Items, prefix);
      }
      else if (complexContent != null)
      {
        data = GetChildElementCompletionData(complexContent, prefix);
      }
      else if (groupRef != null)
      {
        data = GetChildElementCompletionData(groupRef, prefix);
      }
      else if (all != null)
      {
        data = GetChildElementCompletionData(all.Items, prefix);
      }

      return data;
    }
    XmlCompletionDataCollection GetAttributeCompletionData(XmlSchemaSimpleContentExtension extension)
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      data.AddRange(GetAttributeCompletionData(extension.Attributes));

      return data;
    }
    XmlCompletionDataCollection GetChildElementCompletionData(XmlSchemaObjectCollection items, string prefix)
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      foreach (XmlSchemaObject schemaObject in items)
      {

        XmlSchemaElement childElement = schemaObject as XmlSchemaElement;
        XmlSchemaSequence childSequence = schemaObject as XmlSchemaSequence;
        XmlSchemaChoice childChoice = schemaObject as XmlSchemaChoice;
        XmlSchemaGroupRef groupRef = schemaObject as XmlSchemaGroupRef;
        XmlSchemaAny anyRef = schemaObject as XmlSchemaAny;

        if (childElement != null)
        {
          string name = childElement.Name;
          if (name == null)
          {
            name = childElement.RefName.Name;
            XmlSchemaElement element = FindElement(childElement.RefName);
            if (element != null)
            {
              if (element.IsAbstract)
              {
                AddSubstitionGroupElements(data, element.QualifiedName, prefix);
              }
              else
              {
                AddElement(data, name, prefix, element.Annotation ?? element.ElementSchemaType.Annotation);
              }
            }
            else
            {
              AddElement(data, name, prefix, childElement.Annotation ?? childElement.ElementSchemaType.Annotation);
            }
          }
          else
          {
            AddElement(data, name, prefix, childElement.Annotation ?? childElement.ElementSchemaType.Annotation);
          }
        }
        else if (childSequence != null)
        {
          AddElements(data, GetChildElementCompletionData(childSequence.Items, prefix));
        }
        else if (childChoice != null)
        {
          AddElements(data, GetChildElementCompletionData(childChoice.Items, prefix));
        }
        else if (groupRef != null)
        {
          AddElements(data, GetChildElementCompletionData(groupRef, prefix));
        }
        else if (anyRef != null)
        {
          if (string.IsNullOrEmpty(anyRef.Namespace) || anyRef.Namespace == schema.TargetNamespace)
          {
            data.AddRange(GetElementCompletionData(prefix).OfType<XmlCompletionData>().ToArray());
          }
          else if (RelatedSchemas != null)
          {
            var completion = RelatedSchemas[anyRef.Namespace];
            if (completion != null)
            {
              completion._namespaces = _namespaces;
              data.AddRange(completion.GetElementCompletionData(null).OfType<XmlCompletionData>().ToArray());
            }
          }
        }
      }

      return data;
    }
    /// <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);
        string defaultValue = attribute.DefaultValue;
        bool isFixed = false;
        if (string.IsNullOrEmpty(defaultValue))
        {
          defaultValue = attribute.FixedValue;
          if (!string.IsNullOrEmpty(defaultValue)) isFixed = true;
        }
        XmlCompletionData completionData = new XmlCompletionData(name, documentation, XmlCompletionData.DataType.XmlAttribute, defaultValue, isFixed);
        data.Add(completionData);
      }
    }
    XmlCompletionDataCollection GetChildElementCompletionData(XmlSchemaComplexContent complexContent, string prefix)
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      XmlSchemaComplexContentExtension extension = complexContent.Content as XmlSchemaComplexContentExtension;
      if (extension != null)
      {
        data = GetChildElementCompletionData(extension, prefix);
      }
      else
      {
        XmlSchemaComplexContentRestriction restriction = complexContent.Content as XmlSchemaComplexContentRestriction;
        if (restriction != null)
        {
          data = GetChildElementCompletionData(restriction, prefix);
        }
      }

      return data;
    }
			/// <summary>
			///   Initializes a new instance of <see cref='XmlCompletionDataEnumerator'/>.
			/// </summary>
			public XmlCompletionDataEnumerator(XmlCompletionDataCollection mappings)
			{
				this.temp = ((IEnumerable)(mappings));
				this.baseEnumerator = temp.GetEnumerator();
			}
    XmlCompletionDataCollection GetChildElementCompletionData(XmlSchemaComplexContentExtension extension, string prefix)
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      XmlSchemaComplexType complexType = FindNamedType(schema, extension.BaseTypeName);
      if (complexType != null)
      {
        data = GetChildElementCompletionData(complexType, prefix);
      }

      // Add any elements.
      if (extension.Particle != null)
      {
        XmlSchemaSequence sequence = extension.Particle as XmlSchemaSequence;
        XmlSchemaChoice choice = extension.Particle as XmlSchemaChoice;
        XmlSchemaGroupRef groupRef = extension.Particle as XmlSchemaGroupRef;

        if (sequence != null)
        {
          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 = FindGroup(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;
        XmlSchemaGroupRef groupRef = restriction.Particle as XmlSchemaGroupRef;

        if (sequence != null)
        {
          data = GetChildElementCompletionData(sequence.Items, prefix);
        }
        else if (choice != null)
        {
          data = GetChildElementCompletionData(choice.Items, prefix);
        }
        else if (groupRef != null)
        {
          data = GetChildElementCompletionData(groupRef, prefix);
        }
      }

      return data;
    }
 /// <summary>
 ///   Initializes a new instance of <see cref='XmlCompletionDataCollection'/> based on another <see cref='XmlCompletionDataCollection'/>.
 /// </summary>
 /// <param name='val'>
 ///   A <see cref='XmlCompletionDataCollection'/> from which the contents are copied
 /// </param>
 public XmlCompletionDataCollection(XmlCompletionDataCollection val)
 {
     this.AddRange(val);
 }
    XmlCompletionDataCollection GetAttributeValueCompletionData(XmlSchemaSimpleType simpleType)
    {
      XmlCompletionDataCollection data = new XmlCompletionDataCollection();

      XmlSchemaSimpleTypeRestriction simpleTypeRestriction = simpleType.Content as XmlSchemaSimpleTypeRestriction;
      XmlSchemaSimpleTypeUnion union = simpleType.Content as XmlSchemaSimpleTypeUnion;
      XmlSchemaSimpleTypeList list = simpleType.Content as XmlSchemaSimpleTypeList;

      if (simpleTypeRestriction != null)
      {
        data.AddRange(GetAttributeValueCompletionData(simpleTypeRestriction));
      }
      else if (union != null)
      {
        data.AddRange(GetAttributeValueCompletionData(union));
      }
      else if (list != null)
      {
        data.AddRange(GetAttributeValueCompletionData(list));
      }

      return data;
    }