コード例 #1
0
        public override void Transform(Engine engine, Package package)
        {
            GeneralUtils.TimedLog("start Transform");
            Package = package;
            Engine  = engine;
            XmlSerializer serializer;

            Dynamic.Page page;
            bool         hasOutput = HasPackageValue(package, "Output");

            if (hasOutput)
            {
                GeneralUtils.TimedLog("start retrieving previous Output from package");
                String inputValue = package.GetValue("Output");
                GeneralUtils.TimedLog("start creating serializer");
                serializer = new PageSerializer();
                GeneralUtils.TimedLog("finished creating serializer");
                TextReader tr = new StringReader(inputValue);
                GeneralUtils.TimedLog("start deserializing from package");
                page = (Dynamic.Page)serializer.Deserialize(tr);
                GeneralUtils.TimedLog("finished deserializing from package");
            }
            else
            {
                GeneralUtils.ResetLogTimer();
                GeneralUtils.TimedLog("Could not find 'Output' in the package");
                GeneralUtils.TimedLog("Start creating dynamic page from current page in the package");
                page = GetDynamicPage(Manager);
                GeneralUtils.TimedLog("Finished creating dynamic page with title " + page.Title);
                GeneralUtils.TimedLog("start creating serializer");
                serializer = new PageSerializer();
                GeneralUtils.TimedLog("finished creating serializer");
            }

            try
            {
                GeneralUtils.TimedLog("starting transformPage");
                TransformPage(page);
                GeneralUtils.TimedLog("finished transformPage");
            }
            catch (StopChainException)
            {
                GeneralUtils.TimedLog("caught stopchainexception, will not write current page back to the package");
                return;
            }
            StringWriter sw     = new StringWriter();
            MemoryStream ms     = new MemoryStream();
            XmlWriter    writer = new XmlTextWriterFormattedNoDeclaration(ms, Encoding.UTF8);
            string       outputValue;
            //Create our own namespaces for the output
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            //Add an empty namespace and empty value
            ns.Add("", "");

            serializer.Serialize(writer, page, ns);
            outputValue = Encoding.UTF8.GetString(ms.ToArray());

            // for some reason, the .NET serializer leaves an invalid character at the start of the string
            // we will remove everything up to the first < so that the XML can be deserialized later!
            Regex re = new Regex("^[^<]+");

            outputValue = re.Replace(outputValue, "");

            if (hasOutput)
            {
                Item outputItem = package.GetByName("Output");
                package.Remove(outputItem);
                package.PushItem(Package.OutputName, package.CreateStringItem(ContentType.Xml, outputValue));
            }
            else
            {
                package.PushItem(Package.OutputName, package.CreateStringItem(ContentType.Xml, outputValue));
            }

            GeneralUtils.TimedLog("finished Transform");
        }
コード例 #2
0
        public override void Transform(Engine engine, Package package)
        {
            GeneralUtils.TimedLog("start Transform");
            Package = package;
            Engine = engine;
            XmlSerializer serializer;

            Dynamic.Page page;
            bool hasOutput = HasPackageValue(package, "Output");
            if (hasOutput)
            {
                GeneralUtils.TimedLog("start retrieving previous Output from package");
                String inputValue = package.GetValue("Output");
                GeneralUtils.TimedLog("start creating serializer");
                serializer = new PageSerializer();
                GeneralUtils.TimedLog("finished creating serializer");
                TextReader tr = new StringReader(inputValue);
                GeneralUtils.TimedLog("start deserializing from package");
                page = (Dynamic.Page)serializer.Deserialize(tr);
                GeneralUtils.TimedLog("finished deserializing from package");
            }
            else
            {
                GeneralUtils.ResetLogTimer();
                GeneralUtils.TimedLog("Could not find 'Output' in the package");
                GeneralUtils.TimedLog("Start creating dynamic page from current page in the package");
                page = GetDynamicPage(Manager);
                GeneralUtils.TimedLog("Finished creating dynamic page with title " + page.Title);
                GeneralUtils.TimedLog("start creating serializer");
                serializer = new PageSerializer();
                GeneralUtils.TimedLog("finished creating serializer");
            }

            try
            {
                GeneralUtils.TimedLog("starting transformPage");
                TransformPage(page);
                GeneralUtils.TimedLog("finished transformPage");
            }
            catch (StopChainException)
            {
                GeneralUtils.TimedLog("caught stopchainexception, will not write current page back to the package");
                return;
            }
            StringWriter sw = new StringWriter();
            MemoryStream ms = new MemoryStream();
            XmlWriter writer = new XmlTextWriterFormattedNoDeclaration(ms, Encoding.UTF8);
            string outputValue;
            //Create our own namespaces for the output
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            //Add an empty namespace and empty value
            ns.Add("", "");

            serializer.Serialize(writer, page, ns);
            outputValue = Encoding.UTF8.GetString(ms.ToArray());

            // for some reason, the .NET serializer leaves an invalid character at the start of the string
            // we will remove everything up to the first < so that the XML can be deserialized later!
            Regex re = new Regex("^[^<]+");
            outputValue = re.Replace(outputValue, "");

            if (hasOutput)
            {
                Item outputItem = package.GetByName("Output");
                package.Remove(outputItem);
                package.PushItem(Package.OutputName, package.CreateStringItem(ContentType.Xml, outputValue));
            }
            else
            {
                package.PushItem(Package.OutputName, package.CreateStringItem(ContentType.Xml, outputValue));
            }

            GeneralUtils.TimedLog("finished Transform");

        }