Пример #1
0
        private void EndElement(XmlReader input, XmlNodeType prevInputType, string prevInputName, out IPersistIfcEntity writeEntity)
        {
            try
            {
                // before end element, we need to deal with SetCollection
                if (_currentNode is XmlCollectionProperty)
                {
                    // SetCollection will handle SetValue for Collection
                    CollectionType CType = ((XmlCollectionProperty)_currentNode).CType;
                    switch (CType)
                    {
                    case CollectionType.List:
                    case CollectionType.ListUnique:
                        ((XmlCollectionProperty)_currentNode).Entities.Sort(XmlCollectionProperty.CompareNodes);
                        break;

                    case CollectionType.Set:
                        break;

                    default:
                        throw new Exception("Unknown list type, " + CType);
                    }

                    foreach (XmlNode item in ((XmlCollectionProperty)_currentNode).Entities)
                    {
                        if (item is XmlEntity)
                        {
                            XmlEntity             node               = (XmlEntity)item;
                            XmlEntity             collectionOwner    = item.Parent.Parent as XmlEntity;
                            XmlCollectionProperty collection         = item.Parent as XmlCollectionProperty; //the collection to add to;
                            IPersistIfc           ifcCollectionOwner = collectionOwner.Entity;
                            PropertyValue         pv = new PropertyValue();
                            pv.Init(node.Entity);
                            ifcCollectionOwner.IfcParse(collection.PropertyIndex - 1, pv);
                        }
                    }
                }
                else if (_currentNode.Parent is XmlProperty)
                {
                    XmlProperty propNode = (XmlProperty)_currentNode.Parent;
                    if (_currentNode is XmlEntity)
                    {
                        XmlEntity entityNode = (XmlEntity)_currentNode;
                        propNode.SetValue(entityNode.Entity);
                    }
                    else if (_currentNode is XmlExpressType)
                    {
                        //create ExpressType, call ifcparse with propindex and object
                        //((XmlProperty)_currentNode.Parent).SetValue((XmlExpressType)_currentNode);

                        XmlExpressType expressNode = (XmlExpressType)_currentNode;
                        if (expressNode.Type != propNode.Property.PropertyType)
                        {
                            //propNode.SetValue(expressNode);
                            IfcType ifcType;
                            if (IsIfcType(input.LocalName, out ifcType))
                            //we have an IPersistIfc
                            {
                                IPersistIfc ent;
                                object[]    param = new object[1];
                                param[0] = expressNode.Value;
                                ent      = (IPersistIfc)Activator.CreateInstance(ifcType.Type, param);

                                propNode.SetValue(ent);
                            }
                        }
                        else
                        {
                            propNode.SetValue(expressNode.Value, primitives[expressNode.Type.Name]);
                        }
                    }
                    else if (_currentNode is XmlBasicType)
                    {
                        //set PropertyValue to write type boolean, integer, call ifcparse with string

                        XmlBasicType basicNode = (XmlBasicType)_currentNode;
                        propNode.SetValue(basicNode.Value, basicNode.Type);
                    }
                }


                else if (prevInputType == XmlNodeType.Element && prevInputName == input.LocalName &&
                         _currentNode is XmlProperty && _currentNode.Parent != null && _currentNode.Parent is XmlEntity)
                {
                    // WE SHOULDNT EXECUTE THE FOLLOWING CODE IF THIS PROPERTY ALREADY CALLED SETVALUE
                    XmlProperty   node    = (XmlProperty)_currentNode;
                    PropertyValue propVal = new PropertyValue();
                    Type          t       = node.Property.PropertyType;
                    if (t != null && t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
                    {
                        t = Nullable.GetUnderlyingType(t);
                    }
                    ExpressType et = null;
                    if (t != null && typeof(ExpressType).IsAssignableFrom(t))
                    {
                        et = (ExpressType)(Activator.CreateInstance(t));
                    }

                    IfcParserType pt = IfcParserType.Undefined;
                    if (et != null)
                    {
                        pt = primitives[et.UnderlyingSystemType.Name];
                    }
                    else
                    {
                        if (t.IsEnum)
                        {
                            pt = IfcParserType.Enum;
                        }
                        else if (primitives.ContainsKey(t.Name))
                        {
                            pt = primitives[t.Name];
                        }
                    }

                    if (pt != IfcParserType.Undefined)
                    {
                        if (pt.ToString().ToLower() == "string")
                        {
                            propVal.Init("'" + input.Value + "'", pt);
                        }
                        else
                        {
                            if (pt.ToString().ToLower() == "boolean")
                            {
                                propVal.Init(Convert.ToBoolean(input.Value) ? ".T." : ".F", pt);
                            }
                            else
                            {
                                propVal.Init(input.Value, pt);
                            }
                        }

                        ((XmlEntity)node.Parent).Entity.IfcParse(node.PropertyIndex - 1, propVal);
                    }
                }

                else if (_currentNode.Parent is XmlCollectionProperty && !(_currentNode.Parent is XmlUosCollection))
                {
                    if (_currentNode is XmlEntity)
                    {
                        ((XmlCollectionProperty)_currentNode.Parent).Entities.Add(((XmlEntity)_currentNode));
                    }
                    else if (_currentNode is XmlExpressType)
                    {
                        XmlExpressType expressNode = (XmlExpressType)_currentNode;
                        //actualEntityType is the actual type of the value to create
                        Type actualEntityType = expressNode.Type;
                        //Determine if the Express Type is a Nullable, if so get the type of the Nullable
                        if (actualEntityType.IsGenericType && actualEntityType.GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
                        {
                            actualEntityType = Nullable.GetUnderlyingType(actualEntityType);
                        }

                        //need to resolve what the Parser type is
                        //if the generic type of the collection is different from the actualEntityType then we need to create an entity and call Ifc Parse
                        //otherwise we need to call Ifcparse with a string value and the type of the underlying type
                        XmlCollectionProperty collection           = _currentNode.Parent as XmlCollectionProperty; //the collection to add to;
                        Type          collectionValueType          = collection.Property.PropertyType;
                        Type          collectionGenericType        = GetItemTypeFromGenericType(collectionValueType);;
                        bool          genericTypeIsSameAsValueType = (collectionGenericType == actualEntityType);
                        PropertyValue pv = new PropertyValue();

                        if (genericTypeIsSameAsValueType) //call IfcParse with string value and parser type
                        {
                            ExpressType actualEntityValue = (ExpressType)(Activator.CreateInstance(actualEntityType));
                            //resolve the underlying type
                            IfcParserType parserType = primitives[actualEntityValue.UnderlyingSystemType.Name];
                            if (parserType == IfcParserType.String)
                            {
                                pv.Init("'" + expressNode.Value + "'", parserType);
                            }
                            else
                            {
                                pv.Init(expressNode.Value, parserType);
                            }
                        }
                        else //call IfcParse with an entity
                        {
                            object[] param = new object[1];
                            param[0] = expressNode.Value;
                            ExpressType actualEntityValue = (ExpressType)(Activator.CreateInstance(expressNode.Type, param));
                            pv.Init(actualEntityValue);
                        }

                        XmlEntity   collectionOwner    = _currentNode.Parent.Parent as XmlEntity; //go to owner of collection
                        IPersistIfc ifcCollectionOwner = collectionOwner.Entity;
                        ifcCollectionOwner.IfcParse(collection.PropertyIndex - 1, pv);
                    }
                    else if (_currentNode is XmlBasicType)
                    {
                        XmlBasicType          basicNode          = (XmlBasicType)_currentNode;
                        XmlEntity             collectionOwner    = _currentNode.Parent.Parent as XmlEntity;
                        XmlCollectionProperty collection         = _currentNode.Parent as XmlCollectionProperty; //the collection to add to;
                        IPersistIfc           ifcCollectionOwner = collectionOwner.Entity;
                        PropertyValue         pv = new PropertyValue();
                        pv.Init(basicNode.Value, basicNode.Type);
                        ifcCollectionOwner.IfcParse(collection.PropertyIndex - 1, pv);
                    }
                }


                writeEntity = null;
                if (_currentNode.Parent != null) // we are not at UOS yet
                {
                    if (_currentNode is XmlEntity)
                    {
                        writeEntity = ((XmlEntity)_currentNode).Entity;
                    }
                    _currentNode = _currentNode.Parent;
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error reading IfcXML data at node " + input.LocalName, e);
            }
        }