KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > Location


1 /*
2  * This source code is heavily based on source code from the Apache
3  * Ant project. As such the following is included:
4  * ------------------------------------------------------------------
5  *
6  * The Apache Software License, Version 1.1
7  *
8  * Copyright (c) 2000,2002-2003 The Apache Software Foundation. All rights
9  * reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution, if
24  * any, must include the following acknowlegement:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowlegement may appear in the software itself,
28  * if and wherever such third-party acknowlegements normally appear.
29  *
30  * 4. The names "Ant" and "Apache Software
31  * Foundation" must not be used to endorse or promote products derived
32  * from this software without prior written permission. For written
33  * permission, please contact apache@apache.org.
34  *
35  * 5. Products derived from this software may not be called "Apache"
36  * nor may "Apache" appear in their names without prior written
37  * permission of the Apache Group.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Apache Software Foundation. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  */

58
59 package org.oddjob.arooa;
60
61 import java.io.Serializable JavaDoc;
62
63 import org.xml.sax.Locator JavaDoc;
64
65 /**
66  * Stores the location of a piece of text within a file (file name,
67  * line number and column number). Note that the column number is
68  * currently ignored.
69  * <p>
70  * Based on the original by <b>Matt Foemmel</b>.
71  */

72 public class Location implements Serializable JavaDoc {
73
74     /** Name of the file. */
75     private String JavaDoc fileName;
76     /** Line number within the file. */
77     private int lineNumber;
78     /** Column number within the file. */
79     private int columnNumber;
80
81     /** Location to use when one is needed but no information is available */
82     public static final Location UNKNOWN_LOCATION = new Location();
83
84     /**
85      * Creates an "unknown" location.
86      */

87     private Location() {
88         this(null, 0, 0);
89     }
90
91     /**
92      * Creates a location consisting of a file name but no line number or
93      * column number.
94      *
95      * @param fileName The name of the file. May be <code>null</code>,
96      * in which case the location is equivalent to
97      * {@link #UNKNOWN_LOCATION UNKNOWN_LOCATION}.
98      */

99     public Location(String JavaDoc fileName) {
100         this(fileName, 0, 0);
101     }
102
103     /**
104      * Creates a location from the SAX locator using the system ID as
105      * the filename.
106      *
107      * @param loc Must not be <code>null</code>.
108      *
109      * @since Ant 1.6
110      */

111     public Location(Locator JavaDoc loc) {
112         this(loc.getSystemId(), loc.getLineNumber(), loc.getColumnNumber());
113     }
114
115     /**
116      * Creates a location consisting of a file name, line number and
117      * column number.
118      *
119      * @param fileName The name of the file. May be <code>null</code>,
120      * in which case the location is equivalent to
121      * {@link #UNKNOWN_LOCATION UNKNOWN_LOCATION}.
122      *
123      * @param lineNumber Line number within the file. Use 0 for unknown
124      * positions within a file.
125      * @param columnNumber Column number within the line.
126      */

127     public Location(String JavaDoc fileName, int lineNumber, int columnNumber) {
128         this.fileName = fileName;
129         this.lineNumber = lineNumber;
130         this.columnNumber = columnNumber;
131     }
132
133     /**
134      * @return the filename portion of the location
135      */

136     public String JavaDoc getFileName() {
137         return fileName;
138     }
139
140     /**
141      * @return the line number
142      */

143     public int getLineNumber() {
144         return lineNumber;
145     }
146
147     /**
148      * Returns the file name, line number, a colon and a trailing space.
149      * An error message can be appended easily. For unknown locations, an
150      * empty string is returned.
151      *
152      * @return a String of the form <code>"fileName: lineNumber: "</code>
153      * if both file name and line number are known,
154      * <code>"fileName: "</code> if only the file name is known,
155      * and the empty string for unknown locations.
156      */

157     public String JavaDoc toString() {
158         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
159
160         if (fileName != null) {
161             buf.append(fileName);
162
163             if (lineNumber != 0) {
164                 buf.append(":");
165                 buf.append(lineNumber);
166             }
167
168             buf.append(": ");
169         }
170
171         return buf.toString();
172     }
173
174 }
175
Popular Tags