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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
//---------------------------------------------------------------------
// <copyright file="MetadataArtifactLoader.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.Entity;
using System.Diagnostics;
using System.IO;
using System.Runtime.Versioning;
using System.Xml;
/// <summary>
/// This is the base class for the resource metadata artifact loader; derived
/// classes enacpsulate a single resource as well as collections of resources,
/// along the lines of the Composite pattern.
/// </summary>
internal abstract class MetadataArtifactLoader
{
protected readonly static string resPathPrefix = @"res://";
protected readonly static string resPathSeparator = @"/";
protected readonly static string altPathSeparator = @"\";
protected readonly static string wildcard = @"*";
/// <summary>
/// Read-only access to the resource/file path
/// </summary>
public abstract string Path{ get; }
public abstract void CollectFilePermissionPaths(List<string> paths, DataSpace spaceToGet);
/// <summary>
/// This enum is used to indicate the level of extension check to be perfoemed
/// on a metadata URI.
/// </summary>
public enum ExtensionCheck
{
/// <summary>
/// Do not perform any extension check
/// </summary>
None = 0,
/// <summary>
/// Check the extension against a specific value
/// </summary>
Specific,
/// <summary>
/// Check the extension against the set of acceptable extensions
/// </summary>
All
}
[ResourceExposure(ResourceScope.Machine)] //Exposes the file name which is a Machine resource
[ResourceConsumption(ResourceScope.Machine)] //For Create method call. But the path is not created in this method.
public static MetadataArtifactLoader Create(string path,
ExtensionCheck extensionCheck,
string validExtension,
ICollection<string> uriRegistry)
{
return Create(path, extensionCheck, validExtension, uriRegistry, new DefaultAssemblyResolver());
}
/// <summary>
/// Factory method to create an artifact loader. This is where an appropriate
/// subclass of MetadataArtifactLoader is created, depending on the kind of
/// resource it will encapsulate.
/// </summary>
/// <param name="path">The path to the resource(s) to be loaded</param>
/// <param name="extensionCheck">Any URI extension checks to perform</param>
/// <param name="validExtension">A specific extension for an artifact resource</param>
/// <param name="uriRegistry">The global registry of URIs</param>
/// <param name="resolveAssembly"></param>
/// <returns>A concrete instance of an artifact loader.</returns>
[ResourceExposure(ResourceScope.Machine)] //Exposes the file name which is a Machine resource
[ResourceConsumption(ResourceScope.Machine)] //For CheckArtifactExtension method call. But the path is not created in this method.
internal static MetadataArtifactLoader Create(string path,
ExtensionCheck extensionCheck,
string validExtension,
ICollection<string> uriRegistry,
MetadataArtifactAssemblyResolver resolver)
{
Debug.Assert(path != null);
Debug.Assert(resolver != null);
// res:// -based artifacts
//
if (MetadataArtifactLoader.PathStartsWithResPrefix(path))
{
return MetadataArtifactLoaderCompositeResource.CreateResourceLoader(path, extensionCheck, validExtension, uriRegistry, resolver);
}
// Files and Folders
//
string normalizedPath = MetadataArtifactLoader.NormalizeFilePaths(path);
if (Directory.Exists(normalizedPath))
{
return new MetadataArtifactLoaderCompositeFile(normalizedPath, uriRegistry);
}
else if (File.Exists(normalizedPath))
{
switch (extensionCheck)
{
case ExtensionCheck.Specific:
CheckArtifactExtension(normalizedPath, validExtension);
break;
case ExtensionCheck.All:
if (!MetadataArtifactLoader.IsValidArtifact(normalizedPath))
{
throw EntityUtil.Metadata(Strings.InvalidMetadataPath);
}
break;
}
return new MetadataArtifactLoaderFile(normalizedPath, uriRegistry);
}
throw EntityUtil.Metadata(Strings.InvalidMetadataPath);
}
/// <summary>
/// Factory method to create an aggregating artifact loader, one that encapsulates
/// multiple collections.
/// </summary>
/// <param name="allCollections">The list of collections to be aggregated</param>
/// <returns>A concrete instance of an artifact loader.</returns>
public static MetadataArtifactLoader Create(List<MetadataArtifactLoader> allCollections)
{
return new MetadataArtifactLoaderComposite(allCollections);
}
/// <summary>
/// Helper method that wraps a list of file paths in MetadataArtifactLoader instances.
/// </summary>
/// <param name="filePaths">The list of file paths to wrap</param>
/// <param name="validExtension">An acceptable extension for the file</param>
/// <returns>An instance of MetadataArtifactLoader</returns>
[ResourceExposure(ResourceScope.Machine)] //Exposes the file names which are a Machine resource
[ResourceConsumption(ResourceScope.Machine)] //For CreateCompositeFromFilePaths method call. But the path is not created in this method.
public static MetadataArtifactLoader CreateCompositeFromFilePaths(IEnumerable<string> filePaths, string validExtension)
{
Debug.Assert(!string.IsNullOrEmpty(validExtension));
return CreateCompositeFromFilePaths(filePaths, validExtension, new DefaultAssemblyResolver());
}
[ResourceExposure(ResourceScope.Machine)] //Exposes the file names which are a Machine resource
[ResourceConsumption(ResourceScope.Machine)] //For Create method call. But the paths are not created in this method.
internal static MetadataArtifactLoader CreateCompositeFromFilePaths(IEnumerable<string> filePaths, string validExtension, MetadataArtifactAssemblyResolver resolver)
{
ExtensionCheck extensionCheck;
if (string.IsNullOrEmpty(validExtension))
{
extensionCheck = ExtensionCheck.All;
}
else
{
extensionCheck = ExtensionCheck.Specific;
}
List<MetadataArtifactLoader> loaders = new List<MetadataArtifactLoader>();
// The following set is used to remove duplicate paths from the incoming array
HashSet<string> uriRegistry = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach(string path in filePaths)
{
if (string.IsNullOrEmpty(path))
{
throw EntityUtil.Metadata(System.Data.Entity.Strings.NotValidInputPath,
EntityUtil.CollectionParameterElementIsNullOrEmpty("filePaths"));
}
string trimedPath = path.Trim();
if (trimedPath.Length > 0)
{
loaders.Add(MetadataArtifactLoader.Create(
trimedPath,
extensionCheck,
validExtension,
uriRegistry,
resolver)
);
}
}
return MetadataArtifactLoader.Create(loaders);
}
/// <summary>
/// Helper method that wraps a collection of XmlReader objects in MetadataArtifactLoader
/// instances.
/// </summary>
/// <param name="filePaths">The collection of XmlReader objects to wrap</param>
/// <returns>An instance of MetadataArtifactLoader</returns>
public static MetadataArtifactLoader CreateCompositeFromXmlReaders(IEnumerable<XmlReader> xmlReaders)
{
List<MetadataArtifactLoader> loaders = new List<MetadataArtifactLoader>();
foreach (XmlReader reader in xmlReaders)
{
if (reader == null)
{
throw EntityUtil.CollectionParameterElementIsNull("xmlReaders");
}
loaders.Add(new MetadataArtifactLoaderXmlReaderWrapper(reader));
}
return MetadataArtifactLoader.Create(loaders);
}
/// <summary>
/// If the path doesn't have the right extension, throw
/// </summary>
/// <param name="path">The path to the resource</param>
/// <param name="validExtension"></param>
internal static void CheckArtifactExtension(string path, string validExtension)
{
Debug.Assert(!string.IsNullOrEmpty(path));
Debug.Assert(!string.IsNullOrEmpty(validExtension));
string extension = GetExtension(path);
if (!extension.Equals(validExtension, StringComparison.OrdinalIgnoreCase))
{
throw EntityUtil.Metadata(Strings.InvalidFileExtension(path, extension, validExtension));
}
}
/// <summary>
/// Get paths to all artifacts, in the original, unexpanded form
/// </summary>
/// <returns>A List of strings identifying paths to all resources</returns>
public virtual List<string> GetOriginalPaths()
{
return new List<string>(new string[] { Path });
}
/// <summary>
/// Get paths to artifacts for a specific DataSpace, in the original, unexpanded
/// form
/// </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 virtual List<string> GetOriginalPaths(DataSpace spaceToGet)
{
List<string> list = new List<string>();
if (MetadataArtifactLoader.IsArtifactOfDataSpace(Path, spaceToGet))
{
list.Add(Path);
}
return list;
}
public virtual bool IsComposite
{
get
{
return false;
}
}
/// <summary>
/// Get paths to all artifacts
/// </summary>
/// <returns>A List of strings identifying paths to all resources</returns>
public abstract List<string> GetPaths();
/// <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 abstract List<string> GetPaths(DataSpace spaceToGet);
public List<XmlReader> GetReaders()
{
return GetReaders(null);
}
/// <summary>
/// Get XmlReaders for all resources
/// </summary>
/// <returns>A List of XmlReaders for all resources</returns>
public abstract List<XmlReader> GetReaders(Dictionary<MetadataArtifactLoader, XmlReader> sourceDictionary);
/// <summary>
/// Get XmlReaders for a specific DataSpace.
/// </summary>
/// <param name="spaceToGet">The DataSpace for the artifacts of interest</param>
/// <returns>A List of XmlReader object</returns>
public abstract List<XmlReader> CreateReaders(DataSpace spaceToGet);
/// <summary>
/// Helper method to determine whether a given path to a resource
/// starts with the "res://" prefix.
/// </summary>
/// <param name="path">The resource path to test.</param>
/// <returns>true if the path represents a resource location</returns>
internal static bool PathStartsWithResPrefix(string path)
{
return path.StartsWith(MetadataArtifactLoader.resPathPrefix, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Helper method to determine whether a resource identifies a C-Space
/// artifact.
/// </summary>
/// <param name="resource">The resource path</param>
/// <returns>true if the resource identifies a C-Space artifact</returns>
protected static bool IsCSpaceArtifact(string resource)
{
Debug.Assert(!string.IsNullOrEmpty(resource));
string extn = GetExtension(resource);
if (!string.IsNullOrEmpty(extn))
{
return string.Compare(extn, XmlConstants.CSpaceSchemaExtension, StringComparison.OrdinalIgnoreCase) == 0;
}
return false;
}
/// <summary>
/// Helper method to determine whether a resource identifies an S-Space
/// artifact.
/// </summary>
/// <param name="resource">The resource path</param>
/// <returns>true if the resource identifies an S-Space artifact</returns>
protected static bool IsSSpaceArtifact(string resource)
{
Debug.Assert(!string.IsNullOrEmpty(resource));
string extn = GetExtension(resource);
if (!string.IsNullOrEmpty(extn))
{
return string.Compare(extn, XmlConstants.SSpaceSchemaExtension, StringComparison.OrdinalIgnoreCase) == 0;
}
return false;
}
/// <summary>
/// Helper method to determine whether a resource identifies a CS-Space
/// artifact.
/// </summary>
/// <param name="resource">The resource path</param>
/// <returns>true if the resource identifies a CS-Space artifact</returns>
protected static bool IsCSSpaceArtifact(string resource)
{
Debug.Assert(!string.IsNullOrEmpty(resource));
string extn = GetExtension(resource);
if (!string.IsNullOrEmpty(extn))
{
return string.Compare(extn, XmlConstants.CSSpaceSchemaExtension, StringComparison.OrdinalIgnoreCase) == 0;
}
return false;
}
// don't use Path.GetExtension because it is ok for the resource
// name to have characters in it that would be illegal in a path (ie '<' is illegal in a path)
// and when they do, Path.GetExtension throws and ArgumentException
private static string GetExtension(string resource)
{
if(String.IsNullOrEmpty(resource))
return string.Empty;
int pos = resource.LastIndexOf('.');
if (pos < 0)
return string.Empty;
return resource.Substring(pos);
}
/// <summary>
/// Helper method to determine whether a resource identifies a valid artifact.
/// </summary>
/// <param name="resource">The resource path</param>
/// <returns>true if the resource identifies a valid artifact</returns>
internal static bool IsValidArtifact(string resource)
{
Debug.Assert(!string.IsNullOrEmpty(resource));
string extn = GetExtension(resource);
if (!string.IsNullOrEmpty(extn))
{
return (
string.Compare(extn, XmlConstants.CSpaceSchemaExtension, StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(extn, XmlConstants.SSpaceSchemaExtension, StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(extn, XmlConstants.CSSpaceSchemaExtension, StringComparison.OrdinalIgnoreCase) == 0
);
}
return false;
}
/// <summary>
/// This helper method accepts a resource URI and a value from the DataSpace enum
/// and determines whether the resource identifies an artifact of that DataSpace.
/// </summary>
/// <param name="resource">A URI to an artifact resource</param>
/// <param name="dataSpace">A DataSpace enum value</param>
/// <returns>true if the resource identifies an artifact of the specified DataSpace</returns>
protected static bool IsArtifactOfDataSpace(string resource, DataSpace dataSpace)
{
if (dataSpace == DataSpace.CSpace)
return MetadataArtifactLoader.IsCSpaceArtifact(resource);
if (dataSpace == DataSpace.SSpace)
return MetadataArtifactLoader.IsSSpaceArtifact(resource);
if (dataSpace == DataSpace.CSSpace)
return MetadataArtifactLoader.IsCSSpaceArtifact(resource);
Debug.Assert(false, "Invalid DataSpace specified.");
return false;
}
/// <summary>
/// Normalize a file path:
/// 1. Add backslashes if given a drive letter.
/// 2. Resolve the '~' macro in a Web/ASP.NET environment.
/// 3. Expand the |DataDirectory| macro, if found in the argument.
/// 4. Convert relative paths into absolute paths.
/// </summary>
/// <param name="path">the path to normalize</param>
/// <returns>The normalized file path</returns>
[ResourceExposure(ResourceScope.Machine)] //Exposes the file name which is a Machine resource
[ResourceConsumption(ResourceScope.Machine)] //For Path.GetFullPath method call. But the path is not created in this method.
static internal string NormalizeFilePaths(string path)
{
bool getFullPath = true; // used to determine whether we need to invoke GetFullPath()
if (!String.IsNullOrEmpty(path))
{
path = path.Trim();
// If the path starts with a '~' character, try to resolve it as a Web/ASP.NET
// application path.
//
if (path.StartsWith(EdmConstants.WebHomeSymbol, StringComparison.Ordinal))
{
AspProxy aspProxy = new AspProxy();
path = aspProxy.MapWebPath(path);
getFullPath = false;
}
if (path.Length == 2 && path[1] == System.IO.Path.VolumeSeparatorChar)
{
path = path + System.IO.Path.DirectorySeparatorChar;
}
else
{
// See if the path contains the |DataDirectory| macro that we need to
// expand. Note that ExpandDataDirectory() won't process the path unless
// it begins with the macro.
//
string fullPath = System.Data.EntityClient.DbConnectionOptions.ExpandDataDirectory(
System.Data.EntityClient.EntityConnectionStringBuilder.MetadataParameterName, // keyword ("Metadata")
path // value
);
// ExpandDataDirectory() returns null if it doesn't find the macro in its
// argument.
//
if (fullPath != null)
{
path = fullPath;
getFullPath = false;
}
}
}
try
{
if (getFullPath)
{
path = System.IO.Path.GetFullPath(path);
}
}
catch (ArgumentException e)
{
throw EntityUtil.Metadata(System.Data.Entity.Strings.NotValidInputPath, e);
}
catch (NotSupportedException e)
{
throw EntityUtil.Metadata(System.Data.Entity.Strings.NotValidInputPath, e);
}
catch (PathTooLongException)
{
throw EntityUtil.Metadata(System.Data.Entity.Strings.NotValidInputPath);
}
return path;
}
}
}
|