Beispiel #1
0
 private void UpdateAttributeValueType(XmlToClrImporter.ClrTypeDescription type, string name, string value)
 {
     XmlToClrImporter.ClrPropertyDescription propertyDescription = type[name];
     XmlToClrImporter.ClrTypeDescription     existingType        = (XmlToClrImporter.ClrTypeDescription)null;
     if (propertyDescription != null)
     {
         propertyDescription.CountOccurence();
         existingType = propertyDescription.PropertyType;
     }
     if (existingType == this.stringTypeDescription)
     {
         return;
     }
     XmlToClrImporter.ClrTypeDescription newType = XmlToClrImporter.ClrTypeDescription.EmptyAttribute;
     if (!string.IsNullOrEmpty(value))
     {
         newType = this.GetOrCreateLeafDescription(this.xmlToClrAdapter.InferValueType(value));
     }
     XmlToClrImporter.ClrTypeDescription description = this.ReconcilePropertyType(existingType, newType);
     if (propertyDescription == null)
     {
         type.AddProperty(name, description);
     }
     else
     {
         propertyDescription.PropertyType = description;
     }
 }
Beispiel #2
0
 private XmlToClrImporter.ClrTypeDescription GetOrCreateEmptyDescription(string name)
 {
     XmlToClrImporter.ClrTypeDescription empty = this.compositeTypes.Find((Predicate <XmlToClrImporter.ClrTypeDescription>)(t => name == t.Name));
     if (empty == null)
     {
         empty = XmlToClrImporter.ClrTypeDescription.CreateEmpty(name);
         this.compositeTypes.Add(empty);
     }
     return(empty);
 }
Beispiel #3
0
 private XmlToClrImporter.ClrTypeDescription GetOrCreateLeafDescription(IType leafType)
 {
     XmlToClrImporter.ClrTypeDescription leaf = this.leafTypes.Find((Predicate <XmlToClrImporter.ClrTypeDescription>)(t => t.Type == leafType));
     if (leaf == null)
     {
         leaf = XmlToClrImporter.ClrTypeDescription.CreateLeaf(leafType);
         this.leafTypes.Add(leaf);
     }
     return(leaf);
 }
Beispiel #4
0
 private XmlToClrImporter.ClrTypeDescription ReconcilePropertyType(XmlToClrImporter.ClrTypeDescription existingType, XmlToClrImporter.ClrTypeDescription newType)
 {
     if (existingType == newType)
     {
         return(existingType);
     }
     if (existingType == null || existingType == XmlToClrImporter.ClrTypeDescription.EmptyAttribute)
     {
         return(newType);
     }
     if (newType.Category == XmlToClrImporter.ClrTypeCategory.Empty)
     {
         return(existingType);
     }
     if (existingType.Category == XmlToClrImporter.ClrTypeCategory.Empty)
     {
         foreach (XmlToClrImporter.ClrTypeDescription clrTypeDescription in this.compositeTypes)
         {
             if (clrTypeDescription.Category == XmlToClrImporter.ClrTypeCategory.Composite)
             {
                 clrTypeDescription.Properties.ForEach((Action <XmlToClrImporter.ClrPropertyDescription>)(prop =>
                 {
                     if (prop.PropertyType != existingType)
                     {
                         return;
                     }
                     prop.PropertyType = newType;
                 }));
             }
         }
         this.compositeTypes.Remove(existingType);
         return(newType);
     }
     if (existingType.Category == XmlToClrImporter.ClrTypeCategory.Leaf != (newType.Category == XmlToClrImporter.ClrTypeCategory.Leaf))
     {
         throw new InvalidDataException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, ExceptionStringTable.XmlOneNodeUsageNotSupported, new object[1]
         {
             (object)(existingType.Category != XmlToClrImporter.ClrTypeCategory.Leaf ? existingType.Name : newType.Name)
         }));
     }
     return(this.stringTypeDescription);
 }
Beispiel #5
0
        private IType GetOrCreateCollectionType(XmlToClrImporter.ClrTypeDescription description, string collectionTypeBaseName)
        {
            string collectionTypeName = collectionTypeBaseName + this.xmlToClrAdapter.CollectionSuffix;

            if (this.compositeTypes.Find((Predicate <XmlToClrImporter.ClrTypeDescription>)(t => collectionTypeName == t.Name)) != null)
            {
                throw new InvalidDataException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, ExceptionStringTable.XmlTwoNodesUsageNotSupported, new object[2]
                {
                    (object)collectionTypeBaseName,
                    (object)collectionTypeName
                }));
            }
            IType type = this.collectionTypes.Find((Predicate <IType>)(t => collectionTypeName == t.Name));

            if (type != null)
            {
                return(type);
            }
            IType itemType;

            if (description.IsCollectionWrapper)
            {
                XmlToClrImporter.ClrPropertyDescription property = description.Properties[0];
                itemType = property.PropertyType != description?this.GetPropertyType(property) : description.Type;
            }
            else
            {
                itemType = description.Type;
            }
            if (itemType == this.rootType)
            {
                throw new InvalidDataException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, ExceptionStringTable.XmlRecursiveRootTypeNotSupported, new object[1]
                {
                    (object)this.rootType.Name
                }));
            }
            IType collectionType = this.xmlToClrAdapter.CreateCollectionType(collectionTypeName, itemType);

            this.collectionTypes.Add(collectionType);
            return(collectionType);
        }
Beispiel #6
0
 private XmlToClrImporter.ClrTypeDescription GetOrCreateCompositeDescription(string name)
 {
     XmlToClrImporter.ClrTypeDescription composite = this.compositeTypes.Find((Predicate <XmlToClrImporter.ClrTypeDescription>)(t => name == t.Name));
     if (composite == null)
     {
         composite = XmlToClrImporter.ClrTypeDescription.CreateComposite(name);
         this.compositeTypes.Add(composite);
     }
     else if (composite.Category == XmlToClrImporter.ClrTypeCategory.Empty)
     {
         composite.ConvertEmptyToComposite();
     }
     else if (composite == this.rootType)
     {
         throw new InvalidDataException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, ExceptionStringTable.XmlRecursiveRootTypeNotSupported, new object[1]
         {
             (object)this.rootType.Name
         }));
     }
     return(composite);
 }
Beispiel #7
0
 private void ImportFromFile(string xmlFileName)
 {
     PerformanceUtility.StartPerformanceSequence(PerformanceEvent.XmlToClrImport);
     try
     {
         XmlDocument xmlDocument = new XmlDocument();
         xmlDocument.Load(xmlFileName);
         XmlElement element1 = (XmlElement)Enumerable.FirstOrDefault <XmlNode>(Enumerable.OfType <XmlNode>((IEnumerable)xmlDocument.ChildNodes), (Func <XmlNode, bool>)(node => node is XmlElement));
         if (element1 == null || !this.xmlToClrAdapter.ShouldProcessElement(element1.LocalName, element1.Prefix, element1.NamespaceURI))
         {
             throw new InvalidDataException(ExceptionStringTable.EmptyXmlDocument);
         }
         XmlToClrImporter.FilteredElement element2 = new XmlToClrImporter.FilteredElement(element1, this.xmlToClrAdapter);
         this.xmlToClrAdapter.Initialize(element2.Name);
         this.rootType = this.GetOrCreateCompositeDescription(element2.Name);
         if (!string.IsNullOrEmpty(this.xmlToClrAdapter.ExpectedRootTypeName) && this.xmlToClrAdapter.ExpectedRootTypeName != this.rootType.Name)
         {
             throw new InvalidDataException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, ExceptionStringTable.SampleDataXmlReimportWrongRootType, new object[2]
             {
                 (object)this.rootType.Name,
                 (object)this.xmlToClrAdapter.ExpectedRootTypeName
             }));
         }
         this.ParseType(this.rootType, element2);
         this.NormalizeTypeDescriptions();
         this.CreateClrTypes();
         DocumentCompositeNode compositeNode = this.xmlToClrAdapter.CreateCompositeNode(this.rootType.Type);
         if (compositeNode == null)
         {
             return;
         }
         this.ParseValues(compositeNode, element2);
         this.xmlToClrAdapter.Finalize(true);
     }
     finally
     {
         this.xmlToClrAdapter.Finalize(false);
         PerformanceUtility.EndPerformanceSequence(PerformanceEvent.XmlToClrImport);
     }
 }
Beispiel #8
0
 private void ParseType(XmlToClrImporter.ClrTypeDescription description, XmlToClrImporter.FilteredElement element)
 {
     description.ResetPropertyOccurenceCounts();
     if (element.HasAttributes)
     {
         foreach (XmlAttribute xmlAttribute in element.Attributes)
         {
             string clrName = this.xmlToClrAdapter.GetClrName(xmlAttribute.LocalName, xmlAttribute.Prefix, xmlAttribute.NamespaceURI);
             this.UpdateAttributeValueType(description, clrName, xmlAttribute.Value);
         }
     }
     if (!element.HasChildElements)
     {
         return;
     }
     foreach (XmlElement element1 in element.ChildElements)
     {
         XmlToClrImporter.FilteredElement        element2            = new XmlToClrImporter.FilteredElement(element1, this.xmlToClrAdapter);
         XmlToClrImporter.ClrPropertyDescription propertyDescription = description[element2.Name];
         XmlToClrImporter.ClrTypeDescription     existingType        = (XmlToClrImporter.ClrTypeDescription)null;
         if (propertyDescription != null)
         {
             propertyDescription.CountOccurence();
             existingType = propertyDescription.PropertyType;
         }
         XmlToClrImporter.ClrTypeDescription clrTypeDescription;
         if (element2.IsLeaf)
         {
             if (existingType != this.stringTypeDescription)
             {
                 string leafValue = element2.LeafValue;
                 if (!string.IsNullOrEmpty(leafValue))
                 {
                     clrTypeDescription = this.GetOrCreateLeafDescription(this.xmlToClrAdapter.InferValueType(leafValue));
                 }
                 else if (existingType == null)
                 {
                     clrTypeDescription = this.GetOrCreateEmptyDescription(element2.Name);
                 }
                 else
                 {
                     continue;
                 }
             }
             else
             {
                 continue;
             }
         }
         else
         {
             clrTypeDescription = this.GetOrCreateCompositeDescription(element2.Name);
             this.ParseType(clrTypeDescription, element2);
         }
         if (propertyDescription == null)
         {
             propertyDescription = description[element2.Name];
             if (propertyDescription != null)
             {
                 existingType = propertyDescription.PropertyType;
             }
         }
         XmlToClrImporter.ClrTypeDescription description1 = this.ReconcilePropertyType(existingType, clrTypeDescription);
         if (propertyDescription == null)
         {
             description.AddProperty(element2.Name, description1);
         }
         else
         {
             propertyDescription.PropertyType = description1;
         }
     }
 }
Beispiel #9
0
 public XmlToClrImporter.ClrPropertyDescription AddProperty(string name, XmlToClrImporter.ClrTypeDescription description)
 {
     XmlToClrImporter.ClrPropertyDescription propertyDescription = new XmlToClrImporter.ClrPropertyDescription(name, description);
     this.Properties.Add(propertyDescription);
     return(propertyDescription);
 }
Beispiel #10
0
 public ClrPropertyDescription(string name, XmlToClrImporter.ClrTypeDescription description)
 {
     this.Name         = name;
     this.PropertyType = description;
     this.count        = 1;
 }
Beispiel #11
0
 private XmlToClrImporter(IXmlToClrAdapter resolver)
 {
     this.xmlToClrAdapter       = resolver;
     this.stringTypeDescription = XmlToClrImporter.ClrTypeDescription.CreateLeaf(this.xmlToClrAdapter.StringType);
     this.leafTypes.Add(this.stringTypeDescription);
 }