KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > Location


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant;
20
21 import java.io.Serializable JavaDoc;
22 import org.apache.tools.ant.util.FileUtils;
23 import org.xml.sax.Locator JavaDoc;
24
25 /**
26  * Stores the location of a piece of text within a file (file name,
27  * line number and column number). Note that the column number is
28  * currently ignored.
29  *
30  */

31 public class Location implements Serializable JavaDoc {
32
33     /** Name of the file. */
34     private String JavaDoc fileName;
35     /** Line number within the file. */
36     private int lineNumber;
37     /** Column number within the file. */
38     private int columnNumber;
39
40     /** Location to use when one is needed but no information is available */
41     public static final Location UNKNOWN_LOCATION = new Location();
42
43     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
44
45     /**
46      * Creates an "unknown" location.
47      */

48     private Location() {
49         this(null, 0, 0);
50     }
51
52     /**
53      * Creates a location consisting of a file name but no line number or
54      * column number.
55      *
56      * @param fileName The name of the file. May be <code>null</code>,
57      * in which case the location is equivalent to
58      * {@link #UNKNOWN_LOCATION UNKNOWN_LOCATION}.
59      */

60     public Location(String JavaDoc fileName) {
61         this(fileName, 0, 0);
62     }
63
64     /**
65      * Creates a location from the SAX locator using the system ID as
66      * the filename.
67      *
68      * @param loc Must not be <code>null</code>.
69      *
70      * @since Ant 1.6
71      */

72     public Location(Locator JavaDoc loc) {
73         this(loc.getSystemId(), loc.getLineNumber(), loc.getColumnNumber());
74     }
75
76     /**
77      * Creates a location consisting of a file name, line number and
78      * column number.
79      *
80      * @param fileName The name of the file. May be <code>null</code>,
81      * in which case the location is equivalent to
82      * {@link #UNKNOWN_LOCATION UNKNOWN_LOCATION}.
83      *
84      * @param lineNumber Line number within the file. Use 0 for unknown
85      * positions within a file.
86      * @param columnNumber Column number within the line.
87      */

88     public Location(String JavaDoc fileName, int lineNumber, int columnNumber) {
89         if (fileName != null && fileName.startsWith("file:")) {
90             this.fileName = FILE_UTILS.fromURI(fileName);
91         } else {
92             this.fileName = fileName;
93         }
94         this.lineNumber = lineNumber;
95         this.columnNumber = columnNumber;
96     }
97
98     /**
99      * @return the filename portion of the location
100      * @since Ant 1.6
101      */

102     public String JavaDoc getFileName() {
103         return fileName;
104     }
105
106     /**
107      * @return the line number
108      * @since Ant 1.6
109      */

110     public int getLineNumber() {
111         return lineNumber;
112     }
113
114     /**
115      * @return the column number
116      * @since Ant 1.7
117      */

118     public int getColumnNumber() {
119         return columnNumber;
120     }
121
122     /**
123      * Returns the file name, line number, a colon and a trailing space.
124      * An error message can be appended easily. For unknown locations, an
125      * empty string is returned.
126      *
127      * @return a String of the form <code>"fileName:lineNumber: "</code>
128      * if both file name and line number are known,
129      * <code>"fileName: "</code> if only the file name is known,
130      * and the empty string for unknown locations.
131      */

132     public String JavaDoc toString() {
133         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
134
135         if (fileName != null) {
136             buf.append(fileName);
137
138             if (lineNumber != 0) {
139                 buf.append(":");
140                 buf.append(lineNumber);
141             }
142
143             buf.append(": ");
144         }
145
146         return buf.toString();
147     }
148
149     /**
150      * Equality operation.
151      * @param other the object to compare to.
152      * @return true if the other object contains the same information
153      * as this object.
154      * @since Ant 1.6.3
155      */

156     public boolean equals(Object JavaDoc other) {
157         if (this == other) {
158             return true;
159         }
160         if (other == null) {
161             return false;
162         }
163         if (!(other.getClass() == getClass())) {
164             return false;
165         }
166         return toString().equals(other.toString());
167     }
168
169     /**
170      * Hash operation.
171      * @return a hash code value for this location.
172      * @since Ant 1.6.3
173      */

174     public int hashCode() {
175         return toString().hashCode();
176     }
177 }
178
Popular Tags