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