KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > bind > helpers > ValidationEventLocatorImpl


1 /*
2  * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5 package javax.xml.bind.helpers;
6
7 import java.net.URL JavaDoc;
8 import java.net.MalformedURLException JavaDoc;
9 import java.text.MessageFormat JavaDoc;
10
11 import javax.xml.bind.ValidationEventLocator;
12 import org.w3c.dom.Node JavaDoc;
13 import org.xml.sax.Locator JavaDoc;
14 import org.xml.sax.SAXParseException JavaDoc;
15
16 /**
17  * Default implementation of the ValidationEventLocator interface.
18  *
19  * <p>
20  * JAXB providers are allowed to use whatever class that implements
21  * the ValidationEventLocator interface. This class is just provided for a
22  * convenience.
23  *
24  * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul>
25  * @version $Revision: 1.1 $
26  * @see javax.xml.bind.Validator
27  * @see javax.xml.bind.ValidationEventHandler
28  * @see javax.xml.bind.ValidationEvent
29  * @see javax.xml.bind.ValidationEventLocator
30  * @since JAXB1.0
31  */

32 public class ValidationEventLocatorImpl implements ValidationEventLocator
33 {
34     /**
35      * Creates an object with all fields unavailable.
36      */

37     public ValidationEventLocatorImpl() {
38     }
39
40     /**
41      * Constructs an object from an org.xml.sax.Locator.
42      *
43      * The object's ColumnNumber, LineNumber, and URL become available from the
44      * values returned by the locator's getColumnNumber(), getLineNumber(), and
45      * getSystemId() methods respectively. Node, Object, and Offset are not
46      * available.
47      *
48      * @param loc the SAX Locator object that will be used to populate this
49      * event locator.
50      * @throws IllegalArgumentException if the Locator is null
51      */

52     public ValidationEventLocatorImpl( Locator loc ) {
53         if( loc == null ) {
54             throw new IllegalArgumentException JavaDoc(
55                 Messages.format( Messages.MUST_NOT_BE_NULL, "loc" ) );
56         }
57
58         this.url = toURL(loc.getSystemId());
59         this.columnNumber = loc.getColumnNumber();
60         this.lineNumber = loc.getLineNumber();
61     }
62
63     /**
64      * Constructs an object from the location information of a SAXParseException.
65      *
66      * The object's ColumnNumber, LineNumber, and URL become available from the
67      * values returned by the locator's getColumnNumber(), getLineNumber(), and
68      * getSystemId() methods respectively. Node, Object, and Offset are not
69      * available.
70      *
71      * @param e the SAXParseException object that will be used to populate this
72      * event locator.
73      * @throws IllegalArgumentException if the SAXParseException is null
74      */

75     public ValidationEventLocatorImpl( SAXParseException JavaDoc e ) {
76         if( e == null ) {
77             throw new IllegalArgumentException JavaDoc(
78                 Messages.format( Messages.MUST_NOT_BE_NULL, "e" ) );
79         }
80
81         this.url = toURL(e.getSystemId());
82         this.columnNumber = e.getColumnNumber();
83         this.lineNumber = e.getLineNumber();
84     }
85
86     /**
87      * Constructs an object that points to a DOM Node.
88      *
89      * The object's Node becomes available. ColumnNumber, LineNumber, Object,
90      * Offset, and URL are not available.
91      *
92      * @param _node the DOM Node object that will be used to populate this
93      * event locator.
94      * @throws IllegalArgumentException if the Node is null
95      */

96     public ValidationEventLocatorImpl(Node JavaDoc _node) {
97         if( _node == null ) {
98             throw new IllegalArgumentException JavaDoc(
99                 Messages.format( Messages.MUST_NOT_BE_NULL, "_node" ) );
100         }
101
102         this.node = _node;
103     }
104
105     /**
106      * Constructs an object that points to a JAXB content object.
107      *
108      * The object's Object becomes available. ColumnNumber, LineNumber, Node,
109      * Offset, and URL are not available.
110      *
111      * @param _object the Object that will be used to populate this
112      * event locator.
113      * @throws IllegalArgumentException if the Object is null
114      */

115     public ValidationEventLocatorImpl(Object JavaDoc _object) {
116         if( _object == null ) {
117             throw new IllegalArgumentException JavaDoc(
118                 Messages.format( Messages.MUST_NOT_BE_NULL, "_object" ) );
119         }
120
121         this.object = _object;
122     }
123     
124     /** Converts a system ID to an URL object. */
125     private static URL JavaDoc toURL( String JavaDoc systemId ) {
126         try {
127             return new URL JavaDoc(systemId);
128         } catch( MalformedURLException JavaDoc e ) {
129             // TODO: how should we handle system id here?
130
return null; // for now
131
}
132     }
133     
134     private URL JavaDoc url = null;
135     private int offset = -1;
136     private int lineNumber = -1;
137     private int columnNumber = -1;
138     private Object JavaDoc object = null;
139     private Node JavaDoc node = null;
140     
141     
142     /**
143      * @see javax.xml.bind.ValidationEventLocator#getURL()
144      */

145     public URL JavaDoc getURL() {
146         return url;
147     }
148     
149     /**
150      * Set the URL field on this event locator. Null values are allowed.
151      *
152      * @param _url the url
153      */

154     public void setURL( URL JavaDoc _url ) {
155         this.url = _url;
156     }
157     
158     /**
159      * @see javax.xml.bind.ValidationEventLocator#getOffset()
160      */

161     public int getOffset() {
162         return offset;
163     }
164     
165     /**
166      * Set the offset field on this event locator.
167      *
168      * @param _offset the offset
169      */

170     public void setOffset( int _offset ) {
171         this.offset = _offset;
172     }
173     
174     /**
175      * @see javax.xml.bind.ValidationEventLocator#getLineNumber()
176      */

177     public int getLineNumber() {
178         return lineNumber;
179     }
180     
181     /**
182      * Set the lineNumber field on this event locator.
183      *
184      * @param _lineNumber the line number
185      */

186     public void setLineNumber( int _lineNumber ) {
187         this.lineNumber = _lineNumber;
188     }
189     
190     /**
191      * @see javax.xml.bind.ValidationEventLocator#getColumnNumber()
192      */

193     public int getColumnNumber() {
194         return columnNumber;
195     }
196     
197     /**
198      * Set the columnNumber field on this event locator.
199      *
200      * @param _columnNumber the column number
201      */

202     public void setColumnNumber( int _columnNumber ) {
203         this.columnNumber = _columnNumber;
204     }
205     
206     /**
207      * @see javax.xml.bind.ValidationEventLocator#getObject()
208      */

209     public Object JavaDoc getObject() {
210         return object;
211     }
212     
213     /**
214      * Set the Object field on this event locator. Null values are allowed.
215      *
216      * @param _object the java content object
217      */

218     public void setObject( Object JavaDoc _object ) {
219         this.object = _object;
220     }
221     
222     /**
223      * @see javax.xml.bind.ValidationEventLocator#getNode()
224      */

225     public Node JavaDoc getNode() {
226         return node;
227     }
228     
229     /**
230      * Set the Node field on this event locator. Null values are allowed.
231      *
232      * @param _node the Node
233      */

234     public void setNode( Node JavaDoc _node ) {
235         this.node = _node;
236     }
237     
238     /**
239      * Returns a string representation of this object in a format
240      * helpful to debugging.
241      *
242      * @see Object#equals(Object)
243      */

244     public String JavaDoc toString() {
245         return MessageFormat.format("[node={0},object={1},url={2},line={3},col={4},offset={5}]",
246             getNode(),
247             getObject(),
248             getURL(),
249             String.valueOf(getLineNumber()),
250             String.valueOf(getColumnNumber()),
251             String.valueOf(getOffset()));
252     }
253 }
254
Popular Tags