001 /*
002 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.xml.bind.helpers;
026
027 import java.net.URL;
028 import java.net.MalformedURLException;
029 import java.text.MessageFormat;
030
031 import javax.xml.bind.ValidationEventLocator;
032 import org.w3c.dom.Node;
033 import org.xml.sax.Locator;
034 import org.xml.sax.SAXParseException;
035
036 /**
037 * Default implementation of the ValidationEventLocator interface.
038 *
039 * <p>
040 * JAXB providers are allowed to use whatever class that implements
041 * the ValidationEventLocator interface. This class is just provided for a
042 * convenience.
043 *
044 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul>
045 * @version $Revision: 1.1 $
046 * @see javax.xml.bind.Validator
047 * @see javax.xml.bind.ValidationEventHandler
048 * @see javax.xml.bind.ValidationEvent
049 * @see javax.xml.bind.ValidationEventLocator
050 * @since JAXB1.0
051 */
052 public class ValidationEventLocatorImpl implements
053 ValidationEventLocator {
054 /**
055 * Creates an object with all fields unavailable.
056 */
057 public ValidationEventLocatorImpl() {
058 }
059
060 /**
061 * Constructs an object from an org.xml.sax.Locator.
062 *
063 * The object's ColumnNumber, LineNumber, and URL become available from the
064 * values returned by the locator's getColumnNumber(), getLineNumber(), and
065 * getSystemId() methods respectively. Node, Object, and Offset are not
066 * available.
067 *
068 * @param loc the SAX Locator object that will be used to populate this
069 * event locator.
070 * @throws IllegalArgumentException if the Locator is null
071 */
072 public ValidationEventLocatorImpl(Locator loc) {
073 if (loc == null) {
074 throw new IllegalArgumentException(Messages.format(
075 Messages.MUST_NOT_BE_NULL, "loc"));
076 }
077
078 this .url = toURL(loc.getSystemId());
079 this .columnNumber = loc.getColumnNumber();
080 this .lineNumber = loc.getLineNumber();
081 }
082
083 /**
084 * Constructs an object from the location information of a SAXParseException.
085 *
086 * The object's ColumnNumber, LineNumber, and URL become available from the
087 * values returned by the locator's getColumnNumber(), getLineNumber(), and
088 * getSystemId() methods respectively. Node, Object, and Offset are not
089 * available.
090 *
091 * @param e the SAXParseException object that will be used to populate this
092 * event locator.
093 * @throws IllegalArgumentException if the SAXParseException is null
094 */
095 public ValidationEventLocatorImpl(SAXParseException e) {
096 if (e == null) {
097 throw new IllegalArgumentException(Messages.format(
098 Messages.MUST_NOT_BE_NULL, "e"));
099 }
100
101 this .url = toURL(e.getSystemId());
102 this .columnNumber = e.getColumnNumber();
103 this .lineNumber = e.getLineNumber();
104 }
105
106 /**
107 * Constructs an object that points to a DOM Node.
108 *
109 * The object's Node becomes available. ColumnNumber, LineNumber, Object,
110 * Offset, and URL are not available.
111 *
112 * @param _node the DOM Node object that will be used to populate this
113 * event locator.
114 * @throws IllegalArgumentException if the Node is null
115 */
116 public ValidationEventLocatorImpl(Node _node) {
117 if (_node == null) {
118 throw new IllegalArgumentException(Messages.format(
119 Messages.MUST_NOT_BE_NULL, "_node"));
120 }
121
122 this .node = _node;
123 }
124
125 /**
126 * Constructs an object that points to a JAXB content object.
127 *
128 * The object's Object becomes available. ColumnNumber, LineNumber, Node,
129 * Offset, and URL are not available.
130 *
131 * @param _object the Object that will be used to populate this
132 * event locator.
133 * @throws IllegalArgumentException if the Object is null
134 */
135 public ValidationEventLocatorImpl(Object _object) {
136 if (_object == null) {
137 throw new IllegalArgumentException(Messages.format(
138 Messages.MUST_NOT_BE_NULL, "_object"));
139 }
140
141 this .object = _object;
142 }
143
144 /** Converts a system ID to an URL object. */
145 private static URL toURL(String systemId) {
146 try {
147 return new URL(systemId);
148 } catch (MalformedURLException e) {
149 // TODO: how should we handle system id here?
150 return null; // for now
151 }
152 }
153
154 private URL url = null;
155 private int offset = -1;
156 private int lineNumber = -1;
157 private int columnNumber = -1;
158 private Object object = null;
159 private Node node = null;
160
161 /**
162 * @see javax.xml.bind.ValidationEventLocator#getURL()
163 */
164 public URL getURL() {
165 return url;
166 }
167
168 /**
169 * Set the URL field on this event locator. Null values are allowed.
170 *
171 * @param _url the url
172 */
173 public void setURL(URL _url) {
174 this .url = _url;
175 }
176
177 /**
178 * @see javax.xml.bind.ValidationEventLocator#getOffset()
179 */
180 public int getOffset() {
181 return offset;
182 }
183
184 /**
185 * Set the offset field on this event locator.
186 *
187 * @param _offset the offset
188 */
189 public void setOffset(int _offset) {
190 this .offset = _offset;
191 }
192
193 /**
194 * @see javax.xml.bind.ValidationEventLocator#getLineNumber()
195 */
196 public int getLineNumber() {
197 return lineNumber;
198 }
199
200 /**
201 * Set the lineNumber field on this event locator.
202 *
203 * @param _lineNumber the line number
204 */
205 public void setLineNumber(int _lineNumber) {
206 this .lineNumber = _lineNumber;
207 }
208
209 /**
210 * @see javax.xml.bind.ValidationEventLocator#getColumnNumber()
211 */
212 public int getColumnNumber() {
213 return columnNumber;
214 }
215
216 /**
217 * Set the columnNumber field on this event locator.
218 *
219 * @param _columnNumber the column number
220 */
221 public void setColumnNumber(int _columnNumber) {
222 this .columnNumber = _columnNumber;
223 }
224
225 /**
226 * @see javax.xml.bind.ValidationEventLocator#getObject()
227 */
228 public Object getObject() {
229 return object;
230 }
231
232 /**
233 * Set the Object field on this event locator. Null values are allowed.
234 *
235 * @param _object the java content object
236 */
237 public void setObject(Object _object) {
238 this .object = _object;
239 }
240
241 /**
242 * @see javax.xml.bind.ValidationEventLocator#getNode()
243 */
244 public Node getNode() {
245 return node;
246 }
247
248 /**
249 * Set the Node field on this event locator. Null values are allowed.
250 *
251 * @param _node the Node
252 */
253 public void setNode(Node _node) {
254 this .node = _node;
255 }
256
257 /**
258 * Returns a string representation of this object in a format
259 * helpful to debugging.
260 *
261 * @see Object#equals(Object)
262 */
263 public String toString() {
264 return MessageFormat
265 .format(
266 "[node={0},object={1},url={2},line={3},col={4},offset={5}]",
267 getNode(), getObject(), getURL(), String
268 .valueOf(getLineNumber()), String
269 .valueOf(getColumnNumber()), String
270 .valueOf(getOffset()));
271 }
272 }
|