Ejemplo n.º 1
0
 private static void AddAttribute(XamlAttribute attribute, XamlObjectNode objNode)
 {
     if (attribute.Name != "_UnknownContent")
     {
         attribute.Parent = objNode;
         objNode.Attributes.Add(attribute);
     }
 }
Ejemplo n.º 2
0
 public void WriteTranslation(XamlAttribute attribute)
 {
     //{ attribute.XamlType.PreferredXamlNamespace}_: _{ attribute.XamlType.Name} - all the same
     Trace.WriteLine(
         $"({attribute.DeclaringType?.Name},{attribute.XamlType.UnderlyingType})W={attribute.IsWritePublic} {attribute.Name}={attribute.Value?.GetType()}/{attribute.Value}");
 }
Ejemplo n.º 3
0
        //private readonly ProjectDataItemBase _selectedProject;

        //public XamlFileParser(ProjectDataItemBase selectedProject)
        //{
        //    _selectedProject = selectedProject;
        //}

        //public async Task CollectStringsAsync()
        //{
        //    //_selectedProject.UiFiles.Clear();
        //    foreach (FileDataItemBase fileDataItem in _selectedProject.AllFiles)
        //    {
        //        if (fileDataItem.ItemType == FileDataItemBase.EItemType.XamlPage
        //            || fileDataItem.ItemType == FileDataItemBase.EItemType.XamlApplicationDefinition)
        //        {
        //            fileDataItem.Translations.Clear();
        //            XamlRootNode root = Parse(fileDataItem.FullPath);
        //            //XamlNodeTracer.TraceNode(root);
        //            XamlNodeWalker nodeWalker = new XamlNodeWalker();

        //            XamlTranslationWriter translationTracer = new XamlTranslationWriter(fileDataItem);
        //            XamlCodeTextVisitor visitor = new XamlCodeTextVisitor(translationTracer);
        //            nodeWalker.Visit(root, visitor);
        //        }

        //        await Task.Delay(10);
        //    }
        //}

        public static XamlRootNode Parse(string fullPath)
        {
            int          xamlObjectCount = 0;
            XamlRootNode root            = new XamlRootNode();

            try
            {
                //XamlNamespaces namespaces = new XamlNamespaces();
                //root.Children.Add(namespaces);
                Stack <XamlObjectNode>              objStack           = new Stack <XamlObjectNode>();
                Stack <XamlStartMemberTempData>     memberStack        = new Stack <XamlStartMemberTempData>();
                Stack <XamlExtensionObjectNodeBase> extensionsObjStack = new Stack <XamlExtensionObjectNodeBase>();

                XamlStartMemberTempData startMemberTempData = null;
                XamlValueTempData       valueTempData       = null;
                //bool wasGetObject = false;

                XmlReaderSettings readerSettings = new XmlReaderSettings();
                readerSettings.IgnoreComments = false;

                XamlXmlReaderSettings xamlReaderSettings = new XamlXmlReaderSettings();
                xamlReaderSettings.IgnoreUidsOnPropertyElements = false;
                xamlReaderSettings.ProvideLineInfo = true;

                XmlReader     xmlReader  = XmlReader.Create(fullPath, readerSettings);
                XamlXmlReader xamlReader = new XamlXmlReader(xmlReader, xamlReaderSettings);

                xamlReader.Read();
                while (!xamlReader.IsEof)
                {
                    XamlNodeType nodeType = xamlReader.NodeType;
                    Trace.WriteLine($"--{xamlReader.LineNumber}:{xamlReader.LinePosition}.{nodeType}--");
                    switch (nodeType)
                    {
                    case XamlNodeType.None:
                        break;

                    case XamlNodeType.StartObject:
                    {
                        //XamlBindingObjectNode
                        if (xamlObjectCount == 0)
                        {
                            XamlMainObjectNode mainObject = new XamlMainObjectNode(xamlReader.Type)
                            {
                                LineNumberStart   = xamlReader.LineNumber,
                                LinePositionStart = xamlReader.LinePosition
                            };
                            root.MainObject = mainObject;
                            objStack.Push(mainObject);
                        }
                        else
                        {
                            XamlObjectNode obj;
                            if (xamlReader.Type.Name == XamlBindingExtensionObjectNode.Ident)
                            {
                                obj = new XamlBindingExtensionObjectNode(xamlReader.Type);
                                extensionsObjStack.Push((XamlExtensionObjectNodeBase)obj);
                            }
                            else if (xamlReader.Type.Name == XamlTemplateBindingExtensionObjectNode.Ident)
                            {
                                obj = new XamlTemplateBindingExtensionObjectNode(xamlReader.Type);
                                extensionsObjStack.Push((XamlExtensionObjectNodeBase)obj);
                            }
                            else if (xamlReader.Type.Name == XamlDynResObjectNode.Ident)
                            {
                                obj = new XamlDynResObjectNode(xamlReader.Type);
                                extensionsObjStack.Push((XamlExtensionObjectNodeBase)obj);
                            }
                            else if (xamlReader.Type.Name == XamlTypeExtensionObjectNode.Ident)
                            {
                                obj = new XamlTypeExtensionObjectNode(xamlReader.Type);
                                extensionsObjStack.Push((XamlExtensionObjectNodeBase)obj);
                            }
                            else if (xamlReader.Type.Name == XamlStatResObjectNode.Ident)
                            {
                                obj = new XamlStatResObjectNode(xamlReader.Type);
                                extensionsObjStack.Push((XamlExtensionObjectNodeBase)obj);
                            }
                            else if (xamlReader.Type.Name == XamlStaticExtensionObjectNode.Ident)
                            {
                                obj = new XamlStaticExtensionObjectNode(xamlReader.Type);
                                extensionsObjStack.Push((XamlExtensionObjectNodeBase)obj);
                            }
                            else
                            {
                                obj = new XamlObjectNode(xamlReader.Type);
                                //if (memberStack.Count == 0)
                                {
                                    if (objStack.Count > 0)
                                    {
                                        XamlObjectNode objNode = objStack.Peek();
                                        obj.Parent = objNode;
                                        objNode.Children.Add(obj);
                                    }
                                }
                                //else
                                //{
                                //    XamlStartMemberTempData memberTempData = memberStack.Peek();
                                //    XamlObjectNode obj2 = new XamlObjectNode(memberTempData.Type){Name = memberTempData.Name};
                                //    obj2.Children.Add(obj);
                                //}
                            }

                            obj.LineNumberStart   = xamlReader.LineNumber;
                            obj.LinePositionStart = xamlReader.LinePosition;

                            //root.Children.Add(obj);
                            objStack.Push(obj);
                        }

                        XamlSchemaContext schemaContext = xamlReader.SchemaContext;
                        XamlType          readerType    = xamlReader.Type;
                        Trace.WriteLine($"{xamlReader.Type},{xamlReader.Type.ItemType}---{readerType.Name}");
                        xamlObjectCount++;
                    }
                    break;

                    case XamlNodeType.GetObject:
                    {
                        XamlObjectNode obj;

                        //if (startMemberTempData.Name == ResourcesSectionName)
                        //{
                        //    obj = root.Resources;
                        //}
                        //else
                        //{
                        //    obj = new XamlObjectNode(startMemberTempData.Type) { Name = startMemberTempData.Name };
                        //    if (objStack.Count > 0)
                        //    {
                        //        XamlObjectNode objNode = objStack.Peek();
                        //        objNode.Children.Add(obj);
                        //    }
                        //}

                        //obj.LinePositionStart = xamlReader.LinePosition;
                        //obj.LineNumberStart = xamlReader.LineNumber;
                        //objStack.Push(obj);
                    }
                        //wasGetObject = true;
                        break;

                    case XamlNodeType.EndObject:
                    {
                        if (objStack.Count > 0)
                        {
                            //remove current object
                            XamlObjectNode objNode = objStack.Pop();

                            objNode.LineNumberEnd   = xamlReader.LineNumber;
                            objNode.LinePositionEnd = xamlReader.LinePosition;
                            //if (objNode.NodeType == XamlNodeBase.ENodeType.BindingObject)
                            //{
                            //    bindingStack
                            //}
                        }
                        else
                        {
                            //for breakpoint
                            Trace.WriteLine("Error obj stack is empty");
                        }
                    }
                    break;

                    case XamlNodeType.StartMember:
                    {
                        //if (wasGetObject)
                        //{
                        //    break;
                        //}
                        XamlMember member        = xamlReader.Member;
                        XamlType   memberType    = member.Type;
                        XamlType   declaringType = member.DeclaringType;
                        if (declaringType != null && member.Name == ResourcesSectionName)
                        {
                            XamlResourceCollectionNode xamlResourceCollectionNode = new XamlResourceCollectionNode(member.Type);
                            //root.Resources = new List<XamlNodeBase>();
                            root.MainObject.Resources = xamlResourceCollectionNode;

                            xamlResourceCollectionNode.Name = member.Name;
                            xamlResourceCollectionNode.LinePositionStart = xamlReader.LinePosition;
                            xamlResourceCollectionNode.LineNumberStart   = xamlReader.LineNumber;

                            xamlResourceCollectionNode.Parent = root.MainObject;
                            root.MainObject.Children.Add(xamlResourceCollectionNode);
                            objStack.Push(xamlResourceCollectionNode);
                        }

                        IList <string> xamlNamespaces = member.GetXamlNamespaces();
                        string         namespacesStr  = String.Join(";", xamlNamespaces);
                        Trace.WriteLine($"{namespacesStr} ___ {memberType}({declaringType}).{member.Name} === {member}");
                        startMemberTempData                   = new XamlStartMemberTempData();
                        startMemberTempData.Type              = member.Type;
                        startMemberTempData.Name              = member.Name;
                        startMemberTempData.DeclaringType     = member.DeclaringType;
                        startMemberTempData.IsWritePublic     = member.IsWritePublic;
                        startMemberTempData.LineNumberStart   = xamlReader.LineNumber;
                        startMemberTempData.LinePositionStart = xamlReader.LinePosition;
                        valueTempData = null;
                        memberStack.Push(startMemberTempData);
                    }

                    break;

                    case XamlNodeType.EndMember:
                    {
                        if (startMemberTempData != null)
                        {
                            if (valueTempData != null)
                            {
                                //remove last member
                                if (memberStack.Count > 0)
                                {
                                    XamlStartMemberTempData memberTempData = memberStack.Pop();
                                }

                                if (startMemberTempData.Name == "base")
                                {
                                    root.Path = valueTempData.Value?.ToString();
                                }
                                else
                                {
                                    XamlAttribute attribute = new XamlAttribute(
                                        startMemberTempData.Type,
                                        startMemberTempData.Name,
                                        valueTempData.Value);

                                    attribute.LineNumberStart   = startMemberTempData.LineNumberStart;
                                    attribute.LinePositionStart = startMemberTempData.LinePositionStart;
                                    attribute.LineNumberEnd     = xamlReader.LineNumber;
                                    attribute.LinePositionEnd   = xamlReader.LinePosition;
                                    attribute.DeclaringType     = startMemberTempData.DeclaringType;
                                    attribute.IsWritePublic     = startMemberTempData.IsWritePublic;
                                    if (objStack.Count > 0)
                                    {
                                        XamlObjectNode objNode = objStack.Peek();

                                        AddAttribute(attribute, objNode);
                                    }
                                }

                                valueTempData = null;
                            }
                            else
                            {
                                AddAttributesWithObjValue(memberStack, extensionsObjStack, objStack);
                            }

                            startMemberTempData = null;
                        }
                        else
                        {
                            AddAttributesWithObjValue(memberStack, extensionsObjStack, objStack);
                        }
                    }

                    break;

                    case XamlNodeType.Value:
                    {
                        object value = xamlReader.Value;
                        Trace.WriteLine($"{value.GetType()}-{value}");
                        valueTempData = new XamlValueTempData {
                            Type = value.GetType(), Value = value
                        };
                    }

                    break;

                    case XamlNodeType.NamespaceDeclaration:
                    {
                        if (root.Namespaces == null)
                        {
                            root.Namespaces = new List <XamlNamespace>();
                        }

                        NamespaceDeclaration namespaceDeclaration = xamlReader.Namespace;
                        XamlNamespace        ns = new XamlNamespace(namespaceDeclaration.Prefix, namespaceDeclaration.Namespace)
                        {
                            LineNumberStart = xamlReader.LineNumber, LinePositionStart = xamlReader.LinePosition
                        };
                        Trace.WriteLine($"{namespaceDeclaration.Prefix}:{namespaceDeclaration.Namespace} ");
                        root.Namespaces.Add(ns);
                    }
                    break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    bool isNext = xamlReader.Read();
                }
                //XamlReader builderReader = ActivityXamlServices.CreateBuilderReader(xamlReader);
                //ActivityBuilder ab = XamlServices.Load(builderReader) as ActivityBuilder;

                //XmlWriterSettings writerSettings = new XmlWriterSettings { Indent = true };
                //XmlWriter xmlWriter = XmlWriter.Create(File.OpenWrite(args[1]), writerSettings);
                //XamlXmlWriter xamlWriter = new XamlXmlWriter(xmlWriter, new XamlSchemaContext());
                //XamlServices.Save(new ViewStateCleaningWriter(ActivityXamlServices.CreateBuilderWriter(xamlWriter)), ab);

                //Console.WriteLine("{0} written without viewstate information", args[1]);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception encountered {0}", ex);
            }


            return(root);
        }
Ejemplo n.º 4
0
        private static void AddAttributesWithObjValue(
            Stack <XamlStartMemberTempData> memberStack,
            Stack <XamlExtensionObjectNodeBase> extensionsObjStack,
            Stack <XamlObjectNode> objStack)
        {
            if (memberStack.Count > 0)
            {
                XamlExtensionObjectNodeBase extensionObjectNode = null;
                if (extensionsObjStack.Count > 0)
                {
                    extensionObjectNode = extensionsObjStack.Pop();
                }
                XamlStartMemberTempData memberTempData = memberStack.Pop();
                //if (extensionObjectNode != null)
                {
                    XamlAttribute attribute = new XamlAttribute(
                        memberTempData.Type,
                        memberTempData.Name,
                        extensionObjectNode)
                    {
                        LineNumberStart   = memberTempData.LineNumberStart,
                        LinePositionStart = memberTempData.LinePositionStart
                    };

                    if (objStack.Count > 0)
                    {
                        XamlObjectNode objNode = objStack.Peek();
                        //if (attribute.Value != null)
                        {
                            //if (!attribute.Value.Equals(objNode))
                            {
                                AddAttribute(attribute, objNode);
                            }
                        }
                        //else
                        //{
                        //    Trace.WriteLine("Skipped attribute with NULL value");
                        //}

                        if (objNode.IsExtension)
                        {
                            if (extensionsObjStack.Count > 0)
                            {
                                extensionObjectNode = extensionsObjStack.Peek();
                                if (!extensionObjectNode.Equals(objNode))
                                {
                                    extensionsObjStack.Push((XamlExtensionObjectNodeBase)objNode);
                                }
                            }
                            else
                            {
                                extensionsObjStack.Push((XamlExtensionObjectNodeBase)objNode);
                            }
                        }
                        else
                        {
                            ;
                        }
                    }
                }
                //else
                //{
                //    //if (objStack.Count > 0)
                //    //{
                //    //    XamlObjectNode objNode = objStack.Peek();
                //    //    XamlObjectNode xamlNodeBase = new XamlObjectNode(memberTempData.Type){Name = memberTempData.Name };
                //    //    objNode.Children.Add(xamlNodeBase);
                //    //    //objStack.Push(xamlNodeBase);
                //    //}
                //}
            }
        }
Ejemplo n.º 5
0
        public void Visit(XamlNodeBase node, XamlNodeWalker xamlNodeWalker)
        {
            if (_DisableObjMap == null)
            {
                _DisableObjMap = new HashSet <string>();
                _DisableObjMap.Add("Image");
                _DisableObjMap.Add("StackPanel");
                _DisableObjMap.Add("DockPanel");
                _DisableObjMap.Add("EventToCommand");
                _DisableObjMap.Add("GradientStop");
                _DisableObjMap.Add("LinearGradientBrush");
                _DisableObjMap.Add("RenderOptions");
                _DisableObjMap.Add("Rectangle");
                _DisableObjMap.Add("ViewModelSource");
                _DisableObjMap.Add("Trigger");
                _DisableObjMap.Add("ScrollViewer");
            }

            if (_DisableAttrNameMap == null)
            {
                _DisableAttrNameMap = new HashSet <string>();
                _DisableAttrNameMap.Add("AllowBestFit");
                _DisableAttrNameMap.Add("AllowEditing");
                _DisableAttrNameMap.Add("AllowGroup");
                _DisableAttrNameMap.Add("AllowSort");
                _DisableAttrNameMap.Add("AreHeadersSticky");
                _DisableAttrNameMap.Add("AutoCreateColumns");
                _DisableAttrNameMap.Add("AutoGenerateColumns");
                _DisableAttrNameMap.Add("AutoSizeMode");
                _DisableAttrNameMap.Add("AutoWidth");
                _DisableAttrNameMap.Add("Background");
                _DisableAttrNameMap.Add("BorderThickness");
                _DisableAttrNameMap.Add("BorderBrush");
                _DisableAttrNameMap.Add("CanBeCurrentWhenReadOnly");
                _DisableAttrNameMap.Add("CellHorizontalContentAlignment");
                _DisableAttrNameMap.Add("CellVerticalContentAlignment");
                _DisableAttrNameMap.Add("ContainerHeight");
                _DisableAttrNameMap.Add("CornerRadius");
                _DisableAttrNameMap.Add("ContentAlignment");
                _DisableAttrNameMap.Add("DataNavigatorButtons");
                _DisableAttrNameMap.Add("DispatcherPriority");
                _DisableAttrNameMap.Add("DockingStyle");
                _DisableAttrNameMap.Add("DragEnter");
                _DisableAttrNameMap.Add("FieldName");
                _DisableAttrNameMap.Add("FontSize");
                _DisableAttrNameMap.Add("Height");
                _DisableAttrNameMap.Add("HorizontalAlignment");
                _DisableAttrNameMap.Add("HorizontalContentAlignment");
                _DisableAttrNameMap.Add("IsCopyCommandEnabled");
                _DisableAttrNameMap.Add("Icon");
                _DisableAttrNameMap.Add("IsDefault");
                _DisableAttrNameMap.Add("IsFocused");
                _DisableAttrNameMap.Add("IsManipulationEnabled");
                _DisableAttrNameMap.Add("IsReadOnly");
                _DisableAttrNameMap.Add("ItemHeight");
                _DisableAttrNameMap.Add("ItemsSourcePath");
                _DisableAttrNameMap.Add("ItemWidth");
                _DisableAttrNameMap.Add("Margin");
                _DisableAttrNameMap.Add("MaxWidth");
                _DisableAttrNameMap.Add("MinLines");
                _DisableAttrNameMap.Add("MinWidth");
                _DisableAttrNameMap.Add("Loaded");
                _DisableAttrNameMap.Add("NavigationBehavior");
                _DisableAttrNameMap.Add("Orientation");
                _DisableAttrNameMap.Add("Padding");
                _DisableAttrNameMap.Add("Property");
                _DisableAttrNameMap.Add("ReadOnly");
                _DisableAttrNameMap.Add("RecognizesAccessKey");
                _DisableAttrNameMap.Add("Row");
                _DisableAttrNameMap.Add("SelectionChanged");
                _DisableAttrNameMap.Add("ShowColumnHeaders");
                _DisableAttrNameMap.Add("ShowDataNavigator");
                _DisableAttrNameMap.Add("ShowGroupPanel");
                _DisableAttrNameMap.Add("StartupUri");
                _DisableAttrNameMap.Add("TabStripPlacement");
                _DisableAttrNameMap.Add("TargetType");
                _DisableAttrNameMap.Add("ThemeName");
                _DisableAttrNameMap.Add("TitleAlignment");
                _DisableAttrNameMap.Add("UnboundType");
                _DisableAttrNameMap.Add("UseLayoutRounding");
                _DisableAttrNameMap.Add("VerticalAlignment");
                _DisableAttrNameMap.Add("VerticalContentAlignment");
                _DisableAttrNameMap.Add("VerticalGridLineBrush");
                _DisableAttrNameMap.Add("VerticalGridLineThickness");
                _DisableAttrNameMap.Add("VerticalScrollBarVisibility");
                _DisableAttrNameMap.Add("Visibility");
                _DisableAttrNameMap.Add("Width");
                _DisableAttrNameMap.Add("WindowStartupLocation");
                _DisableAttrNameMap.Add("WindowState");
            }

            _translationWriter.WriteEntering(node);

            if (node is XamlAttribute attribute)
            {
                if (attribute.Value != null && attribute.Value is string && attribute.DeclaringType != null)
                {
                    if (!_DisableObjMap.Contains(attribute.DeclaringType.Name) && !_DisableAttrNameMap.Contains(attribute.Name))
                    {
                        bool isAllowed = true;
                        if (attribute.Parent is XamlObjectNode parent)
                        {
                            if (parent.XamlType.Name == "Setter" || parent.XamlType.Name == "Condition")
                            {
                                XamlAttribute parentAttribute = parent.Attributes[0];
                                if (parentAttribute.Name == "Property")
                                {
                                    string parentAttributeValue = parentAttribute.Value.ToString();
                                    if (_DisableAttrNameMap.Contains(parentAttributeValue))
                                    {
                                        isAllowed = false;
                                    }
                                }
                            }
                        }

                        if (isAllowed)
                        {
                            _translationWriter.WriteTranslation(attribute);
                        }
                    }
                }
            }
        }