Exemplo n.º 1
0
        public void UsingCustomXmlResolverWith_SchemaLocation_NoNamespaceSchemaLocation(bool schemaLocation)
        {
            CXmlTestResolver res = new CXmlTestResolver();
            CResolverHolder holder = new CResolverHolder();

            res.CalledResolveUri += new XmlTestResolverEventHandler(holder.CallBackResolveUri);
            res.CalledGetEntity += new XmlTestResolverEventHandler(holder.CallBackGetEntity);

            XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
            XmlSchemaValidator val = new XmlSchemaValidator(new NameTable(),
                                                            new XmlSchemaSet(),
                                                            manager,
                                                            AllFlags);
            val.XmlResolver = res;
            val.Initialize();

            if (schemaLocation)
            {
                manager.AddNamespace("t", "uri:tempuri");
                val.ValidateElement("foo", "", null, "t:type1", null, "uri:tempuri " + TestData + XSDFILE_TARGET_NAMESPACE, null);
            }
            else
            {
                val.ValidateElement("foo", "", null, "type1", null, null, XSDFILE_NO_TARGET_NAMESPACE);
            }

            Assert.True(holder.IsCalledResolveUri);
            Assert.True(holder.IsCalledGetEntity);

            return;
        }
Exemplo n.º 2
0
        public void SetXmlNameTableTo_Empty_Full(String nameTableStatus)
        {
            XmlSchemaValidator val;
            ObservedNameTable nt = new ObservedNameTable();
            XmlSchemaInfo info = new XmlSchemaInfo();
            XmlSchemaSet sch = CreateSchemaSetFromXml("<root />");

            if (nameTableStatus == "full")
            {
                nt.Add("root");
                nt.Add("foo");
                nt.IsAddCalled = false;
                nt.IsGetCalled = false;
            }

            val = new XmlSchemaValidator(nt, sch, new XmlNamespaceManager(new NameTable()), AllFlags);
            Assert.NotEqual(val, null);

            val.Initialize();
            val.ValidateElement("root", "", info);

            Assert.True(nt.IsAddCalled);
            Assert.Equal(nt.IsGetCalled, false);

            return;
        }
Exemplo n.º 3
0
        public void DefaultValueForXmlResolver_XmlUrlResolver()
        {
            XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());

            manager.AddNamespace("t", "uri:tempuri");

            XmlSchemaValidator val = new XmlSchemaValidator(new NameTable(),
                                                            CreateSchemaSetFromXml("<root />"),
                                                            manager,
                                                            AllFlags);
            XmlSchemaInfo info = new XmlSchemaInfo();

            val.XmlResolver = new XmlUrlResolver(); //Adding this as the default resolver is null and not XmlUrlResolver anymore

            val.Initialize();
            val.ValidateElement("foo", "", null, "t:type1", null, "uri:tempuri " + TestData + XSDFILE_TARGET_NAMESPACE, null);
            val.ValidateEndOfAttributes(null);
            val.ValidateElement("bar", "", null);
            val.ValidateEndOfAttributes(null);
            val.ValidateEndElement(null);
            val.ValidateEndElement(info);

            Assert.Equal(info.ContentType, XmlSchemaContentType.ElementOnly);
            Assert.True(info.SchemaType != null);

            return;
        }
 private XmlSchemaValidator CreateTypeFinderValidator(XmlSchemaObject partialValidationType)
 {
     XmlSchemaValidator validator = new XmlSchemaValidator(this.document.NameTable, this.document.Schemas, this.nsManager, XmlSchemaValidationFlags.None);
     validator.ValidationEventHandler += new ValidationEventHandler(this.TypeFinderCallBack);
     if (partialValidationType != null)
     {
         validator.Initialize(partialValidationType);
         return validator;
     }
     validator.Initialize();
     return validator;
 }
Exemplo n.º 5
0
        public void SetSchemaSetToNull()
        {
            XmlSchemaValidator val;

            try
            {
                val = new XmlSchemaValidator(new NameTable(), null, new XmlNamespaceManager(new NameTable()), AllFlags);
            }
            catch (ArgumentNullException)
            {
                return;
            }

            _output.WriteLine("ArgumentNullException was not thrown!");
            Assert.True(false);
        }
 private void CreateValidator(XmlSchemaObject partialValidationType, XmlSchemaValidationFlags validationFlags)
 {
     this.validator = new XmlSchemaValidator(this.nameTable, this.schemas, this.NamespaceResolver, validationFlags);
     this.validator.SourceUri = XmlConvert.ToUri(this.document.BaseURI);
     this.validator.XmlResolver = null;
     this.validator.ValidationEventHandler += this.internalEventHandler;
     this.validator.ValidationEventSender = this;
     if (partialValidationType != null)
     {
         this.validator.Initialize(partialValidationType);
     }
     else
     {
         this.validator.Initialize();
     }
 }
Exemplo n.º 7
0
		public void XsdAnyToSkipAttributeValidation ()
		{
			// bug #358408
			XmlSchemaSet schemas = new XmlSchemaSet ();
			schemas.Add (null, "Test/XmlFiles/xsd/358408.xsd");
			XmlSchemaValidator v = new XmlSchemaValidator (
				new NameTable (),
				schemas,
				new XmlNamespaceManager (new NameTable ()),
				XmlSchemaValidationFlags.ProcessIdentityConstraints);
			v.Initialize ();
			v.ValidateWhitespace (" ");
			XmlSchemaInfo info = new XmlSchemaInfo ();
			ArrayList list = new ArrayList ();

			v.ValidateElement ("configuration", "", info, null, null, null, null);
			v.GetUnspecifiedDefaultAttributes (list);
			v.ValidateEndOfAttributes (info);

			v.ValidateWhitespace (" ");

			v.ValidateElement ("host", "", info, null, null, null, null);
			v.ValidateAttribute ("auto-start", "", "true", info);
			list.Clear ();
			v.GetUnspecifiedDefaultAttributes (list);
			v.ValidateEndOfAttributes (info);
			v.ValidateEndElement (null);//info);

			v.ValidateWhitespace (" ");

			v.ValidateElement ("service-managers", "", info, null, null, null, null);
			list.Clear ();
			v.GetUnspecifiedDefaultAttributes (list);
			v.ValidateEndOfAttributes (info);

			v.ValidateWhitespace (" ");

			v.ValidateElement ("service-manager", "", info, null, null, null, null);
			list.Clear ();
			v.GetUnspecifiedDefaultAttributes (list);
			v.ValidateEndOfAttributes (info);

			v.ValidateWhitespace (" ");

			v.ValidateElement ("foo", "", info, null, null, null, null);
			v.ValidateAttribute ("bar", "", "", info);
		}
Exemplo n.º 8
0
		public static void Validate (this XAttribute source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler, bool addSchemaInfo)
		{
			if (source == null)
				throw new ArgumentNullException ("source");
			if (schemas == null)
				throw new ArgumentNullException ("schemas");
			var nsmgr = new XmlNamespaceManager (new NameTable ());
			var v = new XmlSchemaValidator (nsmgr.NameTable, schemas, nsmgr, XmlSchemaValidationFlags.None);
			if (validationEventHandler != null)
				v.ValidationEventHandler += validationEventHandler;
			if (partialValidationType != null)
				v.Initialize (partialValidationType);
			else
				v.Initialize ();
			var xi = addSchemaInfo ? new XmlSchemaInfo () : null;
			v.ValidateAttribute (source.Name.LocalName, source.Name.NamespaceName, source.Value, xi);
		}
Exemplo n.º 9
0
        public void SetResolverToCustomValidateSomethingSetResolverToNullThenVerify()
        {
            CXmlTestResolver res = new CXmlTestResolver();
            CResolverHolder holder = new CResolverHolder();

            res.CalledResolveUri += new XmlTestResolverEventHandler(holder.CallBackResolveUri);
            res.CalledGetEntity += new XmlTestResolverEventHandler(holder.CallBackGetEntity);

            XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
            XmlSchemaInfo info = new XmlSchemaInfo();
            XmlSchemaValidator val = new XmlSchemaValidator(new NameTable(),
                                                            new XmlSchemaSet(),
                                                            manager,
                                                            AllFlags);
            val.XmlResolver = res;
            val.Initialize();

            val.ValidateElement("foo", "", null, "type1", null, null, TestData + XSDFILE_NO_TARGET_NAMESPACE);
            val.SkipToEndElement(null);

            Assert.True(holder.IsCalledResolveUri);
            Assert.True(holder.IsCalledGetEntity);

            manager.AddNamespace("t", "uri:tempuri");
            val.XmlResolver = null;

            try
            {
                val.ValidateElement("bar", "", null, "t:type1", null, "uri:tempuri " + TestData + XSDFILE_TARGET_NAMESPACE, null);
                Assert.True(false);
            }
            catch (XmlSchemaValidationException)
            {
                //XmlExceptionVerifier.IsExceptionOk(e, "Sch_XsiTypeNotFound", new string[] { "uri:tempuri:type1" });
                return;
            }

            Assert.True(false);
        }
Exemplo n.º 10
0
        public void SetResolverToCustomValidateSomethignChangeResolverThenVerify()
        {
            CXmlTestResolver res = new CXmlTestResolver();
            CResolverHolder holder = new CResolverHolder();

            res.CalledResolveUri += new XmlTestResolverEventHandler(holder.CallBackResolveUri);
            res.CalledGetEntity += new XmlTestResolverEventHandler(holder.CallBackGetEntity);

            XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
            XmlSchemaInfo info = new XmlSchemaInfo();
            XmlSchemaValidator val = new XmlSchemaValidator(new NameTable(),
                                                            new XmlSchemaSet(),
                                                            manager,
                                                            AllFlags);
            val.XmlResolver = res;
            val.Initialize();

            val.ValidateElement("foo", "", null, "type1", null, null, TestData + XSDFILE_NO_TARGET_NAMESPACE);
            val.SkipToEndElement(null);

            Assert.True(holder.IsCalledResolveUri);
            Assert.True(holder.IsCalledGetEntity);

            val.XmlResolver = new XmlUrlResolver();
            holder.IsCalledGetEntity = false;
            holder.IsCalledResolveUri = false;

            val.ValidateElement("foo", "", null, "type1", null, null, TestData + XSDFILE_NO_TARGET_NAMESPACE);

            Assert.True(!holder.IsCalledResolveUri);
            Assert.True(!holder.IsCalledGetEntity);

            return;
        }
Exemplo n.º 11
0
        public void CustomXmlNameSpaceResolverImplementation()
        {
            XmlSchemaValidator val;
            XmlSchemaInfo info = new XmlSchemaInfo();

            XmlSchemaSet sch = new XmlSchemaSet();

            ObservedNamespaceManager nsManager = new ObservedNamespaceManager(new NameTable());
            nsManager.AddNamespace("n1", "uri:tempuri");

            val = new XmlSchemaValidator(new NameTable(), sch, nsManager, AllFlags);

            val.AddSchema(XmlSchema.Read(XmlReader.Create(new StringReader("<?xml version=\"1.0\" ?>\n" +
                                                                     "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" +
                                                                     "			 xmlns:n1=\"uri:tempuri\"\n" +
                                                                     "			 targetNamespace=\"uri:tempuri1\">\n" +
                                                                     "    <xs:complexType name=\"foo\">\n" +
                                                                     "        <xs:sequence>\n" +
                                                                     "            <xs:element name=\"bar\" />\n" +
                                                                     "        </xs:sequence>\n" +
                                                                     "    </xs:complexType>\n" +
                                                                     "</xs:schema>")), null));

            val.Initialize();
            val.ValidateElement("root", "", info, "n1:foo", null, null, null);

            Assert.True(nsManager.IsLookupNamespaceCalled);

            return;
        }
Exemplo n.º 12
0
        public void XmlSchemaValidatorDoesNotEnforceIdentityConstraintsOnDefaultAttributesInSomeCases()
        {
            Initialize();
            string xml = @"<?xml version='1.0'?>
<root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='idF016.xsd'>
	<uid val='test'/>	<uid/></root>";

            string xsd = @"<?xml version='1.0'?>
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'>
	<xsd:element name='root'>
		<xsd:complexType>
			<xsd:sequence>
				<xsd:element ref='uid' maxOccurs='unbounded'/>
			</xsd:sequence>
		</xsd:complexType>
		<xsd:unique id='foo123' name='uuid'>
			<xsd:selector xpath='.//uid'/>
			<xsd:field xpath='@val'/>
		</xsd:unique>
	</xsd:element>
	<xsd:element name='uid' nillable='true'>
		<xsd:complexType>
			<xsd:attribute name='val' type='xsd:string' default='test'/>
		</xsd:complexType>
	</xsd:element>
</xsd:schema>";

            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable());
            XmlSchemaSet schemas = new XmlSchemaSet();
            schemas.Add(null, XmlReader.Create(new StringReader(xsd)));
            schemas.Compile();
            XmlSchemaValidationFlags validationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints |
            XmlSchemaValidationFlags.AllowXmlAttributes;
            XmlSchemaValidator validator = new XmlSchemaValidator(namespaceManager.NameTable, schemas, namespaceManager, validationFlags);
            validator.Initialize();
            using (XmlReader r = XmlReader.Create(new StringReader(xsd)))
            {
                while (r.Read())
                {
                    switch (r.NodeType)
                    {
                        case XmlNodeType.Element:
                            namespaceManager.PushScope();
                            if (r.MoveToFirstAttribute())
                            {
                                do
                                {
                                    if (r.NamespaceURI == "http://www.w3.org/2000/xmlns/")
                                    {
                                        namespaceManager.AddNamespace(r.LocalName, r.Value);
                                    }
                                } while (r.MoveToNextAttribute());
                                r.MoveToElement();
                            }
                            validator.ValidateElement(r.LocalName, r.NamespaceURI, null, null, null, null, null);
                            if (r.MoveToFirstAttribute())
                            {
                                do
                                {
                                    if (r.NamespaceURI != "http://www.w3.org/2000/xmlns/")
                                    {
                                        validator.ValidateAttribute(r.LocalName, r.NamespaceURI, r.Value, null);
                                    }
                                } while (r.MoveToNextAttribute());
                                r.MoveToElement();
                            }
                            validator.ValidateEndOfAttributes(null);
                            if (r.IsEmptyElement) goto case XmlNodeType.EndElement;
                            break;

                        case XmlNodeType.EndElement:
                            validator.ValidateEndElement(null);
                            namespaceManager.PopScope();
                            break;

                        case XmlNodeType.Text:
                            validator.ValidateText(r.Value);
                            break;

                        case XmlNodeType.SignificantWhitespace:
                        case XmlNodeType.Whitespace:
                            validator.ValidateWhitespace(r.Value);
                            break;

                        default:
                            break;
                    }
                }
                validator.EndValidation();
            }
            XmlReaderSettings rs = new XmlReaderSettings();
            rs.ValidationType = ValidationType.Schema;
            rs.Schemas.Add(null, XmlReader.Create(new StringReader(xsd)));

            using (XmlReader r = XmlReader.Create(new StringReader(xml), rs))
            {
                try
                {
                    while (r.Read()) ;
                }
                catch (XmlSchemaValidationException e) { _output.WriteLine(e.Message); return; }
            }
            Assert.True(false);
        }
 private void SetupValidator(XmlReaderSettings readerSettings, XmlReader reader, XmlSchemaObject partialValidationType) {
     validator = new XmlSchemaValidator(coreReaderNameTable, readerSettings.Schemas, thisNSResolver, readerSettings.ValidationFlags);
     validator.XmlResolver = this.xmlResolver;
     validator.SourceUri = XmlConvert.ToUri(reader.BaseURI); //Not using XmlResolver.ResolveUri as it checks for relative Uris,reader.BaseURI will be absolute file paths or string.Empty
     validator.ValidationEventSender = this;
     validator.ValidationEventHandler += readerSettings.GetEventHandler();
     validator.LineInfoProvider = this.lineInfo;
     if (validator.ProcessSchemaHints) {
         validator.SchemaSet.ReaderSettings.DtdProcessing = readerSettings.DtdProcessing;
     }
     validator.SetDtdSchemaInfo(reader.DtdInfo);
     if (partialValidationType != null) {
         validator.Initialize(partialValidationType);
     }
     else {
         validator.Initialize();
     }
 }
Exemplo n.º 14
0
        public void ValidateWithXmlReader(XmlSchemaSet schemas, string xml, string xsd)
        {
            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable());
            XmlSchemaValidationFlags validationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints |
            XmlSchemaValidationFlags.AllowXmlAttributes;
            XmlSchemaValidator validator = new XmlSchemaValidator(namespaceManager.NameTable, schemas, namespaceManager, validationFlags);
            validator.Initialize();
            using (XmlReader r = XmlReader.Create(xsd))
            {
                while (r.Read())
                {
                    switch (r.NodeType)
                    {
                        case XmlNodeType.Element:
                            namespaceManager.PushScope();
                            if (r.MoveToFirstAttribute())
                            {
                                do
                                {
                                    if (r.NamespaceURI == "http://www.w3.org/2000/xmlns/")
                                    {
                                        namespaceManager.AddNamespace(r.LocalName, r.Value);
                                    }
                                } while (r.MoveToNextAttribute());
                                r.MoveToElement();
                            }
                            validator.ValidateElement(r.LocalName, r.NamespaceURI, null, null, null, null, null);
                            if (r.MoveToFirstAttribute())
                            {
                                do
                                {
                                    if (r.NamespaceURI != "http://www.w3.org/2000/xmlns/")
                                    {
                                        validator.ValidateAttribute(r.LocalName, r.NamespaceURI, r.Value, null);
                                    }
                                } while (r.MoveToNextAttribute());
                                r.MoveToElement();
                            }
                            validator.ValidateEndOfAttributes(null);
                            if (r.IsEmptyElement) goto case XmlNodeType.EndElement;
                            break;

                        case XmlNodeType.EndElement:
                            validator.ValidateEndElement(null);
                            namespaceManager.PopScope();
                            break;

                        case XmlNodeType.Text:
                            validator.ValidateText(r.Value);
                            break;

                        case XmlNodeType.SignificantWhitespace:
                        case XmlNodeType.Whitespace:
                            validator.ValidateWhitespace(r.Value);
                            break;

                        default:
                            break;
                    }
                }
                validator.EndValidation();
            }
            XmlReaderSettings rs = new XmlReaderSettings();
            rs.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
            rs.ValidationType = ValidationType.Schema;
            rs.Schemas.XmlResolver = new XmlUrlResolver();
            rs.Schemas.Add(null, XmlReader.Create(xsd));

            using (XmlReader r = XmlReader.Create(xml, rs))
            {
                while (r.Read()) ;
            }
            Assert.Equal(warningCount, 0);
            Assert.Equal(errorCount, 0);
        }
Exemplo n.º 15
0
        public void SetSchemaSetTo_Empty_NotCompiled_Compiled(String schemaSetStatus)
        {
            XmlSchemaValidator val;
            XmlSchemaSet sch = new XmlSchemaSet();

            if (schemaSetStatus != "empty")
            {
                sch.Add("", Path.Combine(TestData, XSDFILE_NO_TARGET_NAMESPACE));
                if (schemaSetStatus == "compiled")
                    sch.Compile();
            }

            val = new XmlSchemaValidator(new NameTable(), sch, new XmlNamespaceManager(new NameTable()), AllFlags);
            Assert.NotEqual(val, null);

            val.Initialize();
            val.ValidateElement("elem1", "", null);
            val.SkipToEndElement(null);
            val.EndValidation();

            return;
        }
Exemplo n.º 16
0
        public void ValidateContext(XmlCache xcache)
        {
            this.cache = xcache;
            if (string.IsNullOrEmpty(cache.FileName)) {
                baseUri = null;
            } else {
                baseUri = new Uri(new Uri(xcache.FileName), new Uri(".", UriKind.Relative));
            }
            ValidationEventHandler handler = new ValidationEventHandler(OnValidationEvent);
            SchemaResolver resolver = xcache.SchemaResolver as SchemaResolver;
            resolver.Handler = handler;
            XmlDocument doc = xcache.Document;
            this.info = new XmlSchemaInfo();
            this.nsResolver = new MyXmlNamespaceResolver(doc.NameTable);
            XmlSchemaSet set = new XmlSchemaSet();
            // Make sure the SchemaCache is up to date with document.
            SchemaCache sc = xcache.SchemaCache;
            foreach (XmlSchema s in doc.Schemas.Schemas()) {
                sc.Add(s);
            }

            // hack to force application to validate with local copy of collada schema
            try
            {
                string appPath = Directory.GetCurrentDirectory();
                set.Add("http://www.w3.org/XML/1998/namespace", appPath + "/xml.xsd");
                set.Add("http://www.collada.org/2005/11/COLLADASchema", appPath + "/collada_schema_1_4.xsd");
            } catch
            {
                Console.WriteLine("Cannot find xml.xmd or collada_schema_1_4.xsd" );
            }

            if (LoadSchemas(doc, set, resolver)) {
                set.ValidationEventHandler += handler;
                set.Compile();
            }

            this.validator = new XmlSchemaValidator(doc.NameTable, set, nsResolver,
                XmlSchemaValidationFlags.AllowXmlAttributes |
                XmlSchemaValidationFlags.ProcessIdentityConstraints |
                XmlSchemaValidationFlags.ProcessInlineSchema);

            this.validator.ValidationEventHandler += handler;
            this.validator.XmlResolver = resolver;
            this.validator.Initialize();

            this.nsResolver.Context = doc;
            ValidateContent(doc);
            this.nsResolver.Context = doc;

            this.validator.EndValidation();
        }
 private XmlSchemaObject GetTypeFromAncestors(XmlElement elementToValidate, XmlSchemaObject ancestorType, int ancestorsCount)
 {
     this.validator = this.CreateTypeFinderValidator(ancestorType);
     this.schemaInfo = new XmlSchemaInfo();
     int num = ancestorsCount - 1;
     bool flag = this.AncestorTypeHasWildcard(ancestorType);
     for (int i = num; i >= 0; i--)
     {
         XmlNode parentNode = this.nodeSequenceToValidate[i];
         XmlElement elementNode = parentNode as XmlElement;
         this.ValidateSingleElement(elementNode, false, this.schemaInfo);
         if (!flag)
         {
             elementNode.XmlName = this.document.AddXmlName(elementNode.Prefix, elementNode.LocalName, elementNode.NamespaceURI, this.schemaInfo);
             flag = this.AncestorTypeHasWildcard(this.schemaInfo.SchemaElement);
         }
         this.validator.ValidateEndOfAttributes(null);
         if (i > 0)
         {
             this.ValidateChildrenTillNextAncestor(parentNode, this.nodeSequenceToValidate[i - 1]);
         }
         else
         {
             this.ValidateChildrenTillNextAncestor(parentNode, elementToValidate);
         }
     }
     this.ValidateSingleElement(elementToValidate, false, this.schemaInfo);
     XmlSchemaObject schemaElement = null;
     if (this.schemaInfo.SchemaElement != null)
     {
         schemaElement = this.schemaInfo.SchemaElement;
     }
     else
     {
         schemaElement = this.schemaInfo.SchemaType;
     }
     if (schemaElement == null)
     {
         if (this.validator.CurrentProcessContents == XmlSchemaContentProcessing.Skip)
         {
             if (this.isPartialTreeValid)
             {
                 return XmlSchemaComplexType.AnyTypeSkip;
             }
             return schemaElement;
         }
         if (this.validator.CurrentProcessContents == XmlSchemaContentProcessing.Lax)
         {
             return XmlSchemaComplexType.AnyType;
         }
     }
     return schemaElement;
 }
Exemplo n.º 18
0
        public void InternalSchemaSetShouldUseSeparateXmlResolver()
        {
            CXmlTestResolver res = new CXmlTestResolver();
            CResolverHolder holder = new CResolverHolder();

            res.CalledResolveUri += new XmlTestResolverEventHandler(holder.CallBackResolveUri);
            res.CalledGetEntity += new XmlTestResolverEventHandler(holder.CallBackGetEntity);

            XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
            XmlSchemaValidator val = new XmlSchemaValidator(new NameTable(),
                                                            new XmlSchemaSet(){ XmlResolver = new XmlUrlResolver()},
                                                            manager,
                                                            AllFlags);
            val.XmlResolver = res;

            val.Initialize();
            val.AddSchema(XmlSchema.Read(XmlReader.Create(TestData + XSDFILE_VALIDATE_ATTRIBUTE), null)); // this schema has xs:import
            val.ValidateElement("NoAttributesElement", "", null);

            Assert.True(!holder.IsCalledResolveUri);
            Assert.True(!holder.IsCalledGetEntity);

            return;
        }
Exemplo n.º 19
0
        public void One_Two_Three_XmlSchemaValidatorWithNullParams(int param)
        {
            XmlSchemaValidator val = null;
            try
            {
                switch (param)
                {
                    case 1:
                        val = new XmlSchemaValidator(null, new XmlSchemaSet(), null, XmlSchemaValidationFlags.None);
                        break;

                    case 2:
                        val = new XmlSchemaValidator(new NameTable(), null, null, XmlSchemaValidationFlags.None);
                        break;

                    case 3:
                        val = new XmlSchemaValidator(new NameTable(), new XmlSchemaSet(), null, XmlSchemaValidationFlags.None);
                        break;
                }
            }
            catch (ArgumentNullException e)
            {
                _output.WriteLine(e.Message);
                return;
            }
            Assert.True(false);
        }
Exemplo n.º 20
0
        public void ValidateWithSchemaInfo(XmlSchemaSet ss)
        {
            string ns = "http://tempuri.org/XMLSchema.xsd";
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(ss.NameTable);
            XmlSchemaValidator val = new XmlSchemaValidator(ss.NameTable, ss, nsmgr, XmlSchemaValidationFlags.ProcessIdentityConstraints);
            val.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
            val.Initialize();
            XmlSchemaInfo si = new XmlSchemaInfo();
            val.ValidateElement("root", ns, si);
            val.ValidateEndOfAttributes(si);
            val.ValidateElement("B", ns, si);
            val.ValidateEndOfAttributes(si);

            val.ValidateElement("part", ns, si);
            val.ValidateEndOfAttributes(si);
            val.ValidateText("1");
            val.ValidateEndElement(si);

            val.ValidateElement("part", ns, si);
            val.ValidateEndOfAttributes(si);
            val.ValidateEndElement(si, "1");

            val.ValidateElement("part", ns, si);
            val.ValidateEndOfAttributes(si);
            val.ValidateText("1");
            val.ValidateEndElement(si);

            val.ValidateEndElement(si);
            val.ValidateEndElement(si);

            Assert.Equal(warningCount, 0);
            Assert.Equal(errorCount, 0);
        }
 private void SetupValidator(XmlReaderSettings readerSettings, XmlReader reader, XmlSchemaObject partialValidationType)
 {
     this.validator = new XmlSchemaValidator(this.coreReaderNameTable, readerSettings.Schemas, this.thisNSResolver, readerSettings.ValidationFlags);
     this.validator.XmlResolver = this.xmlResolver;
     this.validator.SourceUri = XmlConvert.ToUri(reader.BaseURI);
     this.validator.ValidationEventSender = this;
     this.validator.ValidationEventHandler += readerSettings.GetEventHandler();
     this.validator.LineInfoProvider = this.lineInfo;
     if (this.validator.ProcessSchemaHints)
     {
         this.validator.SchemaSet.ReaderSettings.DtdProcessing = readerSettings.DtdProcessing;
     }
     this.validator.SetDtdSchemaInfo(reader.DtdInfo);
     if (partialValidationType != null)
     {
         this.validator.Initialize(partialValidationType);
     }
     else
     {
         this.validator.Initialize();
     }
 }
Exemplo n.º 22
0
		// Extra for XmlSchemaValidtingReader
		// (not in XsdValidatingReader)
		//XsElement element; // ... xsinfo.Element?

		#endregion

		public XmlSchemaValidatingReader (XmlReader reader,
			XmlReaderSettings settings)
		{
			IXmlNamespaceResolver nsResolver = reader as IXmlNamespaceResolver;
			if (nsResolver == null)
			//	throw new ArgumentException ("Argument XmlReader must implement IXmlNamespaceResolver.");
				nsResolver = new XmlNamespaceManager (reader.NameTable);

			XmlSchemaSet schemas = settings.Schemas;
			if (schemas == null)
				schemas = new XmlSchemaSet ();
			options = settings.ValidationFlags;

			this.reader = reader;
			v = new XmlSchemaValidator (
				reader.NameTable,
				schemas,
				nsResolver,
				options);
			if (reader.BaseURI != String.Empty && Uri.IsWellFormedUriString(reader.BaseURI, UriKind.Absolute))
				v.SourceUri = new Uri (reader.BaseURI);

			readerLineInfo = reader as IXmlLineInfo;
			getter = delegate () {
				if (v.CurrentAttributeType != null)
					return v.CurrentAttributeType.ParseValue (Value, NameTable, this);
				else
					return Value; 
				};
			xsinfo = new XmlSchemaInfo (); // transition cache
			v.LineInfoProvider = this;
			v.ValidationEventSender = reader;
			this.nsResolver = nsResolver;
#if !NON_MONO
			ValidationEventHandler += delegate (object o, ValidationEventArgs e) {
				settings.OnValidationError (o, e);
			};
			if (settings != null && settings.Schemas != null)
				v.XmlResolver = settings.Schemas.XmlResolver;
			else
				v.XmlResolver = new XmlUrlResolver ();
#else
			v.XmlResolver = new XmlUrlResolver ();
#endif
			v.Initialize ();
		}
Exemplo n.º 23
0
        private XmlSchemaObject GetTypeFromAncestors(XmlElement elementToValidate, XmlSchemaObject ancestorType, int ancestorsCount)
        {
            //schemaInfo is currentNode's schemaInfo
            _validator = CreateTypeFinderValidator(ancestorType);
            _schemaInfo = new XmlSchemaInfo();

            //start at the ancestor to start validating
            int startIndex = ancestorsCount - 1;

            bool ancestorHasWildCard = AncestorTypeHasWildcard(ancestorType);
            for (int i = startIndex; i >= 0; i--)
            {
                XmlNode node = _nodeSequenceToValidate[i];
                XmlElement currentElement = node as XmlElement;
                ValidateSingleElement(currentElement, false, _schemaInfo);
                if (!ancestorHasWildCard)
                { //store type if ancestor does not have wildcard in its content model
                    currentElement.XmlName = _document.AddXmlName(currentElement.Prefix, currentElement.LocalName, currentElement.NamespaceURI, _schemaInfo);
                    //update wildcard flag
                    ancestorHasWildCard = AncestorTypeHasWildcard(_schemaInfo.SchemaElement);
                }

                _validator.ValidateEndOfAttributes(null);
                if (i > 0)
                {
                    ValidateChildrenTillNextAncestor(node, _nodeSequenceToValidate[i - 1]);
                }
                else
                { //i == 0
                    ValidateChildrenTillNextAncestor(node, elementToValidate);
                }
            }

            Debug.Assert(_nodeSequenceToValidate[0] == elementToValidate.ParentNode);
            //validate element whose type is needed,
            ValidateSingleElement(elementToValidate, false, _schemaInfo);

            XmlSchemaObject schemaInfoFound = null;
            if (_schemaInfo.SchemaElement != null)
            {
                schemaInfoFound = _schemaInfo.SchemaElement;
            }
            else
            {
                schemaInfoFound = _schemaInfo.SchemaType;
            }
            if (schemaInfoFound == null)
            { //Detect if the node was validated lax or skip
                if (_validator.CurrentProcessContents == XmlSchemaContentProcessing.Skip)
                {
                    if (_isPartialTreeValid)
                    { //Then node assessed as skip; if there was error we turn processContents to skip as well. But this is not the same as validating as skip.
                        return XmlSchemaComplexType.AnyTypeSkip;
                    }
                }
                else if (_validator.CurrentProcessContents == XmlSchemaContentProcessing.Lax)
                {
                    return XmlSchemaComplexType.AnyType;
                }
            }
            return schemaInfoFound;
        }
Exemplo n.º 24
0
        public void StringPassedToValidateEndElementDoesNotSatisfyIdentityConstraints()
        {
            Initialize();
            string xsd =
                "<xs:schema targetNamespace='http://tempuri.org/XMLSchema.xsd' elementFormDefault='qualified' xmlns='http://tempuri.org/XMLSchema.xsd' xmlns:mstns='http://tempuri.org/XMLSchema.xsd' xmlns:xs='http://www.w3.org/2001/XMLSchema'>" +
                    "<xs:element name='root'>" +
                        "<xs:complexType> <xs:sequence> <xs:element name='B' type='mstns:B'/> </xs:sequence> </xs:complexType>" +
                        "<xs:unique name='pNumKey'><xs:selector xpath='mstns:B/mstns:part'/><xs:field xpath='.'/></xs:unique>" +
                    "</xs:element>" +
                    "<xs:complexType name='B'><xs:sequence><xs:element name='part' maxOccurs='unbounded' type='xs:string'></xs:element></xs:sequence></xs:complexType>" +
                "</xs:schema>";

            XmlSchemaSet ss = new XmlSchemaSet();
            ss.Add(XmlSchema.Read(new StringReader(xsd), ValidationCallback));
            ss.Compile();

            string ns = "http://tempuri.org/XMLSchema.xsd";
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(ss.NameTable);
            XmlSchemaValidator val = new XmlSchemaValidator(ss.NameTable, ss, nsmgr, XmlSchemaValidationFlags.ProcessIdentityConstraints);
            val.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
            val.Initialize();
            XmlSchemaInfo si = new XmlSchemaInfo();
            val.ValidateElement("root", ns, si);
            val.ValidateEndOfAttributes(si);
            val.ValidateElement("B", ns, si);
            val.ValidateEndOfAttributes(si);

            val.ValidateElement("part", ns, si);
            val.ValidateEndOfAttributes(si);
            val.ValidateText("1");
            val.ValidateEndElement(si);

            val.ValidateElement("part", ns, si);
            val.ValidateEndOfAttributes(si);
            val.ValidateEndElement(si, "1");

            val.ValidateElement("part", ns, si);
            val.ValidateEndOfAttributes(si);
            val.ValidateText("1");
            val.ValidateEndElement(si);

            val.ValidateEndElement(si);
            val.ValidateEndElement(si);

            Assert.Equal(warningCount, 0);
            Assert.Equal(errorCount, 2);
            return;
        }
Exemplo n.º 25
0
 private XmlSchemaValidator CreateTypeFinderValidator(XmlSchemaObject partialValidationType)
 {
     XmlSchemaValidator findTypeValidator = new XmlSchemaValidator(_document.NameTable, _document.Schemas, _nsManager, XmlSchemaValidationFlags.None);
     findTypeValidator.ValidationEventHandler += new ValidationEventHandler(TypeFinderCallBack);
     if (partialValidationType != null)
     {
         findTypeValidator.Initialize(partialValidationType);
     }
     else
     { //If we walked up to the root and no schemaInfo was there, start validating from root 
         findTypeValidator.Initialize();
     }
     return findTypeValidator;
 }
Exemplo n.º 26
0
        public void ValidateContext(XmlCache xcache)
        {
            this.cache = xcache;
            if (string.IsNullOrEmpty(cache.FileName)) {
                baseUri = null;
            } else {
                baseUri = new Uri(new Uri(xcache.FileName), new Uri(".", UriKind.Relative));
            }
            ValidationEventHandler handler = new ValidationEventHandler(OnValidationEvent);
            SchemaResolver resolver = xcache.SchemaResolver as SchemaResolver;
            resolver.Handler = handler;
            XmlDocument doc = xcache.Document;
            this.info = new XmlSchemaInfo();
            this.nsResolver = new MyXmlNamespaceResolver(doc.NameTable);
            XmlSchemaSet set = new XmlSchemaSet();
            // Make sure the SchemaCache is up to date with document.
            SchemaCache sc = xcache.SchemaCache;
            foreach (XmlSchema s in doc.Schemas.Schemas()) {
                sc.Add(s);
            }

            if (LoadSchemas(doc, set, resolver)) {
                set.ValidationEventHandler += handler;
                set.Compile();
            }

            this.validator = new XmlSchemaValidator(doc.NameTable, set, nsResolver,
                XmlSchemaValidationFlags.AllowXmlAttributes |
                XmlSchemaValidationFlags.ProcessIdentityConstraints |
                XmlSchemaValidationFlags.ProcessInlineSchema);

            this.validator.ValidationEventHandler += handler;
            this.validator.XmlResolver = resolver;
            this.validator.Initialize();

            this.nsResolver.Context = doc;
            ValidateContent(doc);
            this.nsResolver.Context = doc;

            this.validator.EndValidation();
        }
Exemplo n.º 27
0
        public void SetSchemaSetWithInvalidContent_TypeCollision()
        {
            XmlSchemaValidator val;
            XmlSchemaInfo info = new XmlSchemaInfo();

            XmlSchemaSet sch = new XmlSchemaSet();
            //sch.Add("", TestData + "Basic.xsd");
            sch.Add("", XmlReader.Create(new StringReader("<?xml version=\"1.0\" ?>\n" +
                                                          "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
                                                          "    <xs:element name=\"root\" type=\"xs:int\" />\n" +
                                                          "</xs:schema>")));
            sch.Add("", XmlReader.Create(new StringReader("<?xml version=\"1.0\" ?>\n" +
                                                          "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
                                                          "    <xs:element name=\"root\" type=\"xs:string\" />\n" +
                                                          "</xs:schema>")));

            try
            {
                val = new XmlSchemaValidator(new NameTable(), sch, new XmlNamespaceManager(new NameTable()), AllFlags);
            }
            catch (XmlSchemaValidationException)
            {
                return;
            }

            return;
        }