You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SoftSourceLocation.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC),
  4. * 2004 Contributors.
  5. * All rights reserved.
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Public License v 2.0
  8. * which accompanies this distribution and is available at
  9. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  10. *
  11. * Contributors:
  12. * Xerox/PARC initial implementation
  13. * Wes Isberg 2004 updates
  14. * ******************************************************************/
  15. package org.aspectj.testing.xml;
  16. import java.io.File;
  17. import org.aspectj.bridge.ISourceLocation;
  18. import org.aspectj.util.LangUtil;
  19. /**
  20. * A mutable ISourceLocation for XML initialization of tests.
  21. * This does not support reading/writing of the attributes
  22. * column, context, or endline.
  23. */
  24. public class SoftSourceLocation implements ISourceLocation {
  25. public static final File NONE = ISourceLocation.NO_FILE;
  26. public static final String XMLNAME = "source";
  27. /**
  28. * Write an ISourceLocation as XML element to an XMLWriter sink.
  29. * @param out the XMLWriter sink
  30. * @param sl the ISourceLocation to write.
  31. */
  32. public static void writeXml(XMLWriter out, ISourceLocation sl) {
  33. if ((null == out) || (null == sl)) {
  34. return;
  35. }
  36. final String elementName = XMLNAME;
  37. out.startElement(elementName, false);
  38. out.printAttribute("line", "" + sl.getLine());
  39. // other attributes not supported
  40. File file = sl.getSourceFile();
  41. if ((null != file) && !ISourceLocation.NO_FILE.equals(file)) {
  42. String value = XMLWriter.attributeValue(file.getPath());
  43. out.printAttribute("file", value);
  44. }
  45. out.endElement(elementName);
  46. }
  47. private File sourceFile;
  48. private int line = -1; // required for no-line comparisons to work
  49. private int column;
  50. private int endLine;
  51. private String context;
  52. public SoftSourceLocation() {
  53. }
  54. public File getSourceFile() {
  55. return (null != sourceFile ? sourceFile : NONE);
  56. }
  57. public int getLine() {
  58. return line;
  59. }
  60. public int getColumn() {
  61. return column;
  62. }
  63. public int getEndLine() {
  64. return line;
  65. }
  66. public String getContext() {
  67. return context;
  68. }
  69. public void setFile(String sourceFile) {
  70. this.sourceFile = new File(sourceFile);
  71. }
  72. public void setLine(String line) {
  73. setLineAsString(line);
  74. }
  75. public void setLineAsString(String line) {
  76. this.line = convert(line);
  77. if (0 == endLine) {
  78. endLine = this.line;
  79. }
  80. }
  81. public String getLineAsString() {
  82. return ""+line;
  83. }
  84. public void setColumn(String column) {
  85. this.column = convert(column);
  86. }
  87. public void setEndLine(String line) {
  88. this.endLine = convert(line);
  89. }
  90. public void setContext(String context) {
  91. this.context = context;
  92. }
  93. private int convert(String in) {
  94. return Integer.parseInt(in);
  95. }
  96. public String getLocationContext() {
  97. return null;
  98. }
  99. public int getOffset() {
  100. return -1;
  101. }
  102. /** @return String : {context\n}file:line:column */
  103. public String toString() {
  104. return (null == context ? "" : context + LangUtil.EOL)
  105. + getSourceFile().getPath()
  106. + ":" + getLine() ;
  107. }
  108. public String getSourceFileName() {
  109. return null;
  110. }
  111. }