/// <summary>Harvest specific summary information from a <see cref="NamingSystem"/> resource.</summary>
 /// <returns><c>true</c> if the current target represents a <see cref="NamingSystem"/> resource, or <c>false</c> otherwise.</returns>
 /// <remarks>The <see cref="ArtifactSummaryGenerator"/> calls this method through a <see cref="ArtifactSummaryHarvester"/> delegate.</remarks>
 public static bool Harvest(IElementNavigator nav, ArtifactSummaryPropertyBag properties)
 {
     if (IsNamingSystemSummary(properties))
     {
         nav.HarvestValues(properties, UniqueIdKey, "uniqueId", "value");
         return(true);
     }
     return(false);
 }
 /// <summary>Harvest common summary information from a conformance resource.</summary>
 /// <returns><c>true</c> if the current target represents a conformance resource, or <c>false</c> otherwise.</returns>
 /// <remarks>
 /// The <see cref="ArtifactSummaryGenerator"/> calls this method through a <see cref="ArtifactSummaryHarvester"/> delegate.
 /// Also called directly by other <see cref="ArtifactSummaryHarvester"/> delegates to harvest summary
 /// information common to all conformance resources, before harvesting any additional type specific
 /// information.
 /// </remarks>
 /// <seealso cref="StructureDefinitionSummaryProperties"/>
 /// <seealso cref="ValueSetSummaryProperties"/>
 /// <seealso cref="ConceptMapSummaryProperties"/>
 public static bool Harvest(IElementNavigator nav, ArtifactSummaryPropertyBag properties)
 {
     if (IsConformanceSummary(properties))
     {
         nav.HarvestValue(properties, CanonicalUrlKey, "url");
         nav.HarvestValue(properties, NameKey, "name");
         nav.HarvestValue(properties, StatusKey, "status");
         return(true);
     }
     return(false);
 }
 /// <summary>Harvest specific summary information from a <see cref="ValueSet"/> resource.</summary>
 /// <returns><c>true</c> if the current target is a ValueSet, or <c>false</c> otherwise.</returns>
 /// <remarks>The <see cref="ArtifactSummaryGenerator"/> calls this method from a <see cref="ArtifactSummaryHarvester"/> delegate.</remarks>
 public static bool Harvest(IElementNavigator nav, ArtifactSummaryPropertyBag properties)
 {
     if (IsValueSetSummary(properties))
     {
         // Explicit extractor chaining
         if (ConformanceSummaryProperties.Harvest(nav, properties))
         {
             nav.HarvestValue(properties, SystemKey, "codeSystem", "system");
         }
         return(true);
     }
     return(false);
 }
        // Generate summary for a single artifact
        static ArtifactSummary generate(
            ArtifactSummaryPropertyBag props,
            IElementNavigator nav,
            ArtifactSummaryHarvester[] harvesters)
        {
            Exception error = null;

            try
            {
                // Harvest summary information via specified harvesters
                // Top-level harvesters receive navigator positioned on the first child element level

                // Catch individual exceptions inside loop, return as AggregateException
                var errors = new List <Exception>();
                if (nav.MoveToFirstChild())
                {
                    foreach (var harvester in harvesters)
                    {
                        try
                        {
                            if (harvester != null && harvester.Invoke(nav, props))
                            {
                                break;
                            }
                        }
                        // TODO Catch specific exceptions
                        // catch (FormatException)
                        catch (Exception ex)
                        {
                            errors.Add(ex);
                        }
                    }
                }

                // Combine all errors into single AggregateException
                error = errors.Count > 0 ? new AggregateException(errors) : null;
            }
            // TODO Catch specific exceptions
            // catch (FormatException)
            // catch (NotSupportedException)
            catch (Exception ex)
            {
                // Error in summary factory?
                // Make sure we always return a valid summary
                error = ex;
            }

            // Create final summary from harvested properties and optional error
            return(new ArtifactSummary(props, error));
        }
Example #5
0
        /// <summary>Create a new <see cref="ArtifactSummary"/> instance from the specified exception.</summary>
        /// <param name="error">An exception that occured while harvesting artifact summary information.</param>
        /// <param name="origin">The original location of the target artifact.</param>
        public static ArtifactSummary FromException(Exception error, string origin)
        {
            if (error == null)
            {
                throw Errors.ArgumentNull(nameof(error));
            }
            // Create a new (default) ArtifactSummaryPropertyBag to store the artifact origin
            var properties = new ArtifactSummaryPropertyBag();

            if (!string.IsNullOrEmpty(origin))
            {
                properties[ArtifactSummaryProperties.OriginKey] = origin;
            }
            return(new ArtifactSummary(properties, error));
        }
 /// <summary>Harvest specific summary information from a <see cref="StructureDefinition"/> resource.</summary>
 /// <returns><c>true</c> if the current target represents a <see cref="StructureDefinition"/> resource, or <c>false</c> otherwise.</returns>
 /// <remarks>The <see cref="ArtifactSummaryGenerator"/> calls this method from a <see cref="ArtifactSummaryHarvester"/> delegate.</remarks>
 public static bool Harvest(IElementNavigator nav, ArtifactSummaryPropertyBag properties)
 {
     if (IsStructureDefinitionSummary(properties))
     {
         // Explicit extractor chaining
         if (ConformanceSummaryProperties.Harvest(nav, properties))
         {
             nav.HarvestValue(properties, FhirVersionKey, "fhirVersion");
             nav.HarvestValue(properties, KindKey, "kind");
             nav.HarvestValue(properties, ConstrainedTypeKey, "constrainedType");
             nav.HarvestValue(properties, ContextTypeKey, "contextType");
             nav.HarvestValue(properties, BaseKey, "base");
         }
         return(true);
     }
     return(false);
 }
        /// <summary>Harvest specific summary information from a <see cref="ConceptMap"/> resource.</summary>
        /// <returns><c>true</c> if the current target represents a <see cref="ConceptMap"/> resource, or <c>false</c> otherwise.</returns>
        /// <remarks>The <see cref="ArtifactSummaryGenerator"/> calls this method from a <see cref="ArtifactSummaryHarvester"/> delegate.</remarks>
        public static bool Harvest(IElementNavigator nav, ArtifactSummaryPropertyBag properties)
        {
            if (IsConceptMapSummary(properties))
            {
                // Explicit extractor chaining
                if (ConformanceSummaryProperties.Harvest(nav, properties))
                {
                    if (!nav.HarvestValue(properties, SourceKey, "sourceUri"))
                    {
                        nav.HarvestValue(properties, SourceKey, "sourceReference", "reference");
                    }

                    if (!nav.HarvestValue(properties, TargetKey, "targetUri"))
                    {
                        nav.HarvestValue(properties, TargetKey, "targetReference", "reference");
                    }
                }
                return(true);
            }
            return(false);
        }
        /// <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);
        }
 internal static void SetResourceUri(this ArtifactSummaryPropertyBag properties, string value)
 {
     properties[ResourceUriKey] = value;
 }
 internal static void SetTypeName(this ArtifactSummaryPropertyBag properties, string value)
 {
     properties[TypeNameKey] = value;
 }
 internal static void SetPosition(this ArtifactSummaryPropertyBag properties, string value)
 {
     properties[PositionKey] = value;
 }
 internal static void SetOrigin(this ArtifactSummaryPropertyBag properties, string value)
 {
     properties[OriginKey] = value;
 }
Example #13
0
 internal static void SetLastModified(this ArtifactSummaryPropertyBag properties, DateTime value)
 {
     properties[LastModifiedKey] = value;
 }
Example #14
0
 internal static void SetFileSize(this ArtifactSummaryPropertyBag properties, long value)
 {
     properties[FileSizeKey] = value;
 }