protected static long ToEnum(string value, Hashtable h, string typeName) { return(XmlCustomFormatter.ToEnum(value, h, typeName, true)); }
internal void AddKeyHash(System.Text.StringBuilder sb) { sb.Append("XA "); KeyHelper.AddField(sb, 1, xmlIgnore); KeyHelper.AddField(sb, 2, xmlns); #if !MOONLIGHT KeyHelper.AddField(sb, 3, xmlAnyAttribute != null); #endif xmlAnyElements.AddKeyHash(sb); xmlArrayItems.AddKeyHash(sb); xmlElements.AddKeyHash(sb); if (xmlArray != null) { xmlArray.AddKeyHash(sb); } if (xmlAttribute != null) { xmlAttribute.AddKeyHash(sb); } if (xmlDefaultValue == null) { sb.Append("n"); } else if (!(xmlDefaultValue is System.DBNull)) { string v = XmlCustomFormatter.ToXmlString(TypeTranslator.GetTypeData(xmlDefaultValue.GetType()), xmlDefaultValue); sb.Append("v" + v); } if (xmlEnum != null) { xmlEnum.AddKeyHash(sb); } if (xmlRoot != null) { xmlRoot.AddKeyHash(sb); } if (xmlText != null) { xmlText.AddKeyHash(sb); } if (xmlType != null) { xmlType.AddKeyHash(sb); } if (xmlChoiceIdentifier != null) { xmlChoiceIdentifier.AddKeyHash(sb); } sb.Append("|"); }
protected static char ToChar(string value) { return(XmlCustomFormatter.ToChar(value)); }
void AddDefaultValueAttribute(CodeMemberField field, CodeAttributeDeclarationCollection metadata, object value, TypeMapping mapping) { #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (!(mapping is PrimitiveMapping)) { throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Default value is invalid for " + mapping.GetType().Name)); } else if (mapping.IsList) { throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Default value is invalid for " + mapping.GetType().Name)); } #endif if (value == null) { return; } CodeExpression valueExpression = null; CodeExpression initExpression = null; CodeExpression typeofValue = null; string typeName = mapping.TypeDesc.FullName; Type type = value.GetType(); CodeAttributeArgument[] arguments = null; if (mapping is EnumMapping) { #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (value.GetType() != typeof(string)) { throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Invalid enumeration type " + value.GetType().Name)); } #endif if (((EnumMapping)mapping).IsFlags) { string[] values = ((string)value).Split(null); for (int i = 0; i < values.Length; i++) { if (values[i].Length == 0) { continue; } CodeExpression enumRef = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeName), values[i]); if (valueExpression != null) { valueExpression = new CodeBinaryOperatorExpression(valueExpression, CodeBinaryOperatorType.BitwiseOr, enumRef); } else { valueExpression = enumRef; } } } else { valueExpression = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeName), (string)value); } initExpression = valueExpression; arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(valueExpression) }; } else if (type == typeof(bool) || type == typeof(Int32) || type == typeof(string) || type == typeof(double)) { initExpression = valueExpression = new CodePrimitiveExpression(value); arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(valueExpression) }; } else if (type == typeof(Int16) || type == typeof(Int64) || type == typeof(float) || type == typeof(byte) || type == typeof(decimal)) { valueExpression = new CodePrimitiveExpression(value.ToString()); typeofValue = new CodeTypeOfExpression(CodeIdentifier.EscapeKeywords(type.FullName)); arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression) }; initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value)); } else if (type == typeof(sbyte) || type == typeof(UInt16) || type == typeof(UInt32) || type == typeof(UInt64)) { // need to promote the non-CLS complient types value = PromoteType(type, value); valueExpression = new CodePrimitiveExpression(value.ToString()); typeofValue = new CodeTypeOfExpression(CodeIdentifier.EscapeKeywords(type.FullName)); arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression) }; initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value)); } else if (type == typeof(DateTime)) { DateTime dt = (DateTime)value; string dtString; long ticks; if (mapping.TypeDesc.FormatterName == "Date") { dtString = XmlCustomFormatter.FromDate(dt); ticks = (new DateTime(dt.Year, dt.Month, dt.Day)).Ticks; } else if (mapping.TypeDesc.FormatterName == "Time") { dtString = XmlCustomFormatter.FromDateTime(dt); ticks = dt.Ticks; } else { dtString = XmlCustomFormatter.FromDateTime(dt); ticks = dt.Ticks; } valueExpression = new CodePrimitiveExpression(dtString); typeofValue = new CodeTypeOfExpression(CodeIdentifier.EscapeKeywords(type.FullName)); arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression) }; initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(DateTime)), new CodeExpression[] { new CodePrimitiveExpression(ticks) }); } if (arguments != null) { if (field != null) { field.InitExpression = initExpression; } AddCustomAttribute(metadata, typeof(DefaultValueAttribute), arguments); } }
static internal string ExportDefaultValue(TypeMapping mapping, object value) { #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (!(mapping is PrimitiveMapping)) { throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default")); } else if (mapping.IsList) { throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default")); } #endif if (mapping is EnumMapping) { EnumMapping em = (EnumMapping)mapping; #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (value.GetType() != typeof(string)) { throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), value.GetType().FullName))); } #endif // check the validity of the value ConstantMapping[] c = em.Constants; if (em.IsFlags) { string[] names = new string[c.Length]; long[] ids = new long[c.Length]; Hashtable values = new Hashtable(); for (int i = 0; i < c.Length; i++) { names[i] = c[i].XmlName; ids[i] = 1 << i; values.Add(c[i].Name, ids[i]); } long val = XmlCustomFormatter.ToEnum((string)value, values, em.TypeName, false); return(val != 0 ? XmlCustomFormatter.FromEnum(val, names, ids) : null); } else { for (int i = 0; i < c.Length; i++) { if (c[i].Name == (string)value) { return(c[i].XmlName); } } return(null); // unknown value } } PrimitiveMapping pm = (PrimitiveMapping)mapping; if (!pm.TypeDesc.HasCustomFormatter) { if (pm.TypeDesc.FormatterName == "String") { return((string)value); } Type formatter = typeof(XmlConvert); Type valueType = Type.GetType(pm.TypeDesc.FullName, false); System.Reflection.MethodInfo format = formatter.GetMethod("ToString", new Type[] { valueType }); if (format != null) { return((string)format.Invoke(formatter, new Object[] { value })); } } else { string defaultValue = XmlCustomFormatter.FromDefaultValue(value, pm.TypeDesc.FormatterName); if (defaultValue == null) { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), pm.TypeDesc.Name)); } return(defaultValue); } throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), pm.TypeDesc.Name)); }
protected static string ToXmlNmTokens(string value) { return(XmlCustomFormatter.ToXmlNmTokens(value)); }
protected void WritePotentiallyReferencingElement(string n, string ns, object o, Type ambientType, bool suppressReference, bool isNullable) { if (o == null) { if (isNullable) { WriteNullTagEncoded(n, ns); } return; } WriteStartElement(n, ns, true); CheckReferenceQueue(); if (callbacks != null && callbacks.ContainsKey(o.GetType())) { WriteCallbackInfo info = (WriteCallbackInfo)callbacks[o.GetType()]; if (o.GetType().IsEnum) { info.Callback(o); } else if (suppressReference) { Writer.WriteAttributeString("id", GetId(o, false)); if (ambientType != o.GetType()) { WriteXsiType(info.TypeName, info.TypeNs); } info.Callback(o); } else { if (!AlreadyQueued(o)) { referencedElements.Enqueue(o); } Writer.WriteAttributeString("href", "#" + GetId(o, true)); } } else { // Must be a primitive type or array of primitives TypeData td = TypeTranslator.GetTypeData(o.GetType()); if (td.SchemaType == SchemaTypes.Primitive) { WriteXsiType(td.XmlType, XmlSchema.Namespace); Writer.WriteString(XmlCustomFormatter.ToXmlString(td, o)); } else if (IsPrimitiveArray(td)) { if (!AlreadyQueued(o)) { referencedElements.Enqueue(o); } Writer.WriteAttributeString("href", "#" + GetId(o, true)); } else { throw new InvalidOperationException("Invalid type: " + o.GetType().FullName); } } WriteEndElement(); }
protected static string FromXmlNCName(string ncName) { return(XmlCustomFormatter.FromXmlNCName(ncName)); }
protected static string FromXmlNmTokens(string nmTokens) { return(XmlCustomFormatter.FromXmlNmTokens(nmTokens)); }
protected static string FromEnum(long value, string[] values, long[] ids) { return(XmlCustomFormatter.FromEnum(value, values, ids)); }
protected static string FromTime(DateTime value) { return(XmlCustomFormatter.FromTime(value)); }
protected static string FromChar(char value) { return(XmlCustomFormatter.FromChar(value)); }
protected static string FromByteArrayHex(byte[] value) { return(XmlCustomFormatter.FromByteArrayHex(value)); }
XmlSchemaParticle GetSchemaElement(XmlSchema currentSchema, XmlTypeMapElementInfo einfo, object defaultValue, bool isTypeMember) { if (einfo.IsTextElement) { return(null); } if (einfo.IsUnnamedAnyElement) { XmlSchemaAny any = new XmlSchemaAny(); any.MinOccurs = 0; any.MaxOccurs = 1; return(any); } XmlSchemaElement selem = new XmlSchemaElement(); if (isTypeMember) { selem.MaxOccurs = 1; selem.MinOccurs = einfo.IsNullable ? 1 : 0; if ((einfo.TypeData.IsValueType && einfo.Member != null && !einfo.Member.IsOptionalValueType) || encodedFormat) { selem.MinOccurs = 1; } } XmlSchema memberSchema = null; if (!encodedFormat) { memberSchema = GetSchema(einfo.Namespace); ImportNamespace(currentSchema, einfo.Namespace); } if (currentSchema == memberSchema || encodedFormat || !isTypeMember) { if (isTypeMember) { selem.IsNillable = einfo.IsNullable; } selem.Name = einfo.ElementName; XmlQualifiedName typeName = new XmlQualifiedName(einfo.TypeData.XmlType, einfo.DataTypeNamespace); if (defaultValue != System.DBNull.Value) { selem.DefaultValue = XmlCustomFormatter.ToXmlString(einfo.TypeData, defaultValue); } if (einfo.Form != XmlSchemaForm.Qualified) { selem.Form = einfo.Form; } switch (einfo.TypeData.SchemaType) { case SchemaTypes.XmlNode: selem.SchemaType = GetSchemaXmlNodeType(); break; case SchemaTypes.XmlSerializable: selem.SchemaType = GetSchemaXmlSerializableType(); break; case SchemaTypes.Enum: selem.SchemaTypeName = new XmlQualifiedName(einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace); ImportNamespace(currentSchema, einfo.MappedType.XmlTypeNamespace); ExportEnumSchema(einfo.MappedType); break; case SchemaTypes.Array: XmlQualifiedName atypeName = ExportArraySchema(einfo.MappedType, currentSchema.TargetNamespace); selem.SchemaTypeName = atypeName; ImportNamespace(currentSchema, atypeName.Namespace); break; case SchemaTypes.Class: if (einfo.MappedType.TypeData.Type != typeof(object)) { selem.SchemaTypeName = new XmlQualifiedName(einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace); ImportNamespace(currentSchema, einfo.MappedType.XmlTypeNamespace); } else if (encodedFormat) { selem.SchemaTypeName = new XmlQualifiedName(einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace); } ExportClassSchema(einfo.MappedType); break; case SchemaTypes.Primitive: selem.SchemaTypeName = new XmlQualifiedName(einfo.TypeData.XmlType, einfo.DataTypeNamespace);; break; } } else { selem.RefName = new XmlQualifiedName(einfo.ElementName, einfo.Namespace); foreach (XmlSchemaObject ob in memberSchema.Items) { if (ob is XmlSchemaElement && ((XmlSchemaElement)ob).Name == einfo.ElementName) { return(selem); } } memberSchema.Items.Add(GetSchemaElement(memberSchema, einfo, defaultValue, false)); } return(selem); }
protected static DateTime ToTime(string value) { return(XmlCustomFormatter.ToTime(value)); }
protected void WriteEmptyTag(string name, string ns) { name = XmlCustomFormatter.FromXmlName(name); WriteStartElement(name, ns); WriteEndElement(); }
protected static string ToXmlNCName(string value) { return(XmlCustomFormatter.ToXmlNCName(value)); }
protected void WriteStartElement(string name, string ns, object o, bool writePrefixed) { if (o != null) { if (serializedObjects.Contains(o)) { throw new InvalidOperationException("A cirtular reference was detected while serializing an object of type " + o.GetType().Name); } else { serializedObjects [o] = o; } } WriteState oldState = Writer.WriteState; string prefix = null; if (topLevelElement && ns != null && ns.Length != 0) { foreach (XmlQualifiedName qn in namespaces) { if (qn.Namespace == ns) { prefix = qn.Name; writePrefixed = true; break; } } } if (writePrefixed && ns != string.Empty) { name = XmlCustomFormatter.FromXmlName(name); if (prefix == null) { prefix = Writer.LookupPrefix(ns); } if (prefix == null || prefix.Length == 0) { prefix = "q" + (++qnameCount); } Writer.WriteStartElement(prefix, name, ns); } else { Writer.WriteStartElement(name, ns); } if (topLevelElement) { if (namespaces != null) { foreach (XmlQualifiedName qn in namespaces) { string currentPrefix = Writer.LookupPrefix(qn.Namespace); if (currentPrefix != null && currentPrefix.Length != 0) { continue; } WriteAttribute("xmlns", qn.Name, xmlNamespace, qn.Namespace); } } topLevelElement = false; } }
private CodeAttributeArgument[] GetDefaultValueArguments(PrimitiveMapping mapping, object value, out CodeExpression initExpression) { initExpression = null; if (value == null) { return(null); } CodeExpression left = null; CodeExpression expression2 = null; Type type = value.GetType(); CodeAttributeArgument[] argumentArray = null; if (mapping is EnumMapping) { if (((EnumMapping)mapping).IsFlags) { string[] strArray = ((string)value).Split(null); for (int i = 0; i < strArray.Length; i++) { if (strArray[i].Length != 0) { CodeExpression right = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), strArray[i]); if (left != null) { left = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.BitwiseOr, right); } else { left = right; } } } } else { left = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), (string)value); } initExpression = left; argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(left) }; } else if (((type == typeof(bool)) || (type == typeof(int))) || ((type == typeof(string)) || (type == typeof(double)))) { initExpression = left = new CodePrimitiveExpression(value); argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(left) }; } else if (((type == typeof(short)) || (type == typeof(long))) || (((type == typeof(float)) || (type == typeof(byte))) || (type == typeof(decimal)))) { left = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo)); expression2 = new CodeTypeOfExpression(type.FullName); argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) }; initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value)); } else if (((type == typeof(sbyte)) || (type == typeof(ushort))) || ((type == typeof(uint)) || (type == typeof(ulong)))) { value = CodeExporter.PromoteType(type, value); left = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo)); expression2 = new CodeTypeOfExpression(type.FullName); argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) }; initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value)); } else if (type == typeof(DateTime)) { string str; long ticks; DateTime time = (DateTime)value; if (mapping.TypeDesc.FormatterName == "Date") { str = XmlCustomFormatter.FromDate(time); DateTime time2 = new DateTime(time.Year, time.Month, time.Day); ticks = time2.Ticks; } else if (mapping.TypeDesc.FormatterName == "Time") { str = XmlCustomFormatter.FromDateTime(time); ticks = time.Ticks; } else { str = XmlCustomFormatter.FromDateTime(time); ticks = time.Ticks; } left = new CodePrimitiveExpression(str); expression2 = new CodeTypeOfExpression(type.FullName); argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) }; initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(DateTime)), new CodeExpression[] { new CodePrimitiveExpression(ticks) }); } else if (type == typeof(Guid)) { left = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo)); expression2 = new CodeTypeOfExpression(type.FullName); argumentArray = new CodeAttributeArgument[] { new CodeAttributeArgument(expression2), new CodeAttributeArgument(left) }; initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(Guid)), new CodeExpression[] { left }); } if ((mapping.TypeDesc.FullName != type.ToString()) && !(mapping is EnumMapping)) { initExpression = new CodeCastExpression(mapping.TypeDesc.FullName, initExpression); } return(argumentArray); }
object ReadTypedPrimitive(XmlQualifiedName qname, bool reportUnknown) { if (qname == null) { qname = GetXsiType(); } TypeData typeData = TypeTranslator.FindPrimitiveTypeData(qname.Name); if (typeData == null || typeData.SchemaType != SchemaTypes.Primitive) { #if MOONLIGHT // skip everything reader.Skip(); return(new Object()); #else // Put everything into a node array readCount++; XmlNode node = Document.ReadNode(reader); if (reportUnknown) { OnUnknownNode(node, null, null); } if (node.ChildNodes.Count == 0 && node.Attributes.Count == 0) { return(new Object()); } XmlElement elem = node as XmlElement; if (elem == null) { return new XmlNode[] { node } } ; else { XmlNode[] nodes = new XmlNode[elem.Attributes.Count + elem.ChildNodes.Count]; int n = 0; foreach (XmlNode no in elem.Attributes) { nodes[n++] = no; } foreach (XmlNode no in elem.ChildNodes) { nodes[n++] = no; } return(nodes); } #endif } if (typeData.Type == typeof(XmlQualifiedName)) { return(ReadNullableQualifiedName()); } readCount++; return(XmlCustomFormatter.FromXmlString(typeData, Reader.ReadElementString())); }