MoveToNextAttribute() public method

public MoveToNextAttribute ( ) : bool
return bool
		// it used to be regarded as MS bug but it was not really.
		public void TestPreserveEntityNotOnDotNet ()
		{
			string intSubset = "<!ELEMENT root EMPTY><!ATTLIST root foo CDATA 'foo-def' bar CDATA 'bar-def'><!ENTITY ent 'entity string'>";
			string dtd = "<!DOCTYPE root [" + intSubset + "]>";
			string xml = dtd + "<root foo='&ent;' bar='internal &ent; value' />";
			dvr = PrepareXmlReader (xml);
			dvr.EntityHandling = EntityHandling.ExpandCharEntities;
			dvr.Read ();	// DTD
			dvr.Read ();
			Assert.AreEqual (XmlNodeType.Element, dvr.NodeType);
			Assert.AreEqual ("root", dvr.Name);
			Assert.IsTrue (dvr.MoveToFirstAttribute ());
			Assert.AreEqual ("foo", dvr.Name);
			Assert.AreEqual ("entity string", dvr.Value);
			//  ReadAttributeValue()
			Assert.IsTrue (dvr.ReadAttributeValue ());
			Assert.AreEqual (XmlNodeType.EntityReference, dvr.NodeType);
			Assert.AreEqual ("ent", dvr.Name);
			Assert.AreEqual (String.Empty, dvr.Value);
			Assert.IsTrue (!dvr.ReadAttributeValue ());

			// bar
			Assert.IsTrue (dvr.MoveToNextAttribute ());
			Assert.AreEqual ("bar", dvr.Name);
			Assert.AreEqual ("internal entity string value", dvr.Value);
			//  ReadAttributeValue()
			Assert.IsTrue (dvr.ReadAttributeValue ());
			Assert.AreEqual (XmlNodeType.Text, dvr.NodeType);
			Assert.AreEqual (String.Empty, dvr.Name);
			Assert.AreEqual ("internal ", dvr.Value);
			Assert.IsTrue (dvr.ReadAttributeValue ());
			Assert.AreEqual (XmlNodeType.EntityReference, dvr.NodeType);
			Assert.AreEqual ("ent", dvr.Name);
			Assert.AreEqual (String.Empty, dvr.Value);
			Assert.IsTrue (dvr.ReadAttributeValue ());
			Assert.AreEqual (XmlNodeType.Text, dvr.NodeType);
			Assert.AreEqual (String.Empty, dvr.Name);
			Assert.AreEqual (" value", dvr.Value);

			// ValidationType = None

			dvr = PrepareXmlReader (xml);
			dvr.EntityHandling = EntityHandling.ExpandCharEntities;
			dvr.ValidationType = ValidationType.None;
			dvr.Read ();	// DTD
			dvr.Read ();
			Assert.AreEqual (XmlNodeType.Element, dvr.NodeType);
			Assert.AreEqual ("root", dvr.Name);

			// foo
			Assert.IsTrue (dvr.MoveToFirstAttribute ());
			Assert.AreEqual ("foo", dvr.Name);
			Assert.AreEqual ("entity string", dvr.Value);
			//  ReadAttributeValue()
			Assert.IsTrue (dvr.ReadAttributeValue ());
			Assert.AreEqual (XmlNodeType.EntityReference, dvr.NodeType);
			Assert.AreEqual ("ent", dvr.Name);
			Assert.AreEqual (String.Empty, dvr.Value);
			Assert.IsTrue (!dvr.ReadAttributeValue ());

			// bar
			Assert.IsTrue (dvr.MoveToNextAttribute ());
			Assert.AreEqual ("bar", dvr.Name);
			Assert.AreEqual ("internal entity string value", dvr.Value);
			//  ReadAttributeValue()
			Assert.IsTrue (dvr.ReadAttributeValue ());
			Assert.AreEqual (XmlNodeType.Text, dvr.NodeType);
			Assert.AreEqual (String.Empty, dvr.Name);
			Assert.AreEqual ("internal ", dvr.Value);
			Assert.IsTrue (dvr.ReadAttributeValue ());
			Assert.AreEqual (XmlNodeType.EntityReference, dvr.NodeType);
			Assert.AreEqual ("ent", dvr.Name);
			Assert.AreEqual (String.Empty, dvr.Value);
			Assert.IsTrue (dvr.ReadAttributeValue ());
			Assert.AreEqual (XmlNodeType.Text, dvr.NodeType);
			Assert.AreEqual (String.Empty, dvr.Name);
			Assert.AreEqual (" value", dvr.Value);
		}
		public void TestNormalization ()
		{
			string intSubset = "<!ELEMENT root EMPTY>"
				+ "<!ATTLIST root foo ID #REQUIRED"
				+ " bar NMTOKEN #IMPLIED "
				+ " baz NMTOKENS #IMPLIED "
				+ " quux CDATA #IMPLIED >";
			string dtd = "<!DOCTYPE root [" + intSubset + "]>";
			string xml = dtd + "<root foo=' id1 ' bar='  nameToken  ' baz=' list  of\r\nname token' quux=' quuux\tquuux\t ' />";
			dvr = PrepareXmlReader (xml);
			((XmlTextReader) dvr.Reader).Normalization = true;
			dvr.EntityHandling = EntityHandling.ExpandEntities;
			dvr.Read ();	// DTD
			dvr.Read ();
			Assert.AreEqual (XmlNodeType.Element, dvr.NodeType);
			Assert.AreEqual ("root", dvr.Name);
			Assert.IsTrue (dvr.MoveToFirstAttribute ());
			Assert.AreEqual ("foo", dvr.Name);
			Assert.AreEqual ("id1", dvr.Value);
			Assert.IsTrue (dvr.MoveToNextAttribute ());
			Assert.AreEqual ("bar", dvr.Name);
			Assert.AreEqual ("nameToken", dvr.Value);
			Assert.IsTrue (dvr.MoveToNextAttribute ());
			Assert.AreEqual ("baz", dvr.Name);
			Assert.AreEqual ("list of name token", dvr.Value);
			Assert.IsTrue (dvr.MoveToNextAttribute ());
			Assert.AreEqual ("quux", dvr.Name);
			Assert.AreEqual (" quuux quuux  ", dvr.Value);
		}
		public void TestExpandEntity ()
		{
			string intSubset = "<!ELEMENT root (#PCDATA)><!ATTLIST root foo CDATA 'foo-def' bar CDATA 'bar-def'><!ENTITY ent 'entity string'>";
			string dtd = "<!DOCTYPE root [" + intSubset + "]>";
			string xml = dtd + "<root foo='&ent;' bar='internal &ent; value'>&ent;</root>";
			dvr = PrepareXmlReader (xml);
			dvr.EntityHandling = EntityHandling.ExpandEntities;
			dvr.Read ();	// DTD
			dvr.Read ();
			Assert.AreEqual (XmlNodeType.Element, dvr.NodeType);
			Assert.AreEqual ("root", dvr.Name);
			Assert.IsTrue (dvr.MoveToFirstAttribute ());
			Assert.AreEqual ("foo", dvr.Name);
			Assert.AreEqual ("entity string", dvr.Value);
			Assert.IsTrue (dvr.MoveToNextAttribute ());
			Assert.AreEqual ("bar", dvr.Name);
			Assert.AreEqual ("internal entity string value", dvr.Value);
			Assert.AreEqual ("entity string", dvr.ReadString ());

			// ValidationType = None

			dvr = PrepareXmlReader (xml);
			dvr.EntityHandling = EntityHandling.ExpandEntities;
			dvr.ValidationType = ValidationType.None;
			dvr.Read ();	// DTD
			dvr.Read ();
			Assert.AreEqual (XmlNodeType.Element, dvr.NodeType);
			Assert.AreEqual ("root", dvr.Name);

			Assert.IsTrue (dvr.MoveToFirstAttribute ());
			Assert.AreEqual ("foo", dvr.Name);
			Assert.AreEqual ("entity string", dvr.Value);

			Assert.IsTrue (dvr.MoveToNextAttribute ());
			Assert.AreEqual ("bar", dvr.Name);
			Assert.AreEqual ("internal entity string value", dvr.Value);
			Assert.AreEqual ("entity string", dvr.ReadString ());
		}
Beispiel #4
0
		// 根据xml编辑框中的内容创建属性
		void BuildAttrs()
		{
			//this.SetFreshValue();

			Debug.Assert(m_attrsExpand == ExpandStyle.Collapse, "不是闭合状态不要用本函数");

			NameTable nt = new NameTable();

			// 创建名字空间
			XmlNamespaceManager nsmgr = GatherOuterNamespaces(
				this,
				nt);

			XmlParserContext context = new XmlParserContext(nt,
				nsmgr,
				null,
				XmlSpace.None);


			string strAttrsXml = "";
			// 2.得到attributes关闭时的text
			XmlText oText = this.GetAttributesText();
			if (oText != null) 
			{
				strAttrsXml = oText.Text.Trim();

				if (strAttrsXml != "")
					strAttrsXml = " " + strAttrsXml;
			}
			else 
			{
				Debug.Assert(false, "必须有text对象");
			}

			string strFragmentXml = "<" + this.Name + strAttrsXml + "/>";

            /*
            TextReader tr = new StringReader(strFragmentXml);
            XmlReaderSettings settings = new XmlReaderSettings();
            settings..XmlResolver = resolver;

            XmlReader reader = XmlReader.Create(tr, settings,
                context);
             * */

			// 过一段时间做XmlSchema校验
			XmlValidatingReader reader =
				new XmlValidatingReader(strFragmentXml,
				XmlNodeType.Element,
				context);
			
			// 不根据schema进行校验
			reader.ValidationType = ValidationType.None; 

			this.attrs = new ItemList();

			while(true)
			{
				if (reader.Read() == false)
					break;

				if (reader.MoveToFirstAttribute() == false)
					goto CONTINUE;

				while(true)
				{
					AttrItem attr = this.m_document.CreateAttrItem(reader.Name);
					attr.Initial(reader);
					this.AppendAttrInternal(attr,false,true);
					if (reader.MoveToNextAttribute() == false)
						break;
				}

			CONTINUE:
				break;
			}
		}
		public void TestReadTypedAttributeValue ()
		{
			string xml = "<root attr='12'></root>";
			XmlSchema schema = new XmlSchema ();
			XmlSchemaElement elem = new XmlSchemaElement ();
			elem.Name = "root";
			XmlSchemaComplexType ct = new XmlSchemaComplexType ();
			XmlSchemaAttribute attr = new XmlSchemaAttribute ();
			attr.Name = "attr";
			attr.SchemaTypeName = new XmlQualifiedName ("int", XmlSchema.Namespace);
			ct.Attributes.Add (attr);
			elem.SchemaType = ct;
			schema.Items.Add (elem);

			xvr = PrepareXmlReader (xml);
			xvr.Schemas.Add (schema);
			xvr.Read ();
			Assert.AreEqual ("root", xvr.Name);
			Assert.IsTrue (xvr.MoveToNextAttribute ());	// attr
			Assert.AreEqual ("attr", xvr.Name);
			XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
			Assert.IsNotNull (dt);
			Assert.AreEqual (typeof (int), dt.ValueType);
			Assert.AreEqual (XmlTokenizedType.None, dt.TokenizedType);
			object o = xvr.ReadTypedValue ();
			Assert.AreEqual (XmlNodeType.Attribute, xvr.NodeType);
			Assert.AreEqual (typeof (int), o.GetType ());
			int n = (int) o;
			Assert.AreEqual (12, n);
			Assert.IsTrue (xvr.ReadAttributeValue ());	// can read = seems not proceed.
		}
        /// <summary>
        /// Creates a new PolicyDocument using the XmlReader instance provided with the possibility of writing.
        /// </summary>
        /// <param name="reader">The XmlReader positioned at the begining of the document.</param>
        /// <param name="schemaVersion">The version of the schema that will be used to validate.</param>
        public PolicyDocumentReadWrite(XmlReader reader, XacmlVersion schemaVersion)
        {
            // Validate the parameters
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            _schemaVersion = schemaVersion;

            // Prepare Xsd validation
#if NET10
			XmlValidatingReader vreader = new XmlValidatingReader( reader );
#endif
#if NET20
            ValidationEventHandler validationHandler = vreader_ValidationEventHandler;
            XmlReaderSettings settings = new XmlReaderSettings {ValidationType = ValidationType.Schema};
            settings.ValidationEventHandler += validationHandler;
            XmlReader vreader = null;
#endif
            try
            {
                switch (schemaVersion)
                {
                    case XacmlVersion.Version10:
                    case XacmlVersion.Version11:
                        {
                            Stream schemaStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(Xacml10PolicySchemaResourceName);
#if NET10
                            XmlTextReader schemaReader = new XmlTextReader(schemaStream);
						    vreader.Schemas.Add( PolicySchema1.Namespaces.Policy, schemaReader );
#endif
#if NET20
                            if (_compiledSchemas11 == null)
                            {
                                _compiledSchemas11 = new XmlSchemaSet();
                                _compiledSchemas11.Add(XmlSchema.Read(schemaStream, validationHandler));
                                _compiledSchemas11.Compile();
                            }
                            settings.Schemas.Add(_compiledSchemas11);
#endif
                            break;
                        }
                    case XacmlVersion.Version20:
                        {
                            Stream schemaStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(Xacml20PolicySchemaResourceName);
#if NET10
                            XmlTextReader schemaReader = new XmlTextReader(schemaStream);
						    vreader.Schemas.Add( PolicySchema2.Namespaces.Policy, schemaReader );
#endif
#if NET20
                            if (_compiledSchemas20 == null)
                            {
                                _compiledSchemas20 = new XmlSchemaSet();
                                _compiledSchemas20.Add(XmlSchema.Read(schemaStream, validationHandler));
                                _compiledSchemas20.Compile();
                            }
                            settings.Schemas.Add(_compiledSchemas20);
#endif
                            break;
                        }
                    default:
                        throw new ArgumentException(Resource.ResourceManager[Resource.MessageKey.exc_invalid_version_parameter_value], "version");
                }
#if NET10
				vreader.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler( vreader_ValidationEventHandler );
#endif
#if NET20
                vreader = XmlReader.Create(reader, settings);
#endif
                // Read and validate the document.
                while (vreader.Read())
                {
                    //Read all the namespaces and keep them in the _namespaces hashtable.
                    if (vreader.HasAttributes)
                    {
                        while (vreader.MoveToNextAttribute())
                        {
                            if (vreader.LocalName == PolicySchema1.Namespaces.XMLNS)
                            {
                                _namespaces.Add(vreader.Prefix, vreader.Value);
                            }
                            else if (vreader.Prefix == PolicySchema1.Namespaces.XMLNS)
                            {
                                _namespaces.Add(vreader.LocalName, vreader.Value);
                            }
                        }
                        vreader.MoveToElement();
                    }

                    // Check the first element of the document and proceeds to read the contents 
                    // depending on the first node name.
                    switch (vreader.LocalName)
                    {
                        case PolicySchema1.PolicySetElement.PolicySet:
                            {
                                _policySet = new PolicySetElementReadWrite(vreader, schemaVersion);
                                return;
                            }
                        case PolicySchema1.PolicyElement.Policy:
                            {
                                _policy = new PolicyElementReadWrite(vreader, schemaVersion);
                                return;
                            }
                    }
                }
            }
            finally
            {
                if (vreader != null)
                {
                    vreader.Close();
                }
            }
        }
		public void TestPreserveEntity ()
		{
			string intSubset = "<!ELEMENT root EMPTY><!ATTLIST root foo CDATA 'foo-def' bar CDATA 'bar-def'><!ENTITY ent 'entity string'>";
			string dtd = "<!DOCTYPE root [" + intSubset + "]>";
			string xml = dtd + "<root foo='&ent;' bar='internal &ent; value' />";
			dvr = PrepareXmlReader (xml);
			dvr.EntityHandling = EntityHandling.ExpandCharEntities;
			dvr.Read ();	// DTD
			dvr.Read ();
			AssertEquals (XmlNodeType.Element, dvr.NodeType);
			AssertEquals ("root", dvr.Name);
			Assert (dvr.MoveToFirstAttribute ());
			AssertEquals ("foo", dvr.Name);
			// MS BUG: it returns "entity string", however, entity should not be exanded.
			//  ReadAttributeValue()
			Assert (dvr.ReadAttributeValue ());
			AssertEquals (XmlNodeType.EntityReference, dvr.NodeType);
			AssertEquals ("ent", dvr.Name);
			AssertEquals ("", dvr.Value);
			Assert (!dvr.ReadAttributeValue ());

			// bar
			Assert (dvr.MoveToNextAttribute ());
			AssertEquals ("bar", dvr.Name);
			//  ReadAttributeValue()
			Assert (dvr.ReadAttributeValue ());
			AssertEquals (XmlNodeType.Text, dvr.NodeType);
			AssertEquals ("", dvr.Name);
			AssertEquals ("internal ", dvr.Value);
			Assert (dvr.ReadAttributeValue ());
			AssertEquals (XmlNodeType.EntityReference, dvr.NodeType);
			AssertEquals ("ent", dvr.Name);
			AssertEquals ("", dvr.Value);
			Assert (dvr.ReadAttributeValue ());
			AssertEquals (XmlNodeType.Text, dvr.NodeType);
			AssertEquals ("", dvr.Name);
			AssertEquals (" value", dvr.Value);

			// ValidationType = None

			dvr = PrepareXmlReader (xml);
			dvr.EntityHandling = EntityHandling.ExpandCharEntities;
			dvr.ValidationType = ValidationType.None;
			dvr.Read ();	// DTD
			dvr.Read ();
			AssertEquals (XmlNodeType.Element, dvr.NodeType);
			AssertEquals ("root", dvr.Name);

			// foo
			Assert (dvr.MoveToFirstAttribute ());
			AssertEquals ("foo", dvr.Name);
			//  ReadAttributeValue()
			Assert (dvr.ReadAttributeValue ());
			AssertEquals (XmlNodeType.EntityReference, dvr.NodeType);
			AssertEquals ("ent", dvr.Name);
			AssertEquals ("", dvr.Value);
			Assert (!dvr.ReadAttributeValue ());

			// bar
			Assert (dvr.MoveToNextAttribute ());
			AssertEquals ("bar", dvr.Name);
			//  ReadAttributeValue()
			Assert (dvr.ReadAttributeValue ());
			AssertEquals (XmlNodeType.Text, dvr.NodeType);
			AssertEquals ("", dvr.Name);
			AssertEquals ("internal ", dvr.Value);
			Assert (dvr.ReadAttributeValue ());
			AssertEquals (XmlNodeType.EntityReference, dvr.NodeType);
			AssertEquals ("ent", dvr.Name);
			AssertEquals ("", dvr.Value);
			Assert (dvr.ReadAttributeValue ());
			AssertEquals (XmlNodeType.Text, dvr.NodeType);
			AssertEquals ("", dvr.Name);
			AssertEquals (" value", dvr.Value);
		}