Example #1
0
 public static void AddAttributeToElement(XAttribute attribute, XElement element)
 {
     // You can't just add an attribute to an element. You have to retrive the list of current attributes on an element, add the new attribute to the list, and then call ReplaceAttributes on element
     List<XAttribute> attributes = element.Attributes().ToList();
     attributes.Add(attribute);
     element.ReplaceAttributes(attributes);
 }
Example #2
0
		void RewriteClass(XamlContext ctx, XElement elem) {
			var type = elem.Annotation<XamlType>();
			if (type == null || type.ResolvedType == null)
				return;

			var typeDef = type.ResolvedType.ResolveTypeDef();
			var comparer = new AssemblyNameComparer(AssemblyNameComparerFlags.All);
			if (typeDef == null || !comparer.Equals(typeDef.DefinitionAssembly, ctx.Module.Assembly))
				return;

			var newType = typeDef.BaseType;
			var xamlType = new XamlType(newType.DefinitionAssembly, newType.ReflectionNamespace, newType.Name);
			xamlType.ResolveNamespace(elem, ctx);

			elem.Name = xamlType.ToXName(ctx);

			var attrName = ctx.GetXamlNsName("Class", elem);

			var attrs = elem.Attributes().ToList();
			if (typeDef.IsNotPublic) {
				var classModifierName = ctx.GetXamlNsName("ClassModifier", elem);
				attrs.Insert(0, new XAttribute(classModifierName, ctx.BamlDecompilerOptions.InternalClassModifier));
			}
			attrs.Insert(0, new XAttribute(attrName, type.ResolvedType.ReflectionFullName));
			elem.ReplaceAttributes(attrs);
		}
        public void ProcessElement(XElement element)
        {
            if (this.initializedAttributes == null)
            {
                throw new InvalidOperationException("AttributeRemovalService not initialized");
            }

            if (!this.IsEnabled)
            {
                return;
            }

            if (!element.HasAttributes)
            {
                return;
            }

            // Process all nodes if NodeNames is empty, otherwise process only specified nodes.
            if ((this.NodeNames.Count == 0) || this.NodeNames.Any(_ => _.IsMatch(element.Name)))
            {
                var elementAttributeList = element.Attributes().ToList();
                var removedAttributeList = new List<XAttribute>();

                foreach (XAttribute elementAttribute in elementAttributeList)
                {
                    if (elementAttribute.IsNamespaceDeclaration)
                    {
                        // Handle namespace declarations separately from other attributes.
                        if (this.NamespaceDeclarations.Any(_ => (_.NamespaceName == elementAttribute.Name.ToString())))
                        {
                            removedAttributeList.Add(elementAttribute);
                        }
                    }
                    else
                    {
                        foreach (AttributeSelector attribute in this.initializedAttributes)
                        {
                            if (attribute.IsMatch(elementAttribute)
                                && (String.IsNullOrEmpty(attribute.Value) || attribute.Value.Equals(elementAttribute.Value)))
                            {
                                removedAttributeList.Add(elementAttribute);
                                continue;
                            }
                        }
                    }
                }

                foreach (XAttribute removedAttribute in removedAttributeList)
                {
                    elementAttributeList.Remove(removedAttribute);
                }

                element.ReplaceAttributes(elementAttributeList);
            }
        }
Example #4
0
        private static XElement StripNamespaces(XElement inRoot)
        {
            var res = new XElement(
                inRoot.Name.LocalName,
                inRoot.HasElements
                    ? inRoot.Elements().Select(X => StripNamespaces(X))
                    : (Object)inRoot.Value
            );

            res.ReplaceAttributes(inRoot.Attributes().Where(X => (!X.IsNamespaceDeclaration)));

            return res;
        }
        public static XElement StripNameSpace(this XElement root)
        {
            var res = new XElement(
               root.Name.LocalName,
               root.HasElements ?
                   root.Elements().Select(StripNameSpace) :
                   (object)root.Value
           );

            res.ReplaceAttributes(
                root.Attributes().Where(attr => (!attr.IsNamespaceDeclaration)));

            return res;
        }
 private static void SetDrawingElementxKey(XElement drawingElement, string name)
 {
     if (string.IsNullOrWhiteSpace(name))
         return;
     var attributes = drawingElement.Attributes().ToList();
     attributes.Insert(0, new XAttribute(nsx + "Key", name)); //place in first position
     drawingElement.ReplaceAttributes(attributes);
 }
Example #7
0
 public static XElement StripNS(XElement root)
 {
     var res = new XElement(root);
     res.ReplaceAttributes(root.Attributes().Where(attr => (!attr.IsNamespaceDeclaration)));
     return res;
 }
Example #8
0
 public void ExecuteXAttributeVariation(XAttribute[] content)
 {
     XElement xElem = new XElement("root", content);
     XElement xElemOriginal = new XElement(xElem);
     using (UndoManager undo = new UndoManager(xElem))
     {
         undo.Group();
         using (EventsHelper eHelper = new EventsHelper(xElem))
         {
             xElem.ReplaceAttributes(new XAttribute("a", "aa"));
             Assert.True(XObject.ReferenceEquals(xElem.FirstAttribute, xElem.LastAttribute), "Did not replace attributes correctly");
             xElem.Verify();
             eHelper.Verify(content.Length + 1);
         }
         undo.Undo();
         Assert.True(xElem.Nodes().SequenceEqual(xElemOriginal.Nodes(), XNode.EqualityComparer), "Undo did not work!");
         Assert.True(xElem.Attributes().EqualsAllAttributes(xElemOriginal.Attributes(), Helpers.MyAttributeComparer), "Undo did not work!");
     }
 }
Example #9
0
                //[Variation(Desc = "Tuple - New Dev10 Types", Param = 1)]
                //[Variation(Desc = "DynamicObject - New Dev10 Types", Param = 2)]
                //[Variation(Desc = "Guid - old type", Param = 3)]
                //[Variation(Desc = "Dictionary - old type", Param = 4)]
                public void CreatingXElementsFromNewDev10Types()
                {
                    object t = null;
                    Type type = typeof(object);
                    int param = (int)this.Variation.Param;
                    switch (param)
                    {
                        case 1: t = Tuple.Create(1, "Melitta", 7.5); type = typeof(Tuple); break;
                        case 3: t = new Guid(); type = typeof(Guid); break;
                        case 4: t = new Dictionary<int, string>(); ((Dictionary<int, string>)t).Add(7, "a"); type = typeof(Dictionary<int, string>); break;
                    }

                    XElement e = new XElement("e1",
                        new XElement("e2"), "text1",
                        new XElement("e3"), t);
                    e.Add(t);
                    e.FirstNode.ReplaceWith(t);

                    XNode n = e.FirstNode.NextNode;
                    n.AddBeforeSelf(t);
                    n.AddAnnotation(t);
                    n.ReplaceWith(t);

                    e.FirstNode.AddAfterSelf(t);
                    e.AddFirst(t);
                    e.Annotation(type);
                    e.Annotations(type);
                    e.RemoveAnnotations(type);
                    e.ReplaceAll(t);
                    e.ReplaceAttributes(t);
                    e.ReplaceNodes(t);
                    e.SetAttributeValue("a", t);
                    e.SetElementValue("e2", t);
                    e.SetValue(t);

                    XAttribute a = new XAttribute("a", t);
                    XStreamingElement se = new XStreamingElement("se", t);
                    se.Add(t);

                    try
                    {
                        new XDocument(t);
                    }
                    catch (ArgumentException)
                    {
                        try
                        {
                            new XDocument(t);
                        }
                        catch (ArgumentException)
                        {
                            return;
                        }
                    }
                    TestLog.Compare(false, "Failed");
                }
Example #10
0
 public static void RemoveAttribute(XAttribute attribute, XElement element)
 {
     List<XAttribute> attributes = element.Attributes().ToList();
     attributes.Remove(attribute);
     element.ReplaceAttributes(attributes);
 }
Example #11
0
        /// <summary>
        /// Replaces oldAttribute with newAttribute
        /// </summary>
        /// <param name="oldAttribute"></param>
        /// <param name="newAttribute"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        public static bool ReplaceAttribute(XAttribute oldAttribute, XAttribute newAttribute, XElement element)
        {
            // Gets the attributes that element has
            List<XAttribute> attList = element.Attributes().ToList();
            // Find the index of the old attribute
            int indexSelectedAttribute = attList.IndexOf(oldAttribute);
            // Replace it with the new attribute
            attList[indexSelectedAttribute] = newAttribute;
            try
            {
                // Update the list of attribute on element, with the new list
                element.ReplaceAttributes(attList);
            }
            catch (System.InvalidOperationException)
            {
                // If the new attribute is invalid (e.g. the attribute name i already in use), we role back the changes
                attList.Remove(newAttribute);
                attList.Add(oldAttribute);
                element.ReplaceAttributes(attList);
                return false;
            }

            return true;
        }
Example #12
0
        private static XElement StripNS(XElement root)
        {
            XElement res = new XElement(
                root.Name.LocalName,
                root.HasElements ?
                    root.Elements().Select(el => StripNS(el)) :
                    (object)root.Value
            );

            res.ReplaceAttributes(
                root.Attributes().Where(attr => (!attr.IsNamespaceDeclaration)));

            return res;
        }
Example #13
0
        private void SortElement(XElement xe, bool sortElements, bool sortAttributes)
        {
            if (!xe.Elements().Any())
            {
                return;
            }

            if (sortElements)
            {
                xe.ReplaceNodes(xe.Elements().OrderBy(x => x.Name.LocalName));
            }
            if (sortAttributes)
            {
                xe.ReplaceAttributes(xe.Attributes().OrderBy(x => x.Name.LocalName));
            }

            foreach (XElement xc in xe.Elements())
            {
                SortElement(xc, sortElements, sortAttributes);
            }
        }