示例#1
0
        /// <summary>
        /// Creates a <see cref="RuleBook"/> by parsing the content of the
        /// indicated configuration file.
        /// </summary>
        /// <param name="filename">The configuration filename.</param>
        /// <returns>A <see cref="RuleBook"/> instance created using the
        ///	details in the configuration file.</returns>
        public static RuleBook Load(string filename)
        {
            RuleBook ruleBook = new RuleBook();

            FileStream  stream   = File.OpenRead(Application.PathTo(filename));
            XmlDocument document = XmlUtility.NonValidatingParse(stream);

            XmlNodeList list = DOM.GetChildElements(document.DocumentElement);

            foreach (XmlElement context in list)
            {
                if (context.LocalName.Equals("identifier"))
                {
                    string name = context.GetAttribute("name");

                    Property [] properties = LoadProperties(XPath.Paths(context, "property"));
                    IFormatter  formatter  = (IFormatter)LoadClass(XPath.Paths(context, "formatter"));

                    ruleBook.Add(new IdentifierRule(name, properties, formatter));
                }
                else
                {
                    log.Warn("Unexpected element '" + context.LocalName + "'");
                }
            }

            return(ruleBook);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="filename">The configuration filename</param>
        /// <returns></returns>
        public static ClassificationScheme Load(string filename)
        {
            ClassificationScheme          scheme     = new ClassificationScheme();
            Dictionary <string, Category> dictionary = new Dictionary <string, Category>();

            try {
                FileStream  stream   = File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename));
                XmlDocument document = XmlUtility.NonValidatingParse(stream);
                foreach (XmlElement element in DOM.GetChildElements(document.DocumentElement))
                {
                    if (element.LocalName.Equals("category"))
                    {
                        Category category;
                        string   attribute  = element.GetAttribute("id");
                        string   name       = element.GetAttribute("name");
                        string   str3       = element.GetAttribute("abstract");
                        string   str4       = element.GetAttribute("superClasses");
                        ExprNode expression = LoadExpr(DOM.GetFirstChild(element));
                        bool     concrete   = (expression != null) && ((str3 == null) || !str3.Equals("true"));
                        if ((str4 != null) && (str4.Length != 0))
                        {
                            string[]   strArray = str4.Split(new char[] { ' ' });
                            Category[] parents  = new Category[strArray.Length];
                            for (int i = 0; i < strArray.Length; i++)
                            {
                                parents [i] = dictionary [strArray [i]];
                            }
                            category = new XmlCategory(scheme, name, concrete, parents, expression);
                        }
                        else
                        {
                            category = new XmlCategory(scheme, name, concrete, expression);
                        }
                        if ((attribute != null) && (attribute.Length != 0))
                        {
                            dictionary [attribute] = category;
                        }
                    }
                    stream.Close();
                }
                return(scheme);
            }
            catch (Exception exception) {
                log.Fatal("Failed to load classification from " + filename, exception);
            }
            return(null);
        }
        /// <summary>
        /// Recursively walks through an XML Schema making the changed indicated
        /// by the override definitions.
        /// </summary>
        /// <param name="element">The <see cref="XmlElement"/> to process.</param>
        private void WalkSchema(XmlElement element)
        {
            // Does this element have a child annotation?
            XmlElement annotation = XPath.Path(element, "annotation");

            if (annotation != null)
            {
                XmlElement appInfo = XPath.Path(annotation, "appinfo");
                if (appInfo != null)
                {
                    bool skipped   = false;
                    bool exclusive = false;
                    bool included  = false;

                    foreach (XmlElement command in DOM.GetChildElements(appInfo))
                    {
                        if (command.LocalName.Equals("exclusive"))
                        {
                            exclusive = true;
                            if (command.GetAttribute("view").Equals(viewOption.Value))
                            {
                                included = true;
                            }

                            appInfo.RemoveChild(command);
                        }
                        else if (command.LocalName.Equals("skip"))
                        {
                            if (command.GetAttribute("view").Equals(viewOption.Value))
                            {
                                skipped = true;
                            }

                            appInfo.RemoveChild(command);
                        }
                        else if (command.LocalName.Equals("override"))
                        {
                            if (command.GetAttribute("view").Equals(viewOption.Value))
                            {
                                // Do the actual attribute overriding
                                foreach (XmlAttribute attribute in command.Attributes)
                                {
                                    if (!attribute.LocalName.Equals("view"))
                                    {
                                        string oldValue = element.GetAttribute(attribute.LocalName, attribute.NamespaceURI);
                                        string newValue = attribute.Value;

                                        element.SetAttribute(attribute.LocalName, attribute.NamespaceURI, attribute.Value);

                                        if (attribute.LocalName.Equals("minOccurs"))
                                        {
                                            if (IsNumber(oldValue) && IsNumber(newValue))
                                            {
                                                if (Int32.Parse(oldValue) > Int32.Parse(newValue))
                                                {
                                                    Console.Out.WriteLine("Invalid minOccurs override on "
                                                                          + element.LocalName + " @" + attribute.LocalName);
                                                }
                                            }
                                        }
                                        else if (attribute.LocalName.Equals("maxOccurs"))
                                        {
                                            if (IsNumber(oldValue) && IsNumber(newValue))
                                            {
                                                if (Int32.Parse(oldValue) < Int32.Parse(newValue))
                                                {
                                                    Console.Out.WriteLine("Invalid maxOccurs override on "
                                                                          + element.LocalName + " @" + attribute.LocalName);
                                                }
                                            }
                                        }
                                    }
                                }

                                XmlNodeList newDocs = XPath.Paths(command, "documentation");

                                // If we have new documentation the replace the old stuff
                                if (newDocs.Count > 0)
                                {
                                    XmlNodeList oldDocs = XPath.Paths(annotation, "documentation");

                                    foreach (XmlNode node in oldDocs)
                                    {
                                        annotation.RemoveChild(node);
                                    }

                                    foreach (XmlNode node in newDocs)
                                    {
                                        annotation.AppendChild(node);
                                    }
                                }
                            }
                            appInfo.RemoveChild(command);
                        }
                    }

                    // Remove elements that are skipped or excluded
                    if (skipped || (exclusive && !included))
                    {
                        element.ParentNode.RemoveChild(element);
                    }

                    // Remove empty appInfo elements
                    if (DOM.GetChildElements(appInfo).Count == 0)
                    {
                        annotation.RemoveChild(appInfo);
                    }
                }

                // Remove empty annotation elements
                if (DOM.GetChildElements(annotation).Count == 0)
                {
                    element.RemoveChild(annotation);
                }
            }

            // Handle view renaming in namespace attributes
            foreach (XmlAttribute attribute in element.Attributes)
            {
                if (attribute.LocalName.StartsWith("xmlns") ||
                    attribute.LocalName.Equals("namespace") ||
                    attribute.LocalName.Equals("targetNamespace"))
                {
                    attribute.Value = attribute.Value.Replace("master", viewOption.Value);
                }
            }

            // Handle file renaming in schemaLocation attributes
            if (suffixOption.Present)
            {
                XmlAttribute attribute = element.GetAttributeNode("schemaLocation");
                if (attribute != null)
                {
                    string location = attribute.Value;

                    foreach (FileInfo info in files)
                    {
                        string filename = info.FullName;
                        string oldname
                            = Path.GetFileName(filename);
                        string newname
                            = Path.GetFileNameWithoutExtension(filename)
                              + suffixOption.Value
                              + Path.GetExtension(filename);

                        location = location.Replace(oldname, newname);
                    }

                    attribute.Value = location;
                }
            }

            // Recurse over any child elements
            foreach (XmlElement child in DOM.GetChildElements(element))
            {
                WalkSchema(child);
            }
        }