2
0

PreservationFile.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. AspxPage = 3,
  39. TopLevelAssembly = 9
  40. }
  41. //
  42. // The attributes of the <preserve> element in a .compiled file are described in
  43. //
  44. // http://msdn.microsoft.com/msdnmag/issues/07/01/cuttingedge/default.aspx?loc=&fig=true#fig4
  45. //
  46. // and a sample file is shown in
  47. //
  48. // http://msdn.microsoft.com/msdnmag/issues/07/01/cuttingedge/default.aspx?loc=&fig=true#fig3
  49. //
  50. class PreservationFile
  51. {
  52. string _filePath;
  53. string _assembly;
  54. Int32 _fileHash;
  55. Int32 _flags;
  56. Int32 _hash;
  57. BuildResultTypeCode _resultType = BuildResultTypeCode.Unknown;
  58. string _virtualPath;
  59. List <string> _filedeps;
  60. public string Assembly {
  61. get { return _assembly; }
  62. set { _assembly = value; }
  63. }
  64. public string FilePath {
  65. get { return _filePath; }
  66. set { _filePath = value; }
  67. }
  68. public Int32 FileHash {
  69. get { return _fileHash; }
  70. set { _fileHash = value; }
  71. }
  72. public int Flags {
  73. get { return _flags; }
  74. set { _flags = value; }
  75. }
  76. public Int32 Hash {
  77. get { return _hash; }
  78. set { _hash = value; }
  79. }
  80. public BuildResultTypeCode ResultType {
  81. get { return _resultType; }
  82. set { _resultType = value; }
  83. }
  84. public string VirtualPath {
  85. get { return _virtualPath; }
  86. set { _virtualPath = value; }
  87. }
  88. public List <string> FileDeps {
  89. get { return _filedeps; }
  90. set { _filedeps = value; }
  91. }
  92. public PreservationFile ()
  93. {
  94. }
  95. public PreservationFile (string filePath)
  96. {
  97. this._filePath = filePath;
  98. Parse (filePath);
  99. }
  100. public void Parse ()
  101. {
  102. if (_filePath == null)
  103. throw new InvalidOperationException ("File path is not defined");
  104. Parse (_filePath);
  105. }
  106. public void Parse (string filePath)
  107. {
  108. if (filePath == null)
  109. throw new ArgumentNullException ("File path is required", "filePath");
  110. XmlDocument doc = new XmlDocument ();
  111. doc.Load (filePath);
  112. XmlNode root = doc.DocumentElement;
  113. if (root.Name != "preserve")
  114. throw new InvalidOperationException ("Invalid assembly mapping file format");
  115. ParseRecursively (root);
  116. }
  117. void ParseRecursively (XmlNode root)
  118. {
  119. _assembly = GetNonEmptyRequiredAttribute (root, "assembly");
  120. // The rest of the values is optional for us and since we don't use them
  121. // at all (at least for now) we also ignore all the integer parsing errors
  122. try {
  123. _virtualPath = GetNonEmptyOptionalAttribute (root, "virtualPath");
  124. _fileHash = GetNonEmptyOptionalAttributeInt32 (root, "filehash");
  125. _hash = GetNonEmptyOptionalAttributeInt32 (root, "hash");
  126. _flags = GetNonEmptyOptionalAttributeInt32 (root, "flags");
  127. _resultType = (BuildResultTypeCode) GetNonEmptyOptionalAttributeInt32 (root, "resultType");
  128. foreach (XmlNode child in root.ChildNodes) {
  129. if (child.NodeType != XmlNodeType.Element)
  130. continue;
  131. if (child.Name != "filedeps")
  132. continue;
  133. ReadFileDeps (child);
  134. }
  135. } catch (Exception) {
  136. }
  137. }
  138. void ReadFileDeps (XmlNode node)
  139. {
  140. string tmp;
  141. if (_filedeps == null)
  142. _filedeps = new List <string> ();
  143. foreach (XmlNode child in node.ChildNodes) {
  144. if (child.NodeType != XmlNodeType.Element)
  145. continue;
  146. if (child.Name != "filedep")
  147. continue;
  148. tmp = GetNonEmptyRequiredAttribute (child, "name");
  149. _filedeps.Add (tmp);
  150. }
  151. }
  152. public void Save ()
  153. {
  154. if (_filePath == null)
  155. throw new InvalidOperationException ("File path is not defined");
  156. Save (_filePath);
  157. }
  158. public void Save (string filePath)
  159. {
  160. if (filePath == null)
  161. throw new ArgumentNullException ("File path is required", "filePath");
  162. XmlWriterSettings xmlSettings = new XmlWriterSettings ();
  163. xmlSettings.Indent = false;
  164. xmlSettings.OmitXmlDeclaration = false;
  165. xmlSettings.NewLineOnAttributes = false;
  166. using (XmlWriter xml = XmlWriter.Create (filePath, xmlSettings)) {
  167. xml.WriteStartElement ("preserve");
  168. xml.WriteAttributeString ("assembly", _assembly);
  169. if (!String.IsNullOrEmpty (_virtualPath))
  170. xml.WriteAttributeString ("virtualPath", _virtualPath);
  171. if (_fileHash != 0)
  172. xml.WriteAttributeString ("filehash", _fileHash.ToString ());
  173. if (_flags != 0)
  174. xml.WriteAttributeString ("flags", _flags.ToString ());
  175. if (_hash != 0)
  176. xml.WriteAttributeString ("hash", _hash.ToString ());
  177. if (_resultType != BuildResultTypeCode.Unknown)
  178. xml.WriteAttributeString ("resultType", ((int)_resultType).ToString ());
  179. if (_filedeps != null && _filedeps.Count > 0) {
  180. xml.WriteStartElement ("filedeps");
  181. foreach (string s in _filedeps) {
  182. xml.WriteStartElement ("filedep");
  183. xml.WriteAttributeString ("name", s);
  184. xml.WriteEndElement ();
  185. }
  186. xml.WriteEndElement ();
  187. }
  188. xml.WriteEndElement ();
  189. }
  190. }
  191. string GetNonEmptyOptionalAttribute (XmlNode n, string name)
  192. {
  193. return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true);
  194. }
  195. Int32 GetNonEmptyOptionalAttributeInt32 (XmlNode n, string name)
  196. {
  197. string tmp = GetNonEmptyOptionalAttribute (n, name);
  198. if (tmp != null)
  199. return Int32.Parse (tmp);
  200. return 0;
  201. }
  202. string GetNonEmptyRequiredAttribute (XmlNode n, string name)
  203. {
  204. return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, false, false);
  205. }
  206. }
  207. }