XmlFragmentWriter.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Util » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Util » XmlFragmentWriter.cs

using System.IO;
using System.Text;
using System.Xml;

namespace ThoughtWorks.CruiseControl.Core.Util{
    /// <summary>
    /// XmlFragmentWriter buffers xml written using the WriteNode method so that 
    /// It swallows any requests to write processing instructions.
    /// </summary>
    public class XmlFragmentWriter : XmlTextWriter
    {
        public XmlFragmentWriter(TextWriter writer) : base(writer)
        {
        }

        public void WriteNode(string xml)
        {
            xml = StripIllegalCharacters(xml);
            XmlReader reader = CreateXmlReader(xml);
            try
            {
                WriteNode(reader, true);
            }
            catch (XmlException ex)
            {
                Log.Debug("Supplied output is not valid xml.  Writing as CDATA");
                Log.Debug("Output: " + xml);
                Log.Debug("Exception: " + ex);
                WriteCData(xml);
            }
            reader.Close();
        }

        /// <summary>
        /// Use XmlValidatingReader in order to bypass root-level rules for document validation.  In other words,
        /// accept xml that is not single rooted (contains text or multiple root elements).
        /// </summary>
        private static XmlReader CreateXmlReader(string xml)
        {
            XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
            xmlReaderSettings.CloseInput = true;

            //use ConformanceLevel.Auto as discussed in
            //https://bugzilla.novell.com/show_bug.cgi?id=520080
            xmlReaderSettings.ConformanceLevel = ConformanceLevel.Auto;

            xmlReaderSettings.IgnoreProcessingInstructions = true;
            return XmlReader.Create(new StringReader(xml), xmlReaderSettings);
        }

        public override void WriteNode(XmlReader reader, bool defattr)
        {
            StringWriter buffer = new StringWriter();
            XmlFragmentWriter writer = CreateXmlWriter(buffer);
            writer.WriteNodeBase(reader, defattr);
            writer.Close();
            WriteRaw(buffer.ToString());
        }

        private XmlFragmentWriter CreateXmlWriter(StringWriter buffer)
        {
            XmlFragmentWriter writer = new XmlFragmentWriter(buffer);
            writer.Formatting = Formatting;
            return writer;
        }

        private void WriteNodeBase(XmlReader reader, bool defattr)
        {
            base.WriteNode(reader, defattr);
        }

        public override void WriteProcessingInstruction(string name, string text)
        {
            // no-op
        }

        public override void WriteCData(string text)
        {
            base.WriteCData(XmlUtil.EncodeCDATA(text));
        }

        /// <summary>
        /// Character values in the range 0x-0x1F (excluding white space characters 0x9, 0xA, and 0xD) are illegal in xml documents.
        /// This method removes all occurrences of these characters from the document.
        /// </summary>
        /// <param name="xml">The xml string to preprocess.</param>
        /// <returns></returns>
        private static string StripIllegalCharacters(string xml)
        {
            StringBuilder builder = new StringBuilder(xml.Length);
            foreach (char c in xml)
            {
                if (c > 31 || c == 9 || c == 10 || c == 13) builder.Append(c);
            }
            return builder.ToString();
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.