internal override Exception CheckValueFacets(XmlQualifiedName value, XmlSchemaDatatype datatype) { RestrictionFacets restriction = datatype.Restriction; RestrictionFlags flags = (restriction != null) ? restriction.Flags : ((RestrictionFlags) 0); if (flags != 0) { int length = value.ToString().Length; if (((flags & RestrictionFlags.Length) != 0) && (restriction.Length != length)) { return new XmlSchemaException("Sch_LengthConstraintFailed", string.Empty); } if (((flags & RestrictionFlags.MinLength) != 0) && (length < restriction.MinLength)) { return new XmlSchemaException("Sch_MinLengthConstraintFailed", string.Empty); } if (((flags & RestrictionFlags.MaxLength) != 0) && (restriction.MaxLength < length)) { return new XmlSchemaException("Sch_MaxLengthConstraintFailed", string.Empty); } if (((flags & RestrictionFlags.Enumeration) != 0) && !this.MatchEnumeration(value, restriction.Enumeration)) { return new XmlSchemaException("Sch_EnumerationConstraintFailed", string.Empty); } } return null; }
internal override Exception CheckValueFacets(decimal value, XmlSchemaDatatype datatype) { RestrictionFacets restriction = datatype.Restriction; RestrictionFlags flags = (restriction != null) ? restriction.Flags : ((RestrictionFlags) 0); XmlValueConverter valueConverter = datatype.ValueConverter; if ((value > this.maxValue) || (value < this.minValue)) { return new OverflowException(Res.GetString("XmlConvert_Overflow", new object[] { value.ToString(CultureInfo.InvariantCulture), datatype.TypeCodeString })); } if (flags == 0) { return null; } if (((flags & RestrictionFlags.MaxInclusive) != 0) && (value > valueConverter.ToDecimal(restriction.MaxInclusive))) { return new XmlSchemaException("Sch_MaxInclusiveConstraintFailed", string.Empty); } if (((flags & RestrictionFlags.MaxExclusive) != 0) && (value >= valueConverter.ToDecimal(restriction.MaxExclusive))) { return new XmlSchemaException("Sch_MaxExclusiveConstraintFailed", string.Empty); } if (((flags & RestrictionFlags.MinInclusive) != 0) && (value < valueConverter.ToDecimal(restriction.MinInclusive))) { return new XmlSchemaException("Sch_MinInclusiveConstraintFailed", string.Empty); } if (((flags & RestrictionFlags.MinExclusive) != 0) && (value <= valueConverter.ToDecimal(restriction.MinExclusive))) { return new XmlSchemaException("Sch_MinExclusiveConstraintFailed", string.Empty); } if (((flags & RestrictionFlags.Enumeration) != 0) && !this.MatchEnumeration(value, restriction.Enumeration, valueConverter)) { return new XmlSchemaException("Sch_EnumerationConstraintFailed", string.Empty); } return this.CheckTotalAndFractionDigits(value, restriction.TotalDigits, restriction.FractionDigits, (flags & RestrictionFlags.TotalDigits) != 0, (flags & RestrictionFlags.FractionDigits) != 0); }
internal Exception CheckValueFacets(string value, XmlSchemaDatatype datatype, bool verifyUri) { int length = value.Length; RestrictionFacets restriction = datatype.Restriction; RestrictionFlags flags = (restriction != null) ? restriction.Flags : ((RestrictionFlags) 0); Exception exception = this.CheckBuiltInFacets(value, datatype.TypeCode, verifyUri); if (exception != null) { return exception; } if (flags != 0) { if (((flags & RestrictionFlags.Length) != 0) && (restriction.Length != length)) { return new XmlSchemaException("Sch_LengthConstraintFailed", string.Empty); } if (((flags & RestrictionFlags.MinLength) != 0) && (length < restriction.MinLength)) { return new XmlSchemaException("Sch_MinLengthConstraintFailed", string.Empty); } if (((flags & RestrictionFlags.MaxLength) != 0) && (restriction.MaxLength < length)) { return new XmlSchemaException("Sch_MaxLengthConstraintFailed", string.Empty); } if (((flags & RestrictionFlags.Enumeration) != 0) && !this.MatchEnumeration(value, restriction.Enumeration, datatype)) { return new XmlSchemaException("Sch_EnumerationConstraintFailed", string.Empty); } } return null; }
internal SchemaElementDecl(XmlSchemaDatatype dtype) { this.attdefs = new Dictionary<XmlQualifiedName, SchemaAttDef>(); this.prohibitedAttributes = new Dictionary<XmlQualifiedName, XmlQualifiedName>(); base.Datatype = dtype; this.contentValidator = System.Xml.Schema.ContentValidator.TextOnly; }
public virtual bool IsDerivedFrom (XmlSchemaDatatype datatype) { // It is documented to return always false, but // actually returns true when the argument is for // the same type (and it does not check null argument). return this == datatype; }
internal XElement AddValueInPosition(XName name, XElement parentElement, bool addToExisting, object value, XmlSchemaDatatype datatype) { NamedContentModelEntity namedEntity = GetNamedEntity(name); if (namedEntity == null) { throw new LinqToXsdException("Name does not belong in content model. Cannot set value for child " + namedEntity.Name); } EditAction editAction = EditAction.None; XElement elementMarker = FindElementPosition(namedEntity, parentElement, addToExisting, out editAction); Debug.Assert(datatype != null); //Simple typed value add or set XElement element = elementMarker; switch (editAction) { case EditAction.Append: element = new XElement(name, XTypedServices.GetXmlString(value, datatype, parentElement)); parentElement.Add(element); break; case EditAction.Update: Debug.Assert(elementMarker != null); elementMarker.Value = XTypedServices.GetXmlString(value, datatype, elementMarker); break; case EditAction.AddBefore: Debug.Assert(elementMarker != null); element = new XElement(name, XTypedServices.GetXmlString(value, datatype, elementMarker)); elementMarker.AddBeforeSelf(element); break; default: throw new InvalidOperationException(); } return(element); }
private void ProcessAttribute(string prefix, string localName, string ns, string value) { attributeIndex++; this.AddAttribute(nodeIndex, localName, ns, prefix != null ? prefix : String.Empty, value, null, lineInfo != null ? lineInfo.LineNumber : 0, lineInfo != null ? lineInfo.LinePosition : 0); if (firstAttributeIndex == 0) { firstAttributeIndex = attributeIndex; } else { attributes [attributeIndex - 1].NextAttribute = attributeIndex; } // Identity infoset if (validatingReader != null) { XmlSchemaDatatype dt = validatingReader.SchemaType as XmlSchemaDatatype; if (dt == null) { XmlSchemaType xsType = validatingReader.SchemaType as XmlSchemaType; if (xsType != null) { dt = xsType.Datatype; } } if (dt != null && dt.TokenizedType == XmlTokenizedType.ID) { idTable.Add(value, nodeIndex); } } }
internal void CheckWhitespaceFacets(ref string s, SimpleTypeValidator type, XmlSchemaWhiteSpace wsPattern) { // before parsing, check whitespace facet RestrictionFacets restriction = type.RestrictionFacets; if (type.Variety == XmlSchemaDatatypeVariety.List) { s = s.Trim(); return; } else if (type.Variety == XmlSchemaDatatypeVariety.Atomic) { XmlSchemaDatatype datatype = type.DataType; if (datatype.GetBuiltInWSFacet() == XmlSchemaWhiteSpace.Collapse) { s = XmlComplianceUtil.NonCDataNormalize(s); } else if (datatype.GetBuiltInWSFacet() == XmlSchemaWhiteSpace.Replace) { s = XmlComplianceUtil.CDataNormalize(s); } else if (restriction != null & (restriction.Flags & RestrictionFlags.WhiteSpace) != 0) { //Restriction has whitespace facet specified if (restriction.WhiteSpace == XmlSchemaWhiteSpace.Replace) { s = XmlComplianceUtil.CDataNormalize(s); } else if (restriction.WhiteSpace == XmlSchemaWhiteSpace.Collapse) { s = XmlComplianceUtil.NonCDataNormalize(s); } } } return; }
// Field generation private void GenerateAttributeField(XmlSchemaAttribute schemaAtt) { XmlSchemaDatatype primitive = schemaAtt.AttributeType as XmlSchemaDatatype; XmlSchemaSimpleType simple = schemaAtt.AttributeType as XmlSchemaSimpleType; XmlSchemaDerivationMethod deriv = XmlSchemaDerivationMethod.None; while (primitive == null) { if (simple == null) // maybe union { break; } primitive = simple.BaseSchemaType as XmlSchemaDatatype; if (primitive == null) { simple = simple.BaseSchemaType as XmlSchemaSimpleType; if (simple != null && simple.DerivedBy != XmlSchemaDerivationMethod.None) { deriv = simple.DerivedBy; } } } Type type = primitive != null ? primitive.ValueType : typeof(object); bool isList = (simple != null && simple.DerivedBy == XmlSchemaDerivationMethod.List); CodeTypeReference cType = new CodeTypeReference(type); cType.ArrayRank = isList ? 1 : 0; CodeMemberField cmf = CreateMemberField(cType, schemaAtt.QualifiedName.Name, XmlStructureType.Attribute); currentType.Members.Add(cmf); }
private void ProcessAttribute(string prefix, string localName, string ns, string value) { attributeIndex++; this.AddAttribute(nodeIndex, AtomicIndex(localName), AtomicIndex(ns), prefix != null ? AtomicIndex(prefix) : 0, NonAtomicIndex(value), lineInfo != null ? lineInfo.LineNumber : 0, lineInfo != null ? lineInfo.LinePosition : 0); if (hasAttributes) { attributes [attributeIndex - 1].NextAttribute = attributeIndex; } else { hasAttributes = true; } // Identity infoset if (validatingReader != null) { XmlSchemaDatatype dt = validatingReader.SchemaType as XmlSchemaDatatype; if (dt == null) { XmlSchemaType xsType = validatingReader.SchemaType as XmlSchemaType; if (xsType != null) { dt = xsType.Datatype; } } if (dt != null && dt.TokenizedType == XmlTokenizedType.ID) { idTable.Add(value, nodeIndex); } } }
string [] GetDerived(string target) { XmlSchemaDatatype strType = GetDatatype(target); List <string> results = new List <string> (); foreach (string name in allTypes) { if (name == target) { continue; } XmlSchemaDatatype deriv = GetDatatype(name); if (deriv.IsDerivedFrom(strType)) { results.Add(name); } else { Console.Error.WriteLine(deriv.GetType() + " is not derived from " + strType.GetType()); } } return(results.ToArray()); }
private bool MatchEnumeration(string value, ArrayList enumeration, XmlSchemaDatatype datatype) { if (datatype.TypeCode == XmlTypeCode.AnyUri) { foreach (Uri correctValue in enumeration) { if (value.Equals(correctValue.OriginalString)) { return(true); } } } else { foreach (string correctValue in enumeration) { if (value.Equals(correctValue)) { return(true); } } } return(false); }
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. }
private bool MatchEnumeration(double value, ArrayList enumeration, XmlSchemaDatatype datatype) { foreach (object correctValue in enumeration) { if (value == (double)datatype.ChangeType(correctValue, typeof(double))) { return true; } } return false; }
internal virtual bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype) { return false; }
internal override bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype) { string strValue = ListSimpleTypeValidator.ToString(value); foreach (object correctArray in enumeration) { if (strValue.Equals(correctArray)) { return true; } } return false; }
private bool MatchEnumeration(XmlQualifiedName value, ArrayList enumeration, XmlSchemaDatatype datatype) { foreach (XmlQualifiedName correctValue in enumeration) { if (value.Equals(correctValue)) { return true; } } return false; }
public CompiledFacets(XmlSchemaDatatype dt) { whiteSpace = dt.GetBuiltInWSFacet(); }
internal virtual bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype) { return(false); }
private void AttributeIdentityConstraints(string name, string ns, object obj, string sobj, XmlSchemaDatatype datatype) { for (int i = this.startIDConstraint; i < this.validationStack.Length; i++) { if (((ValidationState) this.validationStack[i]).Constr != null) { ConstraintStruct[] constr = ((ValidationState) this.validationStack[i]).Constr; for (int j = 0; j < constr.Length; j++) { for (int k = 0; k < constr[j].axisFields.Count; k++) { LocatedActiveAxis axis = (LocatedActiveAxis) constr[j].axisFields[k]; if (axis.MoveToAttribute(name, ns)) { if (axis.Ks[axis.Column] != null) { this.SendValidationEvent("Sch_FieldSingleValueExpected", name); } else { axis.Ks[axis.Column] = new TypedObject(obj, sobj, datatype); } } } } } } }
internal override bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype) { return(MatchEnumeration((string)datatype.ChangeType(value, XTypedServices.typeOfString), enumeration, datatype)); }
internal override bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype) { return(MatchEnumeration((DateTime)datatype.ChangeType(value, typeof(DateTime)), enumeration, datatype)); }
internal override bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype) { return(MatchEnumeration((TimeSpan)value, enumeration, datatype)); }
internal override Exception CheckValueFacets(double value, SimpleTypeValidator type) { RestrictionFacets facets = type.RestrictionFacets; if (facets == null || !facets.HasValueFacets) { return(null); } RestrictionFlags flags = facets.Flags; XmlSchemaDatatype datatype = type.DataType; if ((flags & RestrictionFlags.MinInclusive) != 0) { if (value < (double)datatype.ChangeType(facets.MinInclusive, typeof(double))) { return(new LinqToXsdFacetException(RestrictionFlags.MinInclusive, facets.MinInclusive, value)); } } if ((flags & RestrictionFlags.MinExclusive) != 0) { if (value <= (double)datatype.ChangeType(facets.MinExclusive, typeof(double))) { return(new LinqToXsdFacetException(RestrictionFlags.MinExclusive, facets.MinExclusive, value)); } } if ((flags & RestrictionFlags.MaxInclusive) != 0) { if (value > (double)datatype.ChangeType(facets.MaxInclusive, typeof(double))) { return(new LinqToXsdFacetException(RestrictionFlags.MaxInclusive, facets.MaxInclusive, value)); } } if ((flags & RestrictionFlags.MaxExclusive) != 0) { if (value >= (double)datatype.ChangeType(facets.MaxExclusive, typeof(double))) { return(new LinqToXsdFacetException(RestrictionFlags.MaxExclusive, facets.MaxExclusive, value)); } } if ((flags & RestrictionFlags.Enumeration) != 0) { ArrayList enums = facets.Enumeration; if (!MatchEnumeration(value, enums, datatype)) { return(new LinqToXsdFacetException(RestrictionFlags.Enumeration, facets.Enumeration, value)); } } return(null); }
internal override Exception CheckValueFacets(decimal value, SimpleTypeValidator type) { RestrictionFacets facets = type.RestrictionFacets; if (facets == null || !facets.HasValueFacets) { return(null); } //No need to check built-in type because CLR should've already done that RestrictionFlags flags = facets.Flags; XmlSchemaDatatype datatype = type.DataType; if ((flags & RestrictionFlags.Enumeration) != 0) { ArrayList enums = facets.Enumeration; if (!MatchEnumeration(value, enums, datatype)) { return(new LinqToXsdFacetException(RestrictionFlags.Enumeration, facets.Enumeration, value)); } } if ((flags & RestrictionFlags.FractionDigits) != 0) { Exception e = CheckTotalAndFractionDigits(value, Constants.DecimalMaxPower, facets.FractionDigits, false, true); if (e != null) { return(new LinqToXsdFacetException(RestrictionFlags.FractionDigits, facets.FractionDigits, value)); } } if ((flags & RestrictionFlags.MaxExclusive) != 0) { if (value >= (decimal)datatype.ChangeType(facets.MaxExclusive, typeof(decimal))) { return(new LinqToXsdFacetException(RestrictionFlags.MaxExclusive, facets.MaxExclusive, value)); } } if ((flags & RestrictionFlags.MaxInclusive) != 0) { if (value > (decimal)datatype.ChangeType(facets.MaxInclusive, typeof(decimal))) { return(new LinqToXsdFacetException(RestrictionFlags.MaxInclusive, facets.MaxInclusive, value)); } } if ((flags & RestrictionFlags.MinExclusive) != 0) { if (value <= (decimal)datatype.ChangeType(facets.MinExclusive, typeof(decimal))) { return(new LinqToXsdFacetException(RestrictionFlags.MinExclusive, facets.MinExclusive, value)); } } if ((flags & RestrictionFlags.MinInclusive) != 0) { if (value < (decimal)datatype.ChangeType(facets.MinInclusive, typeof(decimal))) { return(new LinqToXsdFacetException(RestrictionFlags.MinInclusive, facets.MinInclusive, value)); } } if ((flags & RestrictionFlags.TotalDigits) != 0) { Exception e = CheckTotalAndFractionDigits(value, System.Convert.ToInt32(facets.TotalDigits), 0, true, false); if (e != null) { return(new LinqToXsdFacetException(RestrictionFlags.TotalDigits, facets.TotalDigits, value)); } } return(null); }
internal override bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype) { return MatchEnumeration((DateTime)datatype.ChangeType(value, typeof(DateTime)), enumeration, datatype); }
internal override bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype) { return MatchEnumeration((string)datatype.ChangeType(value, XTypedServices.typeOfString), enumeration, datatype); }
private static Type GetIntegerDerivedType(XmlSchemaDatatype type, GeneratorConfiguration configuration, IEnumerable <RestrictionModel> restrictions) { if (configuration.IntegerDataType != null) { return(configuration.IntegerDataType); } var xmlTypeCode = type.TypeCode; Type result = null; if (!(restrictions.SingleOrDefault(r => r is TotalDigitsRestrictionModel) is TotalDigitsRestrictionModel totalDigits) || ((xmlTypeCode == XmlTypeCode.PositiveInteger || xmlTypeCode == XmlTypeCode.NonNegativeInteger) && totalDigits.Value >= 30) || ((xmlTypeCode == XmlTypeCode.Integer || xmlTypeCode == XmlTypeCode.NegativeInteger || xmlTypeCode == XmlTypeCode.NonPositiveInteger) && totalDigits.Value >= 29)) { return(typeof(string)); } switch (xmlTypeCode) { case XmlTypeCode.PositiveInteger: case XmlTypeCode.NonNegativeInteger: switch (totalDigits.Value) { case int n when(n < 3): result = typeof(byte); break; case int n when(n < 5): result = typeof(ushort); break; case int n when(n < 10): result = typeof(uint); break; case int n when(n < 20): result = typeof(ulong); break; case int n when(n < 30): result = typeof(decimal); break; } break; case XmlTypeCode.Integer: case XmlTypeCode.NegativeInteger: case XmlTypeCode.NonPositiveInteger: switch (totalDigits.Value) { case int n when(n < 3): result = typeof(sbyte); break; case int n when(n < 5): result = typeof(short); break; case int n when(n < 10): result = typeof(int); break; case int n when(n < 19): result = typeof(long); break; case int n when(n < 29): result = typeof(decimal); break; } break; } return(result); }
private void EndElementIdentityConstraints(object typedValue, string stringValue, XmlSchemaDatatype datatype) { string localName = this.context.LocalName; string uRN = this.context.Namespace; for (int i = this.validationStack.Length - 1; i >= this.startIDConstraint; i--) { if (((ValidationState) this.validationStack[i]).Constr != null) { ConstraintStruct[] structArray = ((ValidationState) this.validationStack[i]).Constr; for (int j = 0; j < structArray.Length; j++) { KeySequence sequence; for (int k = 0; k < structArray[j].axisFields.Count; k++) { LocatedActiveAxis axis = (LocatedActiveAxis) structArray[j].axisFields[k]; if (axis.isMatched) { axis.isMatched = false; if (axis.Ks[axis.Column] != null) { this.SendValidationEvent("Sch_FieldSingleValueExpected", localName); } else if ((typedValue != null) && (stringValue.Length != 0)) { axis.Ks[axis.Column] = new TypedObject(typedValue, stringValue, datatype); } } axis.EndElement(localName, uRN); } if (structArray[j].axisSelector.EndElement(localName, uRN)) { sequence = structArray[j].axisSelector.PopKS(); switch (structArray[j].constraint.Role) { case CompiledIdentityConstraint.ConstraintRole.Unique: if (sequence.IsQualified()) { if (!structArray[j].qualifiedTable.Contains(sequence)) { goto Label_0283; } this.SendValidationEvent(new XmlSchemaValidationException("Sch_DuplicateKey", new string[] { sequence.ToString(), structArray[j].constraint.name.ToString() }, this.sourceUriString, sequence.PosLine, sequence.PosCol)); } break; case CompiledIdentityConstraint.ConstraintRole.Key: if (sequence.IsQualified()) { goto Label_0195; } this.SendValidationEvent(new XmlSchemaValidationException("Sch_MissingKey", structArray[j].constraint.name.ToString(), this.sourceUriString, sequence.PosLine, sequence.PosCol)); break; case CompiledIdentityConstraint.ConstraintRole.Keyref: if (((structArray[j].qualifiedTable != null) && sequence.IsQualified()) && !structArray[j].qualifiedTable.Contains(sequence)) { structArray[j].qualifiedTable.Add(sequence, sequence); } break; } } continue; Label_0195: if (structArray[j].qualifiedTable.Contains(sequence)) { this.SendValidationEvent(new XmlSchemaValidationException("Sch_DuplicateKey", new string[] { sequence.ToString(), structArray[j].constraint.name.ToString() }, this.sourceUriString, sequence.PosLine, sequence.PosCol)); } else { structArray[j].qualifiedTable.Add(sequence, sequence); } continue; Label_0283: structArray[j].qualifiedTable.Add(sequence, sequence); } } } ConstraintStruct[] constr = ((ValidationState) this.validationStack[this.validationStack.Length - 1]).Constr; if (constr != null) { for (int m = 0; m < constr.Length; m++) { if ((constr[m].constraint.Role != CompiledIdentityConstraint.ConstraintRole.Keyref) && (constr[m].keyrefTable != null)) { foreach (KeySequence sequence2 in constr[m].keyrefTable.Keys) { if (!constr[m].qualifiedTable.Contains(sequence2)) { this.SendValidationEvent(new XmlSchemaValidationException("Sch_UnresolvedKeyref", sequence2.ToString(), this.sourceUriString, sequence2.PosLine, sequence2.PosCol)); } } } } } }
private static IList <T> ParseListValue <T>(string value, XElement element, XName name, ContainerType containerType, XmlSchemaDatatype datatype) { return(new XListContent <T>(value, element, name, containerType, datatype)); }
private Type ConvertDatatype (XmlSchemaDatatype dt) { if (dt == null) return typeof (string); else if (dt.ValueType == typeof (decimal)) { // LAMESPEC: MSDN documentation says it is based // on ValueType. However, in the System.Xml.Schema // context, xs:integer is mapped to Decimal, while // in DataSet context it is mapped to Int64. if (dt == schemaDecimalType) return typeof (decimal); else if (dt == schemaIntegerType) return typeof (long); else return typeof (ulong); } else return dt.ValueType; }
private static T ParseValueFast <T>(string value, XmlSchemaDatatype datatype) { switch (typeof(T).Name) { case nameof(System.String): return((T)(object)value); case nameof(System.Boolean): { switch (value) { case "true": case "1": return((T)(object)true); case "false": case "0": return((T)(object)false); } break; } case nameof(System.Int16): { short retval; if (short.TryParse(value, out retval)) { return((T)(object)retval); } break; } case nameof(System.UInt16): { ushort retval; if (ushort.TryParse(value, out retval)) { return((T)(object)retval); } break; } case nameof(System.Int32): { int retval; if (int.TryParse(value, out retval)) { return((T)(object)retval); } break; } case nameof(System.UInt32): { uint retval; if (uint.TryParse(value, out retval)) { return((T)(object)retval); } break; } case nameof(System.Int64): { long retval; if (long.TryParse(value, out retval)) { return((T)(object)retval); } break; } case nameof(System.UInt64): { ulong retval; if (ulong.TryParse(value, out retval)) { return((T)(object)retval); } break; } case nameof(System.Decimal): { decimal retval; if (decimal.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out retval)) { return((T)(object)retval); } break; } } return((T)datatype.ChangeType(value, typeof(T))); }
private static CodeExpression CreateTypedValueExpression(XmlSchemaDatatype dataType, object value) { XmlTypeCode typeCode = dataType.TypeCode; switch (typeCode) { case XmlTypeCode.String: case XmlTypeCode.Notation: case XmlTypeCode.NormalizedString: case XmlTypeCode.Token: case XmlTypeCode.Language: case XmlTypeCode.Id: string str = value as string; Debug.Assert(str != null); return(new CodePrimitiveExpression(str)); case XmlTypeCode.AnyUri: Debug.Assert(value is Uri); return(new CodeObjectCreateExpression(typeof(Uri), new CodePrimitiveExpression(((Uri)value).OriginalString))); case XmlTypeCode.QName: XmlQualifiedName qname = value as XmlQualifiedName; return(new CodeObjectCreateExpression(typeof(XmlQualifiedName), new CodePrimitiveExpression(qname.Name), new CodePrimitiveExpression(qname.Namespace))); case XmlTypeCode.NmToken: case XmlTypeCode.Name: case XmlTypeCode.NCName: return(CodeDomHelper.CreateMethodCall( new CodeTypeReferenceExpression(typeof(XmlConvert)), "EncodeName", new CodePrimitiveExpression(value.ToString()))); case XmlTypeCode.Boolean: Debug.Assert(value is bool); return(new CodePrimitiveExpression(value)); case XmlTypeCode.Float: case XmlTypeCode.Double: Debug.Assert(value is double || value is float); return(new CodePrimitiveExpression(value)); case XmlTypeCode.Duration: Debug.Assert(value is TimeSpan); TimeSpan ts = (TimeSpan)value; return(new CodeObjectCreateExpression(typeof(TimeSpan), new CodePrimitiveExpression(ts.Ticks))); case XmlTypeCode.Time: case XmlTypeCode.Date: case XmlTypeCode.GYearMonth: case XmlTypeCode.GYear: case XmlTypeCode.GMonthDay: case XmlTypeCode.GDay: case XmlTypeCode.GMonth: case XmlTypeCode.DateTime: Debug.Assert(value is DateTime); DateTime dt = (DateTime)value; return(new CodeObjectCreateExpression(typeof(DateTime), new CodePrimitiveExpression(dt.Ticks))); case XmlTypeCode.Integer: case XmlTypeCode.NonPositiveInteger: case XmlTypeCode.NegativeInteger: case XmlTypeCode.Long: case XmlTypeCode.Int: case XmlTypeCode.Short: case XmlTypeCode.NonNegativeInteger: case XmlTypeCode.UnsignedLong: case XmlTypeCode.UnsignedInt: case XmlTypeCode.UnsignedShort: case XmlTypeCode.PositiveInteger: case XmlTypeCode.Decimal: case XmlTypeCode.Byte: case XmlTypeCode.UnsignedByte: return(new CodePrimitiveExpression(value)); case XmlTypeCode.HexBinary: case XmlTypeCode.Base64Binary: return(CreateByteArrayExpression(value)); case XmlTypeCode.None: case XmlTypeCode.Item: case XmlTypeCode.AnyAtomicType: case XmlTypeCode.Idref: case XmlTypeCode.Entity: throw new InvalidOperationException(); default: throw new InvalidOperationException(); } }
public override void AddElementToParent(XName name, object value, XElement parentElement, bool addToExisting, XmlSchemaDatatype datatype) { base.AddElementToParent(name, value, parentElement, addToExisting, datatype); this.CheckChoiceBranches(name, parentElement); }
private bool MatchEnumeration(byte[] value, ArrayList enumeration, XmlSchemaDatatype datatype) { foreach (byte[] correctValue in enumeration) { if (IsEqual(value, correctValue) ) { return true; } } return false; }
public override XElement AddElementToParent(XName name, object value, XElement parentElement, bool addToExisting, XmlSchemaDatatype datatype, Type elementBaseType) { throw new InvalidOperationException(); }
internal override bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype) { foreach (object correctValue in enumeration) { if (value.Equals(correctValue)) { return true; } } return false; }
public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfiguration configuration, IEnumerable <RestrictionModel> restrictions, bool attribute = false) { Type resultType; switch (type.TypeCode) { case XmlTypeCode.AnyAtomicType: // union resultType = typeof(string); break; case XmlTypeCode.AnyUri: case XmlTypeCode.Duration: case XmlTypeCode.GDay: case XmlTypeCode.GMonth: case XmlTypeCode.GMonthDay: case XmlTypeCode.GYear: case XmlTypeCode.GYearMonth: resultType = typeof(string); break; case XmlTypeCode.Time: resultType = typeof(DateTime); break; case XmlTypeCode.Idref: resultType = typeof(string); break; case XmlTypeCode.Integer: case XmlTypeCode.NegativeInteger: case XmlTypeCode.NonNegativeInteger: case XmlTypeCode.NonPositiveInteger: case XmlTypeCode.PositiveInteger: resultType = GetIntegerDerivedType(type, configuration, restrictions); break; default: resultType = type.ValueType; break; } if (type.Variety == XmlSchemaDatatypeVariety.List) { if (resultType.IsArray) { resultType = resultType.GetElementType(); } // XmlSerializer doesn't support xsd:list for elements, only for attributes: // https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/t84dzyst(v%3dvs.100) // Also, de/serialization fails when the XML schema type is ambiguous // DateTime -> date, datetime, or time // byte[] -> hexBinary or base64Binary if (!attribute || resultType == typeof(DateTime) || resultType == typeof(byte[])) { resultType = typeof(string); } } return(resultType); }
internal bool MatchEnumeration(decimal value, ArrayList enumeration, XmlSchemaDatatype datatype) { foreach (object correctValue in enumeration) { if (value == (decimal)datatype.ChangeType(correctValue, typeof(decimal))) { return true; } } return false; }
/// <include file='doc\XmlSchemaDatatype.uex' path='docs/doc[@for="XmlSchemaDatatype.IsDerivedFrom"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public virtual bool IsDerivedFrom(XmlSchemaDatatype datatype) { return false; }
internal override bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype) { return MatchEnumeration((TimeSpan)value, enumeration, datatype); }
/// <summary> /// Begin the creation of an XmlSchemaSimpleType object that will be used to represent a static built-in type. /// Once StartBuiltinType has been called for all built-in types, FinishBuiltinType should be called in order /// to create links between the types. /// </summary> internal static XmlSchemaSimpleType StartBuiltinType(XmlQualifiedName qname, XmlSchemaDatatype dataType) { XmlSchemaSimpleType simpleType; Debug.Assert(qname != null && dataType != null); simpleType = new XmlSchemaSimpleType(); simpleType.SetQualifiedName(qname); simpleType.SetDatatype(dataType); simpleType.ElementDecl = new SchemaElementDecl(dataType); simpleType.ElementDecl.SchemaType = simpleType; return simpleType; }
private bool MatchEnumeration(DateTime value, ArrayList enumeration, XmlSchemaDatatype datatype) { foreach (DateTime correctValue in enumeration) { if (DateTime.Compare(value, correctValue) == 0) { return true; } } return false; }
internal override bool IsComparable(XmlSchemaDatatype dtype) { XmlTypeCode thisCode = this.TypeCode; XmlTypeCode otherCode = dtype.TypeCode; if (thisCode == otherCode) { //They are both same built-in type or one is list and the other is list's itemType return true; } if (GetPrimitiveTypeCode(thisCode) == GetPrimitiveTypeCode(otherCode)) { return true; } if (this.IsDerivedFrom(dtype) || dtype.IsDerivedFrom(this)) { //One is union and the other is a member of the union return true; } return false; }
private bool MatchEnumeration(string value, ArrayList enumeration, XmlSchemaDatatype datatype) { if (datatype.TypeCode == XmlTypeCode.AnyUri) { foreach (Uri correctValue in enumeration){ if (value.Equals(correctValue.OriginalString)) { return true; } } } else { foreach (string correctValue in enumeration) { if (value.Equals(correctValue)) { return true; } } } return false; }
protected void SetListElement(XName name, object value, XmlSchemaDatatype datatype) { this.SetElement(name, ListSimpleTypeValidator.ToString(value), datatype); }
private void CheckTokenizedTypes(XmlSchemaDatatype dtype, object typedValue, bool attrValue) { if (typedValue != null) { switch (dtype.TokenizedType) { case XmlTokenizedType.ENTITY: case XmlTokenizedType.ID: case XmlTokenizedType.IDREF: if (dtype.Variety == XmlSchemaDatatypeVariety.List) { string[] strArray = (string[]) typedValue; for (int i = 0; i < strArray.Length; i++) { this.ProcessTokenizedType(dtype.TokenizedType, strArray[i], attrValue); } return; } this.ProcessTokenizedType(dtype.TokenizedType, (string) typedValue, attrValue); break; } } }
public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfiguration configuration) { return(GetEffectiveType(type.TypeCode, type.Variety, configuration) ?? type.ValueType); }
internal abstract bool IsComparable(XmlSchemaDatatype dtype);
internal override Exception CheckValueFacets(object value, SimpleTypeValidator type) { RestrictionFacets facets = type.RestrictionFacets; if (facets == null || !facets.HasValueFacets) { return(null); } //Check for facets allowed on lists - Length, MinLength, MaxLength //value is a list IList listValue = null; Exception e = ListSimpleTypeValidator.ToList(value, ref listValue); if (e != null) { return(e); } int length = listValue.Count; XmlSchemaDatatype datatype = type.DataType; RestrictionFlags flags = facets.Flags; if ((flags & RestrictionFlags.Enumeration) != 0) { ArrayList enums = facets.Enumeration; if (!MatchEnumeration(value, enums, datatype)) { return(new LinqToXsdFacetException(RestrictionFlags.Enumeration, facets.Enumeration, value)); } } if ((flags & RestrictionFlags.Length) != 0) { if (length != facets.Length) { return(new LinqToXsdFacetException(RestrictionFlags.Length, facets.Length, value)); } } if ((flags & RestrictionFlags.MaxLength) != 0) { if (length > facets.MaxLength) { return(new LinqToXsdFacetException(RestrictionFlags.MaxLength, facets.MaxLength, value)); } } if ((flags & RestrictionFlags.MinLength) != 0) { if (length < facets.MinLength) { return(new LinqToXsdFacetException(RestrictionFlags.MinLength, facets.MinLength, value)); } } return(null); }
private void SetVariety(XmlSchemaDatatype datatype) { XmlSchemaDatatypeVariety variety = datatype.Variety; if (variety == XmlSchemaDatatypeVariety.List) { typeRefFlags |= ClrTypeRefFlags.IsSchemaList; } else if (variety == XmlSchemaDatatypeVariety.Union) { typeRefFlags |= ClrTypeRefFlags.IsUnion; } }
public void ChangeType_NullValueArgumentInToStringTest() { XmlSchemaDatatype datatype = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Integer).Datatype; datatype.ChangeType(null, typeof(int)); }
public override bool IsDerivedFrom(XmlSchemaDatatype datatype) { if (datatype == null) { return false; } //Common case - Derived by restriction for(DatatypeImplementation dt = this; dt != null; dt = dt.baseType) { if (dt == datatype) { return true; } } if (((DatatypeImplementation)datatype).baseType == null) { //Both are built-in types Type derivedType = this.GetType(); Type baseType = datatype.GetType(); return baseType == derivedType || derivedType.IsSubclassOf(baseType); } else if (datatype.Variety == XmlSchemaDatatypeVariety.Union && !datatype.HasLexicalFacets && !datatype.HasValueFacets && variety != XmlSchemaDatatypeVariety.Union) { //base type is union (not a restriction of union) and derived type is not union return ((Datatype_union)datatype).IsUnionBaseOf(this); } else if ((variety == XmlSchemaDatatypeVariety.Union || variety == XmlSchemaDatatypeVariety.List) && restriction == null) { //derived type is union (not a restriction) return (datatype == anySimpleType.Datatype); } return false; }
public void ChangeType_NullTargetArgumentInFromStringTest() { XmlSchemaDatatype datatype = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Integer).Datatype; datatype.ChangeType("100", null); }
internal SimpleTypeValidator(XmlSchemaDatatypeVariety variety, XmlSchemaSimpleType type, FacetsChecker facetsChecker, RestrictionFacets facets) { this.restrictionFacets = facets; this.facetsChecker = facetsChecker; this.dataType = type.Datatype; this.variety = variety; }
public void ChangeType_NullNamespaceResolverArgumentInFromStringTest() { XmlSchemaDatatype datatype = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Integer).Datatype; datatype.ChangeType("100", typeof(string), null); }
protected void SetListValue(object value, XmlSchemaDatatype datatype) { string strValue = ListSimpleTypeValidator.ToString(value); this.GetUntyped().Value = strValue; }
protected void SetListElement(XName name, object value, XmlSchemaDatatype datatype) { SetElement(name, ListSimpleTypeValidator.ToString(value), datatype); }
public static bool?IsDataTypeAttributeAllowed(this XmlSchemaDatatype type, GeneratorConfiguration configuration) { return(IsDataTypeAttributeAllowed(type.TypeCode, configuration)); }
static XmlSchemaDataImporter () { XmlSchema s = new XmlSchema (); XmlSchemaAttribute a = new XmlSchemaAttribute (); a.Name = "foo"; a.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace); s.Items.Add (a); XmlSchemaAttribute b = new XmlSchemaAttribute (); b.Name = "bar"; b.SchemaTypeName = new XmlQualifiedName ("decimal", XmlSchema.Namespace); s.Items.Add (b); XmlSchemaElement e = new XmlSchemaElement (); e.Name = "bar"; s.Items.Add (e); s.Compile (null); schemaIntegerType = a.AttributeType as XmlSchemaDatatype; schemaDecimalType = b.AttributeType as XmlSchemaDatatype; schemaAnyType = e.ElementType as XmlSchemaComplexType; }