TransformComponent.cs :  » Development » Sandcastle » Microsoft » Ddue » Tools » 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 » Development » Sandcastle 
Sandcastle » Microsoft » Ddue » Tools » TransformComponent.cs
// Copyright  Microsoft Corporation.
// This source file is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace Microsoft.Ddue.Tools{

  public class TransformComponent : BuildComponent {
    
    public TransformComponent (BuildAssembler assembler, XPathNavigator configuration) : base(assembler, configuration) {

      // load the transforms
      XPathNodeIterator transform_nodes = configuration.Select("transform");
      foreach (XPathNavigator transform_node in transform_nodes) {

        // load the transform
        string file = transform_node.GetAttribute("file", String.Empty);
        if (String.IsNullOrEmpty(file)) WriteMessage(MessageLevel.Error, "Each transform element must specify a file attribute.");
        file = Environment.ExpandEnvironmentVariables(file);

        Transform transform = null;
        try {
          transform = new Transform(file);
        } catch (IOException e) {
          WriteMessage(MessageLevel.Error, String.Format("The transform file '{0}' could not be loaded. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e)));
        } catch (XmlException e) {
          WriteMessage(MessageLevel.Error, String.Format("The transform file '{0}' is not a valid XML file. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e)));
        } catch (XsltException e) {
          WriteMessage(MessageLevel.Error, String.Format("The XSL transform '{0}' contains an error. The error message is: {1}", file, BuildComponentUtilities.GetExceptionMessage(e))); 
        }


        transforms.Add(transform);


        // load any arguments
        XPathNodeIterator argument_nodes = transform_node.Select("argument");
        foreach (XPathNavigator argument_node in argument_nodes) {
          string key = argument_node.GetAttribute("key", String.Empty);
          if ((key == null) || (key.Length == 0)) WriteMessage(MessageLevel.Error, "When creating a transform argument, you must specify a key using the key attribute");

                    // set "expand-value" attribute to true to expand environment variables embedded in "value".
                    string expand_attr = argument_node.GetAttribute("expand-value", String.Empty);
                    bool expand_value = String.IsNullOrEmpty(expand_attr) ? false : Convert.ToBoolean(expand_attr);

          string value = argument_node.GetAttribute("value", String.Empty);
          if ((value != null) && (value.Length > 0)) {
                        transform.Arguments.AddParam(key, String.Empty, expand_value ? Environment.ExpandEnvironmentVariables(value) : value);
                    }
                    else {
            transform.Arguments.AddParam(key, String.Empty, argument_node.Clone());
          }
        }      

      }

    }

    // the stored transforms

    private List<Transform> transforms = new List<Transform>();

    // the action of the component

    public override void Apply (XmlDocument document, string key) {

      // iterate over transforms
      foreach (Transform transform in transforms) {

        // add the key as a parameter to the arguments
        transform.Arguments.RemoveParam("key", String.Empty);
        transform.Arguments.AddParam("key", String.Empty, key);

        // create a buffer into which output can be written
        using (MemoryStream buffer = new MemoryStream()) {


          // do the transform, routing output to the buffer
                    XmlWriterSettings settings = transform.Xslt.OutputSettings;
                    XmlWriter writer = XmlWriter.Create(buffer, settings);
                    try {
                        transform.Xslt.Transform(document, transform.Arguments, writer);
                    } catch (XsltException e) {
                        WriteMessage(MessageLevel.Error, String.Format("A error ocurred while executing the transform '{0}', on line {1}, at position {2}. The error message was: {3}", e.SourceUri, e.LineNumber, e.LinePosition, (e.InnerException == null) ? e.Message : e.InnerException.Message));
                    } catch (XmlException e) {
                        WriteMessage(MessageLevel.Error, String.Format("A error ocurred while executing the transform '{0}', on line {1}, at position {2}. The error message was: {3}", e.SourceUri, e.LineNumber, e.LinePosition, (e.InnerException == null) ? e.Message : e.InnerException.Message));
                    } finally {
            writer.Close();
          }

          // replace the document by the contents of the buffer
          buffer.Seek(0, SeekOrigin.Begin);

                    // some settings to ensure that we don't try to go get, parse, and validate using any referenced schemas or DTDs
                    XmlReaderSettings readerSettings = new XmlReaderSettings();
                    readerSettings.ProhibitDtd = false;
                    readerSettings.XmlResolver = null;

                    XmlReader reader = XmlReader.Create(buffer, readerSettings);
                    try {
                        document.Load(reader);
                    } catch (XmlException e) {
                        WriteMessage(MessageLevel.Error, String.Format("A error ocurred while executing the transform '{0}', on line {1}, at position {2}. The error message was: {3}", e.SourceUri, e.LineNumber, e.LinePosition, (e.InnerException == null) ? e.Message : e.InnerException.Message));
                    } finally {
                        reader.Close();
                    }

        }
      }
  
    }

  }


  // a represenataion of a transform action

  internal class Transform {

    public Transform (string file) {
            // The transforms presumably come from a trusted source, so there's no reason
            // not to enable scripting and the document function. The latter is used to read topic
            // info files for the conceptual WebDocs build.
      xslt.Load(file, new XsltSettings(true, true), new XmlUrlResolver());
    }

    private XslCompiledTransform xslt = new XslCompiledTransform();

    private XsltArgumentList arguments = new XsltArgumentList();

    public XslCompiledTransform Xslt {
      get {
        return(xslt);
      }
    }

    public XsltArgumentList Arguments {
      get {
        return(arguments);
      }
    }

  }

}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.