1 //--------------------------------------------------------------------- 2 // <copyright file="MetadataArtifactLoaderXmlReaderWrapper.cs" company="Microsoft"> 3 // Copyright (c) Microsoft Corporation. All rights reserved. 4 // </copyright> 5 // 6 // @owner Microsoft 7 // @backupOwner Microsoft 8 //--------------------------------------------------------------------- 9 10 using System.Collections.Generic; 11 using System.Collections; 12 using System.Diagnostics; 13 using System.Text; 14 using System.Xml; 15 using System.Security.Permissions; 16 17 18 namespace System.Data.Metadata.Edm 19 { 20 /// <summary> 21 /// This class represents a wrapper around an XmlReader to be used to load metadata. 22 /// Note that the XmlReader object isn't created here -- the wrapper simply stores 23 /// a reference to it -- therefore we do not Close() the reader when we Dispose() 24 /// the wrapper, i.e., Dispose() is a no-op. 25 /// </summary> 26 internal class MetadataArtifactLoaderXmlReaderWrapper : MetadataArtifactLoader, IComparable 27 { 28 private readonly XmlReader _reader = null; 29 private readonly string _resourceUri = null; 30 31 /// <summary> 32 /// Constructor - saves off the XmlReader in a private data field 33 /// </summary> 34 /// <param name="xmlReader">The path to the resource to load</param> MetadataArtifactLoaderXmlReaderWrapper(XmlReader xmlReader)35 public MetadataArtifactLoaderXmlReaderWrapper(XmlReader xmlReader) 36 { 37 _reader = xmlReader; 38 _resourceUri = xmlReader.BaseURI; 39 40 } 41 42 public override string Path 43 { 44 get 45 { 46 if (string.IsNullOrEmpty(this._resourceUri)) 47 { 48 return string.Empty; 49 } 50 else 51 { 52 return this._resourceUri; 53 } 54 } 55 } 56 57 /// <summary> 58 /// Implementation of IComparable.CompareTo() 59 /// </summary> 60 /// <param name="obj">The object to compare to</param> 61 /// <returns>0 if the loaders are "equal" (i.e., have the same _path value)</returns> CompareTo(object obj)62 public int CompareTo(object obj) 63 { 64 MetadataArtifactLoaderXmlReaderWrapper loader = obj as MetadataArtifactLoaderXmlReaderWrapper; 65 if (loader != null) 66 { 67 if (Object.ReferenceEquals(this._reader, loader._reader)) 68 { 69 return 0; 70 } 71 else 72 { 73 return -1; 74 } 75 } 76 77 Debug.Assert(false, "object is not a MetadataArtifactLoaderXmlReaderWrapper"); 78 return -1; 79 } 80 81 /// <summary> 82 /// Equals() returns true if the objects have the same _path value 83 /// </summary> 84 /// <param name="obj">The object to compare to</param> 85 /// <returns>true if the objects have the same _path value</returns> Equals(object obj)86 public override bool Equals(object obj) 87 { 88 return this.CompareTo(obj) == 0; 89 } 90 91 /// <summary> 92 /// GetHashCode override that defers the result to the _path member variable. 93 /// </summary> 94 /// <returns></returns> GetHashCode()95 public override int GetHashCode() 96 { 97 return _reader.GetHashCode(); 98 } 99 CollectFilePermissionPaths(List<string> paths, DataSpace spaceToGet)100 public override void CollectFilePermissionPaths(List<string> paths, DataSpace spaceToGet) 101 { 102 // no op 103 } 104 105 /// <summary> 106 /// Get paths to artifacts for a specific DataSpace. 107 /// </summary> 108 /// <param name="spaceToGet">The DataSpace for the artifacts of interest</param> 109 /// <returns>A List of strings identifying paths to all artifacts for a specific DataSpace</returns> GetPaths(DataSpace spaceToGet)110 public override List<string> GetPaths(DataSpace spaceToGet) 111 { 112 List<string> list = new List<string>(); 113 if (MetadataArtifactLoader.IsArtifactOfDataSpace(Path, spaceToGet)) 114 { 115 list.Add(Path); 116 } 117 return list; 118 } 119 120 /// <summary> 121 /// Get paths to all artifacts 122 /// </summary> 123 /// <returns>A List of strings identifying paths to all resources</returns> GetPaths()124 public override List<string> GetPaths() 125 { 126 return new List<string>(new string[] { Path }); 127 } 128 129 /// <summary> 130 /// Get XmlReaders for all resources 131 /// </summary> 132 /// <returns>A List of XmlReaders for all resources</returns> GetReaders(Dictionary<MetadataArtifactLoader, XmlReader> sourceDictionary)133 public override List<XmlReader> GetReaders(Dictionary<MetadataArtifactLoader, XmlReader> sourceDictionary) 134 { 135 List<XmlReader> list = new List<XmlReader>(); 136 137 list.Add(this._reader); 138 if (sourceDictionary != null) 139 { 140 sourceDictionary.Add(this, _reader); 141 } 142 143 return list; 144 } 145 146 /// <summary> 147 /// Create and return an XmlReader around the resource represented by this instance 148 /// if it is of the requested DataSpace type. 149 /// </summary> 150 /// <param name="spaceToGet">The DataSpace corresponding to the requested artifacts</param> 151 /// <returns>A List of XmlReader objects</returns> CreateReaders(DataSpace spaceToGet)152 public override List<XmlReader> CreateReaders(DataSpace spaceToGet) 153 { 154 List<XmlReader> list = new List<XmlReader>(); 155 156 if (MetadataArtifactLoader.IsArtifactOfDataSpace(Path, spaceToGet)) 157 { 158 list.Add(_reader); 159 } 160 161 return list; 162 } 163 } 164 } 165