001: /*
002: * $Id: SimpleLocation.java,v 1.2 2004/07/08 14:29:41 cniles Exp $
003: *
004: * Copyright (c) 2004, Christian Niles, unit12.net
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * * Neither the name of Christian Niles, Unit12, nor the names of its
018: * contributors may be used to endorse or promote products derived from
019: * this software without specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: * POSSIBILITY OF SUCH DAMAGE.
032: *
033: */
034: package javanet.staxutils;
035:
036: import javax.xml.stream.Location;
037:
038: /**
039: * Basic implementation of {@link ExtendedLocation}.
040: *
041: * @author Christian Niles
042: * @version $Revision: 1.2 $
043: */
044: public class SimpleLocation implements ExtendedLocation, StaticLocation {
045:
046: /** The nested location. */
047: private Location nestedLocation;
048:
049: /** The line number; defaults to <code>-1</code>. */
050: private int lineNumber = -1;
051:
052: /** The character offset; defaults to <code>-1</code>. */
053: private int characterOffset = -1;
054:
055: /** The column number; defaults to <code>-1</code>. */
056: private int columnNumber = -1;
057:
058: /** The system ID; defaults to <code>null</code>. */
059: private String publicId;
060:
061: /** The public ID; defaults to <code>null</code>. */
062: private String systemId;
063:
064: public SimpleLocation(String publicId, String systemId,
065: int lineNumber, Location nestedLocation) {
066:
067: this .publicId = publicId;
068: this .systemId = systemId;
069: this .lineNumber = lineNumber;
070: this .nestedLocation = nestedLocation;
071:
072: }
073:
074: public SimpleLocation(String publicId, String systemId,
075: int lineNumber, int columnNumber, Location nestedLocation) {
076:
077: this .publicId = publicId;
078: this .systemId = systemId;
079: this .lineNumber = lineNumber;
080: this .columnNumber = columnNumber;
081: this .nestedLocation = nestedLocation;
082:
083: }
084:
085: public SimpleLocation(String publicId, String systemId,
086: int lineNumber, int columnNumber, int characterOffset,
087: Location nestedLocation) {
088:
089: this .publicId = publicId;
090: this .systemId = systemId;
091: this .lineNumber = lineNumber;
092: this .columnNumber = columnNumber;
093: this .characterOffset = characterOffset;
094: this .nestedLocation = nestedLocation;
095:
096: }
097:
098: public SimpleLocation(Location loc) {
099:
100: this .publicId = loc.getPublicId();
101: this .systemId = loc.getSystemId();
102: this .lineNumber = loc.getLineNumber();
103: this .columnNumber = loc.getColumnNumber();
104: this .characterOffset = loc.getCharacterOffset();
105: if (loc instanceof ExtendedLocation) {
106:
107: this .nestedLocation = ((ExtendedLocation) loc)
108: .getNestedLocation();
109:
110: }
111:
112: }
113:
114: public int getCharacterOffset() {
115:
116: return this .characterOffset;
117:
118: }
119:
120: public int getColumnNumber() {
121:
122: return this .columnNumber;
123:
124: }
125:
126: public int getLineNumber() {
127:
128: return this .lineNumber;
129:
130: }
131:
132: public String getPublicId() {
133:
134: return this .publicId;
135:
136: }
137:
138: public String getSystemId() {
139:
140: return this .systemId;
141:
142: }
143:
144: public Location getNestedLocation() {
145:
146: return nestedLocation;
147:
148: }
149:
150: public String toString() {
151:
152: StringBuffer buffer = new StringBuffer();
153:
154: String publicId = getPublicId();
155: String systemId = getSystemId();
156: if (publicId != null) {
157:
158: buffer.append(publicId);
159: if (systemId != null) {
160:
161: buffer.append("#").append(systemId);
162:
163: }
164:
165: } else if (systemId != null) {
166:
167: buffer.append(publicId);
168:
169: }
170:
171: buffer.append('[');
172: buffer.append("line=").append(getLineNumber());
173: buffer.append("column=").append(getColumnNumber());
174: buffer.append(']');
175:
176: Location nested = getNestedLocation();
177: if (nested != null) {
178:
179: buffer.append("->");
180: buffer.append(nested);
181:
182: }
183:
184: return buffer.toString();
185:
186: }
187:
188: }
|