Browse Source

implement x:Class detection in BamlDecompiler

pull/234/merge
Siegfried Pammer 14 years ago
parent
commit
f20cd83e6d
  1. 14
      ILSpy.BamlDecompiler/BamlResourceEntryNode.cs
  2. 1
      ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj
  3. 29
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs
  4. 34
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlSimpleProperty.cs

14
ILSpy.BamlDecompiler/BamlResourceEntryNode.cs

@ -45,17 +45,11 @@ namespace ILSpy.BamlDecompiler @@ -45,17 +45,11 @@ namespace ILSpy.BamlDecompiler
return true;
}
const string XWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml";
const string DefaultWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
bool LoadBaml(AvalonEditTextOutput output)
{
var asm = this.Ancestors().OfType<AssemblyTreeNode>().FirstOrDefault().LoadedAssembly;
Data.Position = 0;
XDocument xamlDocument = LoadIntoDocument(asm.GetAssemblyResolver(), asm.AssemblyDefinition, Data);
output.Write(xamlDocument.ToString());
return true;
}
@ -73,18 +67,18 @@ namespace ILSpy.BamlDecompiler @@ -73,18 +67,18 @@ namespace ILSpy.BamlDecompiler
static void MoveNamespacesToRoot(XDocument xamlDocument)
{
var additionalXmlns = new List<XAttribute> {
new XAttribute("xmlns", DefaultWPFNamespace),
new XAttribute(XName.Get("x", XNamespace.Xmlns.NamespaceName), XWPFNamespace)
new XAttribute("xmlns", XmlBamlReader.DefaultWPFNamespace),
new XAttribute(XName.Get("x", XNamespace.Xmlns.NamespaceName), XmlBamlReader.XWPFNamespace)
};
foreach (var element in xamlDocument.Root.DescendantsAndSelf()) {
if (element.Name.NamespaceName != DefaultWPFNamespace && !additionalXmlns.Any(ka => ka.Value == element.Name.NamespaceName)) {
if (element.Name.NamespaceName != XmlBamlReader.DefaultWPFNamespace && !additionalXmlns.Any(ka => ka.Value == element.Name.NamespaceName)) {
string newPrefix = new string(element.Name.LocalName.Where(c => char.IsUpper(c)).ToArray()).ToLowerInvariant();
int current = additionalXmlns.Count(ka => ka.Name.Namespace == XNamespace.Xmlns && ka.Name.LocalName.TrimEnd(ch => char.IsNumber(ch)) == newPrefix);
if (current > 0)
newPrefix += (current + 1).ToString();
XName defaultXmlns = XName.Get(newPrefix, XNamespace.Xmlns.NamespaceName);
if (element.Name.NamespaceName != DefaultWPFNamespace)
if (element.Name.NamespaceName != XmlBamlReader.DefaultWPFNamespace)
additionalXmlns.Add(new XAttribute(defaultXmlns, element.Name.NamespaceName));
}
}

1
ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj

@ -98,6 +98,7 @@ @@ -98,6 +98,7 @@
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlBamlProperty.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlBamlPropertyElement.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlBamlReader.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlBamlSimpleProperty.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlBamlText.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlNamespace.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlPIMapping.cs" />

29
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs

@ -55,6 +55,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -55,6 +55,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
private readonly TypeDeclaration XamlTypeDeclaration;
private readonly XmlNameTable _nameTable = new NameTable();
private IDictionary<string, string> _rootNamespaces;
public const string XWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml";
public const string DefaultWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
#endregion
@ -160,7 +163,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -160,7 +163,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
public override bool MoveToFirstAttribute()
{
intoAttribute = false;
if (nodes.Count > 0 && nodes.Peek() is XmlBamlProperty)
if (nodes.Count > 0 && (nodes.Peek() is XmlBamlProperty || nodes.Peek() is XmlBamlSimpleProperty))
{
_currentNode = nodes.Dequeue();
return true;
@ -179,7 +182,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -179,7 +182,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
public override bool MoveToNextAttribute()
{
intoAttribute = false;
if (nodes.Count > 0 && nodes.Peek() is XmlBamlProperty)
if (nodes.Count > 0 && (nodes.Peek() is XmlBamlProperty || nodes.Peek() is XmlBamlSimpleProperty))
{
_currentNode = nodes.Dequeue();
return true;
@ -197,7 +200,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -197,7 +200,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
///
public override bool MoveToElement()
{
while (nodes.Peek() is XmlBamlProperty)
while (nodes.Peek() is XmlBamlProperty || nodes.Peek() is XmlBamlSimpleProperty)
{
nodes.Dequeue();
}
@ -295,6 +298,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -295,6 +298,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
_currentNode = nodes.Dequeue();
if ((_currentNode is XmlBamlProperty)) continue;
if ((_currentNode is XmlBamlSimpleProperty)) continue;
if (this.NodeType == XmlNodeType.EndElement)
{
@ -580,7 +584,10 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -580,7 +584,10 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
String localName = String.Empty;
XmlBamlNode node = this.CurrentNode;
if (node is XmlBamlProperty)
if (node is XmlBamlSimpleProperty) {
var simpleNode = (XmlBamlSimpleProperty)node;
localName = simpleNode.LocalName;
} else if (node is XmlBamlProperty)
{
PropertyDeclaration pd = ((XmlBamlProperty)node).PropertyDeclaration;
localName = FormatPropertyDeclaration(pd, false, true, true);
@ -1185,13 +1192,19 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1185,13 +1192,19 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
// the type is defined in the local assembly, i.e., the main assembly
// and this is the root element
TypeDeclaration oldDeclaration = null;
if (_resolver.IsLocalAssembly(declaration.Assembly) && parentElement == null) {
oldDeclaration = declaration;
declaration = GetKnownTypeDeclarationByName(declaration.Type.BaseType.AssemblyQualifiedName);
}
element.TypeDeclaration = declaration;
elements.Push(element);
nodes.Enqueue(element);
if (oldDeclaration != null) {
nodes.Enqueue(new XmlBamlSimpleProperty(XWPFNamespace, "Class", string.Format("{0}.{1}", oldDeclaration.Namespace, oldDeclaration.Name)));
}
if (parentElement != null && complexPropertyOpened == 0)
{
@ -1609,7 +1622,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1609,7 +1622,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
TypeDeclaration declaration;
XmlBamlNode node = this.CurrentNode;
if (node is XmlBamlProperty)
if (node is XmlBamlSimpleProperty)
return ((XmlBamlSimpleProperty)node).NamespaceName;
else if (node is XmlBamlProperty)
{
declaration = ((XmlBamlProperty)node).PropertyDeclaration.DeclaringType;
TypeDeclaration elementDeclaration = this.readingElements.Peek().TypeDeclaration;
@ -1685,7 +1700,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1685,7 +1700,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
get
{
XmlBamlNode node = this.CurrentNode;
if (node is XmlBamlProperty)
if (node is XmlBamlSimpleProperty)
return ((XmlBamlSimpleProperty)node).Value;
else if (node is XmlBamlProperty)
return ((XmlBamlProperty)node).Value.ToString();
else if (node is XmlBamlText)
return ((XmlBamlText)node).Text;

34
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlSimpleProperty.cs

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
// This code is distributed under the MS-PL (for details please see \doc\MS-PL.txt)
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Ricciolo.StylesExplorer.MarkupReflection
{
class XmlBamlSimpleProperty : XmlBamlNode
{
public string NamespaceName { get; private set; }
public string LocalName { get; private set; }
public string Value { get; private set; }
public XmlBamlSimpleProperty(string namespaceName, string localName, string value)
{
if (string.IsNullOrWhiteSpace(namespaceName))
throw new ArgumentException("namespaceName");
if (string.IsNullOrWhiteSpace(localName))
throw new ArgumentException("localName");
if (value == null)
throw new ArgumentNullException("value");
this.NamespaceName = namespaceName;
this.LocalName = localName;
this.Value = value;
}
public override XmlNodeType NodeType {
get { return XmlNodeType.Attribute; }
}
}
}
Loading…
Cancel
Save