001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant;
020:
021: import java.io.Serializable;
022: import org.apache.tools.ant.util.FileUtils;
023: import org.xml.sax.Locator;
024:
025: /**
026: * Stores the location of a piece of text within a file (file name,
027: * line number and column number). Note that the column number is
028: * currently ignored.
029: *
030: */
031: public class Location implements Serializable {
032:
033: /** Name of the file. */
034: private String fileName;
035: /** Line number within the file. */
036: private int lineNumber;
037: /** Column number within the file. */
038: private int columnNumber;
039:
040: /** Location to use when one is needed but no information is available */
041: public static final Location UNKNOWN_LOCATION = new Location();
042:
043: private static final FileUtils FILE_UTILS = FileUtils
044: .getFileUtils();
045:
046: /**
047: * Creates an "unknown" location.
048: */
049: private Location() {
050: this (null, 0, 0);
051: }
052:
053: /**
054: * Creates a location consisting of a file name but no line number or
055: * column number.
056: *
057: * @param fileName The name of the file. May be <code>null</code>,
058: * in which case the location is equivalent to
059: * {@link #UNKNOWN_LOCATION UNKNOWN_LOCATION}.
060: */
061: public Location(String fileName) {
062: this (fileName, 0, 0);
063: }
064:
065: /**
066: * Creates a location from the SAX locator using the system ID as
067: * the filename.
068: *
069: * @param loc Must not be <code>null</code>.
070: *
071: * @since Ant 1.6
072: */
073: public Location(Locator loc) {
074: this (loc.getSystemId(), loc.getLineNumber(), loc
075: .getColumnNumber());
076: }
077:
078: /**
079: * Creates a location consisting of a file name, line number and
080: * column number.
081: *
082: * @param fileName The name of the file. May be <code>null</code>,
083: * in which case the location is equivalent to
084: * {@link #UNKNOWN_LOCATION UNKNOWN_LOCATION}.
085: *
086: * @param lineNumber Line number within the file. Use 0 for unknown
087: * positions within a file.
088: * @param columnNumber Column number within the line.
089: */
090: public Location(String fileName, int lineNumber, int columnNumber) {
091: if (fileName != null && fileName.startsWith("file:")) {
092: this .fileName = FILE_UTILS.fromURI(fileName);
093: } else {
094: this .fileName = fileName;
095: }
096: this .lineNumber = lineNumber;
097: this .columnNumber = columnNumber;
098: }
099:
100: /**
101: * @return the filename portion of the location
102: * @since Ant 1.6
103: */
104: public String getFileName() {
105: return fileName;
106: }
107:
108: /**
109: * @return the line number
110: * @since Ant 1.6
111: */
112: public int getLineNumber() {
113: return lineNumber;
114: }
115:
116: /**
117: * @return the column number
118: * @since Ant 1.7
119: */
120: public int getColumnNumber() {
121: return columnNumber;
122: }
123:
124: /**
125: * Returns the file name, line number, a colon and a trailing space.
126: * An error message can be appended easily. For unknown locations, an
127: * empty string is returned.
128: *
129: * @return a String of the form <code>"fileName:lineNumber: "</code>
130: * if both file name and line number are known,
131: * <code>"fileName: "</code> if only the file name is known,
132: * and the empty string for unknown locations.
133: */
134: public String toString() {
135: StringBuffer buf = new StringBuffer();
136:
137: if (fileName != null) {
138: buf.append(fileName);
139:
140: if (lineNumber != 0) {
141: buf.append(":");
142: buf.append(lineNumber);
143: }
144:
145: buf.append(": ");
146: }
147:
148: return buf.toString();
149: }
150:
151: /**
152: * Equality operation.
153: * @param other the object to compare to.
154: * @return true if the other object contains the same information
155: * as this object.
156: * @since Ant 1.6.3
157: */
158: public boolean equals(Object other) {
159: if (this == other) {
160: return true;
161: }
162: if (other == null) {
163: return false;
164: }
165: if (!(other.getClass() == getClass())) {
166: return false;
167: }
168: return toString().equals(other.toString());
169: }
170:
171: /**
172: * Hash operation.
173: * @return a hash code value for this location.
174: * @since Ant 1.6.3
175: */
176: public int hashCode() {
177: return toString().hashCode();
178: }
179: }
|