コード例 #1
0
        /// <summary>
        /// Add a configuration value node.
        /// </summary>
        /// <param name="parent">Parent Configuration node</param>
        /// <param name="name">Value node name</param>
        /// <param name="value">Value</param>
        private void AddValueNode(AbstractConfigNode parent, string name, string value, bool encrypted)
        {
            ConfigValueNode vn = new ConfigValueNode(parent.Configuration, parent);

            vn.Encrypted = encrypted;
            vn.Name      = name;
            vn.SetValue(value);

            if (parent.GetType() == typeof(ConfigParametersNode))
            {
                ConfigParametersNode node = (ConfigParametersNode)parent;
                node.Add(vn);
            }
            else if (parent.GetType() == typeof(ConfigPropertiesNode))
            {
                ConfigPropertiesNode node = (ConfigPropertiesNode)parent;
                node.Add(vn);
            }
            else if (parent.GetType() == typeof(ConfigListValueNode))
            {
                ConfigListValueNode node = (ConfigListValueNode)parent;
                node.Add(vn);
            }
            else if (parent.GetType() == typeof(ConfigPathNode))
            {
                ConfigPathNode node = (ConfigPathNode)parent;
                node.AddChildNode(vn);
            }
            else
            {
                throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add value node to parent. [parent={0}]", parent.GetType().FullName));
            }
        }
コード例 #2
0
        /// <summary>
        /// Parse a included configuration reference.
        /// </summary>
        /// <param name="parent">Parent Config Node</param>
        /// <param name="elem">XML Element</param>
        private void AddIncludeNode(ConfigPathNode parent, XmlElement elem)
        {
            string configName = elem.GetAttribute(ConstXmlConfigIncludeNode.XML_CONFIG_INCLUDE_NAME);

            if (String.IsNullOrWhiteSpace(configName))
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlConfigIncludeNode.XML_CONFIG_INCLUDE_NAME);
            }
            string path = elem.GetAttribute(ConstXmlConfigIncludeNode.XML_CONFIG_INCLUDE_PATH);

            if (String.IsNullOrWhiteSpace(path))
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlConfigIncludeNode.XML_CONFIG_INCLUDE_PATH);
            }
            string st = elem.GetAttribute(ConstXmlConfigIncludeNode.XML_CONFIG_INCLUDE_TYPE);

            if (String.IsNullOrWhiteSpace(st))
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlConfigIncludeNode.XML_CONFIG_INCLUDE_TYPE);
            }
            EUriScheme type = EUriScheme.none.ParseScheme(st);

            if (type == EUriScheme.none)
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlConfigIncludeNode.XML_CONFIG_INCLUDE_TYPE);
            }
            string vs = elem.GetAttribute(ConstXmlConfigIncludeNode.XML_CONFIG_INCLUDE_VERSION);

            if (String.IsNullOrWhiteSpace(vs))
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlConfigIncludeNode.XML_CONFIG_INCLUDE_VERSION);
            }
            Version version = Version.Parse(vs);

            ConfigIncludeNode node = new ConfigIncludeNode(configuration, parent);

            node.Name       = elem.Name;
            node.ConfigName = configName;
            node.Path       = ReaderTypeHelper.ParseUri(path);
            node.ReaderType = type;
            node.Version    = version;

            using (AbstractReader reader = ReaderTypeHelper.GetReader(type, node.Path))
            {
                reader.Open();
                XmlConfigParser parser = new XmlConfigParser();
                parser.Parse(node.ConfigName, reader, node.Version, settings);
                Configuration config = parser.GetConfiguration();
                node.Reference = config;
                node.Node      = config.RootConfigNode;
                node.UpdateConfiguration(configuration);
            }
            parent.AddChildNode(node);
        }
コード例 #3
0
        /// <summary>
        /// Create a Resource node instance.
        /// </summary>
        /// <param name="parent">Parent Config node</param>
        /// <param name="elem">XML Element</param>
        private void AddResourceNode(ConfigPathNode parent, XmlElement elem)
        {
            string resourceName = elem.GetAttribute(ConstXmlResourceNode.XML_CONFIG_ATTR_RESOURCE_NAME);

            if (String.IsNullOrWhiteSpace(resourceName))
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlResourceNode.XML_CONFIG_ATTR_RESOURCE_NAME);
            }
            string st = elem.GetAttribute(ConstXmlResourceNode.XML_CONFIG_ATTR_RESOURCE_TYPE);

            if (String.IsNullOrWhiteSpace(st))
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlResourceNode.XML_CONFIG_ATTR_RESOURCE_TYPE);
            }
            EResourceType type = Enum.Parse <EResourceType>(st);

            if (type == EResourceType.NONE)
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlResourceNode.XML_CONFIG_ATTR_RESOURCE_TYPE);
            }
            Uri uri = null;

            if (elem.HasChildNodes)
            {
                foreach (XmlNode nn in elem.ChildNodes)
                {
                    if (nn.NodeType == XmlNodeType.Element && nn.Name == ConstXmlResourceNode.XML_CONFIG_NODE_RESOURCE_URL)
                    {
                        string su = nn.InnerText;
                        if (String.IsNullOrWhiteSpace(su))
                        {
                            throw ConfigurationException.PropertyMissingException(ConstXmlResourceNode.XML_CONFIG_NODE_RESOURCE_URL);
                        }
                        uri = new Uri(su);
                        break;
                    }
                }
            }

            if (uri == null)
            {
                throw ConfigurationException.PropertyMissingException(ConstXmlResourceNode.XML_CONFIG_NODE_RESOURCE_URL);
            }
            ConfigResourceNode node = null;

            switch (type)
            {
            case EResourceType.FILE:
                node = new ConfigResourceFile(configuration, parent);
                break;

            case EResourceType.DIRECTORY:
                node = new ConfigDirectoryResource(configuration, parent);
                break;

            case EResourceType.ZIP:
                node = new ConfigResourceZip(configuration, parent);
                break;
            }
            node.Name         = elem.Name;
            node.Type         = type;
            node.Location     = uri;
            node.ResourceName = resourceName;
            if (settings.DownloadOptions == EDownloadOptions.LoadRemoteResourcesOnStartup)
            {
                if (type == EResourceType.ZIP || type == EResourceType.FILE)
                {
                    ConfigResourceFile fnode = (ConfigResourceFile)node;
                    ConfigResourceHelper.DownloadResource(fnode);
                }
            }

            if (type == EResourceType.DIRECTORY)
            {
                ConfigDirectoryResource dnode = (ConfigDirectoryResource)node;
                if (!dnode.Location.IsFile)
                {
                    throw new ConfigurationException(String.Format("Invalid URI: Must be a local/mounted directory. [uri={0}]", dnode.Location.ToString()));
                }
                string        dir = dnode.Location.LocalPath;
                DirectoryInfo di  = new DirectoryInfo(dir);
                if (di.Exists)
                {
                    throw new ConfigurationException(String.Format("Invalid URI: Directory not found. [uri={0}]", dnode.Location.ToString()));
                }
                dnode.Downloaded = true;
                dnode.Directory  = di;
            }
            parent.AddChildNode(node);
        }
コード例 #4
0
        /// <summary>
        /// Parse a configuration node.
        /// </summary>
        /// <param name="name">Config node name</param>
        /// <param name="elem">XML Element</param>
        /// <param name="nodeStack">Current Node Stack</param>
        private void ParseBodyNode(string name, XmlElement elem, Stack <AbstractConfigNode> nodeStack)
        {
            bool popStack             = false;
            bool processed            = false;
            AbstractConfigNode parent = nodeStack.Peek();

            if (IsTextNode(elem))
            {
                bool encrypted = false;
                if (elem.HasAttributes)
                {
                    string attr = elem.Attributes[XML_VALUE_ENCRYPTED].Value;
                    if (!String.IsNullOrWhiteSpace(attr))
                    {
                        if (attr.CompareTo("true") == 0)
                        {
                            encrypted = true;
                        }
                    }
                }
                AddValueNode(parent, elem.Name, elem.FirstChild.Value, encrypted);
            }
            else
            {
                XmlNodeType nt = IsListNode(elem);
                if (nt == XmlNodeType.Element)
                {
                    if (parent.GetType() != typeof(ConfigPathNode))
                    {
                        throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName));
                    }
                    ConfigPathNode pnode = (ConfigPathNode)parent;

                    ConfigElementListNode nodeList = new ConfigElementListNode(parent.Configuration, parent);
                    nodeList.Name = name;
                    pnode.AddChildNode(nodeList);

                    nodeStack.Push(nodeList);
                    popStack = true;
                }
                else if (nt == XmlNodeType.Text)
                {
                    if (parent.GetType() != typeof(ConfigPathNode))
                    {
                        throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName));
                    }
                    ConfigPathNode pnode = (ConfigPathNode)parent;

                    ConfigListValueNode nodeList = new ConfigListValueNode(parent.Configuration, parent);
                    nodeList.Name = name;
                    pnode.AddChildNode(nodeList);

                    nodeStack.Push(nodeList);
                    popStack = true;
                }
                else
                {
                    if (elem.Name == ConstXmlConfigIncludeNode.XML_CONFIG_INCLUDE)
                    {
                        if (parent.GetType() != typeof(ConfigPathNode))
                        {
                            throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName));
                        }
                        ConfigPathNode pnode = (ConfigPathNode)parent;
                        AddIncludeNode(pnode, elem);
                        processed = true;
                    }
                    else if (elem.Name == ConstXmlResourceNode.XML_CONFIG_NODE_RESOURCE)
                    {
                        if (parent.GetType() != typeof(ConfigPathNode))
                        {
                            throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName));
                        }
                        ConfigPathNode pnode = (ConfigPathNode)parent;
                        AddResourceNode(pnode, elem);
                        processed = true;
                    }
                    else if (elem.Name == settings.ParametersNodeName)
                    {
                        if (parent.GetType() != typeof(ConfigPathNode))
                        {
                            throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName));
                        }
                        ConfigPathNode       pnode     = (ConfigPathNode)parent;
                        ConfigParametersNode paramNode = new ConfigParametersNode(parent.Configuration, parent);
                        paramNode.Name = name;
                        pnode.AddChildNode(paramNode);

                        nodeStack.Push(paramNode);
                        popStack = true;
                    }
                    else if (elem.Name == settings.PropertiesNodeName)
                    {
                        if (parent.GetType() != typeof(ConfigPathNode))
                        {
                            throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add List node to parent. [parent={0}]", parent.GetType().FullName));
                        }
                        ConfigPathNode       pnode    = (ConfigPathNode)parent;
                        ConfigPropertiesNode propNode = new ConfigPropertiesNode(parent.Configuration, parent);
                        propNode.Name = name;
                        pnode.AddChildNode(propNode);

                        nodeStack.Push(propNode);
                        popStack = true;
                    }
                    else
                    {
                        ConfigPathNode cnode = new ConfigPathNode(parent.Configuration, parent);
                        cnode.Name = name;
                        if (parent.GetType() == typeof(ConfigPathNode))
                        {
                            ConfigPathNode pnode = (ConfigPathNode)parent;
                            pnode.AddChildNode(cnode);
                        }
                        else if (parent.GetType() == typeof(ConfigElementListNode))
                        {
                            ConfigElementListNode nodeList = (ConfigElementListNode)parent;
                            nodeList.Add(cnode);
                        }
                        else
                        {
                            throw new ConfigurationException(String.Format("Invalid Stack State: Cannot add path node to parent. [parent={0}]", parent.GetType().FullName));
                        }
                        nodeStack.Push(cnode);
                        popStack = true;
                    }
                }
                if (!processed)
                {
                    if (elem.HasAttributes)
                    {
                        AbstractConfigNode pp = nodeStack.Peek();
                        if (pp.GetType() == typeof(ConfigPathNode))
                        {
                            ConfigPathNode       cp    = (ConfigPathNode)pp;
                            ConfigAttributesNode attrs = new ConfigAttributesNode(cp.Configuration, cp);
                            cp.AddChildNode(attrs);
                            foreach (XmlAttribute attr in elem.Attributes)
                            {
                                ConfigValueNode vn = new ConfigValueNode(attrs.Configuration, attrs);
                                vn.Name = attr.Name;
                                vn.SetValue(attr.Value);

                                attrs.Add(vn);
                            }
                        }
                    }
                    if (elem.HasChildNodes)
                    {
                        foreach (XmlNode cnode in elem.ChildNodes)
                        {
                            if (cnode.NodeType == XmlNodeType.Element)
                            {
                                ParseBodyNode(cnode.Name, (XmlElement)cnode, nodeStack);
                            }
                        }
                    }
                }
                if (popStack)
                {
                    nodeStack.Pop();
                }
            }
        }