Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
 /// <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;
 }
Beispiel #3
0
        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);
        }
Beispiel #4
0
 /// <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));
 }
Beispiel #5
0
 /// <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));
 }
Beispiel #6
0
        /// <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);
        }
Beispiel #7
0
 /// <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));
 }