PageRenderTime 108ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/project/core/Config/preprocessor/XHelpers.cs

https://github.com/psgirard/CruiseControl.NET
C# | 121 lines | 75 code | 8 blank | 38 comment | 8 complexity | 8829a5bb4cf438ca7c06f7e91446524d MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml;
  5. using System.Xml.Linq;
  6. namespace ThoughtWorks.CruiseControl.Core.Config.Preprocessor
  7. {
  8. /// <summary>
  9. /// Extension methods for System.Xml.Linq
  10. /// </summary>
  11. public static class XHelpers
  12. {
  13. /// <summary>
  14. /// Does the element have an attribute of the given name?
  15. /// </summary>
  16. /// <param name="element">element to check</param>
  17. /// <param name="attrName">attribute to check for</param>
  18. /// <returns>true/false</returns>
  19. public static bool HasAttribute(this XElement element, XName attrName)
  20. {
  21. return element.Attribute(attrName) != null;
  22. }
  23. /// <summary>
  24. /// Returns the first sibling that follows the given element.
  25. /// </summary>
  26. /// <param name="element">Element whose sibling is returned</param>
  27. /// <returns>First following sibling element, or null if no siblings exist</returns>
  28. public static XElement NextSiblingElement(this XElement element)
  29. {
  30. return element.ElementsAfterSelf().FirstOrDefault();
  31. }
  32. /// <summary>
  33. /// Extracts file and line/position information from the given object, in displayable form,
  34. /// for use in error messages.
  35. /// </summary>
  36. /// <param name="obj">Xml object for whom to return the context</param>
  37. /// <returns>A string of the form "File: [file_path] line XXX, pos YYY"</returns>
  38. public static string ErrorContext(this XObject obj)
  39. {
  40. string obj_info = "";
  41. var xattribute = obj as XAttribute;
  42. if (xattribute != null)
  43. {
  44. obj_info = string.Format(System.Globalization.CultureInfo.CurrentCulture,"Attribute '{0}'", xattribute.Name);
  45. }
  46. else
  47. {
  48. var xelement = obj as XElement;
  49. if (xelement != null)
  50. {
  51. obj_info = string.Format(System.Globalization.CultureInfo.CurrentCulture,"Element '{0}'", xelement.Name);
  52. }
  53. }
  54. IXmlLineInfo line_info = obj;
  55. string line_and_pos = "line and position unknown";
  56. if (line_info.HasLineInfo())
  57. {
  58. line_and_pos = string.Format(System.Globalization.CultureInfo.CurrentCulture,"line {0}, pos {1}", line_info.LineNumber,
  59. line_info.LinePosition);
  60. }
  61. return string.Format(System.Globalization.CultureInfo.CurrentCulture,"File: {0} ({1}:{2})", obj.BaseUri, line_and_pos, obj_info);
  62. }
  63. /// <summary>
  64. /// Returns the given element's named attribute value as a string
  65. /// </summary>
  66. /// <param name="element">Element whose value is returned</param>
  67. /// <param name="attrName">Name of attribute to return</param>
  68. /// <returns>Attribute value, or empty string if no such attribute exists</returns>
  69. public static string GetAttributeValue(this XElement element, XName attrName)
  70. {
  71. XAttribute attr = element.Attribute(attrName);
  72. return attr == null ? "" : attr.Value;
  73. }
  74. /// <summary>
  75. /// Returns the concatenated text values of the given nodeset
  76. /// </summary>
  77. /// <param name="nodes">Nodeset whose values are returned</param>
  78. /// <returns>Untrimmed, concatenated text values of the given nodeset</returns>
  79. public static string GetTextValue(this IEnumerable<XNode> nodes)
  80. {
  81. return String.Concat(nodes.Select<XNode, String>(_ValueOf).ToArray());
  82. }
  83. private static string _ValueOf(XNode node)
  84. {
  85. switch (node.NodeType)
  86. {
  87. case XmlNodeType.Element:
  88. return ((XElement)node).Value;
  89. case XmlNodeType.Text:
  90. return ((XText)node).Value;
  91. case XmlNodeType.Comment:
  92. case XmlNodeType.ProcessingInstruction:
  93. return String.Empty;
  94. default:
  95. throw new InvalidOperationException(
  96. string.Format(System.Globalization.CultureInfo.CurrentCulture,"{0} Unhandled node type {1}",
  97. node.ErrorContext(),
  98. node.NodeType));
  99. }
  100. }
  101. }
  102. /// <summary>
  103. ///
  104. /// </summary>
  105. public static class XmlNs
  106. {
  107. /// <summary>
  108. ///
  109. /// </summary>
  110. /// <remarks></remarks>
  111. public static XNamespace PreProcessor = XNamespace.Get("urn:ccnet.config.builder");
  112. }
  113. }