Ver Fonte

2010-01-27 Atsushi Enomoto <[email protected]>

	* XmlAtomicValue.cs: handle Decimal here to process xs:decimal as
	  a valid base type for simple types. patch by Luke Ravitch,
	  Fixed bug #557452.

	* XmlSchemaValidatorTests.cs : added test for bug #557452, by
	  Luke Ravitch.


svn path=/trunk/mcs/; revision=150280
Atsushi Eno há 16 anos atrás
pai
commit
eede1f63d3

+ 6 - 0
mcs/class/System.XML/System.Xml.Schema/ChangeLog

@@ -1,3 +1,9 @@
+2010-01-27  Atsushi Enomoto  <[email protected]>
+
+	* XmlAtomicValue.cs: handle Decimal here to process xs:decimal as
+	  a valid base type for simple types. patch by Luke Ravitch,
+	  Fixed bug #557452.
+
 2010-01-26  Atsushi Enomoto  <[email protected]>
 
 	* XmlSchemaComplexType.cs : validate base type first and then

+ 38 - 0
mcs/class/System.XML/System.Xml.Schema/XmlAtomicValue.cs

@@ -285,6 +285,8 @@ namespace System.Xml.Schema
 			case XmlTypeCode.Short:
 			case XmlTypeCode.UnsignedShort:
 				return ValueAsInt;
+			case XmlTypeCode.Decimal:
+				return ValueAsDecimal;
 			case XmlTypeCode.Double:
 			case XmlTypeCode.Float:
 				return ValueAsDouble;
@@ -340,6 +342,8 @@ namespace System.Xml.Schema
 					return ValueAsBoolean;
 				case XmlTypeCode.DateTime:
 					return ValueAsDateTime;
+				case XmlTypeCode.Decimal:
+					return ValueAsDecimal;
 				case XmlTypeCode.Float:
 				case XmlTypeCode.Double:
 					return ValueAsDouble;
@@ -368,6 +372,9 @@ namespace System.Xml.Schema
 				case XmlTypeCode.Double:
 					stringValue = XQueryConvert.DoubleToString (ValueAsDouble);
 					break;
+				case XmlTypeCode.Decimal:
+					stringValue = XQueryConvert.DecimalToString (ValueAsDecimal);
+					break;
 				case XmlTypeCode.NonPositiveInteger:
 				case XmlTypeCode.NonNegativeInteger:
 				case XmlTypeCode.NegativeInteger:
@@ -479,6 +486,37 @@ namespace System.Xml.Schema
 			}
 		}
 
+		// Unlike the other ValueAs...() functions, this is not part
+		// of the XPathItem abstract class, so it's not an override
+		public decimal ValueAsDecimal {
+			get {
+				switch (xmlTypeCode) {
+				case XmlTypeCode.Boolean:
+					return XQueryConvert.BooleanToDecimal (booleanValue);
+				case XmlTypeCode.Decimal:
+					return decimalValue;
+				case XmlTypeCode.Double:
+					return XQueryConvert.DoubleToDecimal (doubleValue);
+				case XmlTypeCode.Long:
+					return XQueryConvert.IntegerToDecimal (longValue);
+				case XmlTypeCode.Int:
+					return XQueryConvert.IntToDecimal (intValue);
+				case XmlTypeCode.Float:
+					return XQueryConvert.FloatToDecimal (floatValue);
+				case XmlTypeCode.String:
+					return XQueryConvert.StringToDecimal (stringValue);
+				case XmlTypeCode.None:
+				case XmlTypeCode.Item:
+				case XmlTypeCode.AnyAtomicType:
+					if (objectValue is decimal)
+						return (decimal) objectValue;
+					break;
+				}
+
+				throw new InvalidCastException (String.Format ("Conversion from {0} to {1} is not supported", schemaType.QualifiedName, XmlSchemaSimpleType.XsDecimal.QualifiedName));
+			}
+		}
+
 		public override double ValueAsDouble {
 			get {
 				switch (xmlTypeCode) {

+ 5 - 0
mcs/class/System.XML/Test/System.Xml.Schema/ChangeLog

@@ -1,3 +1,8 @@
+2010-01-27  Atsushi Enomoto  <[email protected]>
+
+	* XmlSchemaValidatorTests.cs : added test for bug #557452, by
+	  Luke Ravitch.
+
 2010-01-26  Atsushi Enomoto  <[email protected]>
 
 	* XmlSchemaValidatorTests.cs : added test for bug #502251, by

+ 31 - 0
mcs/class/System.XML/Test/System.Xml.Schema/XmlSchemaValidatorTests.cs

@@ -259,6 +259,37 @@ namespace MonoTests.System.Xml
 			doc.Schemas.Add (schema);
 			doc.Validate (null);
 		}
+
+		[Test]
+		public void Bug557452 ()
+		{
+			string xsd = @"
+			<xs:schema id='Settings'
+				targetNamespace='foo'
+				xmlns='foo'
+				xmlns:xs='http://www.w3.org/2001/XMLSchema'>
+
+				<xs:element name='Settings' type='Settings'/>
+
+				<xs:complexType name='Settings'>
+					<xs:attribute name='port' type='PortNumber' use='required'/>
+				</xs:complexType>
+
+				<xs:simpleType name='PortNumber'>
+					<xs:restriction base='xs:decimal'>
+						<xs:minInclusive value='1'/>
+						<xs:maxInclusive value='65535'/>
+					</xs:restriction>
+				</xs:simpleType>
+			</xs:schema>";
+
+			string xml = @"<Settings port='1337' xmlns='foo'/>";
+
+			XmlDocument doc = new XmlDocument ();
+			doc.LoadXml (xml);
+			doc.Schemas.Add (XmlSchema.Read (XmlReader.Create (new StringReader (xsd)), null));
+			doc.Validate (null);
+		}
 	}
 }