1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
//---------------------------------------------------------------------
// <copyright file="MetadataArtifactLoaderResource.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
namespace System.Data.Metadata.Edm
{
using System.Collections.Generic;
using System.Data.EntityModel.SchemaObjectModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml;
/// <summary>
/// This class represents one resource item to be loaded from an assembly.
/// </summary>
internal class MetadataArtifactLoaderResource : MetadataArtifactLoader, IComparable
{
private readonly bool _alreadyLoaded = false;
private readonly Assembly _assembly;
private readonly string _resourceName;
/// <summary>
/// Constructor - loads the resource stream
/// </summary>
/// <param name="path">The path to the resource to load</param>
/// <param name="uriRegistry">The global registry of URIs</param>
internal MetadataArtifactLoaderResource(Assembly assembly, string resourceName, ICollection<string> uriRegistry)
{
Debug.Assert(assembly != null);
Debug.Assert(resourceName != null);
_assembly = assembly;
_resourceName = resourceName;
string tempPath = MetadataArtifactLoaderCompositeResource.CreateResPath(_assembly, _resourceName);
_alreadyLoaded = uriRegistry.Contains(tempPath);
if (!_alreadyLoaded)
{
uriRegistry.Add(tempPath);
// '_alreadyLoaded' is not set because while we would like to prevent
// other instances of MetadataArtifactLoaderFile that wrap the same
// _path from being added to the list of paths/readers, we do want to
// include this particular instance.
}
}
public override string Path
{
get
{
return MetadataArtifactLoaderCompositeResource.CreateResPath(_assembly, _resourceName);
}
}
/// <summary>
/// Implementation of IComparable.CompareTo()
/// </summary>
/// <param name="obj">The object to compare to</param>
/// <returns>0 if the loaders are "equal" (i.e., have the same _path value)</returns>
public int CompareTo(object obj)
{
MetadataArtifactLoaderResource loader = obj as MetadataArtifactLoaderResource;
if (loader != null)
{
return string.Compare(Path, loader.Path, StringComparison.OrdinalIgnoreCase);
}
Debug.Assert(false, "object is not a MetadataArtifactLoaderResource");
return -1;
}
/// <summary>
/// Equals() returns true if the objects have the same _path value
/// </summary>
/// <param name="obj">The object to compare to</param>
/// <returns>true if the objects have the same _path value</returns>
public override bool Equals(object obj)
{
return this.CompareTo(obj) == 0;
}
/// <summary>
/// GetHashCode override that defers the result to the _path member variable.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return Path.GetHashCode();
}
public override void CollectFilePermissionPaths(List<string> paths, DataSpace spaceToGet)
{
// does not apply
}
/// <summary>
/// Get paths to artifacts for a specific DataSpace.
/// </summary>
/// <param name="spaceToGet">The DataSpace for the artifacts of interest</param>
/// <returns>A List of strings identifying paths to all artifacts for a specific DataSpace</returns>
public override List<string> GetPaths(DataSpace spaceToGet)
{
List<string> list = new List<string>();
if (!_alreadyLoaded && MetadataArtifactLoader.IsArtifactOfDataSpace(Path, spaceToGet))
{
list.Add(Path);
}
return list;
}
/// <summary>
/// Get paths to all artifacts
/// </summary>
/// <returns>A List of strings identifying paths to all resources</returns>
public override List<string> GetPaths()
{
List<string> list = new List<string>();
if (!_alreadyLoaded)
{
list.Add(Path);
}
return list;
}
/// <summary>
/// Create and return an XmlReader around the resource represented by this instance.
/// </summary>
/// <returns>A List of XmlReaders for all resources</returns>
public override List<XmlReader> GetReaders(Dictionary<MetadataArtifactLoader, XmlReader> sourceDictionary)
{
List<XmlReader> list = new List<XmlReader>();
if (!_alreadyLoaded)
{
XmlReader reader = CreateReader();
list.Add(reader);
if (sourceDictionary != null)
{
sourceDictionary.Add(this, reader);
}
}
return list;
}
private XmlReader CreateReader()
{
Stream stream = LoadResource();
XmlReaderSettings readerSettings = Schema.CreateEdmStandardXmlReaderSettings();
// close the stream when the xmlreader is closed
// now the reader owns the stream
readerSettings.CloseInput = true;
// we know that we aren't reading a fragment
readerSettings.ConformanceLevel = ConformanceLevel.Document;
XmlReader reader = XmlReader.Create(stream, readerSettings);
// cannot set the base URI because res:// URIs cause the schema parser
// to choke
return reader;
}
/// <summary>
/// Create and return an XmlReader around the resource represented by this instance
/// if it is of the requested DataSpace type.
/// </summary>
/// <param name="spaceToGet">The DataSpace corresponding to the requested artifacts</param>
/// <returns>A List of XmlReader objects</returns>
public override List<XmlReader> CreateReaders(DataSpace spaceToGet)
{
List<XmlReader> list = new List<XmlReader>();
if (!_alreadyLoaded)
{
if (MetadataArtifactLoader.IsArtifactOfDataSpace(Path, spaceToGet))
{
XmlReader reader = CreateReader();
list.Add(reader);
}
}
return list;
}
/// <summary>
/// This method parses the path to the resource and attempts to load it.
/// The method also accounts for the wildcard assembly name.
/// </summary>
private Stream LoadResource()
{
Stream resourceStream;
if (TryCreateResourceStream(out resourceStream))
{
return resourceStream;
}
throw EntityUtil.Metadata(System.Data.Entity.Strings.UnableToLoadResource);
}
private bool TryCreateResourceStream(out Stream resourceStream)
{
resourceStream = _assembly.GetManifestResourceStream(_resourceName);
return resourceStream != null;
}
}
}
|