Exemplo n.º 1
0
        /// <summary>
        /// Read object definitions from the given DOM element, and register
        /// them with the given object registry.
        /// </summary>
        /// <param name="doc">The DOM element containing object definitions, usually the
        /// root (document) element.</param>
        /// <param name="readerContext">The current context of the reader.  Includes
        /// the resource being parsed</param>
        /// <returns>
        /// The number of object definitions that were loaded.
        /// </returns>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// In case of parsing errors.
        /// </exception>
        public void RegisterObjectDefinitions(XmlDocument doc, XmlReaderContext readerContext)
        {
            //int objectDefinitionCounter = 0;

            this.readerContext = readerContext;

            if (log.IsEnabled(LogLevel.Debug))
            {
                log.LogDebug("Loading object definitions.");
            }

            XmlElement root = doc.DocumentElement;

            ObjectDefinitionParserHelper parserHelper = CreateHelper(readerContext, root);


            PreProcessXml(root);

            ParseObjectDefinitions(root, parserHelper);

            PostProcessXml(root);

            if (log.IsEnabled(LogLevel.Debug))
            {
                log.LogDebug(
                    $"Found {readerContext.Registry.ObjectDefinitionCount} <{ObjectDefinitionConstants.ObjectElement}> elements defining objects.");
            }
        }
        public void WhenConfigFileIsNotValid()
        {
            //            const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
            //<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
            //    <v:required test='#this'>
            //        <v:message id='error.airportCode.required' providers='summary'/>
            //        <v:action type='Spring.Validation.Actions.ExpressionAction, Spring.Core' when='true'/>
            //    </v:required>
            //</objects>
            //";
            XmlDocument doc = GetValidatedXmlResource("_WhenConfigFileIsNotValid.xml");

            MockObjectDefinitionRegistry registry = new MockObjectDefinitionRegistry();
            IObjectDefinitionDocumentReader reader = new DefaultObjectDefinitionDocumentReader();

            XmlReaderContext readerContext = new XmlReaderContext(null, new XmlObjectDefinitionReader(registry));
            ObjectDefinitionParserHelper helper = new ObjectDefinitionParserHelper(readerContext);
            helper.InitDefaults(doc.DocumentElement);
            ParserContext parserContext = new ParserContext(helper.ReaderContext, helper);

            ValidationNamespaceParser parser = new ValidationNamespaceParser();
            foreach (XmlElement element in doc.DocumentElement.ChildNodes)
            {
                if (element.NamespaceURI == "http://www.springframework.net/validation")
                {
                    parser.ParseElement(element, parserContext);
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ObjectDefinitionParserHelper"/> class.
 /// </summary>
 /// <param name="readerContext">The reader context.</param>
 /// <param name="root">The root element of the definition document to parse</param>
 public ObjectDefinitionParserHelper(XmlReaderContext readerContext, XmlElement root)
 {
     log = LogManager.GetLogger(this.GetType());
     this.readerContext = readerContext;
     this.objectsNamespaceParser = (ObjectsNamespaceParser) readerContext.NamespaceParserResolver.Resolve(ObjectsNamespaceParser.Namespace);
     if (root != null)
     {
         InitDefaults(root);
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ObjectDefinitionParserHelper"/> class.
 /// </summary>
 /// <param name="readerContext">The reader context.</param>
 /// <param name="root">The root element of the definition document to parse</param>
 public ObjectDefinitionParserHelper(XmlReaderContext readerContext, XmlElement root)
 {
     log = LogManager.GetLogger(this.GetType());
     this.readerContext          = readerContext;
     this.objectsNamespaceParser = (ObjectsNamespaceParser)readerContext.NamespaceParserResolver.Resolve(ObjectsNamespaceParser.Namespace);
     if (root != null)
     {
         InitDefaults(root);
     }
 }
        /// <summary>
        /// Register the object definitions contained in the given DOM document.
        /// </summary>
        /// <param name="doc">The DOM document.</param>
        /// <param name="resource">
        /// The original resource from where the <see cref="System.Xml.XmlDocument"/>
        /// was read.
        /// </param>
        /// <returns>
        /// The number of object definitions that were registered.
        /// </returns>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// In case of parsing errors.
        /// </exception>
        public int RegisterObjectDefinitions(
            XmlDocument doc, IResource resource)
        {
            IObjectDefinitionDocumentReader documentReader = CreateObjectDefinitionDocumentReader();

            //TODO make void return and get object count from registry.
            int countBefore = Registry.ObjectDefinitionCount;
            XmlReaderContext readerContext = CreateReaderContext(resource);

            readerContext.NamespaceParserResolver = this.NamespaceParserResolver;
            documentReader.RegisterObjectDefinitions(doc, readerContext);
            return(Registry.ObjectDefinitionCount - countBefore);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Read object definitions from the given DOM element, and register
        /// them with the given object registry.
        /// </summary>
        /// <param name="doc">The DOM element containing object definitions, usually the
        /// root (document) element.</param>
        /// <param name="readerContext">The current context of the reader.  Includes
        /// the resource being parsed</param>
        /// <returns>
        /// The number of object definitions that were loaded.
        /// </returns>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// In case of parsing errors.
        /// </exception>
        public void RegisterObjectDefinitions(XmlDocument doc, XmlReaderContext readerContext)
        {
            //int objectDefinitionCounter = 0;

            this.readerContext = readerContext;

            #region Instrumentation

            if (log.IsDebugEnabled)
            {
                log.Debug("Loading object definitions.");
            }

            #endregion

            XmlElement root = doc.DocumentElement;

            ObjectDefinitionParserHelper parserHelper = CreateHelper(readerContext, root);


            PreProcessXml(root);

            ParseObjectDefinitions(root, parserHelper);

            PostProcessXml(root);

            #region Instrumentation

            if (log.IsDebugEnabled)
            {
                log.Debug(
                    string.Format(
                        "Found {0} <{1}> elements defining objects.",
                        readerContext.Registry.ObjectDefinitionCount,
                        ObjectDefinitionConstants.ObjectElement));
            }

            #endregion
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ObjectDefinitionParserHelper"/> class.
 /// </summary>
 /// <param name="readerContext">The reader context.</param>
 public ObjectDefinitionParserHelper(XmlReaderContext readerContext)
     : this(readerContext, null)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="WebObjectDefinitionParserHelper"/> class.
 /// </summary>
 /// <param name="webObjectNameGenerator">used for generating object definition names from web object types (page, control)</param>
 /// <param name="readerContext">The reader context.</param>
 /// <param name="root">The root element of the xml document to parse</param>
 public WebObjectDefinitionParserHelper(IWebObjectNameGenerator webObjectNameGenerator, XmlReaderContext readerContext, XmlElement root)
     : base(readerContext, root)
 {
     AssertUtils.ArgumentNotNull(webObjectNameGenerator, "webObjectNameGenerator");
     this.webObjectNameGenerator = webObjectNameGenerator;
 }
Exemplo n.º 9
0
 public ParserContext(XmlReaderContext readerContext, ObjectDefinitionParserHelper parserHelper)
 {
     this.readerContext = readerContext;
     this.parserHelper = parserHelper;
 }
Exemplo n.º 10
0
        /// <summary>
        /// Creates an <see cref="ObjectDefinitionParserHelper"/> instance for the given <paramref name="readerContext"/> and <paramref name="root"/> element.
        /// </summary>
        /// <param name="readerContext">the <see cref="XmlReaderContext"/> to create the <see cref="ObjectDefinitionParserHelper"/> </param>
        /// <param name="root">the root <see cref="XmlElement"/> to start reading from</param>
        /// <returns>a new <see cref="ObjectDefinitionParserHelper"/> instance</returns>
        protected virtual ObjectDefinitionParserHelper CreateHelper(XmlReaderContext readerContext, XmlElement root)
        {
            ObjectDefinitionParserHelper helper = new ObjectDefinitionParserHelper(readerContext, root);

            return(helper);
        }
        /// <summary>
        /// Read object definitions from the given DOM element, and register
        /// them with the given object registry.
        /// </summary>
        /// <param name="doc">The DOM element containing object definitions, usually the
        /// root (document) element.</param>
        /// <param name="readerContext">The current context of the reader.  Includes
        /// the resource being parsed</param>
        /// <returns>
        /// The number of object definitions that were loaded.
        /// </returns>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// In case of parsing errors.
        /// </exception>
        public void RegisterObjectDefinitions(XmlDocument doc, XmlReaderContext readerContext)
        {
            //int objectDefinitionCounter = 0;

            this.readerContext = readerContext;

            #region Instrumentation

            if (log.IsDebugEnabled)
            {
                log.Debug("Loading object definitions.");
            }

            #endregion

            XmlElement root = doc.DocumentElement;

            ObjectDefinitionParserHelper parserHelper = CreateHelper(readerContext, root);


            PreProcessXml(root);

            ParseObjectDefinitions(root, parserHelper);

            PostProcessXml(root);

            #region Instrumentation

            if (log.IsDebugEnabled)
            {
                log.Debug(
                    string.Format(
                        "Found {0} <{1}> elements defining objects.",
                        readerContext.Registry.ObjectDefinitionCount,
                        ObjectDefinitionConstants.ObjectElement));
            }

            #endregion
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ObjectDefinitionParserHelper"/> class.
 /// </summary>
 /// <param name="readerContext">The reader context.</param>
 public ObjectDefinitionParserHelper(XmlReaderContext readerContext)
     : this(readerContext, null)
 {
 }
Exemplo n.º 13
0
 public ParserContext(XmlReaderContext readerContext, ObjectDefinitionParserHelper parserHelper)
 {
     this.readerContext = readerContext;
     this.parserHelper  = parserHelper;
 }
Exemplo n.º 14
0
 public void RegisterObjectDefinitions(XmlDocument doc, XmlReaderContext readerContext)
 {
     throw new TestException("RegisterObjectDefinitions");
 }
Exemplo n.º 15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ParserContext"/> class.
 /// </summary>
 /// <param name="parserHelper">The parser helper.</param>
 /// <param name="containingObjectDefinition">The containing object definition.</param>
 public ParserContext(ObjectDefinitionParserHelper parserHelper, IObjectDefinition containingObjectDefinition)
 {
     this.readerContext = parserHelper.ReaderContext;
     this.parserHelper = parserHelper;
     this.containingObjectDefinition = containingObjectDefinition;
 }
Exemplo n.º 16
0
//        private Stack containingComponents = new Stack();

        /// <summary>
        /// Initializes a new instance of the <see cref="ParserContext"/> class.
        /// </summary>
        /// <param name="parserHelper">The parser helper.</param>
        public ParserContext(ObjectDefinitionParserHelper parserHelper)
        {
            this.readerContext = parserHelper.ReaderContext;
            this.parserHelper = parserHelper;
        }
 /// <summary>
 /// Creates an <see cref="WebObjectDefinitionParserHelper"/> instance for the given
 /// <paramref name="readerContext"/> and <paramref name="root"/> element.
 /// </summary>
 protected override ObjectDefinitionParserHelper CreateHelper(XmlReaderContext readerContext, System.Xml.XmlElement root)
 {
     return(new WebObjectDefinitionParserHelper(webObjectNameGenerator, readerContext, root));
 }
 /// <summary>
 /// Creates an <see cref="ObjectDefinitionParserHelper"/> instance for the given <paramref name="readerContext"/> and <paramref name="root"/> element.
 /// </summary>
 /// <param name="readerContext">the <see cref="XmlReaderContext"/> to create the <see cref="ObjectDefinitionParserHelper"/> </param>
 /// <param name="root">the root <see cref="XmlElement"/> to start reading from</param>
 /// <returns>a new <see cref="ObjectDefinitionParserHelper"/> instance</returns>
 protected virtual ObjectDefinitionParserHelper CreateHelper(XmlReaderContext readerContext, XmlElement root)
 {
     ObjectDefinitionParserHelper helper = new ObjectDefinitionParserHelper(readerContext, root);
     return helper;
 }
Exemplo n.º 19
0
//        private Stack containingComponents = new Stack();

        /// <summary>
        /// Initializes a new instance of the <see cref="ParserContext"/> class.
        /// </summary>
        /// <param name="parserHelper">The parser helper.</param>
        public ParserContext(ObjectDefinitionParserHelper parserHelper)
        {
            this.readerContext = parserHelper.ReaderContext;
            this.parserHelper  = parserHelper;
        }
 public void RegisterObjectDefinitions(XmlDocument doc, XmlReaderContext readerContext)
 {
     throw new TestException("RegisterObjectDefinitions");
 }
Exemplo n.º 21
0
 public ParserContext(XmlReaderContext readerContext, ObjectDefinitionParserHelper parserHelper, IObjectDefinition containingObjectDefinition)
 {
     this.readerContext = readerContext;
     this.parserHelper  = parserHelper;
     this.containingObjectDefinition = containingObjectDefinition;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="WebObjectDefinitionParserHelper"/> class.
 /// </summary>
 /// <param name="webObjectNameGenerator">used for generating object definition names from web object types (page, control)</param>
 /// <param name="readerContext">The reader context.</param>
 /// <param name="root">The root element of the xml document to parse</param>
 public WebObjectDefinitionParserHelper(IWebObjectNameGenerator webObjectNameGenerator, XmlReaderContext readerContext, XmlElement root)
     : base(readerContext, root)
 {
     AssertUtils.ArgumentNotNull(webObjectNameGenerator, "webObjectNameGenerator");
     this.webObjectNameGenerator = webObjectNameGenerator;
 }
        public void WhenConfigFileIsValid()
        {
            XmlDocument doc = GetValidatedXmlResource("_WhenConfigFileIsValid.xml");

            MockObjectDefinitionRegistry registry = new MockObjectDefinitionRegistry();

            XmlReaderContext readerContext = new XmlReaderContext(null, new XmlObjectDefinitionReader(registry));
            ObjectDefinitionParserHelper helper = new ObjectDefinitionParserHelper(readerContext);
            helper.InitDefaults(doc.DocumentElement);
            ParserContext parserContext = new ParserContext(helper);

            ValidationNamespaceParser parser = new ValidationNamespaceParser();
            foreach (XmlElement element in doc.DocumentElement.ChildNodes)
            {
                if (element.NamespaceURI == "http://www.springframework.net/validation")
                {
                    parser.ParseElement(element, parserContext);
                }
            }
            IList<IObjectDefinition> defs = registry.GetObjectDefinitions();
            Assert.AreEqual(9, defs.Count);

            IObjectDefinition def = registry.GetObjectDefinition("destinationAirportValidator");
            Assert.IsTrue(def.IsSingleton);
            Assert.IsTrue(def.IsLazyInit);
            Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));
            
            PropertyValue fastValidateProperty = def.PropertyValues.GetPropertyValue("FastValidate");
            Assert.IsNotNull(fastValidateProperty);
            Assert.AreEqual("true", fastValidateProperty.Value);

            PropertyValue validatorsProperty = def.PropertyValues.GetPropertyValue("Validators");
            Assert.IsNotNull(validatorsProperty);
            object validatorsObject = validatorsProperty.Value;
            Assert.AreEqual(typeof(ManagedList), validatorsObject.GetType());
            ManagedList validators = (ManagedList)validatorsObject;
            Assert.AreEqual(4, validators.Count);

            def = (IObjectDefinition)validators[3];
            Assert.IsTrue(def.IsSingleton);
            Assert.IsTrue(def.IsLazyInit);
            Assert.AreEqual(typeof(RegularExpressionValidator), def.ObjectType);
            Assert.AreEqual("[A-Z]*", def.PropertyValues.GetPropertyValue("Expression").Value);


            def = registry.GetObjectDefinition("simpleAirportValidator");
            Assert.IsTrue(def.IsSingleton);
            Assert.IsTrue(def.IsLazyInit);
            Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));
            PropertyValue actionsProperty = def.PropertyValues.GetPropertyValue("Actions");
            Assert.IsNotNull(actionsProperty);
            object actionsObject = actionsProperty.Value;
            Assert.AreEqual(typeof(ManagedList), actionsObject.GetType());
            ManagedList actions = (ManagedList)actionsObject;
            Assert.AreEqual(1, actions.Count);

            IObjectDefinition exceptionActionDefinition = (IObjectDefinition)actions[0];
            Assert.AreEqual(typeof(ExceptionAction), exceptionActionDefinition.ObjectType);
            Assert.AreEqual("'new System.InvalidOperationException(\"invalid\")' []", exceptionActionDefinition.ConstructorArgumentValues.GenericArgumentValues[0].ToString());
            //
            
            def = registry.GetObjectDefinition("airportCodeValidator");
            Assert.IsTrue(def.IsSingleton);
            Assert.IsTrue(def.IsLazyInit);
            Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));
            actionsProperty = def.PropertyValues.GetPropertyValue("Actions");
            Assert.IsNotNull(actionsProperty);
            actionsObject = actionsProperty.Value;
            Assert.AreEqual(typeof(ManagedList), actionsObject.GetType());
            actions = (ManagedList)actionsObject;
            Assert.AreEqual(4, actions.Count);

            IObjectDefinition messageDefinition = (IObjectDefinition)actions[1];
            Assert.AreEqual(typeof(ErrorMessageAction), messageDefinition.ObjectType);

            IObjectDefinition actionDefinition = (IObjectDefinition)actions[2];
            Assert.AreEqual(typeof(ExpressionAction), actionDefinition.ObjectType);
            Assert.AreEqual("#now = DateTime.Now", actionDefinition.PropertyValues.GetPropertyValue("Valid").Value);

            def = registry.GetObjectDefinition("regex");
            Assert.IsTrue(def.IsSingleton);
            Assert.IsTrue(def.IsLazyInit);
            Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));
            PropertyValue expressionProperty = def.PropertyValues.GetPropertyValue("Expression");
            Assert.IsNotNull(expressionProperty);
            Assert.AreEqual("RegExp", expressionProperty.Value);
        }
 /// <summary>
 /// Creates an <see cref="WebObjectDefinitionParserHelper"/> instance for the given 
 /// <paramref name="readerContext"/> and <paramref name="root"/> element.
 /// </summary>
 protected override ObjectDefinitionParserHelper CreateHelper(XmlReaderContext readerContext, System.Xml.XmlElement root)
 {
     return new WebObjectDefinitionParserHelper(webObjectNameGenerator, readerContext, root);
 }