1 | /*
|
---|
2 | * Copyright 2000,2002-2005 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 |
|
---|
18 | package org.apache.tools.ant;
|
---|
19 |
|
---|
20 | import java.io.Serializable;
|
---|
21 | import org.apache.tools.ant.util.FileUtils;
|
---|
22 | import org.xml.sax.Locator;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Stores the location of a piece of text within a file (file name,
|
---|
26 | * line number and column number). Note that the column number is
|
---|
27 | * currently ignored.
|
---|
28 | *
|
---|
29 | */
|
---|
30 | public class Location implements Serializable {
|
---|
31 |
|
---|
32 | /** Name of the file. */
|
---|
33 | private String fileName;
|
---|
34 | /** Line number within the file. */
|
---|
35 | private int lineNumber;
|
---|
36 | /** Column number within the file. */
|
---|
37 | private int columnNumber;
|
---|
38 |
|
---|
39 | /** Location to use when one is needed but no information is available */
|
---|
40 | public static final Location UNKNOWN_LOCATION = new Location();
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Creates an "unknown" location.
|
---|
44 | */
|
---|
45 | private Location() {
|
---|
46 | this(null, 0, 0);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Creates a location consisting of a file name but no line number or
|
---|
51 | * column number.
|
---|
52 | *
|
---|
53 | * @param fileName The name of the file. May be <code>null</code>,
|
---|
54 | * in which case the location is equivalent to
|
---|
55 | * {@link #UNKNOWN_LOCATION UNKNOWN_LOCATION}.
|
---|
56 | */
|
---|
57 | public Location(String fileName) {
|
---|
58 | this(fileName, 0, 0);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Creates a location from the SAX locator using the system ID as
|
---|
63 | * the filename.
|
---|
64 | *
|
---|
65 | * @param loc Must not be <code>null</code>.
|
---|
66 | *
|
---|
67 | * @since Ant 1.6
|
---|
68 | */
|
---|
69 | public Location(Locator loc) {
|
---|
70 | this(loc.getSystemId(), loc.getLineNumber(), loc.getColumnNumber());
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Creates a location consisting of a file name, line number and
|
---|
75 | * column number.
|
---|
76 | *
|
---|
77 | * @param fileName The name of the file. May be <code>null</code>,
|
---|
78 | * in which case the location is equivalent to
|
---|
79 | * {@link #UNKNOWN_LOCATION UNKNOWN_LOCATION}.
|
---|
80 | *
|
---|
81 | * @param lineNumber Line number within the file. Use 0 for unknown
|
---|
82 | * positions within a file.
|
---|
83 | * @param columnNumber Column number within the line.
|
---|
84 | */
|
---|
85 | public Location(String fileName, int lineNumber, int columnNumber) {
|
---|
86 | if (fileName != null && fileName.startsWith("file:")) {
|
---|
87 | this.fileName = FileUtils.newFileUtils().fromURI(fileName);
|
---|
88 | } else {
|
---|
89 | this.fileName = fileName;
|
---|
90 | }
|
---|
91 | this.lineNumber = lineNumber;
|
---|
92 | this.columnNumber = columnNumber;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * @return the filename portion of the location
|
---|
97 | * @since Ant 1.6
|
---|
98 | */
|
---|
99 | public String getFileName() {
|
---|
100 | return fileName;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * @return the line number
|
---|
105 | * @since Ant 1.6
|
---|
106 | */
|
---|
107 | public int getLineNumber() {
|
---|
108 | return lineNumber;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Returns the file name, line number, a colon and a trailing space.
|
---|
113 | * An error message can be appended easily. For unknown locations, an
|
---|
114 | * empty string is returned.
|
---|
115 | *
|
---|
116 | * @return a String of the form <code>"fileName:lineNumber: "</code>
|
---|
117 | * if both file name and line number are known,
|
---|
118 | * <code>"fileName: "</code> if only the file name is known,
|
---|
119 | * and the empty string for unknown locations.
|
---|
120 | */
|
---|
121 | public String toString() {
|
---|
122 | StringBuffer buf = new StringBuffer();
|
---|
123 |
|
---|
124 | if (fileName != null) {
|
---|
125 | buf.append(fileName);
|
---|
126 |
|
---|
127 | if (lineNumber != 0) {
|
---|
128 | buf.append(":");
|
---|
129 | buf.append(lineNumber);
|
---|
130 | }
|
---|
131 |
|
---|
132 | buf.append(": ");
|
---|
133 | }
|
---|
134 |
|
---|
135 | return buf.toString();
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Equality operation.
|
---|
140 | * @param other the object to compare to.
|
---|
141 | * @return true if the other object contains the same information
|
---|
142 | * as this object.
|
---|
143 | * @since Ant 1.6.3
|
---|
144 | */
|
---|
145 | public boolean equals(Object other) {
|
---|
146 | if (this == other) {
|
---|
147 | return true;
|
---|
148 | }
|
---|
149 | if (other == null) {
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 | if (!(other.getClass() == getClass())) {
|
---|
153 | return false;
|
---|
154 | }
|
---|
155 | return toString().equals(other.toString());
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Hash operation.
|
---|
160 | * @return a hash code value for this location.
|
---|
161 | * @since Ant 1.6.3
|
---|
162 | */
|
---|
163 | public int hashCode() {
|
---|
164 | return toString().hashCode();
|
---|
165 | }
|
---|
166 | }
|
---|