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