/// <summary>Generate a list of artifact summary information from an <see cref="INavigatorStream"/> instance.</summary>
        /// <param name="origin">The original location of the target artifact (or the containing Bundle).</param>
        /// <param name="harvesters">
        /// An optional list of <see cref="ArtifactSummaryHarvester"/> delegates that the generator will call
        /// instead of the default harvesters to harvest summary information from an artifact.
        /// </param>
        /// <returns>A list of new <see cref="ArtifactSummary"/> instances.</returns>
        /// <remarks>
        /// For each artifact, the generator executes all (default or specified) harvester delegates
        /// in the specified order. When a delegate returns <c>true</c> to signal that harvesting has
        /// finished, the generator will not call any of the remaining delegates and immediately
        /// proceed to create the final <see cref="ArtifactSummary"/> return value.
        /// <para>
        /// By default, if the <paramref name="harvesters"/> parameter value is null or empty, the
        /// <see cref="ArtifactSummaryGenerator"/> calls the built-in default harvesters
        /// as specified by <see cref="ArtifactSummaryGenerator.DefaultHarvesters"/>.
        /// However if the caller specifies one or more harvester delegates, then the summary
        /// generator calls only the provided delegates, in the specified order.
        /// A custom delegate array may include one or more of the default harvesters.
        /// </para>
        /// <para>
        /// The generator catches all runtime exceptions that occur during harvesting and returns
        /// them as <see cref="ArtifactSummary"/> instances with <see cref="ArtifactSummary.IsFaulted"/>
        /// equal to <c>true</c> and <see cref="ArtifactSummary.Error"/> returning the exception.
        /// </para>
        /// </remarks>
        public static List <ArtifactSummary> Generate(
            string origin,
            params ArtifactSummaryHarvester[] harvesters)
        {
            var result = new List <ArtifactSummary>();

            // In case of error, return completed summaries and error info
            INavigatorStream navStream = null;

            try
            {
                // Call default navigator factory
                navStream = DefaultNavigatorStreamFactory.Create(origin);

                // Get some source file properties
                var fi = new FileInfo(origin);

                // Factory returns null for unknown file formats
                if (navStream == null)
                {
                    return(result);
                }

                // Run default or specified (custom) harvesters
                if (harvesters == null || harvesters.Length == 0)
                {
                    harvesters = DefaultHarvesters;
                }

                while (navStream.MoveNext())
                {
                    var current = navStream.Current;
                    if (current != null)
                    {
                        var properties = new ArtifactSummaryPropertyBag();

                        // Initialize default summary information
                        // Note: not exposed by IElementNavigator, cannot use harvester
                        properties.SetOrigin(origin);
                        properties.SetFileSize(fi.Length);
                        properties.SetLastModified(fi.LastWriteTimeUtc);
                        properties.SetPosition(navStream.Position);
                        properties.SetTypeName(current.Type);
                        properties.SetResourceUri(navStream.Position);

                        var summary = generate(properties, current, harvesters);

                        result.Add(summary);
                    }
                }
            }
            // TODO Catch specific exceptions
            // catch (System.IO.FileNotFoundException)
            // catch (UnauthorizedAccessException)
            // catch (System.Security.SecurityException)
            // catch (FormatException)
            catch (Exception ex)
            {
                result.Add(ArtifactSummary.FromException(ex, origin));
            }
            finally
            {
                navStream?.Dispose();
            }
            return(result);
        }
Ejemplo n.º 2
0
 /// <summary>Try to load a structure definition from the summary origin.</summary>
 public static StructureDefinition LoadStructure(this ArtifactSummary summary) => summary.LoadResource <StructureDefinition>();
Ejemplo n.º 3
0
        // [WMR 20171204] SummaryGenerator only returns summaries for XML and JSON files (not XSD)
        // So currently, this method only serves as a fall-back for xml/json files that
        // the FHIR parser cannot deserialize (non-FHIR, invalid, incompatible FHIR version, ...)

        /// <summary>Try to open the summary origin for reading.</summary>
        /// <returns>A <see cref="Stream"/> instance.</returns>
        /// <remarks>
        /// Allows processing non-FHIR/invalid/incompatible artifacts.
        /// Use the <seealso cref="ArtifactSummary.LoadResource{T}"/> method to load FHIR resources.
        /// </remarks>
        public static Stream LoadArtifact(this ArtifactSummary summary) => File.OpenRead(summary.Origin);
Ejemplo n.º 4
0
        // Extension methods for ArtifactSummary


        /// <summary>Try to load a resource from the summary origin.</summary>
        public static Resource LoadResource(this ArtifactSummary summary) => summary.LoadResource <Resource>();