001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
003: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
004: */
005: package javax.xml.bind.helpers;
006:
007: import java.net.URL;
008: import java.net.MalformedURLException;
009: import java.text.MessageFormat;
010:
011: import javax.xml.bind.ValidationEventLocator;
012: import org.w3c.dom.Node;
013: import org.xml.sax.Locator;
014: import org.xml.sax.SAXParseException;
015:
016: /**
017: * Default implementation of the ValidationEventLocator interface.
018: *
019: * <p>
020: * JAXB providers are allowed to use whatever class that implements
021: * the ValidationEventLocator interface. This class is just provided for a
022: * convenience.
023: *
024: * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul>
025: * @version $Revision: 1.2 $
026: * @see javax.xml.bind.Validator
027: * @see javax.xml.bind.ValidationEventHandler
028: * @see javax.xml.bind.ValidationEvent
029: * @see javax.xml.bind.ValidationEventLocator
030: * @since JAXB1.0
031: */
032: public class ValidationEventLocatorImpl implements
033: ValidationEventLocator {
034: /**
035: * Creates an object with all fields unavailable.
036: */
037: public ValidationEventLocatorImpl() {
038: }
039:
040: /**
041: * Constructs an object from an org.xml.sax.Locator.
042: *
043: * The object's ColumnNumber, LineNumber, and URL become available from the
044: * values returned by the locator's getColumnNumber(), getLineNumber(), and
045: * getSystemId() methods respectively. Node, Object, and Offset are not
046: * available.
047: *
048: * @param loc the SAX Locator object that will be used to populate this
049: * event locator.
050: * @throws IllegalArgumentException if the Locator is null
051: */
052: public ValidationEventLocatorImpl(Locator loc) {
053: if (loc == null) {
054: throw new IllegalArgumentException(Messages.format(
055: Messages.MUST_NOT_BE_NULL, "loc"));
056: }
057:
058: this .url = toURL(loc.getSystemId());
059: this .columnNumber = loc.getColumnNumber();
060: this .lineNumber = loc.getLineNumber();
061: }
062:
063: /**
064: * Constructs an object from the location information of a SAXParseException.
065: *
066: * The object's ColumnNumber, LineNumber, and URL become available from the
067: * values returned by the locator's getColumnNumber(), getLineNumber(), and
068: * getSystemId() methods respectively. Node, Object, and Offset are not
069: * available.
070: *
071: * @param e the SAXParseException object that will be used to populate this
072: * event locator.
073: * @throws IllegalArgumentException if the SAXParseException is null
074: */
075: public ValidationEventLocatorImpl(SAXParseException e) {
076: if (e == null) {
077: throw new IllegalArgumentException(Messages.format(
078: Messages.MUST_NOT_BE_NULL, "e"));
079: }
080:
081: this .url = toURL(e.getSystemId());
082: this .columnNumber = e.getColumnNumber();
083: this .lineNumber = e.getLineNumber();
084: }
085:
086: /**
087: * Constructs an object that points to a DOM Node.
088: *
089: * The object's Node becomes available. ColumnNumber, LineNumber, Object,
090: * Offset, and URL are not available.
091: *
092: * @param _node the DOM Node object that will be used to populate this
093: * event locator.
094: * @throws IllegalArgumentException if the Node is null
095: */
096: public ValidationEventLocatorImpl(Node _node) {
097: if (_node == null) {
098: throw new IllegalArgumentException(Messages.format(
099: 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 _object) {
116: if (_object == null) {
117: throw new IllegalArgumentException(Messages.format(
118: 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 toURL(String systemId) {
126: try {
127: return new URL(systemId);
128: } catch (MalformedURLException e) {
129: // TODO: how should we handle system id here?
130: return null; // for now
131: }
132: }
133:
134: private URL url = null;
135: private int offset = -1;
136: private int lineNumber = -1;
137: private int columnNumber = -1;
138: private Object object = null;
139: private Node node = null;
140:
141: /**
142: * @see javax.xml.bind.ValidationEventLocator#getURL()
143: */
144: public URL getURL() {
145: return url;
146: }
147:
148: /**
149: * Set the URL field on this event locator. Null values are allowed.
150: *
151: * @param _url the url
152: */
153: public void setURL(URL _url) {
154: this .url = _url;
155: }
156:
157: /**
158: * @see javax.xml.bind.ValidationEventLocator#getOffset()
159: */
160: public int getOffset() {
161: return offset;
162: }
163:
164: /**
165: * Set the offset field on this event locator.
166: *
167: * @param _offset the offset
168: */
169: public void setOffset(int _offset) {
170: this .offset = _offset;
171: }
172:
173: /**
174: * @see javax.xml.bind.ValidationEventLocator#getLineNumber()
175: */
176: public int getLineNumber() {
177: return lineNumber;
178: }
179:
180: /**
181: * Set the lineNumber field on this event locator.
182: *
183: * @param _lineNumber the line number
184: */
185: public void setLineNumber(int _lineNumber) {
186: this .lineNumber = _lineNumber;
187: }
188:
189: /**
190: * @see javax.xml.bind.ValidationEventLocator#getColumnNumber()
191: */
192: public int getColumnNumber() {
193: return columnNumber;
194: }
195:
196: /**
197: * Set the columnNumber field on this event locator.
198: *
199: * @param _columnNumber the column number
200: */
201: public void setColumnNumber(int _columnNumber) {
202: this .columnNumber = _columnNumber;
203: }
204:
205: /**
206: * @see javax.xml.bind.ValidationEventLocator#getObject()
207: */
208: public Object getObject() {
209: return object;
210: }
211:
212: /**
213: * Set the Object field on this event locator. Null values are allowed.
214: *
215: * @param _object the java content object
216: */
217: public void setObject(Object _object) {
218: this .object = _object;
219: }
220:
221: /**
222: * @see javax.xml.bind.ValidationEventLocator#getNode()
223: */
224: public Node getNode() {
225: return node;
226: }
227:
228: /**
229: * Set the Node field on this event locator. Null values are allowed.
230: *
231: * @param _node the Node
232: */
233: public void setNode(Node _node) {
234: this .node = _node;
235: }
236:
237: /**
238: * Returns a string representation of this object in a format
239: * helpful to debugging.
240: *
241: * @see Object#equals(Object)
242: */
243: public String toString() {
244: return MessageFormat
245: .format(
246: "[node={0},object={1},url={2},line={3},col={4},offset={5}]",
247: getNode(), getObject(), getURL(), String
248: .valueOf(getLineNumber()), String
249: .valueOf(getColumnNumber()), String
250: .valueOf(getOffset()));
251: }
252: }
|