void ImportChoiceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaChoice choice, CodeIdentifiers classIds, bool multiValue)
{
XmlTypeMapElementInfoList choices = new XmlTypeMapElementInfoList ();
multiValue = ImportChoices (typeQName, null, choices, choice.Items) || multiValue;
if (choices.Count == 0) return;
if (choice.MaxOccurs > 1) multiValue = true;
XmlTypeMapMemberElement member;
if (multiValue)
{
member = new XmlTypeMapMemberFlatList ();
member.Name = classIds.AddUnique ("Items", member);
ListMap listMap = new ListMap ();
listMap.ItemInfo = choices;
((XmlTypeMapMemberFlatList)member).ListMap = listMap;
}
else
{
member = new XmlTypeMapMemberElement ();
member.Name = classIds.AddUnique ("Item", member);
}
// If all choices have the same type, use that type for the member.
// If not use System.Object.
// If there are at least two choices with the same type, use a choice
// identifier attribute
TypeData typeData = null;
bool twoEqual = false;
bool allEqual = true;
Hashtable types = new Hashtable ();
for (int n = choices.Count - 1; n >= 0; n--)
{
XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) choices [n];
// In some complex schemas, we may end up with several options
// with the same name. It is better to ignore the extra options
// than to crash. It's the best we can do, and btw it works
// better than in MS.NET.
if (cmap.GetElement (einfo.ElementName, einfo.Namespace, einfo.ExplicitOrder) != null ||
choices.IndexOfElement (einfo.ElementName, einfo.Namespace) != n)
{
choices.RemoveAt (n);
continue;
}
if (types.ContainsKey (einfo.TypeData)) twoEqual = true;
else types.Add (einfo.TypeData, einfo);
TypeData choiceType = einfo.TypeData;
if (choiceType.SchemaType == SchemaTypes.Class)
{
// When comparing class types, use the most generic class in the
// inheritance hierarchy
XmlTypeMapping choiceMap = GetTypeMapping (choiceType);
BuildPendingMap (choiceMap);
while (choiceMap.BaseMap != null) {
choiceMap = choiceMap.BaseMap;
BuildPendingMap (choiceMap);
choiceType = choiceMap.TypeData;
}
}
if (typeData == null) typeData = choiceType;
else if (typeData != choiceType) allEqual = false;
}
if (!allEqual)
typeData = TypeTranslator.GetTypeData (typeof(object));
if (twoEqual)
{
// Create the choice member
XmlTypeMapMemberElement choiceMember = new XmlTypeMapMemberElement ();
choiceMember.Ignore = true;
choiceMember.Name = classIds.AddUnique (member.Name + "ElementName", choiceMember);
member.ChoiceMember = choiceMember.Name;
// Create the choice enum
XmlTypeMapping enumMap = CreateTypeMapping (new XmlQualifiedName (member.Name + "ChoiceType", typeQName.Namespace), SchemaTypes.Enum, null);
enumMap.IncludeInSchema = false;
CodeIdentifiers codeIdents = new CodeIdentifiers ();
EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [choices.Count];
for (int n=0; n<choices.Count; n++)
{
XmlTypeMapElementInfo it =(XmlTypeMapElementInfo) choices[n];
bool extraNs = (it.Namespace != null && it.Namespace != "" && it.Namespace != typeQName.Namespace);
string xmlName = extraNs ? it.Namespace + ":" + it.ElementName : it.ElementName;
string enumName = codeIdents.AddUnique (CodeIdentifier.MakeValid (it.ElementName), it);
members [n] = new EnumMap.EnumMapMember (xmlName, enumName);
}
enumMap.ObjectMap = new EnumMap (members, false);
choiceMember.TypeData = multiValue ? enumMap.TypeData.ListTypeData : enumMap.TypeData;
choiceMember.ElementInfo.Add (CreateElementInfo (typeQName.Namespace, choiceMember, choiceMember.Name, choiceMember.TypeData, false, XmlSchemaForm.None, -1));
cmap.AddMember (choiceMember);
}
if (typeData == null)
return;
if (multiValue)
typeData = typeData.ListTypeData;
member.ElementInfo = choices;
member.Documentation = GetDocumentation (choice);
member.TypeData = typeData;
cmap.AddMember (member);
}