1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
/* * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.xml.bind.helpers; import java.net.URL; import java.net.MalformedURLException; import java.text.MessageFormat; import javax.xml.bind.ValidationEventLocator; import org.w3c.dom.Node; import org.xml.sax.Locator; import org.xml.sax.SAXParseException; /** * Default implementation of the ValidationEventLocator interface. * * <p> * JAXB providers are allowed to use whatever class that implements * the ValidationEventLocator interface. This class is just provided for a * convenience. * * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul> * @version $Revision: 1.1 $ * @see javax.xml.bind.Validator * @see javax.xml.bind.ValidationEventHandler * @see javax.xml.bind.ValidationEvent * @see javax.xml.bind.ValidationEventLocator * @since JAXB1.0 */ public class ValidationEventLocatorImpl implements ValidationEventLocator { /** * Creates an object with all fields unavailable. */ public ValidationEventLocatorImpl() { } /** * Constructs an object from an org.xml.sax.Locator. * * The object's ColumnNumber, LineNumber, and URL become available from the * values returned by the locator's getColumnNumber(), getLineNumber(), and * getSystemId() methods respectively. Node, Object, and Offset are not * available. * * @param loc the SAX Locator object that will be used to populate this * event locator. * @throws IllegalArgumentException if the Locator is null */ public ValidationEventLocatorImpl( Locator loc ) { if( loc == null ) { throw new IllegalArgumentException( Messages.format( Messages.MUST_NOT_BE_NULL, "loc" ) ); } this.url = toURL(loc.getSystemId()); this.columnNumber = loc.getColumnNumber(); this.lineNumber = loc.getLineNumber(); } /** * Constructs an object from the location information of a SAXParseException. * * The object's ColumnNumber, LineNumber, and URL become available from the * values returned by the locator's getColumnNumber(), getLineNumber(), and * getSystemId() methods respectively. Node, Object, and Offset are not * available. * * @param e the SAXParseException object that will be used to populate this * event locator. * @throws IllegalArgumentException if the SAXParseException is null */ public ValidationEventLocatorImpl( SAXParseException e ) { if( e == null ) { throw new IllegalArgumentException( Messages.format( Messages.MUST_NOT_BE_NULL, "e" ) ); } this.url = toURL(e.getSystemId()); this.columnNumber = e.getColumnNumber(); this.lineNumber = e.getLineNumber(); } /** * Constructs an object that points to a DOM Node. * * The object's Node becomes available. ColumnNumber, LineNumber, Object, * Offset, and URL are not available. * * @param _node the DOM Node object that will be used to populate this * event locator. * @throws IllegalArgumentException if the Node is null */ public ValidationEventLocatorImpl(Node _node) { if( _node == null ) { throw new IllegalArgumentException( Messages.format( Messages.MUST_NOT_BE_NULL, "_node" ) ); } this.node = _node; } /** * Constructs an object that points to a JAXB content object. * * The object's Object becomes available. ColumnNumber, LineNumber, Node, * Offset, and URL are not available. * * @param _object the Object that will be used to populate this * event locator. * @throws IllegalArgumentException if the Object is null */ public ValidationEventLocatorImpl(Object _object) { if( _object == null ) { throw new IllegalArgumentException( Messages.format( Messages.MUST_NOT_BE_NULL, "_object" ) ); } this.object = _object; } /** Converts a system ID to an URL object. */ private static URL toURL( String systemId ) { try { return new URL(systemId); } catch( MalformedURLException e ) { // TODO: how should we handle system id here? return null; // for now } } private URL url = null; private int offset = -1; private int lineNumber = -1; private int columnNumber = -1; private Object object = null; private Node node = null; /** * @see javax.xml.bind.ValidationEventLocator#getURL() */ public URL getURL() { return url; } /** * Set the URL field on this event locator. Null values are allowed. * * @param _url the url */ public void setURL( URL _url ) { this.url = _url; } /** * @see javax.xml.bind.ValidationEventLocator#getOffset() */ public int getOffset() { return offset; } /** * Set the offset field on this event locator. * * @param _offset the offset */ public void setOffset( int _offset ) { this.offset = _offset; } /** * @see javax.xml.bind.ValidationEventLocator#getLineNumber() */ public int getLineNumber() { return lineNumber; } /** * Set the lineNumber field on this event locator. * * @param _lineNumber the line number */ public void setLineNumber( int _lineNumber ) { this.lineNumber = _lineNumber; } /** * @see javax.xml.bind.ValidationEventLocator#getColumnNumber() */ public int getColumnNumber() { return columnNumber; } /** * Set the columnNumber field on this event locator. * * @param _columnNumber the column number */ public void setColumnNumber( int _columnNumber ) { this.columnNumber = _columnNumber; } /** * @see javax.xml.bind.ValidationEventLocator#getObject() */ public Object getObject() { return object; } /** * Set the Object field on this event locator. Null values are allowed. * * @param _object the java content object */ public void setObject( Object _object ) { this.object = _object; } /** * @see javax.xml.bind.ValidationEventLocator#getNode() */ public Node getNode() { return node; } /** * Set the Node field on this event locator. Null values are allowed. * * @param _node the Node */ public void setNode( Node _node ) { this.node = _node; } /** * Returns a string representation of this object in a format * helpful to debugging. * * @see Object#equals(Object) */ public String toString() { return MessageFormat.format("[node={0},object={1},url={2},line={3},col={4},offset={5}]", getNode(), getObject(), getURL(), String.valueOf(getLineNumber()), String.valueOf(getColumnNumber()), String.valueOf(getOffset())); } }