예제 #1
0
 public void Execute(FileRuleContext ruleContext)
 {
     Config config = new Config();
     config.ReadFromXmlNode( ruleContext.XmlSettings );
     if (String.IsNullOrEmpty( config.TargetFile ))
         config.TargetFile = Path.Combine(
             Path.GetDirectoryName(ruleContext.RuleFileName),
             Path.GetFileName(ruleContext.RuleFileName).Substring(1));
     ProcessFileRule(ruleContext, config );
 }
예제 #2
0
        public void Execute(FileRuleContext ruleContext)
        {
            Config config = new Config();

            config.ReadFromXmlNode(ruleContext.XmlSettings);
            if (String.IsNullOrEmpty(config.TargetFile))
            {
                config.TargetFile = Path.Combine(
                    Path.GetDirectoryName(ruleContext.RuleFileName),
                    Path.GetFileName(ruleContext.RuleFileName).Substring(1));
            }
            ProcessFileRule(ruleContext, config);
        }
예제 #3
0
        protected void ProcessFileRule(FileRuleContext ruleContext, Config config)
        {
            // target text file
            string targetFilePath    = config.TargetFile;
            string targetFileContent = ruleContext.FileManager.Read(targetFilePath);
            bool   targetChanged     = false;
            string changedContent    = targetFileContent;

            if (config.RegexMarker != null)
            {
                targetChanged = ApplyRegexRule(config, targetFileContent, out changedContent);
            }
            else
            {
                targetChanged = ApplyStartEndRule(config, targetFileContent, out changedContent);
            }

            ruleContext.FileManager.Write(targetFilePath, changedContent);
        }
예제 #4
0
        public void InsertTextFileTest()
        {
            CreateTestDirectoryAndFiles();

            string insertNode = "test1";
            string test_text = "<text-insert file=\"test3.result.xml\" start=\"&lt;test&gt;\"><" + insertNode + "/></text-insert>";

            string xmlContent = "<test><t2>bbb</t2></test>";

            ModifyTextFileRule modifyTextFileRule = new ModifyTextFileRule();
            IFileManager fileManager = new LocalFileManager(TestDiretoryPath);

            using (FileStream fs = File.Create(TestFilePath))
            {
                AddText(fs, test_text);
                fs.Flush();
                fs.Close();
            }
            using (FileStream fs = File.Create(TestXmlFilePath))
            {
                AddText(fs, xmlContent);
                fs.Flush();
                fs.Close();
            }

            XPathDocument ruleXPathDoc = new XPathDocument(new StringReader(test_text) );
            XPathNavigator ruleFileNav = ruleXPathDoc.CreateNavigator().SelectSingleNode("/*");
            Console.WriteLine(ruleFileNav.LocalName);

            bool isMatched = modifyTextFileRule.IsMatch(ruleFileNav);
            Assert.AreEqual(true, isMatched);

            FileRuleContext fileRuleContext = new FileRuleContext(TestFilePath, fileManager, ruleFileNav);

            modifyTextFileRule.Execute(fileRuleContext);
            Assert.AreEqual(true, File.ReadAllText(TestXmlFilePath).Contains(insertNode));

            Assert.AreEqual(true, IsNodeInXmlFile(TestXmlFilePath, insertNode));
            DeleteTestDirectoryAndFiles();
        }
예제 #5
0
        protected void ProcessFileRule(FileRuleContext ruleContext, Config config)
        {
            // target text file
            string targetFilePath    = config.TargetFile;
            string targetFileContent = ruleContext.FileManager.Read(targetFilePath);

            // TODO: handle exceptions
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.PreserveWhitespace = true;
            xmlDoc.LoadXml(targetFileContent);
            // deal with namespaces
            IDictionary <string, string> namespaces = config.Xml.GetNamespacesInScope(XmlNamespaceScope.All);
            XmlNamespaceManager          xmlNsMgr   = new XmlNamespaceManager(xmlDoc.NameTable);

            foreach (KeyValuePair <string, string> ns in namespaces)
            {
                xmlNsMgr.AddNamespace(ns.Key, ns.Value);
            }

            bool targetChanged = false;

            if (config.XPath != null)
            {
                targetChanged = ApplyXPathRule(config, xmlDoc, xmlNsMgr);
            }

            if (targetChanged)
            {
                ruleContext.FileManager.Write(targetFilePath, PrepareXmlContent(xmlDoc.OuterXml));
            }
            else
            {
                log.Write(LogEvent.Warn, new { Msg = "Rule is not matched", Config = config });
            }
        }
예제 #6
0
        protected void ProcessFileRule(FileRuleContext ruleContext, Config config)
        {
            // target text file
            string targetFilePath = config.TargetFile;
            string targetFileContent = ruleContext.FileManager.Read(targetFilePath);
            bool targetChanged = false;
            string changedContent = targetFileContent;
            if (config.RegexMarker!=null) {
                targetChanged = ApplyRegexRule(config, targetFileContent, out changedContent);
            } else {
                targetChanged = ApplyStartEndRule(config, targetFileContent, out changedContent);
            }

            ruleContext.FileManager.Write(targetFilePath, changedContent);
        }
예제 #7
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);
                    }
                }

            }
        }
예제 #8
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);
                    }
                }
            }
        }
예제 #9
0
        public void RemoveItemFromFileTest()
        {
            CreateTestDirectoryAndFiles();
            string test_text = "<text-remove file=\"test3.result.xml\" regex=\"bbb\"></text-remove>";

            string xmlContent = "<test><t2>bbb</t2></test>";

            ModifyTextFileRule modifyTextFileRule = new ModifyTextFileRule();
            IFileManager fileManager = new LocalFileManager(TestDiretoryPath);

            using (FileStream fs = File.Create(TestFilePath))
            {
                AddText(fs, test_text);
                fs.Flush();
                fs.Close();
            }
            using (FileStream fs = File.Create(TestXmlFilePath))
            {
                AddText(fs, xmlContent);
                fs.Flush();
                fs.Close();
            }
            XPathDocument ruleXPathDoc = new XPathDocument(new StringReader(test_text));
            XPathNavigator ruleFileNav = ruleXPathDoc.CreateNavigator().SelectSingleNode("/*");

            FileRuleContext fileRuleContext = new FileRuleContext(TestFilePath, fileManager,ruleFileNav);

            modifyTextFileRule.Execute(fileRuleContext);

            Assert.AreEqual(false, File.ReadAllText(TestXmlFilePath).Contains("bbb"));
        }
예제 #10
0
        protected void ProcessFileRule(FileRuleContext ruleContext, Config config)
        {
            // target text file
            string targetFilePath = config.TargetFile;
            string targetFileContent = ruleContext.FileManager.Read(targetFilePath);

            // TODO: handle exceptions
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.PreserveWhitespace = true;
            xmlDoc.LoadXml(targetFileContent);
            // deal with namespaces
            IDictionary<string,string> namespaces = config.Xml.GetNamespacesInScope(XmlNamespaceScope.All);
            XmlNamespaceManager xmlNsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
            foreach (KeyValuePair<string,string> ns in namespaces) {
                xmlNsMgr.AddNamespace(ns.Key, ns.Value);
            }

            bool targetChanged = false;
            if (config.XPath!=null) {
                targetChanged = ApplyXPathRule(config, xmlDoc, xmlNsMgr);
            }

            if (targetChanged)
                ruleContext.FileManager.Write(targetFilePath, PrepareXmlContent(xmlDoc.OuterXml) );
            else {
                log.Write(LogEvent.Warn, new {Msg = "Rule is not matched", Config = config });
            }
        }