XIncludingReader class implements streamable subset of the XInclude 1.0 in a fast, non-caching, forward-only fashion. To put it another way XIncludingReader is XML Base and XInclude 1.0 aware XmlReader.

See XInclude.NET homepage for more info.

Author: Oleg Tkachenko, http://www.xmllab.net.

상속: XmlReader, IXmlLineInfo
예제 #1
0
        static void Load(String inputFile)
        {
            Console.WriteLine ("Probing " + Path.GetFileName (inputFile));
            String outputFile = Path.ChangeExtension (inputFile, ".xml");

            using (XIncludingReader reader = new XIncludingReader(inputFile)) {
                using (StreamWriter writer = new StreamWriter(outputFile)) {
                    XmlDocument doc = new XmlDocument ();
                    doc.Load (reader);

                    // Remove "xml:base attribute"
                    RemoveXmlBase (doc.DocumentElement);

                    doc.Save (writer);

                    XDocument doc2 = ToXDocument(doc);
                    Console.WriteLine (doc2.Elements().Count());

                    //dictionary dictionary = new dictionary(doc2.Root);
                    //Console.WriteLine (dictionary.title);
                }
            }

            using (StreamReader reader = new StreamReader(outputFile)) {
                XmlSerializer serializer = new XmlSerializer (typeof(dictionary));
                dictionary dict = (dictionary)serializer.Deserialize (reader);
                Console.WriteLine (dict.title);
            }
        }
예제 #2
0
 public void SerializerTest()
 {
     XmlSerializer ser = new XmlSerializer(typeof(Document));
     using (XIncludingReader r = new XIncludingReader("../../XInclude/tests/Document3.xml"))
     {
         Document doc = (Document)ser.Deserialize(r);
         Assert.IsNotNull(doc);
         Assert.IsTrue(doc.Name == "Foo");
         Assert.IsNotNull(doc.Items);
         Assert.IsTrue(doc.Items.Length == 1);
         Assert.IsTrue(doc.Items[0].Value == "Bar");
     }            
 }
예제 #3
0
 public void GetAttributeTest4()
 {
     XIncludingReader xir = new XIncludingReader("../../XInclude/tests/document2.xml");
     xir.MakeRelativeBaseUri = false;
     while (xir.Read())
     {
         if (xir.NodeType == XmlNodeType.Element && xir.Name == "disclaimer")
         {
             Assert.IsTrue(xir.AttributeCount == 1);
             xir.MoveToAttribute(0);
             Console.WriteLine(xir.Value);
             Assert.IsTrue(xir.Name == "xml:base");
             Assert.IsTrue(xir.Value.EndsWith("tests/disclaimerWithXmlBase.xml"));                                        
         }
     }
 }
예제 #4
0
 public void GetAttributeTest3()
 {
     XIncludingReader xir = new XIncludingReader("../../XInclude/tests/document.xml");
     while (xir.Read())
     {
         if (xir.NodeType == XmlNodeType.Element && xir.Name == "disclaimer")
         {
             Assert.IsTrue(xir.AttributeCount == 1);
             xir.MoveToAttribute(0);
             Assert.IsTrue(xir.Name == "xml:base");
             Assert.IsTrue(xir.Value.EndsWith("disclaimer.xml"));
         }
     }
 }
예제 #5
0
        private static void ParseListOfFiles(string path)
        {
            var reader = new XIncludingReader(XmlReader.Create(path));
         
            var document = XDocument.Load(reader);

            var files = document.Root;

            var scriptElements = files.Elements(Script.TAGNAME);

            foreach (var scriptElement in scriptElements)
            {
                Script.Load(scriptElement, scriptElements);
            }
        }
예제 #6
0
 /// <summary>
 /// Creates acquired infoset.
 /// </summary>
 /// <param name="reader">Source reader</param>
 private string CreateAcquiredInfoset(XmlReader reader)
 {
     //TODO: Try to stream out this stuff
     XIncludingReader xir = new XIncludingReader(reader);
     xir.XmlResolver = this._xmlResolver;
     StringWriter sw = new StringWriter();
     XmlTextWriter w = new XmlTextWriter(sw);
     try
     {
         while (xir.Read())
             w.WriteNode(xir, false);
     }
     finally
     {
         if (xir != null)
             xir.Close();
         if (w != null)
             w.Close();
     }
     return sw.ToString();
 }
예제 #7
0
        /// <summary>
        /// Utility method for running tests.
        /// </summary>        
        public static void RunAndCompare(string source, string result, bool textAsCDATA, XmlResolver resolver) 
        {                                 
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;                  
            XIncludingReader xir = new XIncludingReader(source);
            if (resolver != null)
                xir.XmlResolver = resolver;
            xir.ExposeTextInclusionsAsCDATA = textAsCDATA;            
//            while (xir.Read()) 
//            {
//                Console.WriteLine("{0} | {1} | {2} | {3}", xir.NodeType, xir.Name, xir.Value, xir.IsEmptyElement);                
//            }
//            throw new Exception();
            try 
            {
                doc.Load(xir);
            } 
            catch (Exception e)
            {
                xir.Close();
                throw e;
            }
            xir.Close();
            XmlReaderSettings s = new XmlReaderSettings();
            s.ProhibitDtd = false;
            s.IgnoreWhitespace = true;           
            XmlReader r1 = XmlReader.Create(new StringReader(doc.OuterXml), s, doc.BaseURI);                        
            XmlReader r2 = XmlReader.Create(result, s);                        
            try 
            {
                while (r1.Read()) 
                {
                    Assert.IsTrue(r2.Read()); 
                    while (r1.NodeType == XmlNodeType.XmlDeclaration ||
                        r1.NodeType == XmlNodeType.Whitespace)
                        r1.Read();
                    while (r2.NodeType == XmlNodeType.XmlDeclaration ||
                        r2.NodeType == XmlNodeType.Whitespace)
                        r2.Read();
                    Assert.AreEqual(r1.XmlLang, r2.XmlLang);
                    switch (r1.NodeType) 
                    {
                        case XmlNodeType.Attribute:
                            Assert.AreEqual(r2.NodeType, XmlNodeType.Attribute);
                            Assert.AreEqual(r1.Name, r2.Name);
                            Assert.AreEqual(r1.LocalName, r2.LocalName);
                            Assert.AreEqual(r1.NamespaceURI, r2.NamespaceURI);
                            Assert.AreEqual(r1.Value, r2.Value);
                            break;                   
                        case XmlNodeType.CDATA:
                            Assert.IsTrue(r2.NodeType == XmlNodeType.CDATA || r2.NodeType == XmlNodeType.Text);
                            Assert.AreEqual(r1.Value, r2.Value);
                            break;
                        case XmlNodeType.Comment:
                            Assert.AreEqual(r2.NodeType, XmlNodeType.Comment);
                            Assert.AreEqual(r1.Value, r2.Value);
                            break;
                        case XmlNodeType.DocumentType:
                            Assert.AreEqual(r2.NodeType, XmlNodeType.DocumentType);
                            Assert.AreEqual(r1.Name, r2.Name);                        
                            //Ok, don't compare DTD content
                            //Assert.AreEqual(r1.Value, r2.Value);
                            break;
                        case XmlNodeType.Element:
                            Assert.AreEqual(r2.NodeType, XmlNodeType.Element);
                            Assert.AreEqual(r1.Name, r2.Name);
                            Assert.AreEqual(r1.LocalName, r2.LocalName);
                            Assert.AreEqual(r1.NamespaceURI, r2.NamespaceURI);
                            Assert.AreEqual(r1.Value, r2.Value);
                            break;
                        case XmlNodeType.Entity:
                            Assert.AreEqual(r2.NodeType, XmlNodeType.Entity);
                            Assert.AreEqual(r1.Name, r2.Name);
                            Assert.AreEqual(r1.Value, r2.Value);
                            break;
                        case XmlNodeType.EndElement:
                            Assert.AreEqual(r2.NodeType, XmlNodeType.EndElement);                        
                            break;
                        case XmlNodeType.EntityReference:
                            Assert.AreEqual(r2.NodeType, XmlNodeType.EntityReference);
                            Assert.AreEqual(r1.Name, r2.Name);
                            Assert.AreEqual(r1.Value, r2.Value);
                            break;
                        case XmlNodeType.Notation:
                            Assert.AreEqual(r2.NodeType, XmlNodeType.Notation);
                            Assert.AreEqual(r1.Name, r2.Name);
                            Assert.AreEqual(r1.Value, r2.Value);
                            break;
                        case XmlNodeType.ProcessingInstruction:
                            Assert.AreEqual(r2.NodeType, XmlNodeType.ProcessingInstruction);
                            Assert.AreEqual(r1.Name, r2.Name);
                            Assert.AreEqual(r1.Value, r2.Value);
                            break;
                        case XmlNodeType.SignificantWhitespace:
                            Assert.AreEqual(r2.NodeType, XmlNodeType.SignificantWhitespace);
                            Assert.AreEqual(r1.Value, r2.Value);
                            break;
                        case XmlNodeType.Text:
                            Assert.IsTrue(r2.NodeType == XmlNodeType.CDATA || r2.NodeType == XmlNodeType.Text);
                            Assert.AreEqual(r1.Value.Replace("\r\n", "\n").Replace("\r", "").Trim(), r2.Value.Replace("\r\n", "\n").Replace("\r", "").Trim());
                            break;                    
                        default:
                            break;
                    }                     
                }
                Assert.IsFalse(r2.Read());
                Assert.IsTrue(r1.ReadState == ReadState.EndOfFile || r1.ReadState == ReadState.Closed);
                Assert.IsTrue(r2.ReadState == ReadState.EndOfFile || r2.ReadState == ReadState.Closed);
            } 
            catch(Exception e) 
            {
                r1.Close();
                r1 = null;
                r2.Close();
                r2 = null;
                ReportResults(result, doc);
                throw e;
            }
            finally 
            {                
                if (r1 != null)
                    r1.Close();
                if (r2 != null)
                    r2.Close();
            }
            ReportResults(result, doc);
        }    
예제 #8
0
        public void TestXPointerIndentationBug()
        {

            XmlUrlResolver resolver = new XmlUrlResolver();
            resolver.Credentials = CredentialCache.DefaultCredentials;
            XsltSettings xsltSettings = new XsltSettings();
            xsltSettings.EnableDocumentFunction = true;
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ProhibitDtd = false;
            XmlReader reader = XmlReader.Create("../../XInclude/tests/Transform.xsl", settings);
            XIncludingReader xInputReader = new XIncludingReader("../../XInclude/tests/FileA.xml");
            try
            {
                MvpXslTransform processor = new MvpXslTransform(false);
                processor.Load(reader, xsltSettings, resolver);                
                //xInputReader.XmlResolver = new XMLBase();
                XmlDocument xInputDoc = new XmlDocument();
                xInputDoc.Load(xInputReader);
                XmlInput xInput = new XmlInput(xInputDoc);
                StringWriter stringW = new StringWriter();
                XmlOutput xOutput = new XmlOutput(stringW);
                processor.Transform(xInput, null, xOutput);
                processor.TemporaryFiles.Delete();
                Assert.AreEqual("<?xml version=\"1.0\" encoding=\"utf-16\"?>NodeA Content", stringW.ToString());
            }
            finally
            {
                 reader.Close();
                 xInputReader.Close();
            }
        }
예제 #9
0
        public void UnknownParseAttribute() 
        {
			XIncludingReader xir = new XIncludingReader("../../XInclude/tests/unknownparseattr.xml");
            while (xir.Read());
            xir.Close();
        }     
예제 #10
0
        public void FallbackNotChildOfInclude() 
        {
			XIncludingReader xir = new XIncludingReader("../../XInclude/tests/fallbacknotchildinclude.xml");
            while (xir.Read());
            xir.Close();
        }     
예제 #11
0
        public void IncludeChildOfInclude() 
        {
			XIncludingReader xir = new XIncludingReader("../../XInclude/tests/includechildofinclude.xml");
            while (xir.Read());
            xir.Close();
        }
예제 #12
0
        public void NoHrefAndNoXPointerAttributes() 
        {
			XIncludingReader xir = new XIncludingReader("../../XInclude/tests/nohref.xml");
            while (xir.Read());
            xir.Close();
        }        
예제 #13
0
파일: Utils.cs 프로젝트: zanyants/mvp.xml
 public static XmlReader CreateReader(Stream stream, XmlReaderSettings settings, NXsltOptions options, XmlResolver resolver)
 {
     if (options.ProcessXInclude)
     {
         XIncludingReader xir = new XIncludingReader(Directory.GetCurrentDirectory(), stream, resolver);
         xir.XmlResolver = resolver;
         return XmlReader.Create(xir, settings);
     }
     else
     {
         return XmlReader.Create(stream, settings);
     }
 }
예제 #14
0
파일: Utils.cs 프로젝트: zanyants/mvp.xml
 public static XmlReader CreateReader(string filename, XmlReaderSettings settings, NXsltOptions options, XmlResolver resolver)
 {
     if (options.ProcessXInclude)
     {
         XIncludingReader xir = new XIncludingReader(filename, (resolver != null && resolver is DTDAllowingResolver)? new XmlUrlResolver() : resolver);
         xir.XmlResolver = resolver;
         return XmlReader.Create(xir, settings);
     }
     else
     {
         return XmlReader.Create(filename, settings);
     }
 }
예제 #15
0
        /// <summary>
        ///   Generates a wrapper.
        /// </summary>
        /// <param name = "prefix">The prefix.</param>
        /// <param name = "inputFile">The input file.</param>
        public void Generate(String prefix, String inputFile)
        {
            String outputFile = prefix + "." + this.Extension;
            String workingFile = Path.ChangeExtension(inputFile, ".xml");
            if (!File.Exists(inputFile))
            {
                throw new ArgumentException("Input file not found: " + inputFile);
            }

            // Use a XInclude compliant parser to load all the inclusions
            Console.WriteLine("Processing '" + inputFile + "' ...");
            using (XIncludingReader reader = new XIncludingReader(inputFile))
            {
                // Load the definition file
                // All the XInclude extensions will be unfolded
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);

                // Remove "xml:base attribute"
                // This cause error when using XmlSerializer
                RemoveXmlBase (doc.DocumentElement);

                doc.Save(workingFile);

                // Convert the document to a Linq compatible object
                XDocument xDoc = ToXDocument(doc);

                // Output the code
                using (TextWriter writer = new StreamWriter(outputFile))
                {
                    this.Generate(prefix, xDoc, writer);
                }
            }
        }
예제 #16
0
 public void EncodingTest()
 {
     XmlTextReader r = new XmlTextReader(new StringReader("<foo/>"));
     XIncludingReader xir = new XIncludingReader(r);
     xir.ExposeTextInclusionsAsCDATA = true;
     xir.MoveToContent();
     Assert.IsTrue(xir.Encoding == UnicodeEncoding.Unicode);            
 }
예제 #17
0
 public void ShouldPreserveCDATA()
 {
     string xml = "<HTML><![CDATA[<img src=\"/_layouts/images/\">]]></HTML>";
     XIncludingReader xir = new XIncludingReader(new StringReader(xml));
     xir.Read();
     Assert.AreEqual("<HTML><![CDATA[<img src=\"/_layouts/images/\">]]></HTML>", xir.ReadOuterXml());
 }
예제 #18
0
        public void TwoFallbacks() 
        {
			XIncludingReader xir = new XIncludingReader("../../XInclude/tests/twofallbacks.xml");
            while (xir.Read());
            xir.Close();
        }             
예제 #19
0
 public void TestLineInfo()
 {
     XIncludingReader r = new XIncludingReader("../../XInclude/tests/document.xml");            
     IXmlLineInfo lineInfo = ((IXmlLineInfo)r);
     Assert.IsTrue(lineInfo.HasLineInfo());
     r.Read();
     Assert.AreEqual(1, lineInfo.LineNumber);
     Assert.AreEqual(3, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(1, lineInfo.LineNumber);
     Assert.AreEqual(22, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(2, lineInfo.LineNumber);
     Assert.AreEqual(2, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(2, lineInfo.LineNumber);
     Assert.AreEqual(54, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(3, lineInfo.LineNumber);
     Assert.AreEqual(6, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(3, lineInfo.LineNumber);
     Assert.AreEqual(8, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(3, lineInfo.LineNumber);
     Assert.AreEqual(54, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(3, lineInfo.LineNumber);
     Assert.AreEqual(56, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(1, lineInfo.LineNumber);
     Assert.AreEqual(22, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(2, lineInfo.LineNumber);
     Assert.AreEqual(5, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(2, lineInfo.LineNumber);
     Assert.AreEqual(17, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(3, lineInfo.LineNumber);
     Assert.AreEqual(3, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(3, lineInfo.LineNumber);
     Assert.AreEqual(12, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(4, lineInfo.LineNumber);
     Assert.AreEqual(2, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(4, lineInfo.LineNumber);
     Assert.AreEqual(13, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(5, lineInfo.LineNumber);
     Assert.AreEqual(4, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(5, lineInfo.LineNumber);
     Assert.AreEqual(6, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(7, lineInfo.LineNumber);
     Assert.AreEqual(18, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(7, lineInfo.LineNumber);
     Assert.AreEqual(20, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(8, lineInfo.LineNumber);
     Assert.AreEqual(3, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(4, lineInfo.LineNumber);
     Assert.AreEqual(75, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(6, lineInfo.LineNumber);
     Assert.AreEqual(3, lineInfo.LinePosition);
     r.Read();
     Assert.AreEqual(6, lineInfo.LineNumber);
     Assert.AreEqual(12, lineInfo.LinePosition);
 }
예제 #20
0
        public void InnerXmlTest() 
        {
			XIncludingReader xir = new XIncludingReader("../../XInclude/tests/document.xml");
            xir.MoveToContent();
            string innerXml = xir.ReadInnerXml();
            xir.Close();
			xir = new XIncludingReader("../../XInclude/tests/document.xml");
            xir.MoveToContent();
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.Load(xir);            
            string innerXml2 = doc.DocumentElement.InnerXml;
            Assert.AreEqual(innerXml, innerXml2);
        }        
예제 #21
0
 /// <summary>
 /// Creates acquired infoset.
 /// </summary>        
 private string CreateAcquiredInfoset(Uri includeLocation)
 {
     if (_cache == null)
         _cache = new Dictionary<string, WeakReference>();
     WeakReference wr;
     if (_cache.TryGetValue(includeLocation.AbsoluteUri, out wr) && wr.IsAlive)
     {
         return (string)wr.Target;
     }
     else
     {
         //Not cached or GCollected
         WebResponse wRes;
         Stream stream = GetResource(includeLocation.AbsoluteUri,
             _reader.GetAttribute(_keywords.Accept),
             _reader.GetAttribute(_keywords.AcceptLanguage), out wRes);
         XIncludingReader xir = new XIncludingReader(wRes.ResponseUri.AbsoluteUri, stream, _nameTable);
         xir.WhitespaceHandling = _whiteSpaceHandling;
         StringWriter sw = new StringWriter();
         XmlTextWriter w = new XmlTextWriter(sw);
         try
         {
             while (xir.Read())
                 w.WriteNode(xir, false);
         }
         finally
         {
             if (xir != null)
                 xir.Close();
             if (w != null)
                 w.Close();
         }
         string content = sw.ToString();
         lock (_cache)
         {
             if (!_cache.ContainsKey(includeLocation.AbsoluteUri))
                 _cache.Add(includeLocation.AbsoluteUri, new WeakReference(content));
         }
         return content;
     }
 }
예제 #22
0
        public void DepthTest() 
        {
			XIncludingReader xir = new XIncludingReader("../../XInclude/tests/document.xml");
            StringBuilder sb = new StringBuilder();
            while (xir.Read()) 
            {
                Console.WriteLine("{0} | {1} | {2} | {3}", 
                    xir.NodeType, xir.Name, xir.Value, xir.Depth);
                sb.Append(xir.Depth);
            }
            string expected = "00011211111111223221100";
            Assert.AreEqual(sb.ToString(), expected);
        }
예제 #23
0
        public void NoBaseURITest() 
        {
			StreamReader sr = new StreamReader("../../XInclude/tests/document.xml");
            string xml = sr.ReadToEnd();
            sr.Close();
            XIncludingReader xir = new XIncludingReader(new StringReader(xml));
            XmlWriter w = XmlWriter.Create(Console.Out);
            while (xir.Read());                
        }
예제 #24
0
 /// <summary>
 /// Prepares stylesheet XML reader.
 /// </summary>
 /// <returns>XmlReader over source XML</returns>
 private XmlReader PrepareStylesheetReader(XmlResolver stylesheetResolver)
 {
     XmlReaderSettings stylesheetReaderSettings = new XmlReaderSettings();
     stylesheetReaderSettings.ProhibitDtd = false;
     if (options.ValidateDocs)
     {
         stylesheetReaderSettings.ValidationType = ValidationType.DTD;
     }
     if (options.StripWhiteSpace)
     {
         stylesheetReaderSettings.IgnoreWhitespace = true;
     }
     if (!options.ResolveExternals)
         stylesheetReaderSettings.XmlResolver = null;
     else
     {
         stylesheetReaderSettings.XmlResolver = stylesheetResolver;
     }
     XmlReader stylesheetReader;
     if (options.IdentityTransformMode)
     {
         //No XSLT - use identity transformation                
         stylesheetReader = XmlReader.Create(new StringReader(NXsltStrings.IdentityTransformation), stylesheetReaderSettings);
     }
     else if (options.LoadStylesheetFromStdin)
     {
         //Get stylesheet from stdin            
         if (options.ProcessXIncludeInXSLT)
         {
             XIncludingReader xir = new XIncludingReader(Directory.GetCurrentDirectory(),
                 Console.OpenStandardInput(), stylesheetResolver);
             xir.XmlResolver = stylesheetResolver;
             stylesheetReader = XmlReader.Create(xir, stylesheetReaderSettings);
         }
         else
         {
             stylesheetReader = XmlReader.Create(Console.OpenStandardInput(), stylesheetReaderSettings);
         }
     }
     else
     {
         //Get source from URI
         if (options.ProcessXIncludeInXSLT)
         {
             XIncludingReader xir = new XIncludingReader(options.Stylesheet,
                 (stylesheetResolver != null && stylesheetResolver is XmlLab.nxslt.Utils.DTDAllowingResolver) ? new XmlUrlResolver() : stylesheetResolver);
             xir.XmlResolver = stylesheetResolver;
             stylesheetReader = XmlReader.Create(xir, stylesheetReaderSettings);
         }
         else
         {                    
             stylesheetReader = XmlReader.Create(options.Stylesheet, stylesheetReaderSettings);
         }
     }
     //Chain schema validaring reader on top
     if (options.ValidateDocs)
     {
         stylesheetReaderSettings.ValidationType = ValidationType.Schema;
         stylesheetReaderSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
         return XmlReader.Create(stylesheetReader, stylesheetReaderSettings);
     }
     return stylesheetReader;
 }