/// <summary> /// Recursively resolve given prefix in this context. Prefix must have some value. /// </summary> /// <returns> Empty string if prefix is not found </returns> public string ResolvePrefix(string prefix) { if (string.IsNullOrEmpty(prefix)) { throw new ArgumentException("No prefix given", "prefix"); } // Implicit namesapces if (prefix == "xml") { return(XmlNamespace); } if (prefix == "xmlns") { return(XmlnsNamespace); } AXmlElement current = this; while (current != null) { string namesapce = current.GetAttributeValue(XmlnsNamespace, prefix); if (namesapce != null) { return(namesapce); } current = current.Parent as AXmlElement; } return(NoNamespace); // Can not find prefix }
/// <inheritdoc/> internal override bool UpdateDataFrom(AXmlObject source) { if (!base.UpdateDataFrom(source)) { return(false); } AXmlElement src = (AXmlElement)source; // Clear the cache for this - quite expensive attributesAndElements = null; if (this.IsProperlyNested != src.IsProperlyNested || this.HasStartOrEmptyTag != src.HasStartOrEmptyTag || this.HasEndTag != src.HasEndTag) { OnChanging(); this.IsProperlyNested = src.IsProperlyNested; this.HasStartOrEmptyTag = src.HasStartOrEmptyTag; this.HasEndTag = src.HasEndTag; OnChanged(); return(true); } else { return(false); } }
public ElementWrapper(AXmlElement element) { this.LocalName = element.LocalName; this.Prefix = element.Prefix; this.Namespace = element.Namespace; this.Offset = element.StartOffset; this.Attributes = element.Attributes.Select(attr => new AttributeWrapper(attr)).ToList(); }
/// <summary> Find the defualt namespace for this context </summary> public string FindDefaultNamespace() { AXmlElement current = this; while (current != null) { string namesapce = current.GetAttributeValue(NoNamespace, "xmlns"); if (namesapce != null) { return(namesapce); } current = current.Parent as AXmlElement; } return(string.Empty); // No namesapce }
/// <summary> Visit RawDocument </summary> public override void VisitDocument(AXmlDocument document) { foreach (AXmlObject child in document.Children) { AXmlTag childAsTag = child as AXmlTag; // Only procssing instructions or elements if (childAsTag != null && childAsTag.IsProcessingInstruction && childAsTag.Name != "xml") { VisitTag(childAsTag); } else { AXmlElement childAsElement = child as AXmlElement; if (childAsElement != null) { VisitElement(childAsElement); } } } }
/// <summary> Visit RawElement </summary> public virtual void VisitElement(AXmlElement element) { foreach(AXmlObject child in element.Children) child.AcceptVisitor(this); }
static IReturnType TypeFromXmlNode(XamlCompilationUnit cu, AXmlElement element) { return cu.CreateType(element.Namespace, element.LocalName); }
IClass AddClass(string className, AXmlElement element) { if (projectContent.Language == LanguageProperties.VBNet && projectContent.Project is IProject) className = ((IProject)projectContent.Project).RootNamespace + "." + className; DefaultClass c = new DefaultClass(CompilationUnit, className); string modifierValue = (element.GetAttributeValue(CompletionDataHelper.XamlNamespace, "ClassModifier") ?? string.Empty).Trim(); c.Modifiers = ModifierEnum.Partial; string internalString = currentAmbience.ConvertAccessibility(ModifierEnum.Internal).Trim(); if (projectContent.Language.NameComparer.Compare(modifierValue, internalString) == 0) c.Modifiers |= ModifierEnum.Internal; else c.Modifiers |= ModifierEnum.Public; c.Region = CreateRegion(element.StartOffset, element.EndOffset); var baseType = TypeFromXmlNode(CompilationUnit, element); if (baseType != null) c.BaseTypes.Add(baseType); CompilationUnit.Classes.Add(c); DefaultMethod initializeComponent = new DefaultMethod( "InitializeComponent", projectContent.SystemTypes.Void, ModifierEnum.Public | ModifierEnum.Synthetic, c.Region, DomRegion.Empty, c); c.Methods.Add(initializeComponent); return c; }
public override void VisitElement(AXmlElement element) { AXmlTag tag = element.Children.FirstOrDefault() as AXmlTag; if (tag != null && tag.IsStartOrEmptyTag) { NodeWrapper node = new NodeWrapper() { ElementName = element.LocalName, StartOffset = element.StartOffset, EndOffset = element.EndOffset, Name = element.GetAttributeValue("Name") ?? element.GetAttributeValue(CompletionDataHelper.XamlNamespace, "Name"), Children = new List<NodeWrapper>() }; if (CompilationUnit.TreeRootNode == null) { CompilationUnit.TreeRootNode = node; nodeStack.Push(CompilationUnit.TreeRootNode); } else { if (nodeStack.Count > 0) nodeStack.Peek().Children.Add(node); if (!tag.IsEmptyTag) nodeStack.Push(node); } } base.VisitElement(element); if (tag != null && tag.IsStartTag) nodeStack.PopOrDefault(); }
/// <summary> Visit RawElement </summary> public override void VisitElement(AXmlElement element) { base.VisitElement(element); }
/// <summary> Get posible configurations after considering given object </summary> Configurations ProcessObject(Configurations oldConfigs, AXmlObject obj) { AXmlParser.Log("Processing {0}", obj); AXmlTag objAsTag = obj as AXmlTag; AXmlElement objAsElement = obj as AXmlElement; AXmlParser.DebugAssert(objAsTag != null || objAsElement != null || obj is AXmlText, obj.GetType().Name + " not expected"); if (objAsElement != null) { AXmlParser.Assert(objAsElement.IsProperlyNested, "Element not properly nested"); } Configurations newConfigs = new Configurations(); foreach (var kvp in oldConfigs) { Configuration oldConfig = kvp.Value; var oldStartTags = oldConfig.StartTags; var oldDocument = oldConfig.Document; int oldCost = oldConfig.Cost; if (objAsTag != null && objAsTag.IsStartTag) { newConfigs.Add(new Configuration { // Push start-tag (cost 0) StartTags = oldStartTags.Push(objAsTag), Document = oldDocument.Push(objAsTag), Cost = oldCost, }); } else if (objAsTag != null && objAsTag.IsEndTag) { newConfigs.Add(new Configuration { // Ignore (cost 1) StartTags = oldStartTags, Document = oldDocument.Push(StartTagPlaceholder).Push(objAsTag), Cost = oldCost + 1, }); if (!oldStartTags.IsEmpty && oldStartTags.Peek().Name != objAsTag.Name) { newConfigs.Add(new Configuration { // Pop 1 item (cost 1) - not mathcing StartTags = oldStartTags.Pop(), Document = oldDocument.Push(objAsTag), Cost = oldCost + 1, }); } int popedCount = 0; var startTags = oldStartTags; var doc = oldDocument; foreach (AXmlTag poped in oldStartTags) { popedCount++; if (poped.Name == objAsTag.Name) { newConfigs.Add(new Configuration { // Pop 'x' items (cost x-1) - last one is matching StartTags = startTags.Pop(), Document = doc.Push(objAsTag), Cost = oldCost + popedCount - 1, }); } startTags = startTags.Pop(); doc = doc.Push(EndTagPlaceholder); } } else { // Empty tag or other tag type or text or properly nested element newConfigs.Add(new Configuration { // Ignore (cost 0) StartTags = oldStartTags, Document = oldDocument.Push(obj), Cost = oldCost, }); } } // Log("New configurations:" + newConfigs.ToString()); Configurations bestNewConfigurations = new Configurations( newConfigs.Values.OrderBy(v => v.Cost).Take(maxConfigurationCount) ); // AXmlParser.Log("Best new configurations:" + bestNewConfigurations.ToString()); return(bestNewConfigurations); }