/Project/Test/AddIns/CSharp/Parser/CSharpParserTestRules/CsParserDump.cs

# · C# · 273 lines · 129 code · 41 blank · 103 comment · 9 complexity · 3b5130bfc5174b2f956d0c23bde1804a MD5 · raw file

  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="CsParserDump.cs" company="http://stylecop.codeplex.com">
  3. // MS-PL
  4. // </copyright>
  5. // <license>
  6. // This source code is subject to terms and conditions of the Microsoft
  7. // Public License. A copy of the license can be found in the License.html
  8. // file at the root of this distribution. If you cannot locate the
  9. // Microsoft Public License, please send an email to dlr@microsoft.com.
  10. // By using this source code in any fashion, you are agreeing to be bound
  11. // by the terms of the Microsoft Public License. You must not remove this
  12. // notice, or any other, from this software.
  13. // </license>
  14. // <summary>
  15. // Dumps the parsed object model from the CsParser into an Xml file.
  16. // </summary>
  17. // --------------------------------------------------------------------------------------------------------------------
  18. namespace StyleCop.CSharpParserTest
  19. {
  20. using System;
  21. using System.IO;
  22. using System.Xml;
  23. using StyleCop.CSharp;
  24. /// <summary>
  25. /// Dumps the parsed object model from the CsParser into an Xml file.
  26. /// </summary>
  27. [SourceAnalyzer(typeof(CsParser))]
  28. public class CsParserDump : SourceAnalyzer
  29. {
  30. #region Constructors and Destructors
  31. /// <summary>
  32. /// Initializes a new instance of the CsParserDump class.
  33. /// </summary>
  34. public CsParserDump()
  35. {
  36. }
  37. #endregion
  38. #region Public Methods
  39. /// <summary>
  40. /// Checks the placement of brackets within the given document.
  41. /// </summary>
  42. /// <param name="document">
  43. /// The document to check.
  44. /// </param>
  45. public override void AnalyzeDocument(CodeDocument document)
  46. {
  47. Param.RequireNotNull(document, "document");
  48. XmlDocument contents = new XmlDocument();
  49. XmlNode root = contents.CreateElement("StyleCopCsParserObjectModel");
  50. contents.AppendChild(root);
  51. CsDocument csdocument = (CsDocument)document;
  52. if (csdocument.RootElement != null)
  53. {
  54. this.ProcessElement(csdocument.RootElement, root);
  55. }
  56. // Get the location where the output file should be stored.
  57. string testOutputDirectory = (string)this.Core.HostTag;
  58. if (string.IsNullOrEmpty(testOutputDirectory))
  59. {
  60. throw new InvalidOperationException("The HostTag has not been properly set in StyleCopCore.");
  61. }
  62. // Save the output to the file.
  63. string outputFileLocation = Path.Combine(testOutputDirectory, Path.GetFileNameWithoutExtension(document.SourceCode.Path) + "ObjectModelResults.xml");
  64. contents.Save(outputFileLocation);
  65. }
  66. #endregion
  67. #region Methods
  68. /// <summary>
  69. /// Records information about the given element, under the given node.
  70. /// </summary>
  71. /// <param name="element">
  72. /// The element to record.
  73. /// </param>
  74. /// <param name="parentNode">
  75. /// The Xml node to record this element beneath.
  76. /// </param>
  77. /// <returns>
  78. /// Returns the new Xml node describing this element.
  79. /// </returns>
  80. private static XmlNode RecordElement(CsElement element, XmlNode parentNode)
  81. {
  82. Param.AssertNotNull(element, "element");
  83. Param.AssertNotNull(parentNode, "parentNode");
  84. // Create a new node for this element and add it to the parent.
  85. XmlNode elementNode = parentNode.OwnerDocument.CreateElement("Element");
  86. parentNode.AppendChild(elementNode);
  87. // Add the name and type of the element.
  88. XmlAttribute name = parentNode.OwnerDocument.CreateAttribute("Name");
  89. name.Value = element.Declaration.Name;
  90. elementNode.Attributes.Append(name);
  91. XmlAttribute type = parentNode.OwnerDocument.CreateAttribute("Type");
  92. type.Value = element.GetType().Name;
  93. elementNode.Attributes.Append(type);
  94. return elementNode;
  95. }
  96. /// <summary>
  97. /// Records information about the given expression, under the given node.
  98. /// </summary>
  99. /// <param name="expression">
  100. /// The expression to record.
  101. /// </param>
  102. /// <param name="parentNode">
  103. /// The Xml node to record this expression beneath.
  104. /// </param>
  105. /// <returns>
  106. /// Returns the new Xml node describing this expression.
  107. /// </returns>
  108. private static XmlNode RecordExpression(Expression expression, XmlNode parentNode)
  109. {
  110. Param.AssertNotNull(expression, "expression");
  111. Param.AssertNotNull(parentNode, "parentNode");
  112. // Create a new node for this expression and add it to the parent.
  113. XmlNode expressionNode = parentNode.OwnerDocument.CreateElement("Expression");
  114. parentNode.AppendChild(expressionNode);
  115. // Add the name and contents of the expression.
  116. if (expression.ExpressionType == ExpressionType.Literal)
  117. {
  118. XmlAttribute text = parentNode.OwnerDocument.CreateAttribute("Text");
  119. text.Value = expression.ToString();
  120. expressionNode.Attributes.Append(text);
  121. }
  122. XmlAttribute type = parentNode.OwnerDocument.CreateAttribute("Type");
  123. type.Value = expression.GetType().Name;
  124. expressionNode.Attributes.Append(type);
  125. return expressionNode;
  126. }
  127. /// <summary>
  128. /// Records information about the given statement, under the given node.
  129. /// </summary>
  130. /// <param name="statement">
  131. /// The statement to record.
  132. /// </param>
  133. /// <param name="parentNode">
  134. /// The Xml node to record this statement beneath.
  135. /// </param>
  136. /// <returns>
  137. /// Returns the new Xml node describing this statement.
  138. /// </returns>
  139. private static XmlNode RecordStatement(Statement statement, XmlNode parentNode)
  140. {
  141. Param.AssertNotNull(statement, "statement");
  142. Param.AssertNotNull(parentNode, "parentNode");
  143. // Create a new node for this statement and add it to the parent.
  144. XmlNode statementNode = parentNode.OwnerDocument.CreateElement("Statement");
  145. parentNode.AppendChild(statementNode);
  146. // Add the name and contents of the statement.
  147. ////XmlAttribute text = parentNode.OwnerDocument.CreateAttribute("Text");
  148. ////text.Value = statement.ToString();
  149. ////statementNode.Attributes.Append(text);
  150. XmlAttribute type = parentNode.OwnerDocument.CreateAttribute("Type");
  151. type.Value = statement.GetType().Name;
  152. statementNode.Attributes.Append(type);
  153. return statementNode;
  154. }
  155. /// <summary>
  156. /// Processes the given element and its children.
  157. /// </summary>
  158. /// <param name="element">
  159. /// The element to process.
  160. /// </param>
  161. /// <param name="parentNode">
  162. /// The Xml node to record this element under.
  163. /// </param>
  164. private void ProcessElement(CsElement element, XmlNode parentNode)
  165. {
  166. Param.AssertNotNull(element, "element");
  167. Param.AssertNotNull(parentNode, "parentNode");
  168. XmlNode elementNode = RecordElement(element, parentNode);
  169. foreach (Statement statement in element.ChildStatements)
  170. {
  171. this.ProcessStatement(statement, elementNode);
  172. }
  173. foreach (CsElement child in element.ChildElements)
  174. {
  175. this.ProcessElement(child, elementNode);
  176. }
  177. }
  178. /// <summary>
  179. /// Processes the given expression and its child statements and expressions.
  180. /// </summary>
  181. /// <param name="expression">
  182. /// The expression to process.
  183. /// </param>
  184. /// <param name="parentNode">
  185. /// The Xml node to record this expression under.
  186. /// </param>
  187. private void ProcessExpression(Expression expression, XmlNode parentNode)
  188. {
  189. Param.AssertNotNull(expression, "expression");
  190. Param.AssertNotNull(parentNode, "parentNode");
  191. XmlNode expressionNode = RecordExpression(expression, parentNode);
  192. foreach (Expression childExpression in expression.ChildExpressions)
  193. {
  194. this.ProcessExpression(childExpression, expressionNode);
  195. }
  196. foreach (Statement childStatement in expression.ChildStatements)
  197. {
  198. this.ProcessStatement(childStatement, expressionNode);
  199. }
  200. }
  201. /// <summary>
  202. /// Processes the given statement and its child statements and expressions.
  203. /// </summary>
  204. /// <param name="statement">
  205. /// The statement to process.
  206. /// </param>
  207. /// <param name="parentNode">
  208. /// The Xml node to record this statement under.
  209. /// </param>
  210. private void ProcessStatement(Statement statement, XmlNode parentNode)
  211. {
  212. Param.AssertNotNull(statement, "statement");
  213. Param.AssertNotNull(parentNode, "parentNode");
  214. XmlNode statementNode = RecordStatement(statement, parentNode);
  215. if (statement.ChildExpressions != null)
  216. {
  217. foreach (Expression expression in statement.ChildExpressions)
  218. {
  219. this.ProcessExpression(expression, statementNode);
  220. }
  221. }
  222. if (statement.ChildStatements != null)
  223. {
  224. foreach (Statement childStatement in statement.ChildStatements)
  225. {
  226. this.ProcessStatement(childStatement, statementNode);
  227. }
  228. }
  229. }
  230. #endregion
  231. }
  232. }