Ejemplo n.º 1
0
        public string Provide(XmlConfigRuleContext ruleContext)
        {
            XslTransformRule.Context context = new XslTransformRule.Context();
            context.FileManager = ruleContext.FileManager;
            context.ReadFromXmlNode(ruleContext.RuleConfig);

            string result = Rule.Provide(context);
            // test for result
            XPathNavigator nav       = ruleContext.RuleConfig.CreateNavigator();
            XPathNavigator resultNav = nav.SelectSingleNode("result");

            if (resultNav != null)
            {
                // store result in file
                string resultFileName = resultNav.GetAttribute("file", String.Empty);
                if (!String.IsNullOrEmpty(resultFileName))
                {
                    if (log.IsEnabledFor(LogEvent.Debug))
                    {
                        log.Write(LogEvent.Debug,
                                  new{ Action = "writing transform result to file", Filename = resultFileName });
                    }
                    ruleContext.FileManager.Write(resultFileName, result);
                    result = null;
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public void Execute(FileRuleContext ruleContext)
        {
            XslTransformRule.Context xsltContext = new XslTransformRule.Context();
            xsltContext.FileManager = ruleContext.FileManager;
            xsltContext.ReadFromXmlNode(ruleContext.XmlSettings);
            string resContent = TransformRule.Provide(xsltContext);

            XPathNavigator resultFileNameNav = ruleContext.XmlSettings.SelectSingleNode("result/@file");

            if (resultFileNameNav != null)
            {
                if (!String.IsNullOrEmpty(resultFileNameNav.Value))
                {
                    if (log.IsEnabledFor(LogEvent.Debug))
                    {
                        log.Write(LogEvent.Debug,
                                  new {
                            Msg  = "Writing XSL transformation result to file",
                            File = resultFileNameNav.Value
                        });
                    }
                    ruleContext.FileManager.Write(resultFileNameNav.Value, PrepareTransformedContent(resContent));
                }
                else
                {
                    log.Write(LogEvent.Warn, "Nothing to do with XSLT result: output file name is not specified.");
                }
            }
            else
            {
                // may be result-dependent files are configured?
                XPathNavigator resultFileNav = ruleContext.XmlSettings.SelectSingleNode("result/file");
                if (resultFileNav != null)
                {
                    // read result
                    var resultXPathDoc = new XPathDocument(new StringReader(resContent));

                    // check @xpath attr
                    var xPathNav = resultFileNav.SelectSingleNode("@xpath");
                    if (xPathNav == null)
                    {
                        log.Write(LogEvent.Warn, "Nothing to do with XSLT result: XPath for output file is not specified.");
                        return;
                    }
                    string xPath = xPathNav.Value;
                    // determine file name xpath
                    string fileNameXPath    = null;
                    var    fileNameXPathNav = resultFileNav.SelectSingleNode("name/@xpath");
                    if (fileNameXPathNav != null)
                    {
                        fileNameXPath = fileNameXPathNav.Value;
                    }
                    // determine file content xpath
                    string fileContentXPath    = null;
                    var    fileContentXPathNav = resultFileNav.SelectSingleNode("content/@xpath");
                    if (fileContentXPathNav != null)
                    {
                        fileContentXPath = fileContentXPathNav.Value;
                    }

                    // iterate
                    var results = resultXPathDoc.CreateNavigator().Select(xPath);
                    if (log.IsEnabledFor(LogEvent.Info))
                    {
                        log.Write(LogEvent.Info, "Matched {0} file generation results.", results.Count);
                    }
                    foreach (XPathNavigator nav in results)
                    {
                        // determine file name
                        var currentFileNameNav = nav.SelectSingleNode(fileNameXPath);
                        if (currentFileNameNav == null)
                        {
                            log.Write(LogEvent.Warn, new {
                                Msg = "Result is matched but output file name is not matched."
                            });
                            continue;
                        }
                        // determine file contents
                        var resultFileContentNav = nav.SelectSingleNode(fileContentXPath);
                        if (resultFileContentNav == null)
                        {
                            log.Write(LogEvent.Warn, new {
                                Msg = "Result is matched but output file content is not matched."
                            });
                            continue;
                        }

                        string fileContent = PrepareTransformedContent(resultFileContentNav.InnerXml);
                        ruleContext.FileManager.Write(currentFileNameNav.Value, fileContent);
                    }
                }
            }
        }