internal static XamlDocument Parse(XmlDocument document, XamlParserSettings settings) { if (document == null) { throw new ArgumentNullException("document"); } if (settings == null) { throw new ArgumentNullException("settings"); } XamlParser p = new XamlParser(); p.settings = settings; p.errorSink = (IXamlErrorSink)settings.ServiceProvider.GetService(typeof(IXamlErrorSink)); p.document = new XamlDocument(document, settings); try { var root = p.ParseObject(document.DocumentElement); p.document.ParseComplete(root); } catch (Exception x) { p.ReportException(x, document.DocumentElement); } return(p.document); }
/// <summary> /// Internal constructor, used by XamlParser. /// </summary> internal XamlDocument(XmlDocument xmlDoc, XamlParserSettings settings) { this._xmlDoc = xmlDoc; this._typeFinder = settings.TypeFinder; this._serviceProvider = settings.ServiceProvider; this._currentProjectAssemblyName = settings.CurrentProjectAssemblyName; }
/// <summary> /// Method use to parse a piece of Xaml. /// </summary> /// <param name="root">The Root XamlObject of the current document.</param> /// <param name="xaml">The Xaml being parsed.</param> /// <param name="settings">Parser settings used by <see cref="XamlParser"/>.</param> /// <returns>Returns the XamlObject of the parsed <paramref name="xaml"/>.</returns> public static XamlObject ParseSnippet(XamlObject root, string xaml, XamlParserSettings settings) { XmlTextReader reader = new XmlTextReader(new StringReader(xaml)); var element = root.OwnerDocument.XmlDocument.ReadNode(reader); if (element != null) { XmlAttribute xmlnsAttribute = null; foreach (XmlAttribute attrib in element.Attributes) { if (attrib.Name == "xmlns") { xmlnsAttribute = attrib; } } if (xmlnsAttribute != null) { element.Attributes.Remove(xmlnsAttribute); } RemoveRootNamespacesFromNodeAndChildNodes(root, element); XamlParser parser = new XamlParser(); parser.settings = settings; parser.document = root.OwnerDocument; var xamlObject = parser.ParseObject(element as XmlElement); if (xamlObject != null) { return(xamlObject); } } return(null); }
/// <summary> /// Parses a XAML document using an XmlReader. /// </summary> public static XamlDocument Parse(XmlReader reader, XamlParserSettings settings) { if (reader == null) { throw new ArgumentNullException("reader"); } if (settings == null) { throw new ArgumentNullException("settings"); } XmlDocument doc = new PositionXmlDocument(); var errorSink = (IXamlErrorSink)settings.ServiceProvider.GetService(typeof(IXamlErrorSink)); try { doc.Load(reader); return(Parse(doc, settings)); } catch (XmlException x) { if (errorSink != null) { errorSink.ReportError(x.Message, x.LineNumber, x.LinePosition); } else { throw; } } return(null); }
/// <summary> /// Parses a XAML document using a stream. /// </summary> public static XamlDocument Parse(Stream stream, XamlParserSettings settings) { if (stream == null) { throw new ArgumentNullException("stream"); } return(Parse(XmlReader.Create(stream), settings)); }
/// <summary> /// Parses a XAML document using a TextReader. /// </summary> public static XamlDocument Parse(TextReader reader, XamlParserSettings settings) { if (reader == null) { throw new ArgumentNullException("reader"); } return(Parse(XmlReader.Create(reader), settings)); }
/// <summary> /// Method use to parse a piece of Xaml. /// </summary> /// <param name="root">The Root XamlObject of the current document.</param> /// <param name="xaml">The Xaml being parsed.</param> /// <param name="settings">Parser settings used by <see cref="XamlParser"/>.</param> /// <returns>Returns the XamlObject of the parsed <paramref name="xaml"/>.</returns> public static XamlObject ParseSnippet(XamlObject root, string xaml, XamlParserSettings settings) { XmlTextReader reader = new XmlTextReader(new StringReader(xaml)); var element = root.OwnerDocument.XmlDocument.ReadNode(reader); if (element != null) { XmlAttribute xmlnsAttribute = null; foreach (XmlAttribute attrib in element.Attributes) { if (attrib.Name == "xmlns") { xmlnsAttribute = attrib; } } if (xmlnsAttribute != null) { element.Attributes.Remove(xmlnsAttribute); } //Remove namespace Attributes defined in the Xaml Root from the Pasted Snippet! List <XmlAttribute> removeAttributes = new List <XmlAttribute>(); foreach (XmlAttribute attrib in element.Attributes) { if (attrib.Name.StartsWith("xmlns:")) { var rootPrefix = root.OwnerDocument.GetPrefixForNamespace(attrib.Value); if (rootPrefix == null) { //todo: check if we can add to root, (maybe same ns exists) root.OwnerDocument.XmlDocument.Attributes.Append((XmlAttribute)attrib.CloneNode(true)); removeAttributes.Add(attrib); } else if (rootPrefix == attrib.Name.Substring(6)) { removeAttributes.Add(attrib); } } } foreach (var removeAttribute in removeAttributes) { element.Attributes.Remove(removeAttribute); } //end remove XamlParser parser = new XamlParser(); parser.settings = settings; parser.document = root.OwnerDocument; var xamlObject = parser.ParseObject(element as XmlElement); if (xamlObject != null) { return(xamlObject); } } return(null); }
public static string ParseXamlToHtml(string xaml, Assembly[] assemblies) { var settings = new XamlParserSettings(); foreach (var assembly in assemblies) { settings.TypeFinder.RegisterAssembly(assembly); } var config = AngleSharp.Configuration.Default.WithCss(x => x.Options = new CssParserOptions() { IsIncludingUnknownDeclarations = true }); using (var xmlReader = XmlReader.Create(new StringReader(xaml))) { var xamlObj = XamlParser.Parse(xmlReader, settings); var parser = new HtmlParser(config); var htmlDocument = parser.Parse(""); ParseObject(xamlObj.RootElement, htmlDocument, htmlDocument.DocumentElement); return htmlDocument.DocumentElement.OuterHtml; } }
/// <summary> /// Parses a XAML document using an XmlReader. /// </summary> public static XamlDocument Parse(XmlReader reader, XamlParserSettings settings) { if (reader == null) throw new ArgumentNullException("reader"); if (settings == null) throw new ArgumentNullException("settings"); XmlDocument doc = new PositionXmlDocument(); var errorSink = (IXamlErrorSink)settings.ServiceProvider.GetService(typeof(IXamlErrorSink)); try { doc.Load(reader); return Parse(doc, settings); } catch (XmlException x) { if (errorSink != null) { errorSink.ReportError(x.Message, x.LineNumber, x.LinePosition); } else { throw; } } return null; }
internal static XamlDocument Parse(XmlDocument document, XamlParserSettings settings) { if (document == null) throw new ArgumentNullException("document"); if (settings == null) throw new ArgumentNullException("settings"); XamlParser p = new XamlParser(); p.settings = settings; p.errorSink = (IXamlErrorSink)settings.ServiceProvider.GetService(typeof(IXamlErrorSink)); p.document = new XamlDocument(document, settings); try { var root = p.ParseObject(document.DocumentElement); p.document.ParseComplete(root); } catch (Exception x) { p.ReportException(x, p.currentParsedNode); } return p.document; }
/// <summary> /// Method use to parse a piece of Xaml. /// </summary> /// <param name="root">The Root XamlObject of the current document.</param> /// <param name="xaml">The Xaml being parsed.</param> /// <param name="settings">Parser settings used by <see cref="XamlParser"/>.</param> /// <returns>Returns the XamlObject of the parsed <paramref name="xaml"/>.</returns> public static XamlObject ParseSnippet(XamlObject root, string xaml, XamlParserSettings settings) { return(ParseSnippet(root, xaml, settings, null)); }
/// <summary> /// Creates a new XamlDesignContext instance. /// </summary> public XamlDesignContext(XmlReader xamlReader, XamlLoadSettings loadSettings) { if (xamlReader == null) throw new ArgumentNullException("xamlReader"); if (loadSettings == null) throw new ArgumentNullException("loadSettings"); this.Services.AddService(typeof(ISelectionService), new DefaultSelectionService()); this.Services.AddService(typeof(IToolService), new DefaultToolService(this)); this.Services.AddService(typeof(UndoService), new UndoService()); this.Services.AddService(typeof(IErrorService), new DefaultErrorService(this)); this.Services.AddService(typeof(ViewService), new DefaultViewService(this)); this.Services.AddService(typeof(OptionService), new OptionService()); var xamlErrorService = new XamlErrorService(); this.Services.AddService(typeof(XamlErrorService), xamlErrorService); this.Services.AddService(typeof(IXamlErrorSink), xamlErrorService); _componentService = new XamlComponentService(this); this.Services.AddService(typeof(IComponentService), _componentService); foreach (Action<XamlDesignContext> action in loadSettings.CustomServiceRegisterFunctions) { action(this); } // register default versions of overridable services: if (this.Services.GetService(typeof(ITopLevelWindowService)) == null) { this.Services.AddService(typeof(ITopLevelWindowService), new WpfTopLevelWindowService()); } // register extensions from the designer assemblies: foreach (Assembly designerAssembly in loadSettings.DesignerAssemblies) { this.Services.ExtensionManager.RegisterAssembly(designerAssembly); EditorManager.RegisterAssembly(designerAssembly); } _parserSettings = new XamlParserSettings(); _parserSettings.TypeFinder = loadSettings.TypeFinder; _parserSettings.CreateInstanceCallback = this.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory; _parserSettings.ServiceProvider = this.Services; _doc = XamlParser.Parse(xamlReader, _parserSettings); loadSettings.ReportErrors(xamlErrorService); if (_doc == null) { string message; if (xamlErrorService != null && xamlErrorService.Errors.Count > 0) message = xamlErrorService.Errors[0].Message; else message = "Could not load document."; throw new XamlLoadException(message); } _rootItem = _componentService.RegisterXamlComponentRecursive(_doc.RootElement); if(_rootItem!=null){ var rootBehavior=new RootItemBehavior(); rootBehavior.Intialize(this); } _xamlEditOperations=new XamlEditOperations(this,_parserSettings); }
/// <summary> /// Internal constructor, used by XamlParser. /// </summary> internal XamlDocument(XmlDocument xmlDoc, XamlParserSettings settings) { this._xmlDoc = xmlDoc; this._typeFinder = settings.TypeFinder; this._serviceProvider = settings.ServiceProvider; }
/// <summary> /// Method use to parse a piece of Xaml. /// </summary> /// <param name="root">The Root XamlObject of the current document.</param> /// <param name="xaml">The Xaml being parsed.</param> /// <param name="settings">Parser settings used by <see cref="XamlParser"/>.</param> /// <param name="parentObject">Parent Object, where the Parsed snippet will be inserted (Needed for Example for Bindings).</param> /// <returns>Returns the XamlObject of the parsed <paramref name="xaml"/>.</returns> public static XamlObject ParseSnippet(XamlObject root, string xaml, XamlParserSettings settings, XamlObject parentObject) { XmlTextReader reader = new XmlTextReader(new StringReader(xaml)); var element = root.OwnerDocument.XmlDocument.ReadNode(reader); if (element != null) { XmlAttribute xmlnsAttribute=null; foreach (XmlAttribute attrib in element.Attributes) { if (attrib.Name == "xmlns") xmlnsAttribute = attrib; } if(xmlnsAttribute!=null) element.Attributes.Remove(xmlnsAttribute); XamlParser parser = new XamlParser(); parser.settings = settings; parser.errorSink = (IXamlErrorSink)settings.ServiceProvider.GetService(typeof(IXamlErrorSink)); parser.document = root.OwnerDocument; parser.currentXamlObject = parentObject; var xamlObject = parser.ParseObject(element as XmlElement); RemoveRootNamespacesFromNodeAndChildNodes(root, element); if (xamlObject != null) return xamlObject; } return null; }
/// <summary> /// Method use to parse a piece of Xaml. /// </summary> /// <param name="root">The Root XamlObject of the current document.</param> /// <param name="xaml">The Xaml being parsed.</param> /// <param name="settings">Parser settings used by <see cref="XamlParser"/>.</param> /// <returns>Returns the XamlObject of the parsed <paramref name="xaml"/>.</returns> public static XamlObject ParseSnippet(XamlObject root, string xaml, XamlParserSettings settings) { return ParseSnippet(root, xaml, settings, null); }
/// <summary> /// Parses a XAML document using a TextReader. /// </summary> public static XamlDocument Parse(TextReader reader, XamlParserSettings settings) { if (reader == null) throw new ArgumentNullException("reader"); return Parse(XmlReader.Create(reader), settings); }
/// <summary> /// Method use to parse a piece of Xaml. /// </summary> /// <param name="root">The Root XamlObject of the current document.</param> /// <param name="xaml">The Xaml being parsed.</param> /// <param name="settings">Parser settings used by <see cref="XamlParser"/>.</param> /// <returns>Returns the XamlObject of the parsed <paramref name="xaml"/>.</returns> public static XamlObject ParseSnippet(XamlObject root, string xaml, XamlParserSettings settings) { XmlTextReader reader = new XmlTextReader(new StringReader(xaml)); var element = root.OwnerDocument.XmlDocument.ReadNode(reader); if (element != null) { XmlAttribute xmlnsAttribute=null; foreach (XmlAttribute attrib in element.Attributes) { if (attrib.Name == "xmlns") xmlnsAttribute = attrib; } if(xmlnsAttribute!=null) element.Attributes.Remove(xmlnsAttribute); //Remove namespace Attributes defined in the Xaml Root from the Pasted Snippet! List<XmlAttribute> removeAttributes = new List<XmlAttribute>(); foreach (XmlAttribute attrib in element.Attributes) { if (attrib.Name.StartsWith("xmlns:")) { var rootPrefix = root.OwnerDocument.GetPrefixForNamespace(attrib.Value); if (rootPrefix == null) { //todo: check if we can add to root, (maybe same ns exists) root.OwnerDocument.XmlDocument.Attributes.Append((XmlAttribute)attrib.CloneNode(true)); removeAttributes.Add(attrib); } else if (rootPrefix == attrib.Name.Substring(6)) { removeAttributes.Add(attrib); } } } foreach (var removeAttribute in removeAttributes) { element.Attributes.Remove(removeAttribute); } //end remove XamlParser parser = new XamlParser(); parser.settings = settings; parser.document = root.OwnerDocument; var xamlObject = parser.ParseObject(element as XmlElement); if (xamlObject != null) return xamlObject; } return null; }
public XamlEditOperations(XamlDesignContext context, XamlParserSettings settings) { this._context = context; this._settings = settings; }
/// <summary> /// Method use to parse a piece of Xaml. /// </summary> /// <param name="root">The Root XamlObject of the current document.</param> /// <param name="xaml">The Xaml being parsed.</param> /// <param name="settings">Parser settings used by <see cref="XamlParser"/>.</param> /// <returns>Returns the XamlObject of the parsed <paramref name="xaml"/>.</returns> public static XamlObject ParseSnippet(XamlObject root, string xaml, XamlParserSettings settings) { XmlTextReader reader = new XmlTextReader(new StringReader(xaml)); var element = root.OwnerDocument.XmlDocument.ReadNode(reader); if (element != null) { XmlAttribute xmlnsAttribute=null; foreach (XmlAttribute attrib in element.Attributes) { if (attrib.Name == "xmlns") xmlnsAttribute = attrib; } if(xmlnsAttribute!=null) element.Attributes.Remove(xmlnsAttribute); XamlParser parser = new XamlParser(); parser.settings = settings; parser.document = root.OwnerDocument; var xamlObject = parser.ParseObject(element as XmlElement); if (xamlObject != null) return xamlObject; } return null; }
/// <summary> /// Parses a XAML document using a stream. /// </summary> public static XamlDocument Parse(Stream stream, XamlParserSettings settings) { if (stream == null) throw new ArgumentNullException("stream"); return Parse(XmlReader.Create(stream), settings); }