1. /*
  2. * Copyright 2000,2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant;
  18. import java.io.Serializable;
  19. import org.apache.tools.ant.util.FileUtils;
  20. import org.xml.sax.Locator;
  21. /**
  22. * Stores the location of a piece of text within a file (file name,
  23. * line number and column number). Note that the column number is
  24. * currently ignored.
  25. *
  26. */
  27. public class Location implements Serializable {
  28. /** Name of the file. */
  29. private String fileName;
  30. /** Line number within the file. */
  31. private int lineNumber;
  32. /** Column number within the file. */
  33. private int columnNumber;
  34. /** Location to use when one is needed but no information is available */
  35. public static final Location UNKNOWN_LOCATION = new Location();
  36. /**
  37. * Creates an "unknown" location.
  38. */
  39. private Location() {
  40. this(null, 0, 0);
  41. }
  42. /**
  43. * Creates a location consisting of a file name but no line number or
  44. * column number.
  45. *
  46. * @param fileName The name of the file. May be <code>null</code>,
  47. * in which case the location is equivalent to
  48. * {@link #UNKNOWN_LOCATION UNKNOWN_LOCATION}.
  49. */
  50. public Location(String fileName) {
  51. this(fileName, 0, 0);
  52. }
  53. /**
  54. * Creates a location from the SAX locator using the system ID as
  55. * the filename.
  56. *
  57. * @param loc Must not be <code>null</code>.
  58. *
  59. * @since Ant 1.6
  60. */
  61. public Location(Locator loc) {
  62. this(loc.getSystemId(), loc.getLineNumber(), loc.getColumnNumber());
  63. }
  64. /**
  65. * Creates a location consisting of a file name, line number and
  66. * column number.
  67. *
  68. * @param fileName The name of the file. May be <code>null</code>,
  69. * in which case the location is equivalent to
  70. * {@link #UNKNOWN_LOCATION UNKNOWN_LOCATION}.
  71. *
  72. * @param lineNumber Line number within the file. Use 0 for unknown
  73. * positions within a file.
  74. * @param columnNumber Column number within the line.
  75. */
  76. public Location(String fileName, int lineNumber, int columnNumber) {
  77. if (fileName != null && fileName.startsWith("file:")) {
  78. this.fileName = FileUtils.newFileUtils().fromURI(fileName);
  79. } else {
  80. this.fileName = fileName;
  81. }
  82. this.lineNumber = lineNumber;
  83. this.columnNumber = columnNumber;
  84. }
  85. /**
  86. * @return the filename portion of the location
  87. * @since Ant 1.6
  88. */
  89. public String getFileName() {
  90. return fileName;
  91. }
  92. /**
  93. * @return the line number
  94. * @since Ant 1.6
  95. */
  96. public int getLineNumber() {
  97. return lineNumber;
  98. }
  99. /**
  100. * Returns the file name, line number, a colon and a trailing space.
  101. * An error message can be appended easily. For unknown locations, an
  102. * empty string is returned.
  103. *
  104. * @return a String of the form <code>"fileName: lineNumber: "</code>
  105. * if both file name and line number are known,
  106. * <code>"fileName: "</code> if only the file name is known,
  107. * and the empty string for unknown locations.
  108. */
  109. public String toString() {
  110. StringBuffer buf = new StringBuffer();
  111. if (fileName != null) {
  112. buf.append(fileName);
  113. if (lineNumber != 0) {
  114. buf.append(":");
  115. buf.append(lineNumber);
  116. }
  117. buf.append(": ");
  118. }
  119. return buf.toString();
  120. }
  121. }