ICompletionData[] GetAttributeValueCompletionData(XmlElementPath path, string name)
    {
      ICompletionData[] completionData = null;

      XmlSchemaCompletionData schema = FindSchema(path);
      if (schema != null) {
        completionData = schema.GetAttributeValueCompletionData(path, name);
      }

      return completionData;
    }
    /// <summary>
    /// Finds the schema given the xml element path.
    /// </summary>
    public XmlSchemaCompletionData FindSchema(XmlElementPath path)
    {
      if (path.Elements.Count > 0) {
        string namespaceUri = path.Elements[0].Namespace;
        if (namespaceUri.Length > 0) {
          var result = schemaCompletionDataItems[namespaceUri];
          if (result == null && defaultSchemaCompletionData.NamespaceUri == namespaceUri) result = defaultSchemaCompletionData;
          return result;
        } else if (defaultSchemaCompletionData != null) {

          // Use the default schema namespace if none
          // specified in a xml element path, otherwise
          // we will not find any attribute or element matches
          // later.
          foreach (QualifiedName name in path.Elements) {
            if (name.Namespace.Length == 0) {
              name.Namespace = defaultSchemaCompletionData.NamespaceUri;
            }
          }
          return defaultSchemaCompletionData;
        }
      }
      return null;
    }
    ICompletionData[] GetChildElementCompletionData(XmlElementPath path)
    {
      ICompletionData[] completionData = null;

      XmlSchemaCompletionData schema = FindSchema(path);
      if (schema != null) {
        completionData = schema.GetChildElementCompletionData(path);
      }

      return completionData;
    }
Example #4
0
    /// <summary>
    /// Gets path of the xml element start tag that the specified
    /// <paramref name="index"/> is currently inside.
    /// </summary>
    /// <remarks>If the index outside the start tag then an empty path
    /// is returned.</remarks>
    public static XmlElementPath GetActiveElementStartPath(string xml, int index)
    {
      XmlElementPath path = new XmlElementPath();

      string elementText = GetActiveElementStartText(xml, index);

      if (elementText != null)
      {
        QualifiedName elementName = GetElementName(elementText);
        NamespaceURI elementNamespace = GetElementNamespace(elementText);

        path = GetParentElementPathCore(xml.Substring(0, index));
        string namespaceUri;
        if (elementNamespace.Namespace.Length == 0
          && !string.IsNullOrEmpty(elementName.Prefix)
          && path.Namespaces != null
          && path.Namespaces.TryGetValue(elementName.Prefix, out namespaceUri))
        {
          elementNamespace.Namespace = namespaceUri;
          elementNamespace.Prefix = elementName.Prefix;
        }
        path.Elements.Add(new QualifiedName(elementName.Name, elementNamespace.Namespace, elementNamespace.Prefix));
        path.Compact(elementNamespace.Namespace);
      }

      return path;
    }
        public virtual IEnumerable <ICompletionData> HandleTextEntered(ITextSource source, int caret, string insertText = null)
        {
            insertText = insertText ?? source.GetText(caret - 1, 1);
            var text = source.GetText(0, caret);

            ICompletionData[]            result = null;
            IEnumerable <XmlElementPath> parentPaths;

            switch (insertText)
            {
            case "=":
                // Namespace intellisense.
                if (XmlParser.IsNamespaceDeclaration(text, text.Length))
                {
                    result = schemaCompletionDataItems.GetNamespaceCompletionData();
                }
                break;

            case "<":
                // Child element intellisense.
                parentPaths = XmlParser.GetParentElementPaths(text);
                if (parentPaths.Any())
                {
                    foreach (var path in parentPaths)
                    {
                        result = GetChildElementCompletionData(path);
                        if (result.Any())
                        {
                            break;
                        }
                    }
                }
                else if (defaultSchemaCompletionData != null)
                {
                    result = defaultSchemaCompletionData.GetElementCompletionData(defaultNamespacePrefix).ToArray();
                }
                break;

            case " ":
                // Attribute intellisense.
                if (!XmlParser.IsInsideAttributeValue(text, text.Length))
                {
                    XmlElementPath path = XmlParser.GetActiveElementStartPath(text, text.Length);
                    if (path.Elements.Count > 0)
                    {
                        result = GetAttributeCompletionData(path);
                    }
                }
                break;

            default:

                // Attribute value intellisense.
                if (XmlParser.IsAttributeValueChar(insertText[0]))
                {
                    string attributeName = XmlParser.GetAttributeName(text, text.Length);
                    if (attributeName.Length > 0)
                    {
                        XmlElementPath elementPath = XmlParser.GetActiveElementStartPath(text, text.Length);
                        if (elementPath.Elements.Count > 0)
                        {
                            //preSelection = insertText.ToString();
                            result = GetAttributeValueCompletionData(elementPath, attributeName);
                        }
                    }
                }
                break;
            }

            if (result != null)
            {
                return(result.OrderBy(x => x.Text));
            }

            return(Enumerable.Empty <ICompletionData>());
        }
Example #6
0
    public static XmlElementPath GetParentElementPathCore(string xml)
    {
      XmlElementPath path = new XmlElementPath();

      try
      {
        StringReader reader = new StringReader(xml);
        XmlTextReader xmlReader = new XmlTextReader(reader);
        xmlReader.XmlResolver = null;
        while (xmlReader.Read())
        {
          switch (xmlReader.NodeType)
          {
            case XmlNodeType.Element:
              if (!xmlReader.IsEmptyElement)
              {
                QualifiedName elementName = new QualifiedName(xmlReader.LocalName, xmlReader.NamespaceURI, xmlReader.Prefix);
                path.Elements.Add(elementName);
                path.Namespaces = xmlReader.GetNamespacesInScope(XmlNamespaceScope.All);
              }
              break;
            case XmlNodeType.EndElement:
              path.Elements.RemoveLast();
              break;
          }
        }
      }
      catch (XmlException)
      {
        // Do nothing.
      }
      catch (WebException)
      {
        // Do nothing.
      }

      return path;
    }
 /// <summary>
 /// Finds the element that exists at the specified path.
 /// </summary>
 /// <remarks>This method is not used when generating completion data,
 /// but is a useful method when locating an element so we can jump
 /// to its schema definition.</remarks>
 /// <returns><see langword="null"/> if no element can be found.</returns>
 public virtual XmlSchemaElement FindElement(XmlElementPath path)
 {
   _namespaces = path.Namespaces;
   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;
 }
    /// <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();
    }
    /// <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>
    /// 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();
    }