|
using System;
using System.Xml.Schema;
namespace Puma.Xml
{
internal class ValueConstructor
{
internal static string ConstructDefaultValue(XsdNode XsdNode, ConstructParams.ConstructDefaultValueParams ConstructDefaultValueParams)
{
System.Diagnostics.Debug.Assert(!(XsdNode.XmlSchemaElement.ElementType is XmlSchemaComplexType));
if (ConstructDefaultValueParams.ValueProvider == null)
{
string defVal;
defVal = XsdNode.XmlSchemaElement.DefaultValue;
if (defVal != null) return defVal;
defVal = TreateSpecialCases(XsdNode, ConstructDefaultValueParams);
if (defVal != null) return defVal;
defVal = GetMinValue(XsdNode.XsdSimpleType != null ? XsdNode.XsdSimpleType.DataType : XsdNode.XmlSchemaElement.ElementType as XmlSchemaDatatype);
if (defVal != null) return defVal;
return null;
}
else
{
System.Type type = XsdNode.XsdSimpleType.ValueType;
System.Diagnostics.Debug.Assert(type != null);
if (type == typeof(string))
{
System.Diagnostics.Debug.Assert(XsdNode.XsdSimpleType.Restriction != null);
int maxLen = XsdNode.XsdSimpleType.Restriction.MaxLengthRestriction;
System.Diagnostics.Debug.Assert(maxLen != -1);
byte[] buf = new byte[maxLen];
int i = ConstructDefaultValueParams.ValueProvider.Read(buf, 0, maxLen);
System.Diagnostics.Debug.Assert(i == maxLen);
return Puma.Converters.ByteArrayConverter.ConvertToString(TrimZeroBytes(buf));
}
else
{
int sz = Puma.Converters.ObjectInfo.GetSizeOf(type);
System.Diagnostics.Debug.Assert(sz != -1);
byte[] buf = new byte[sz];
int i = ConstructDefaultValueParams.ValueProvider.Read(buf, 0, sz);
System.Diagnostics.Debug.Assert(i == sz);
object obj = Puma.Converters.ByteArrayConverter.ConvertTo(buf, type);
//Else not found appropeiated converter
System.Diagnostics.Debug.Assert(obj != null);
return obj.ToString();
}
}
}
private static byte[] TrimZeroBytes(byte[] SrcArray)
{
System.Collections.ArrayList al = new System.Collections.ArrayList(SrcArray);
int i = al.IndexOf((byte)0);
al.RemoveRange(i, al.Count - i);
return (byte[])al.ToArray(typeof(byte));
}
private static string GetMinValue(XmlSchemaDatatype XmlSchemaDatatype)
{
if (XmlSchemaDatatype == null) return null;
System.Diagnostics.Debug.Assert(XmlSchemaDatatype.ValueType != null);
if (!XmlSchemaDatatype.ValueType.IsValueType) return null;
System.Reflection.FieldInfo fieldInfo = XmlSchemaDatatype.ValueType.GetField("MinValue");
System.Diagnostics.Debug.Assert(fieldInfo != null);
return fieldInfo.GetValue(XmlSchemaDatatype).ToString();
}
private static string TreateSpecialCases(XsdNode XsdNode, ConstructParams.ConstructDefaultValueParams ConstructDefaultValueParams)
{
if (XsdNode.XsdSimpleType == null) return null;
EnumerationRestriction enumerationRestriction = XsdNode.XsdSimpleType.Restriction.EnumerationRestriction;
int minLength = XsdNode.XsdSimpleType.Restriction.MinLengthRestriction;
//case 1
if (minLength != -1)
{
return new string(ConstructDefaultValueParams != null ? ConstructDefaultValueParams.StringFillChar : ' ', minLength);
}
//case 2
if (enumerationRestriction != null)
{
return (string)enumerationRestriction.Values[0];
}
return null;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.