Beispiel #1
0
        internal XmlUtil(Stream stream, string name, bool readToFirstElement, ConfigurationSchemaErrors schemaErrors)
        {
            try
            {
                Filename = name;
                _stream  = stream;
                Reader   = new XmlTextReader(_stream)
                {
                    XmlResolver = null
                };

                // config reads never require a resolver

                SchemaErrors      = schemaErrors;
                _lastLineNumber   = 1;
                _lastLinePosition = 1;

                // When parsing config that we don't intend to copy, skip all content
                // before the first element.
                if (!readToFirstElement)
                {
                    return;
                }
                Reader.WhitespaceHandling = WhitespaceHandling.None;

                bool done = false;
                while (!done && Reader.Read())
                {
                    switch (Reader.NodeType)
                    {
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.Comment:
                    case XmlNodeType.DocumentType:
                        break;

                    case XmlNodeType.Element:
                        done = true;
                        break;

                    default:
                        throw new ConfigurationErrorsException(SR.Config_base_unrecognized_element, this);
                    }
                }
            }
            catch
            {
                ReleaseResources();
                throw;
            }
        }
        internal XmlUtil(Stream stream, string name, bool readToFirstElement, ConfigurationSchemaErrors schemaErrors)
        {
            try {
                _streamName = name;
                _stream = stream;
                _reader = XmlReader.Create(_stream);

                // config reads never require a resolver
                // _reader.XmlResolver = null;

                _schemaErrors = schemaErrors;
                _lastLineNumber = 1;
                _lastLinePosition = 1;

                //
                // When parsing config that we don't intend to copy, skip all content
                // before the first element.
                //
                if (readToFirstElement) {
                    // _reader.WhitespaceHandling = WhitespaceHandling.None;

                    bool done = false;
                    while (!done && _reader.Read()) {
                        switch (_reader.NodeType) {
                            case XmlNodeType.XmlDeclaration:
                            case XmlNodeType.Comment:
                            case XmlNodeType.DocumentType:
                                break;

                            case XmlNodeType.Element:
                                done = true;
                                break;

                            default:
                                throw new ConfigurationErrorsException(SR.GetString(SR.Config_base_unrecognized_element), this);
                        }
                    }
                }
            }
            catch {
                ReleaseResources();
                throw;
            }
        }
Beispiel #3
0
        internal XmlUtil(Stream stream, string name, bool readToFirstElement, ConfigurationSchemaErrors schemaErrors)
        {
            try
            {
                this._streamName         = name;
                this._stream             = stream;
                this._reader             = new XmlTextReader(this._stream);
                this._reader.XmlResolver = null;
                this._schemaErrors       = schemaErrors;
                this._lastLineNumber     = 1;
                this._lastLinePosition   = 1;
                if (readToFirstElement)
                {
                    this._reader.WhitespaceHandling = WhitespaceHandling.None;
                    bool flag = false;
                    while (!flag && this._reader.Read())
                    {
                        switch (this._reader.NodeType)
                        {
                        case XmlNodeType.Comment:
                        case XmlNodeType.DocumentType:
                        case XmlNodeType.XmlDeclaration:
                        {
                            continue;
                        }

                        case XmlNodeType.Element:
                        {
                            flag = true;
                            continue;
                        }
                        }
                        throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_base_unrecognized_element"), this);
                    }
                }
            }
            catch
            {
                this.ReleaseResources();
                throw;
            }
        }
 internal XmlUtil(Stream stream, string name, bool readToFirstElement, ConfigurationSchemaErrors schemaErrors)
 {
     try
     {
         this._streamName = name;
         this._stream = stream;
         this._reader = new XmlTextReader(this._stream);
         this._reader.XmlResolver = null;
         this._schemaErrors = schemaErrors;
         this._lastLineNumber = 1;
         this._lastLinePosition = 1;
         if (readToFirstElement)
         {
             this._reader.WhitespaceHandling = WhitespaceHandling.None;
             bool flag = false;
             while (!flag && this._reader.Read())
             {
                 switch (this._reader.NodeType)
                 {
                     case XmlNodeType.Comment:
                     case XmlNodeType.DocumentType:
                     case XmlNodeType.XmlDeclaration:
                     {
                         continue;
                     }
                     case XmlNodeType.Element:
                     {
                         flag = true;
                         continue;
                     }
                 }
                 throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_base_unrecognized_element"), this);
             }
         }
     }
     catch
     {
         this.ReleaseResources();
         throw;
     }
 }
        // RefreshFactoryRecord
        //
        // Refresh the Factory Record for a particular section.
        //
        private void RefreshFactoryRecord(string configKey) {
            Hashtable                 factoryList   = null;
            FactoryRecord             factoryRecord = null;
            ConfigurationSchemaErrors errors;

            errors = new ConfigurationSchemaErrors();

            // Get Updated Factory List from File
            int lineNumber = 0;
            try {
                using (Impersonate()) {
                    using (Stream stream = Host.OpenStreamForRead(ConfigStreamInfo.StreamName)) {
                        if (stream != null) {
                            ConfigStreamInfo.HasStream = true;

                            using (XmlUtil xmlUtil = new XmlUtil(stream, ConfigStreamInfo.StreamName, true, errors)) {
                                try {
                                    factoryList = ScanFactories(xmlUtil);
                                    ThrowIfParseErrors(xmlUtil.SchemaErrors);
                                }
                                catch {
                                    lineNumber = xmlUtil.LineNumber;
                                    throw;
                                }
                            }
                        }
                    }
                }

                // Add implicit sections to the factory list
                if (factoryList == null) {
                    // But if factoryList isn't found in this config, we still try to
                    // add implicit sections to an empty factoryList.
                    factoryList = new Hashtable();
                }
                
                AddImplicitSections(factoryList);

                if (factoryList != null) {
                    factoryRecord = (FactoryRecord) factoryList[configKey];
                }
            }
            //
            // Guarantee that exceptions contain the name of the stream and an approximate line number if available.
            //
            // And don't allow frames up the stack to run exception filters while impersonated.
            catch (Exception e) {
                errors.AddError(
                        ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_error_loading_XML_file), e, ConfigStreamInfo.StreamName, lineNumber),
                        ExceptionAction.Global);

            } 
            catch {
                errors.AddError(
                        ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_error_loading_XML_file), null, ConfigStreamInfo.StreamName, lineNumber),
                        ExceptionAction.Global);
            }
                    
            // Set/Add/Remove Record
            // Note that the _factoryRecords hashtable is protected by the hierarchy lock.
            if (factoryRecord != null || HasFactoryRecords) {
                EnsureFactories()[configKey] = factoryRecord;
            }
            
            // Throw accumulated errors
            ThrowIfParseErrors(errors);
        }
 // ThrowIfParseErrors
 //
 // Throw if there were parse errors detected
 //
 private void ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors) {
     schemaErrors.ThrowIfErrors(ClassFlags[ClassIgnoreLocalErrors]);
 }
        internal void Init(
                IInternalConfigRoot         configRoot,
                BaseConfigurationRecord     parent,
                string                      configPath,
                string                      locationSubPath) {

            _initErrors = new ConfigurationSchemaErrors();

            //
            // try/catch here is only for unexpected exceptions due to errors in
            // our own code, as we always want the configuration record to be 
            // usable
            //
            try {
                _configRoot = (InternalConfigRoot) configRoot;
                _parent = parent;
                _configPath = configPath;
                _locationSubPath = locationSubPath;
                _configName = ConfigPathUtility.GetName(configPath);

                if (IsLocationConfig) {
                    _configStreamInfo = _parent.ConfigStreamInfo;
                }
                else {
                    _configStreamInfo = new ConfigRecordStreamInfo();
                }

                // no more initialization in case of root config
                if (IsRootConfig)
                    return;

                // determine what features we support
                _flags[SupportsChangeNotifications] = ClassFlags[ClassSupportsChangeNotifications] && Host.SupportsChangeNotifications;
                _flags[SupportsRefresh] = ClassFlags[ClassSupportsRefresh] && Host.SupportsRefresh;
                _flags[SupportsKeepInputs] = ClassFlags[ClassSupportsKeepInputs] || _flags[SupportsRefresh];
                _flags[SupportsPath] = Host.SupportsPath;
                _flags[SupportsLocation] = Host.SupportsLocation;

                // get static state based on the configPath
                if (_flags[SupportsLocation]) {
                    _flags[IsAboveApplication] = Host.IsAboveApplication(_configPath);
                }

                _flags[IsTrusted] = Host.IsTrustedConfigPath(_configPath);

                ArrayList locationSubPathInputs = null;

                if (_flags[SupportsLocation]) {
                    //
                    // Treat location inputs from parent record
                    // as though they were bonafide sections in this record.
                    //
                    if (IsLocationConfig && _parent._locationSections != null) {
                        // Resolve paths and check for errors in location sections.
                        _parent.ResolveLocationSections();

                        int i = 0;
                        while (i < _parent._locationSections.Count) {
                            LocationSectionRecord locationSectionRecord = (LocationSectionRecord) _parent._locationSections[i];

                            if (!StringUtil.EqualsIgnoreCase(locationSectionRecord.SectionXmlInfo.SubPath, _locationSubPath)) {
                                i++;
                            }
                            else {
                                // remove the locationSectionRecord from the list
                                _parent._locationSections.RemoveAt(i);

                                if (locationSubPathInputs == null) {
                                    locationSubPathInputs = new ArrayList();
                                }

                                locationSubPathInputs.Add(locationSectionRecord);
                            }
                        }
                    }

                    //
                    // Add location inputs that apply to this path, all the way up the parent hierarchy.
                    //
                    if (Host.IsLocationApplicable(_configPath)) {
                        BaseConfigurationRecord current = _parent;
                        while (!current.IsRootConfig) {
                            if (current._locationSections != null) {
                                current.ResolveLocationSections();
                                foreach (LocationSectionRecord locationSectionRecord in current._locationSections) {
                                    // We use the location tag input if:
                                    // - The path matches, and
                                    // - It's not skipped due to inheritInChildApplications setting.
                                    if (StringUtil.EqualsIgnoreCase(locationSectionRecord.SectionXmlInfo.TargetConfigPath, this._configPath) &&
                                        !ShouldSkipDueToInheritInChildApplications(locationSectionRecord.SectionXmlInfo.SkipInChildApps)) {
                                        // add the location input for this section
                                        SectionRecord sectionRecord = EnsureSectionRecord(locationSectionRecord.ConfigKey, true);
                                        SectionInput sectionInput = new SectionInput(
                                                locationSectionRecord.SectionXmlInfo, locationSectionRecord.ErrorsList);

                                        sectionRecord.AddLocationInput(sectionInput);

                                        // copy the initialization errors to the record
                                        if (locationSectionRecord.HasErrors) {
                                            this._initErrors.AddSavedLocalErrors(locationSectionRecord.Errors);
                                        }
                                    }
                                }
                            }
    
                            current = current._parent;
                        }
                    }
                }

                if (!IsLocationConfig) {
                    //
                    // If config file exists, open it and parse it once so we can
                    // find what to evaluate later.
                    //
                    InitConfigFromFile();
                }
                else {
                    // Add location sections for this record as bonafide sections.
                    if (locationSubPathInputs != null) {
                        foreach (LocationSectionRecord locationSectionRecord in locationSubPathInputs) {
                            // add the file input
                            SectionRecord sectionRecord = EnsureSectionRecord(locationSectionRecord.ConfigKey, true);
                            SectionInput sectionInput = new SectionInput(locationSectionRecord.SectionXmlInfo, locationSectionRecord.ErrorsList);
                            sectionRecord.AddFileInput(sectionInput);

                            // copy the initialization errors to the record
                            if (locationSectionRecord.HasErrors) {
                                this._initErrors.AddSavedLocalErrors(locationSectionRecord.Errors);
                            }
                        }
                    }
                }
            }
            //
            // Capture all exceptions and include them in initialization errors.
            //
            catch (Exception e) {
                string streamname = (ConfigStreamInfo != null) ? ConfigStreamInfo.StreamName : null;

                _initErrors.AddError(
                        ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_error_loading_XML_file), e, streamname, 0),
                        ExceptionAction.Global);
            }
            catch {
                string streamname = (ConfigStreamInfo != null) ? ConfigStreamInfo.StreamName : null;

                _initErrors.AddError(
                        ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_error_loading_XML_file), null, streamname, 0),
                        ExceptionAction.Global);
            }
        }