示例#1
0
        public Base BuildFrom(ISourceNode source, Type dataType = null)
        {
            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }

            if (dataType != null)
            {
                return(buildInternal(source, dataType));
            }
            else
            {
                var rti = source.GetResourceTypeIndicator();
                if (rti == null)
                {
                    ExceptionHandler.NotifyOrThrow(this,
                                                   ExceptionNotification.Error(new StructuralTypeException($"No type indication on source to build POCO's for.")));
                    return(null);
                }
                else
                {
                    return(BuildFrom(source, rti));
                }
            }
        }
示例#2
0
        /// <summary>
        /// Build a POCO from an ISourceNode.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="dataType">Optional. Type of POCO to build. Should be one of the generated POCO classes.</param>
        /// <returns></returns>
        /// <remarks>If <paramref name="dataType"/> is not supplied, or is <code>Resource</code> or <code>DomainResource</code>,
        /// the builder will try to determine the actual type to create from the <paramref name="source"/>. </remarks>
        public Base BuildFrom(ISourceNode source, Type dataType = null)
        {
            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }

            return(BuildFrom(source, dataType != null ?
                             findMappingOrReport(dataType) : null));

            ClassMapping findMappingOrReport(Type t)
            {
                var typeFound = _inspector.FindClassMapping(dataType);

                if (typeFound == null)
                {
                    ExceptionHandler.NotifyOrThrow(this,
                                                   ExceptionNotification.Error(
                                                       new StructuralTypeException($"While building a POCO: The .NET type '{dataType.Name}' does not represent a FHIR type.")));

                    return(null);
                }

                return(typeFound);
            }
        }
示例#3
0
        private void raiseTypeError(string message, object source, bool warning = false)
        {
            var exc          = new StructuralTypeException("Type checking the data: " + message);
            var notification = warning ?
                               ExceptionNotification.Warning(exc) :
                               ExceptionNotification.Error(exc);

            ExceptionHandler.NotifyOrThrow(source, notification);
        }
        private void raiseTypeError(string message, object source, bool warning = false, string location = null)
        {
            var exMessage = $"Type checking the data: {message}";

            if (!string.IsNullOrEmpty(location))
            {
                exMessage += $" (at {location})";
            }

            var exc          = new StructuralTypeException(exMessage);
            var notification = warning ?
                               ExceptionNotification.Warning(exc) :
                               ExceptionNotification.Error(exc);

            ExceptionHandler.NotifyOrThrow(source, notification);
        }
        /// <summary>
        /// Build a POCO from an ITypedElement.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public Base BuildFrom(ITypedElement source)
        {
            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }

            if (source is IExceptionSource)
            {
                using (source.Catch((o, a) => ExceptionHandler.NotifyOrThrow(o, a)))
                {
                    return(build());
                }
            }
            else
            {
                return(build());
            }

            Base build()
            {
                var typeToBuild = ModelInfo.GetTypeForFhirType(source.InstanceType);

                if (typeToBuild == null)
                {
                    ExceptionHandler.NotifyOrThrow(this,
                                                   ExceptionNotification.Error(
                                                       new StructuralTypeException($"While building a POCO: There is no .NET type representing the FHIR type '{source.InstanceType}'.")));

                    return(null);
                }

                var settings = new ParserSettings
                {
                    AcceptUnknownMembers   = _settings.IgnoreUnknownMembers,
                    AllowUnrecognizedEnums = _settings.AllowUnrecognizedEnums
                };

                return(typeToBuild.CanBeTreatedAsType(typeof(Resource))
                    ? new ResourceReader(source, settings).Deserialize()
                    : new ComplexTypeReader(source, settings).Deserialize());
            }
        }
示例#6
0
        public Base BuildFrom(ISourceNode source, string dataType)
        {
            if (dataType == null)
            {
                throw Error.ArgumentNull(nameof(dataType));
            }

            var typeFound = ModelInfo.GetTypeForFhirType(dataType);

            if (typeFound == null)
            {
                ExceptionNotification.Error(
                    new StructuralTypeException($"There is no .NET type representing the FHIR type '{dataType}'."));

                return(null);
            }
            else
            {
                return(buildInternal(source, typeFound));
            }
        }
        /// <summary>
        /// Build a POCO from an ISourceNode.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="dataType">Optional. Type of POCO to build. Should be one of the generated POCO classes.</param>
        /// <returns></returns>
        /// <remarks>If <paramref name="dataType"/> is not supplied, or is <code>Resource</code> or <code>DomainResource</code>,
        /// the builder will try to determine the actual type to create from the <paramref name="source"/>. </remarks>
        public Base BuildFrom(ISourceNode source, Type dataType = null)
        {
            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }
            string typeFound = null;

            if (dataType != null)
            {
                typeFound = ModelInfo.GetFhirTypeNameForType(dataType);

                if (typeFound == null)
                {
                    ExceptionNotification.Error(
                        new StructuralTypeException($"While building a POCO: The .NET type '{dataType.Name}' does not represent a FHIR type."));

                    return(null);
                }
            }
            return(BuildFrom(source, typeFound));
        }
        internal bool MustSerializeMember(ITypedElement source, out IElementDefinitionSummary info)
        {
            info = source.Definition;

            if (info == null && !_roundtripMode)
            {
                var message = $"Element '{source.Location}' is missing type information.";

                if (_settings.IgnoreUnknownElements)
                {
                    ExceptionHandler.NotifyOrThrow(source, ExceptionNotification.Warning(
                                                       new MissingTypeInformationException(message)));
                }
                else
                {
                    ExceptionHandler.NotifyOrThrow(source, ExceptionNotification.Error(
                                                       new MissingTypeInformationException(message)));
                }

                return(false);
            }

            return(true);
        }
示例#9
0
 private void raiseFormatError(string message, JToken node)
 {
     var(lineNumber, linePosition) = getPosition(node);
     ExceptionHandler.NotifyOrThrow(this, ExceptionNotification.Error(Error.Format("Parser: " + message, lineNumber, linePosition)));
 }
 public void Test(string message)
 {
     ExceptionHandler.NotifyOrThrow(this, ExceptionNotification.Error(new FormatException(message)));
 }