PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/ABB.SrcML.Data/SrcMLLocation.cs

https://github.com/nkcsgexi/SrcML.NET
C# | 108 lines | 57 code | 11 blank | 40 comment | 20 complexity | 0fdd482b35579b62469ea9af8b455c17 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. namespace ABB.SrcML.Data {
  7. /// <summary>
  8. /// Represents a location in a SrcML document.
  9. /// This extends SourceLocation to include an XPath, and other relevant properties.
  10. /// </summary>
  11. [Serializable]
  12. public class SrcMLLocation : SourceLocation {
  13. /// <summary>
  14. /// The XPath query that identifies this scope
  15. /// </summary>
  16. public string XPath { get; set; }
  17. /// <summary>
  18. /// True if this location is a reference; false if it is a definition
  19. /// </summary>
  20. public bool IsReference { get; set; }
  21. /// <summary>
  22. /// Creates a new srcML location object
  23. /// </summary>
  24. /// <param name="element">The srcML element that this location refers to</param>
  25. /// <param name="fileName">The filename</param>
  26. public SrcMLLocation(XElement element, string fileName) : this(element, fileName, false) { }
  27. /// <summary>
  28. /// Creates a new srcML location object based on the given <see cref="System.Xml.Linq.XElement">XML element</see> and <see cref="ABB.SrcML.SRC.Unit">file unit</see>
  29. /// </summary>
  30. /// <param name="element">The element (should contain <see cref="ABB.SrcML.POS"/> attributes</param>
  31. /// <param name="fileUnit">The file unit (must be a <see cref="ABB.SrcML.SRC.Unit"/>)</param>
  32. public SrcMLLocation(XElement element, XElement fileUnit) : this(element, fileUnit, false) { }
  33. /// <summary>
  34. /// Creates a new srcML location object
  35. /// </summary>
  36. /// <param name="element">The srcML element that this location refers to</param>
  37. /// <param name="fileUnit">The file unit that contains <paramref name="element"/></param>
  38. /// <param name="isReferenceLocation">true if this is a reference location; false otherwise</param>
  39. public SrcMLLocation(XElement element, XElement fileUnit, bool isReferenceLocation) {
  40. if(element == null) throw new ArgumentNullException("element");
  41. if(fileUnit == null) throw new ArgumentNullException("fileUnit");
  42. this.SourceFileName = SrcMLElement.GetFileNameForUnit(fileUnit);
  43. this.StartingLineNumber = element.GetSrcLineNumber();
  44. this.StartingColumnNumber = element.GetSrcLinePosition();
  45. this.XPath = element.GetXPath(false);
  46. this.IsReference = isReferenceLocation;
  47. SetEndingLocation(element);
  48. }
  49. /// <summary>
  50. /// Creates a new srcML location object
  51. /// </summary>
  52. /// <param name="element">The srcML element that this location refers to</param>
  53. /// <param name="fileName">The filename</param>
  54. /// <param name="isReferenceLocation">true if this is a reference location; false otherwise</param>
  55. public SrcMLLocation(XElement element, string fileName, bool isReferenceLocation) {
  56. if(element == null) throw new ArgumentNullException("element");
  57. this.SourceFileName = fileName;
  58. this.StartingLineNumber = element.GetSrcLineNumber();
  59. this.StartingColumnNumber = element.GetSrcLinePosition();
  60. this.XPath = element.GetXPath(false);
  61. this.IsReference = isReferenceLocation;
  62. SetEndingLocation(element);
  63. }
  64. /// <summary>
  65. /// Determines whether the given source location occurs within this location.
  66. /// This will be determined using the XPath, if set.
  67. /// </summary>
  68. /// <param name="otherLoc">The SourceLocation to test</param>
  69. /// <returns>True if this location subsumes the given location, False otherwise.</returns>
  70. public override bool Contains(SourceLocation otherLoc) {
  71. if(otherLoc == null) throw new ArgumentNullException("otherLoc");
  72. var otherSrcMLLoc = otherLoc as SrcMLLocation;
  73. if(otherSrcMLLoc != null && !string.IsNullOrWhiteSpace(XPath) && !string.IsNullOrWhiteSpace(otherSrcMLLoc.XPath)) {
  74. //return XPath.StartsWith(otherSrcMLLoc.XPath);
  75. return otherSrcMLLoc.XPath.StartsWith(this.XPath);
  76. }
  77. return base.Contains(otherLoc);
  78. }
  79. private void SetEndingLocation(XElement element) {
  80. if(element == null) throw new ArgumentNullException("element");
  81. var current = element;
  82. XElement nextSibling = null;
  83. //navigate up until we find a sibling (or the top of the file)
  84. while(nextSibling == null && current != null) {
  85. nextSibling = current.ElementsAfterSelf().FirstOrDefault();
  86. current = current.Parent;
  87. }
  88. if(null != nextSibling) {
  89. this.EndingLineNumber = nextSibling.GetSrcLineNumber();
  90. this.EndingColumnNumber = nextSibling.GetSrcLinePosition();
  91. } else {
  92. this.EndingLineNumber = int.MaxValue;
  93. this.EndingColumnNumber = int.MaxValue;
  94. }
  95. }
  96. }
  97. }