1 /*
2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25 package javax.xml.bind.helpers;
26
27 import java.net.URL;
28 import java.net.MalformedURLException;
29 import java.text.MessageFormat;
30
31 import javax.xml.bind.ValidationEventLocator;
32 import org.w3c.dom.Node;
33 import org.xml.sax.Locator;
34 import org.xml.sax.SAXParseException;
35
36 /**
37 * Default implementation of the ValidationEventLocator interface.
38 *
39 * <p>
40 * JAXB providers are allowed to use whatever class that implements
41 * the ValidationEventLocator interface. This class is just provided for a
42 * convenience.
43 *
44 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul>
45 * @see javax.xml.bind.Validator
46 * @see javax.xml.bind.ValidationEventHandler
47 * @see javax.xml.bind.ValidationEvent
48 * @see javax.xml.bind.ValidationEventLocator
49 * @since JAXB1.0
50 */
51 public class ValidationEventLocatorImpl implements ValidationEventLocator
52 {
53 /**
54 * Creates an object with all fields unavailable.
55 */
56 public ValidationEventLocatorImpl() {
57 }
58
59 /**
60 * Constructs an object from an org.xml.sax.Locator.
61 *
62 * The object's ColumnNumber, LineNumber, and URL become available from the
63 * values returned by the locator's getColumnNumber(), getLineNumber(), and
64 * getSystemId() methods respectively. Node, Object, and Offset are not
65 * available.
66 *
67 * @param loc the SAX Locator object that will be used to populate this
68 * event locator.
69 * @throws IllegalArgumentException if the Locator is null
70 */
71 public ValidationEventLocatorImpl( Locator loc ) {
72 if( loc == null ) {
73 throw new IllegalArgumentException(
74 Messages.format( Messages.MUST_NOT_BE_NULL, "loc" ) );
75 }
76
77 this.url = toURL(loc.getSystemId());
78 this.columnNumber = loc.getColumnNumber();
79 this.lineNumber = loc.getLineNumber();
80 }
81
82 /**
83 * Constructs an object from the location information of a SAXParseException.
84 *
85 * The object's ColumnNumber, LineNumber, and URL become available from the
86 * values returned by the locator's getColumnNumber(), getLineNumber(), and
87 * getSystemId() methods respectively. Node, Object, and Offset are not
88 * available.
89 *
90 * @param e the SAXParseException object that will be used to populate this
91 * event locator.
92 * @throws IllegalArgumentException if the SAXParseException is null
93 */
94 public ValidationEventLocatorImpl( SAXParseException e ) {
95 if( e == null ) {
96 throw new IllegalArgumentException(
97 Messages.format( Messages.MUST_NOT_BE_NULL, "e" ) );
98 }
99
100 this.url = toURL(e.getSystemId());
101 this.columnNumber = e.getColumnNumber();
102 this.lineNumber = e.getLineNumber();
103 }
104
105 /**
106 * Constructs an object that points to a DOM Node.
107 *
108 * The object's Node becomes available. ColumnNumber, LineNumber, Object,
109 * Offset, and URL are not available.
110 *
111 * @param _node the DOM Node object that will be used to populate this
112 * event locator.
113 * @throws IllegalArgumentException if the Node is null
114 */
115 public ValidationEventLocatorImpl(Node _node) {
116 if( _node == null ) {
117 throw new IllegalArgumentException(
118 Messages.format( Messages.MUST_NOT_BE_NULL, "_node" ) );
119 }
120
121 this.node = _node;
122 }
123
124 /**
125 * Constructs an object that points to a JAXB content object.
126 *
127 * The object's Object becomes available. ColumnNumber, LineNumber, Node,
128 * Offset, and URL are not available.
129 *
130 * @param _object the Object that will be used to populate this
131 * event locator.
132 * @throws IllegalArgumentException if the Object is null
133 */
134 public ValidationEventLocatorImpl(Object _object) {
135 if( _object == null ) {
136 throw new IllegalArgumentException(
137 Messages.format( Messages.MUST_NOT_BE_NULL, "_object" ) );
138 }
139
140 this.object = _object;
141 }
142
143 /** Converts a system ID to an URL object. */
144 private static URL toURL( String systemId ) {
145 try {
146 return new URL(systemId);
147 } catch( MalformedURLException e ) {
148 // TODO: how should we handle system id here?
149 return null; // for now
150 }
151 }
152
153 private URL url = null;
154 private int offset = -1;
155 private int lineNumber = -1;
156 private int columnNumber = -1;
157 private Object object = null;
158 private Node node = null;
159
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.format("[node={0},object={1},url={2},line={3},col={4},offset={5}]",
265 getNode(),
266 getObject(),
267 getURL(),
268 String.valueOf(getLineNumber()),
269 String.valueOf(getColumnNumber()),
270 String.valueOf(getOffset()));
271 }
272 }