예제 #1
0
        /// <summary>
        /// Initializes the ConfigurationManager with an absolute path to the file that describes the root
        /// configuration node.
        /// </summary>
        /// <param name="rootPath">
        /// The absolute path to the file that describes the root configuration node.
        /// </param>
        /// <exception cref="ConfigurationException">
        /// If the ConfigurationManager has already been initialized.
        /// In case of any error during the initialization process.
        /// </exception>
        public static void Initialize(string rootPath)
        {
            if (rootPath == null || rootPath.Length == 0)
            {
                throw new ArgumentNullException("rootPath");
            }
            if (isInitialized)
            {
                throw new ConfigurationException("The ConfigurationManager has already been initialized.");
            }

            ConfigurationObject.UnregisterAllConfigObjects();
            RCPackageFormatMap.Clear();
            ConstantsTable.Clear();
            TraceManager.UnregisterAllTraceFilters();

            currentContext = new ConfigurationContext();
            FileInfo rootNodeFile = new FileInfo(rootPath);

            rootNode = new ConfigurationNode(rootNodeFile);
            rootNode.LoadConfigObjects(currentContext);

            isInitialized = true;
        }
예제 #2
0
        /// <see cref="ConfigObjectContents.Load_i"/>
        protected override void Load_i(XElement rootElem)
        {
            IEnumerable <XElement> formatElems = rootElem.Elements(PACKAGE_FORMAT_ELEM);

            foreach (XElement fmtElem in formatElems)
            {
                XAttribute formatNamespace = fmtElem.Attribute(NAMESPACE_ATTR);
                XAttribute formatName      = fmtElem.Attribute(NAME_ATTR);
                XAttribute formatID        = fmtElem.Attribute(ID_ATTR);
                if (formatNamespace != null && formatName != null && formatID != null)
                {
                    string newFormatName = string.Format("{0}.{1}", formatNamespace.Value, formatName.Value);
                    if (RCPackageFormatMap.Contains(newFormatName))
                    {
                        /// Error: package format with the same name already exists
                        throw new ConfigurationException(string.Format("RCPackageFormat {0} already exists!", newFormatName));
                    }
                    RCPackageFormat newFormat = new RCPackageFormat(newFormatName);

                    bool hasFields = false;
                    IEnumerable <XElement> fieldElems = fmtElem.Elements(FIELD_ELEM);
                    foreach (XElement fieldElem in fieldElems)
                    {
                        XAttribute fieldTypeAttr = fieldElem.Attribute(FIELD_TYPE_ATTR);
                        if (fieldTypeAttr != null)
                        {
                            RCPackageFieldType fieldType;
                            if (EnumMap <RCPackageFieldType, string> .TryDemap(fieldTypeAttr.Value, out fieldType))
                            {
                                newFormat.DefineField(fieldType);
                                hasFields = true;
                            }
                            else
                            {
                                /// Error: unexpected package field type
                                throw new ConfigurationException(string.Format("Unexpected package field type {0}!", fieldTypeAttr.Value));
                            }
                        }
                        else
                        {
                            /// Error: no field type defined
                            throw new ConfigurationException("No package field type defined!");
                        }
                    }

                    if (hasFields)
                    {
                        int newFormatID = XmlHelper.LoadInt(formatID.Value);
                        RCPackageFormat.RegisterFormat(newFormat, newFormatID);
                        RCPackageFormatMap.Add(newFormatName, newFormatID);
                    }
                    else
                    {
                        /// Error: no fields for the current package format
                        throw new ConfigurationException("No package fields defined!");
                    }
                }
                else
                {
                    /// Error: no namespace, name or ID defined
                    throw new ConfigurationException("No namespace, name or ID defined for package format!");
                }
            }

            return;
        }