2018-10-02 01:13:54 +03:00
|
|
|
|
using System;
|
2018-11-03 04:24:57 +03:00
|
|
|
|
using System.Collections.Generic;
|
2018-10-02 01:13:54 +03:00
|
|
|
|
using System.IO;
|
2018-11-03 04:24:57 +03:00
|
|
|
|
using System.Linq;
|
2018-10-02 01:13:54 +03:00
|
|
|
|
using System.Xml;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
namespace XAMLator
|
|
|
|
|
{
|
|
|
|
|
public class XAMLDocument
|
|
|
|
|
{
|
2018-11-03 04:24:57 +03:00
|
|
|
|
public XAMLDocument(string filePath, string xaml, string type, List<string> styleSheets)
|
2018-10-02 01:13:54 +03:00
|
|
|
|
{
|
2018-11-03 04:24:57 +03:00
|
|
|
|
FilePath = filePath;
|
2018-10-02 01:13:54 +03:00
|
|
|
|
XAML = xaml;
|
|
|
|
|
Type = type;
|
2018-11-03 04:24:57 +03:00
|
|
|
|
StyleSheets = styleSheets;
|
2018-10-02 01:13:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-03 04:24:57 +03:00
|
|
|
|
public string XAML { get; set; }
|
|
|
|
|
|
|
|
|
|
public string Type { get; set; }
|
|
|
|
|
|
|
|
|
|
public string FilePath { get; set; }
|
|
|
|
|
|
|
|
|
|
public List<string> StyleSheets { get; set; }
|
|
|
|
|
|
|
|
|
|
public static XAMLDocument Parse(string filePath, string xaml)
|
2018-10-02 01:13:54 +03:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var stream = new StringReader(xaml))
|
|
|
|
|
{
|
|
|
|
|
var reader = XmlReader.Create(stream);
|
|
|
|
|
var xdoc = XDocument.Load(reader);
|
|
|
|
|
XNamespace x = "http://schemas.microsoft.com/winfx/2009/xaml";
|
2018-11-03 04:24:57 +03:00
|
|
|
|
XNamespace xm = "http://xamarin.com/schemas/2014/forms";
|
2018-10-02 01:13:54 +03:00
|
|
|
|
var classAttribute = xdoc.Root.Attribute(x + "Class");
|
|
|
|
|
CleanAutomationIds(xdoc.Root);
|
2018-11-03 14:58:20 +03:00
|
|
|
|
var styleSheetElements = xdoc.Root
|
|
|
|
|
.Descendants()
|
|
|
|
|
.Where(e => e.Name.ToString().EndsWith("StyleSheet"));
|
|
|
|
|
CleanStyleSheets(styleSheetElements);
|
|
|
|
|
var styleSheets = styleSheetElements
|
|
|
|
|
.Select(e => e.Attribute("Source"))
|
|
|
|
|
.Where(e => e != null)
|
|
|
|
|
.Select(e => e.Value)
|
|
|
|
|
.ToList();
|
2018-10-02 01:13:54 +03:00
|
|
|
|
xaml = xdoc.ToString();
|
2018-11-03 04:24:57 +03:00
|
|
|
|
return new XAMLDocument(filePath, xaml, classAttribute.Value, styleSheets);
|
2018-10-02 01:13:54 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Exception(ex);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-03 14:58:20 +03:00
|
|
|
|
static void CleanStyleSheets(IEnumerable<XElement> elements)
|
|
|
|
|
{
|
|
|
|
|
foreach (var element in elements)
|
|
|
|
|
{
|
|
|
|
|
var attr = element.Attributes().SingleOrDefault(a => a.Name == "Source");
|
|
|
|
|
if (attr == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (attr.Value.StartsWith("/"))
|
|
|
|
|
{
|
|
|
|
|
attr.SetValue(Constants.ROOT_REPLACEMENT + attr.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 01:13:54 +03:00
|
|
|
|
static void CleanAutomationIds(XElement xdoc)
|
|
|
|
|
{
|
|
|
|
|
xdoc.SetAttributeValue("AutomationId", null);
|
|
|
|
|
foreach (var el in xdoc.Elements())
|
|
|
|
|
{
|
|
|
|
|
CleanAutomationIds(el);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|