2
0

PreservationFile.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // System.Web.Compilation.AppCodeCompiler: A compiler for the App_Code folder
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2006 Marek Habersack
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using System.Xml;
  33. namespace System.Web.Compilation
  34. {
  35. enum BuildResultTypeCode
  36. {
  37. Unknown = 0,
  38. AppCodeSubFolder = 1,
  39. Handler = 2,
  40. PageOrControl = 3,
  41. AppCode = 6,
  42. Global = 8,
  43. TopLevelAssembly = 9
  44. }
  45. //
  46. // The attributes of the <preserve> element in a .compiled file are described in
  47. //
  48. // http://msdn.microsoft.com/msdnmag/issues/07/01/cuttingedge/default.aspx?loc=&fig=true#fig4
  49. //
  50. // and a sample file is shown in
  51. //
  52. // http://msdn.microsoft.com/msdnmag/issues/07/01/cuttingedge/default.aspx?loc=&fig=true#fig3
  53. //
  54. class PreservationFile
  55. {
  56. string _filePath;
  57. string _assembly;
  58. Int32 _fileHash;
  59. Int32 _flags;
  60. Int32 _hash;
  61. BuildResultTypeCode _resultType = BuildResultTypeCode.Unknown;
  62. string _virtualPath;
  63. List <string> _filedeps;
  64. public string Assembly {
  65. get { return _assembly; }
  66. set { _assembly = value; }
  67. }
  68. public string FilePath {
  69. get { return _filePath; }
  70. set { _filePath = value; }
  71. }
  72. public Int32 FileHash {
  73. get { return _fileHash; }
  74. set { _fileHash = value; }
  75. }
  76. public int Flags {
  77. get { return _flags; }
  78. set { _flags = value; }
  79. }
  80. public Int32 Hash {
  81. get { return _hash; }
  82. set { _hash = value; }
  83. }
  84. public BuildResultTypeCode ResultType {
  85. get { return _resultType; }
  86. set { _resultType = value; }
  87. }
  88. public string VirtualPath {
  89. get { return _virtualPath; }
  90. set { _virtualPath = value; }
  91. }
  92. public List <string> FileDeps {
  93. get { return _filedeps; }
  94. set { _filedeps = value; }
  95. }
  96. public PreservationFile ()
  97. {
  98. }
  99. public PreservationFile (string filePath)
  100. {
  101. this._filePath = filePath;
  102. Parse (filePath);
  103. }
  104. public void Parse ()
  105. {
  106. if (_filePath == null)
  107. throw new InvalidOperationException ("File path is not defined");
  108. Parse (_filePath);
  109. }
  110. public void Parse (string filePath)
  111. {
  112. if (filePath == null)
  113. throw new ArgumentNullException ("File path is required", "filePath");
  114. XmlDocument doc = new XmlDocument ();
  115. doc.Load (filePath);
  116. XmlNode root = doc.DocumentElement;
  117. if (root.Name != "preserve")
  118. throw new InvalidOperationException ("Invalid assembly mapping file format");
  119. ParseRecursively (root);
  120. }
  121. void ParseRecursively (XmlNode root)
  122. {
  123. _assembly = GetNonEmptyRequiredAttribute (root, "assembly");
  124. // The rest of the values is optional for us and since we don't use them
  125. // at all (at least for now) we also ignore all the integer parsing errors
  126. try {
  127. _virtualPath = GetNonEmptyOptionalAttribute (root, "virtualPath");
  128. _fileHash = GetNonEmptyOptionalAttributeInt32 (root, "filehash");
  129. _hash = GetNonEmptyOptionalAttributeInt32 (root, "hash");
  130. _flags = GetNonEmptyOptionalAttributeInt32 (root, "flags");
  131. _resultType = (BuildResultTypeCode) GetNonEmptyOptionalAttributeInt32 (root, "resultType");
  132. foreach (XmlNode child in root.ChildNodes) {
  133. if (child.NodeType != XmlNodeType.Element)
  134. continue;
  135. if (child.Name != "filedeps")
  136. continue;
  137. ReadFileDeps (child);
  138. }
  139. } catch (Exception) {
  140. }
  141. }
  142. void ReadFileDeps (XmlNode node)
  143. {
  144. string tmp;
  145. if (_filedeps == null)
  146. _filedeps = new List <string> ();
  147. foreach (XmlNode child in node.ChildNodes) {
  148. if (child.NodeType != XmlNodeType.Element)
  149. continue;
  150. if (child.Name != "filedep")
  151. continue;
  152. tmp = GetNonEmptyRequiredAttribute (child, "name");
  153. _filedeps.Add (tmp);
  154. }
  155. }
  156. public void Save ()
  157. {
  158. if (_filePath == null)
  159. throw new InvalidOperationException ("File path is not defined");
  160. Save (_filePath);
  161. }
  162. public void Save (string filePath)
  163. {
  164. if (filePath == null)
  165. throw new ArgumentNullException ("File path is required", "filePath");
  166. XmlWriterSettings xmlSettings = new XmlWriterSettings ();
  167. xmlSettings.Indent = false;
  168. xmlSettings.OmitXmlDeclaration = false;
  169. xmlSettings.NewLineOnAttributes = false;
  170. using (XmlWriter xml = XmlWriter.Create (filePath, xmlSettings)) {
  171. xml.WriteStartElement ("preserve");
  172. xml.WriteAttributeString ("assembly", _assembly);
  173. if (!String.IsNullOrEmpty (_virtualPath))
  174. xml.WriteAttributeString ("virtualPath", _virtualPath);
  175. if (_fileHash != 0)
  176. xml.WriteAttributeString ("filehash", _fileHash.ToString ());
  177. if (_flags != 0)
  178. xml.WriteAttributeString ("flags", _flags.ToString ());
  179. if (_hash != 0)
  180. xml.WriteAttributeString ("hash", _hash.ToString ());
  181. if (_resultType != BuildResultTypeCode.Unknown)
  182. xml.WriteAttributeString ("resultType", ((int)_resultType).ToString ());
  183. if (_filedeps != null && _filedeps.Count > 0) {
  184. xml.WriteStartElement ("filedeps");
  185. foreach (string s in _filedeps) {
  186. xml.WriteStartElement ("filedep");
  187. xml.WriteAttributeString ("name", s);
  188. xml.WriteEndElement ();
  189. }
  190. xml.WriteEndElement ();
  191. }
  192. xml.WriteEndElement ();
  193. }
  194. }
  195. string GetNonEmptyOptionalAttribute (XmlNode n, string name)
  196. {
  197. return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true);
  198. }
  199. Int32 GetNonEmptyOptionalAttributeInt32 (XmlNode n, string name)
  200. {
  201. string tmp = GetNonEmptyOptionalAttribute (n, name);
  202. if (tmp != null)
  203. return Int32.Parse (tmp);
  204. return 0;
  205. }
  206. string GetNonEmptyRequiredAttribute (XmlNode n, string name)
  207. {
  208. return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, false, false);
  209. }
  210. }
  211. }