コード例 #1
0
ファイル: NodeAppearPair.cs プロジェクト: xchgdzq233/testing
 public NodeAppearPair(ref NodeClass nodeClass)
 {
     this.nodeClass = nodeClass;
     totalAppearCount = 1;
     isRequired = true;
     maxAppearCount = 0;
 }
コード例 #2
0
ファイル: NodeAppearPair.cs プロジェクト: xchgdzq233/testing
 public NodeAppearPair(String nodeClassName)
 {
     nodeClass = new NodeClass(nodeClassName);
     totalAppearCount = 1;
     isRequired = true;
     maxAppearCount = 0;
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: xchgdzq233/testing
        static void LoadChildRule(XmlNode currentEl, NodeClass parentRule)
        {
            //jump <table>
            //if (currentEl.LocalName.Equals("table"))
            //    return;

            //check new rule
            NodeClass currentRule = null;
            foreach (NodeClass rule in resultRule)
                if (rule.name.Equals(currentEl.LocalName))
                {
                    currentRule = rule;
                    currentRule.appearCount++;
                    break;
                }
            if (Object.ReferenceEquals(currentRule, null))
            {
                currentRule = new NodeClass(currentEl.LocalName);
                if (currentEl.LocalName.Equals("#text"))
                {
                    parentRule.containText = true;
                    return;
                }
                if (currentEl.LocalName.Equals("#text"))
                    return;
                resultRule.Add(currentRule);
            }

            //add attributes
            if (!Object.ReferenceEquals(currentEl.Attributes, null) && currentEl.Attributes.Count > 0)
                foreach (XmlAttribute xmlAttr in currentEl.Attributes)
                {
                    AttributeClass currentAttr = null;

                    foreach (AttributeClass attr in currentRule.attributes)
                        if (attr.name.Equals(xmlAttr.LocalName))
                        {
                            //old attribute
                            currentAttr = attr;
                            currentAttr.appearCount++;
                            break;
                        }
                    //new attribute
                    if (Object.ReferenceEquals(currentAttr, null))
                    {
                        currentAttr = new AttributeClass(xmlAttr.LocalName, xmlAttr.Value);
                        currentRule.attributes.Add(currentAttr);
                    }

                    //check prefix
                    if (!String.IsNullOrEmpty(xmlAttr.Prefix))
                        currentAttr.prefix = xmlAttr.Prefix;
                }

            //check new child of its parent
            if (!Object.ReferenceEquals(parentRule, null))
                if (!parentRule.HasChild(currentEl.LocalName) && !currentRule.name.Equals("#text") && !currentRule.name.Equals("#comment"))
                    parentRule.childNodes.Add(new NodeAppearPair(ref currentRule));

            //check child
            if (currentEl.HasChildNodes)
                foreach (XmlNode childEl in currentEl)
                    LoadChildRule(childEl, currentRule);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: xchgdzq233/testing
        static void PrintTree(NodeClass currentNode, TextWriter writer, int depth = 0)
        {
            String logLine = "";
            for (int i = 0; i < depth; i++)
                logLine += "-";
            logLine += currentNode.name;
            LogUtility.LogIt(logLine, writer);

            if (currentNode.childNodes.Count > 0)
                foreach (NodeAppearPair childNodeAppearPair in currentNode.childNodes)
                    PrintTree(childNodeAppearPair.nodeClass, writer, depth + 1);
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: xchgdzq233/testing
        static void Main(string[] args)
        {
            //DateTime start = DateTime.Now;

            //List<String> uniqueElements = GetAllUniqueElements();
            //Console.WriteLine("Total unique elmenets = " + uniqueElements.Count);
            //foreach (String el in uniqueElements)
            //{
            //    Console.WriteLine(el);
            //}

            //DateTime end = DateTime.Now;
            //Console.WriteLine("total time: " + (end - start).Seconds);

            //Environment.Exit(0);

            if (!Directory.Exists(logFolder))
                Directory.CreateDirectory(logFolder);
            logPrefix = String.Format("{0}T{1}-", DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("hhmmss"));
            GlobalContext.Properties["LogName"] = logPrefix + "Log.txt";
            logger = LogManager.GetLogger(typeof(Program));

            Console.WriteLine("Extracting files from the directory...");
            String[] files = Directory.GetFiles(xmlFilesFolder, "*.dita", SearchOption.AllDirectories);
            if (files.Length == 0)
            {
                Console.WriteLine("Cannot find .dita files in the directory.");
                Environment.Exit(0);
            }
            Console.WriteLine(String.Format("Found {0} files.\n", files.Length));
            Console.WriteLine("Loading data...");

            finished = 0;
            failed = 0;
            resultTree = new NodeClass();
            resultRule = new List<NodeClass>();

            foreach (String file in files)
            {
                currentFileName = Path.GetFileName(file);
                if (finished % 100 == 0)
                    Console.Title = String.Format("Loaded {0:F2}% of files...", ((decimal)finished * 100 / (decimal)files.Length));

                //generate schema from xml file
                if (!validateSchema)
                    using (Stream xmlStream = File.OpenRead(file))
                    {
                        if (xmlStream.Length == 0)
                        {
                            WriteToLog("Cannot read file: " + currentFileName, null, "ERROR");
                            failed++;
                            continue;
                        }

                        XmlDocument xml = new XmlDocument();
                        xml.Load(xmlStream);

                        LoadEls(xml.DocumentElement);
                    }
                else
                {
                    //validate schema against xml file
                    //load schema
                    using (Stream xmlStream = File.OpenRead(file))
                    using (Stream schemaStream = File.OpenRead(schemaPath))
                    {
                        if (schemaStream.Length == 0)
                        {
                            WriteToLog("Cannot read schema: " + schemaPath, null, "ERROR");
                            failed++;
                            break;
                        }
                        if (xmlStream.Length == 0)
                        {
                            WriteToLog("Cannot read file: " + currentFileName, null, "ERROR");
                            failed++;
                            continue;
                        }

                        XmlReaderSettings settings = new XmlReaderSettings();
                        settings.ValidationType = ValidationType.Schema;
                        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
                        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
                        settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
                        settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
                        settings.Schemas.Add(@"http://www.w3.org/2001/XMLSchema", schemaPath);

                        XmlReader xmlReader = XmlReader.Create(xmlStream, settings);
                        failedValidation = false;
                        while (xmlReader.Read())
                            if (failedValidation)
                            {
                                failed++;
                                //break;
                            }
                    }
                }

                finished++;
            }
            Console.Title = String.Format("Loaded 100% of files.");
            Console.WriteLine(String.Format("Data loaded. {0} of files failed.\n", failed));
            if (failed > 0)
                WriteToLog(String.Format("Data loaded complete. {0} of files failed.\n", failed), null, "INFO");

            //check min/maxOccurs
            if (!validateSchema)
            {
                Console.WriteLine(String.Format("Processing all {0} files.", files.Length));
                finished = 0;
                failed = 0;

                foreach (String file in files)
                {
                    currentFileName = Path.GetFileName(file);
                    if (finished % 100 == 0)
                        Console.Title = String.Format("Processed {0:F2}% of files...", ((decimal)finished * 100 / (decimal)files.Length));

                    using (Stream xmlStream = File.OpenRead(file))
                    {
                        if (xmlStream.Length == 0)
                        {
                            WriteToLog("Cannot read file: " + currentFileName, null, "ERROR");
                            failed++;
                            continue;
                        }

                        XmlDocument xml = new XmlDocument();
                        xml.Load(xmlStream);

                        ProcessLoadedData(xml.DocumentElement);
                    }
                    finished++;
                }
                Console.Title = "Processed 100% of files.";
                Console.WriteLine(String.Format("Data processed. {0} of files failed.\n", failed));
                if (failed > 0)
                    WriteToLog(String.Format("Data processed. {0} of files failed.\n", failed), null, "ERROR");
            }

            //print results
            Console.WriteLine("Printing Tree/Rule/Logs to files at:\n" + logFolder);
            if (!validateSchema)
                PrintResults();

            //clean empty log
            String logPath = Path.Combine(logFolder, logPrefix + "Log.txt");
            if (File.Exists(logPath))
            {
                Stream reader = File.OpenRead(logPath);
                if (reader.Length == 0)
                    File.Delete(logPath);
            }

            Console.WriteLine("Printed.\n");
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: xchgdzq233/testing
        static void LoadChildTree(XmlNode parentEl, NodeClass parentTree)
        {
            foreach (XmlNode currentEl in parentEl)
            {
                //jump <table>
                if (currentEl.LocalName.Equals("table"))
                    continue;

                NodeAppearPair currentNodeTreePair = parentTree.GetChild(currentEl.LocalName);
                if (Object.ReferenceEquals(currentNodeTreePair, null))
                {
                    //new tree
                    NodeClass currentNodeTree = new NodeClass(currentEl.LocalName);
                    parentTree.childNodes.Add(new NodeAppearPair(ref currentNodeTree));
                }
                else
                    //old tree
                    currentNodeTreePair.totalAppearCount++;

                //check child
                if (currentEl.HasChildNodes)
                    LoadChildTree(currentEl, currentNodeTreePair.nodeClass);
            }
        }