internal bool TrySetObjectValue(IPersist host, int paramIndex, int refId, int[] listNextLevel)
        {
            if (_deferListItems)
            {
                return(false);
            }
            try
            {
                if (host != null && _entities.TryGetValue(refId, out IPersist refEntity))
                {
                    PropertyValue.Init(refEntity);
                    (host).Parse(paramIndex, PropertyValue, listNextLevel);
                    return(true);
                }
            }
            catch (Exception)
            {
                if (ErrorCount > MaxErrorCount)
                {
                    throw new XbimParserException("Too many errors in file, parser execution terminated");
                }
                ErrorCount++;
                Logger?.LogWarning("Entity #{0,-5} {1}, error at parameter {2}",
                                   refId, host.GetType().Name.ToUpper(), paramIndex + 1);

                // this means the case is handled. It would be added to defered references and caused new errors
                return(true);
            }
            return(false);
        }
        /// <summary>
        /// Handles the case where a property was not expected for this entity.
        /// </summary>
        /// <param name="persistIfc">The item being parsed.</param>
        /// <param name="propIndex">Index of the property.</param>
        /// <param name="value">The value of the property.</param>

        internal static void HandleUnexpectedAttribute(this IPersist persistIfc, int propIndex, IPropertyValue value)
        {
            // TODO: Review this workaround for older IFC files with extraneous properties
            if (value.Type == StepParserType.Enum && string.CompareOrdinal(value.EnumVal, "NOTDEFINED") == 0)
            {
                return;
            }

            throw new XbimParserException(string.Format("Attribute index {0} is out of range for {1}", propIndex + 1,
                                                        persistIfc.GetType().Name.ToUpper()));
        }
        public void Load(IPersist targetObject, FileStream stream)
        {
            FieldInfo[] fields = targetObject.GetType().GetFields(
                BindingFlags.Public | BindingFlags.NonPublic |
                BindingFlags.Instance);

            foreach (FieldInfo field in fields)
            {
                string fieldName  = field.Name;
                object fieldValue = field.GetValue(targetObject);
            }
        }
Exemple #4
0
        private IEnumerable <string> ToList()
        {
            if (_args != null)
            {
                foreach (var arg in _args)
                {
                    yield return(arg);
                }
            }
            else if (_instance != null)
            {
                var tp     = _instance.GetType();
                var schema = "";
                var asArr  = tp.FullName.Split(new[] { "." }, StringSplitOptions.None);
                schema = asArr[1];
                while (tp != null)
                {
                    yield return(string.Format("{0}.{1}", schema, tp.Name).ToUpperInvariant());

                    tp = tp.BaseType;
                }
            }
        }
 internal bool TrySetObjectValue(IPersist host, int paramIndex, int refId, int[] listNextLevel)
 {
     if (_deferListItems)
     {
         return(false);
     }
     try
     {
         if (host != null && Entities.TryGetValue(refId, out IPersist refEntity))
         {
             PropertyValue.Init(refEntity);
             host.Parse(paramIndex, PropertyValue, listNextLevel);
             return(true);
         }
     }
     catch (Exception e)
     {
         // return silently if this kind of error has already been reported
         if (_errors.AddPropertyNotSet(host, paramIndex, PropertyValue, e))
         {
             Logger?.LogError(LogEventIds.FailedPropertySetter, e, "Entity #{0,-5} {1}, error at parameter {2}", refId, host.GetType().Name.ToUpper(), paramIndex + 1);
             ErrorCount++;
         }
     }
     return(false);
 }
Exemple #6
0
 /// <summary>
 /// Returns the ExpressType of the specified entity
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public ExpressType ExpressType(IPersist entity)
 {
     return(ExpressType(entity.GetType()));
 }
Exemple #7
0
 public short ExpressTypeId(IPersist entity)
 {
     return(_typeToExpressTypeLookup[entity.GetType()].TypeId);
 }
Exemple #8
0
 public ExpressType this[IPersist ent]
 {
     get { return(this[ent.GetType()]); }
 }
Exemple #9
0
        protected static IEnumerable <ValidationResult> GetSchemaErrors(IPersist instance, ExpressMetaProperty prop, ValidationFlags validateLevel, bool hierarchical)
        {
            var attr     = prop.EntityAttribute;
            var propVal  = prop.PropertyInfo.GetValue(instance, null);
            var propName = prop.PropertyInfo.Name;

            if (propVal is IExpressValueType)
            {
                var val            = ((IExpressValueType)propVal).Value;
                var underlyingType = ((IExpressValueType)propVal).UnderlyingSystemType;
                if (attr.State == EntityAttributeState.Mandatory && val == null && underlyingType != typeof(bool?))
                {
                    yield return(new ValidationResult()
                    {
                        Item = instance,
                        IssueType = ValidationFlags.Properties,
                        IssueSource = propName,
                        Message = string.Format("{0}.{1} is not optional.", instance.GetType().Name, propName)
                    });
                }
                if (validateLevel.HasFlag(ValidationFlags.TypeWhereClauses) && propVal is IExpressValidatable)
                {
                    var hierResult = new ValidationResult()
                    {
                        Item = instance, IssueType = ValidationFlags.None
                    };
                    foreach (var issue in ((IExpressValidatable)propVal).Validate())
                    {
                        if (hierarchical)
                        {
                            hierResult.AddDetail(issue);
                        }
                        else
                        {
                            yield return(issue);
                        }
                    }
                    if (hierarchical && hierResult.IssueType != ValidationFlags.None)
                    {
                        // the IssueType is populated if any children have been added.
                        hierResult.Message = string.Format("Property {0} has validation failures.", prop.Name);
                        yield return(hierResult);
                    }
                }
                yield break;
            }

            if (attr.State == EntityAttributeState.Mandatory && propVal == null)
            {
                yield return new ValidationResult()
                       {
                           Item        = instance,
                           IssueType   = ValidationFlags.Properties,
                           IssueSource = propName,
                           Message     = string.Format("{0}.{1} is not optional.", instance.GetType().Name, propName)
                       }
            }
            ;
            if (attr.State == EntityAttributeState.Optional && propVal == null)
            {
                //if it is null and optional then it is ok
                yield break;
            }
            if (attr.State == EntityAttributeState.Optional && propVal is IOptionalItemSet && !((IOptionalItemSet)propVal).Initialized)
            {
                //if it is non-initialized list and optional then it is ok
                yield break;
            }
            if (attr.IsEnumerable)
            {
                if (
                    (attr.MinCardinality == null || attr.MinCardinality.Length == 0 || attr.MinCardinality.All(c => c < 0)) &&
                    (attr.MaxCardinality == null || attr.MaxCardinality.Length == 0 || attr.MaxCardinality.All(c => c < 1))) //we don't care how many so don't check
                {
                    yield break;
                }

                var depth = attr.MinCardinality.Length;
                if (depth != attr.MaxCardinality.Length)
                {
                    throw new System.Exception("Inconsistent metadata: minimal and maximal cardinality has to have the same length.");
                }

                var sb    = new StringBuilder();
                var items = (IEnumerable)propVal;
                CheckCardinality(attr.MinCardinality, attr.MaxCardinality, items, 0, sb);
                var msg = sb.ToString();
                if (string.IsNullOrWhiteSpace(msg))
                {
                    yield break;
                }

                yield return(new ValidationResult()
                {
                    Item = instance,
                    IssueType = ValidationFlags.Properties,
                    IssueSource = propName,
                    Message = string.Format("{0}.{1}: {2}", instance.GetType().Name, prop.Name, msg)
                });
            }
        }