Ejemplo n.º 1
0
        public void Transform(Engine engine, Template template, Package package)
        {
            m_Engine = engine;
            m_Package = package;

            m_Logger.Debug(String.Format("XSLT Meditator executing: '{0}' ", template.Id));

            ReadParameters();

            using (MemoryStream templateStream = GetStringAsStream(template.Content))
            {
                var xslt = new XPathDocument(templateStream);// Create an new XmlDocument to hold the XSLT of the current Template Building Block // Load the contents (xslt) of the TBB into the XmlDocument
                var input = new XmlTextReader(new StringReader(m_XmlInput.GetAsString())); //need to use the stringreader for xml in the package marked with encoding=utf-16 otherwise an encoding exception is thrown during the transformation

                //no need to worry about XsltSettings, the default should work for SAXON
                // var xsltSettings = new XsltSettings(); // Create an XsltSettings object to override the default XSLT settings for XslCompiledTransform
                //xsltSettings.EnableDocumentFunction = true; // Enable script and document() funtion (default in .NET is disabled
                //xsltSettings.EnableScript = true;

                var resolver = new TcmUriResolver(engine); //Create a new UriResolver which supports TridionURIs (Only http:// and file:// are supported in the default one)

                //Run Saxon s9 api
                // Create a Processor instance.
                Processor processor = new Processor();

                XsltCompiler compiler = processor.NewXsltCompiler();

                //set resolver
                compiler.XmlResolver = resolver;

                //compile the xslt
                XsltTransformer transformer = compiler.Compile(templateStream).Load();

                //set parameters
                //TODO:  AddExtensionObjects(XsltArgumentList arglist)  Those are a list of extension functions needs to be added
                //

                //  args.AddParam("TEMPLATE_URI", "", m_Engine.PublishingContext.ResolvedItem.Template.Id.ToString()); // Add the URI of the current template as the parameter
                //transformer.SetParameter(new QName("", "", "TEMPLATE_URI"), new XdmAtomicValue(m_Engine.PublishingContext.ResolvedItem.Template.Id.ToString()));

                //set paramters
                SetParameterCollection(transformer);

                transformer.InputXmlResolver = resolver;

                using (var results = new StringWriter()) // Create an XmlWriter which has the same output settings as the transformer
                {
                    using (var xmlwriter = XmlWriter.Create(results))
                    {
                        // Create a serializer.
                       // Serializer serializer = new Serializer();

                        // Set the root node of the source document to be the initial context node
                        //transformer.InitialTemplate = new QName("","","go");

                       // serializer.SetOutputWriter(new TextWriterDestination(xmlwriter));
                        //serializer.SetOutputProperty(Serializer.INDENT, "yes");

                        // serializer.SetOutputFile(Server.MapPath("test.html")); //for file

                        // Transform the source XML
                        transformer.Run(new TextWriterDestination(xmlwriter));

                        Tridion.ContentManager.Templating.Item xsltOutputItem = package.CreateStringItem(GetContentType(template.Content), results.ToString());

                        if (m_OutputItemName == Package.OutputName)
                        {
                            xsltOutputItem.Properties[OUTPUT_BASE_URI] = template.Id; //mimic the behaviour of the DW mediator when the output item name is "Output"
                        }

                        package.PushItem(m_OutputItemName, xsltOutputItem); // Push the results into the package
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Transform(Engine engine, Template template, Package package)
        {
            m_Engine = engine;
            m_Package = package;

            m_Logger.Debug(String.Format("XSLT Meditator executing: '{0}' ",template.Id));

            ReadParameters();

            using (MemoryStream templateStream = GetStringAsStream(template.Content))
            {
                var xslt = new XPathDocument(templateStream);// Create an new XmlDocument to hold the XSLT of the current Template Building Block // Load the contents (xslt) of the TBB into the XmlDocument
                var input = new XmlTextReader(new StringReader(m_XmlInput.GetAsString())); //need to use the stringreader for xml in the package marked with encoding=utf-16 otherwise an encoding exception is thrown during the transformation

                var xsltSettings = new XsltSettings(); // Create an XsltSettings object to override the default XSLT settings for XslCompiledTransform
                xsltSettings.EnableDocumentFunction = true; // Enable script and document() funtion (default in .NET is disabled
                xsltSettings.EnableScript = true;

                var resolver = new TcmUriResolver(engine); //Create a new UriResolver which supports TridionURIs (Only http:// and file:// are supported in the default one)

                XslCompiledTransform transform = new XslCompiledTransform(); // Create a transformer
                transform.Load(xslt, xsltSettings, resolver); // Load the XSLT into the Transformer using the settings and the resolver

                XsltArgumentList args = CreateArgumentCollection();

                using (var results = new StringWriter()) // Create an XmlWriter which has the same output settings as the transformer
                {
                    using (var xmlwriter = XmlWriter.Create(results, transform.OutputSettings))
                    {
                        transform.Transform(input, args, xmlwriter, resolver); // Transform the object

                        Item xsltOutputItem = package.CreateStringItem(GetContentType(template.Content), results.ToString());

                        if (m_OutputItemName == Package.OutputName)
                        {
                            xsltOutputItem.Properties[OUTPUT_BASE_URI] = template.Id; //mimic the behaviour of the DW mediator when the output item name is "Output"
                        }

                        package.PushItem(m_OutputItemName, xsltOutputItem); // Push the results into the package
                    }
                }
            }
        }