Home » xml-commons-external-1.4.01-src » javax » xml » bind » helpers » [javadoc | source]

    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    * @version $Revision: 1.2 $
   46    * @see javax.xml.bind.Validator
   47    * @see javax.xml.bind.ValidationEventHandler
   48    * @see javax.xml.bind.ValidationEvent
   49    * @see javax.xml.bind.ValidationEventLocator
   50    * @since JAXB1.0
   51    */
   52   public class ValidationEventLocatorImpl implements ValidationEventLocator
   53   {
   54       /**
   55        * Creates an object with all fields unavailable.
   56        */
   57       public ValidationEventLocatorImpl() {
   58       }
   59   
   60       /**
   61        * Constructs an object from an org.xml.sax.Locator.
   62        *
   63        * The object's ColumnNumber, LineNumber, and URL become available from the
   64        * values returned by the locator's getColumnNumber(), getLineNumber(), and
   65        * getSystemId() methods respectively. Node, Object, and Offset are not
   66        * available.
   67        *
   68        * @param loc the SAX Locator object that will be used to populate this
   69        * event locator.
   70        * @throws IllegalArgumentException if the Locator is null
   71        */
   72       public ValidationEventLocatorImpl( Locator loc ) {
   73           if( loc == null ) {
   74               throw new IllegalArgumentException(
   75                   Messages.format( Messages.MUST_NOT_BE_NULL, "loc" ) );
   76           }
   77   
   78           this.url = toURL(loc.getSystemId());
   79           this.columnNumber = loc.getColumnNumber();
   80           this.lineNumber = loc.getLineNumber();
   81       }
   82   
   83       /**
   84        * Constructs an object from the location information of a SAXParseException.
   85        *
   86        * The object's ColumnNumber, LineNumber, and URL become available from the
   87        * values returned by the locator's getColumnNumber(), getLineNumber(), and
   88        * getSystemId() methods respectively. Node, Object, and Offset are not
   89        * available.
   90        *
   91        * @param e the SAXParseException object that will be used to populate this
   92        * event locator.
   93        * @throws IllegalArgumentException if the SAXParseException is null
   94        */
   95       public ValidationEventLocatorImpl( SAXParseException e ) {
   96           if( e == null ) {
   97               throw new IllegalArgumentException(
   98                   Messages.format( Messages.MUST_NOT_BE_NULL, "e" ) );
   99           }
  100   
  101           this.url = toURL(e.getSystemId());
  102           this.columnNumber = e.getColumnNumber();
  103           this.lineNumber = e.getLineNumber();
  104       }
  105   
  106       /**
  107        * Constructs an object that points to a DOM Node.
  108        *
  109        * The object's Node becomes available.  ColumnNumber, LineNumber, Object,
  110        * Offset, and URL are not available.
  111        *
  112        * @param _node the DOM Node object that will be used to populate this
  113        * event locator.
  114        * @throws IllegalArgumentException if the Node is null
  115        */
  116       public ValidationEventLocatorImpl(Node _node) {
  117           if( _node == null ) {
  118               throw new IllegalArgumentException(
  119                   Messages.format( Messages.MUST_NOT_BE_NULL, "_node" ) );
  120           }
  121   
  122           this.node = _node;
  123       }
  124   
  125       /**
  126        * Constructs an object that points to a JAXB content object.
  127        *
  128        * The object's Object becomes available. ColumnNumber, LineNumber, Node,
  129        * Offset, and URL are not available.
  130        *
  131        * @param _object the Object that will be used to populate this
  132        * event locator.
  133        * @throws IllegalArgumentException if the Object is null
  134        */
  135       public ValidationEventLocatorImpl(Object _object) {
  136           if( _object == null ) {
  137               throw new IllegalArgumentException(
  138                   Messages.format( Messages.MUST_NOT_BE_NULL, "_object" ) );
  139           }
  140   
  141           this.object = _object;
  142       }
  143   
  144       /** Converts a system ID to an URL object. */
  145       private static URL toURL( String systemId ) {
  146           try {
  147               return new URL(systemId);
  148           } catch( MalformedURLException e ) {
  149               // TODO: how should we handle system id here?
  150               return null;    // for now
  151           }
  152       }
  153   
  154       private URL url = null;
  155       private int offset = -1;
  156       private int lineNumber = -1;
  157       private int columnNumber = -1;
  158       private Object object = null;
  159       private Node node = null;
  160   
  161   
  162       /**
  163        * @see javax.xml.bind.ValidationEventLocator#getURL()
  164        */
  165       public URL getURL() {
  166           return url;
  167       }
  168   
  169       /**
  170        * Set the URL field on this event locator.  Null values are allowed.
  171        *
  172        * @param _url the url
  173        */
  174       public void setURL( URL _url ) {
  175           this.url = _url;
  176       }
  177   
  178       /**
  179        * @see javax.xml.bind.ValidationEventLocator#getOffset()
  180        */
  181       public int getOffset() {
  182           return offset;
  183       }
  184   
  185       /**
  186        * Set the offset field on this event locator.
  187        *
  188        * @param _offset the offset
  189        */
  190       public void setOffset( int _offset ) {
  191           this.offset = _offset;
  192       }
  193   
  194       /**
  195        * @see javax.xml.bind.ValidationEventLocator#getLineNumber()
  196        */
  197       public int getLineNumber() {
  198           return lineNumber;
  199       }
  200   
  201       /**
  202        * Set the lineNumber field on this event locator.
  203        *
  204        * @param _lineNumber the line number
  205        */
  206       public void setLineNumber( int _lineNumber ) {
  207           this.lineNumber = _lineNumber;
  208       }
  209   
  210       /**
  211        * @see javax.xml.bind.ValidationEventLocator#getColumnNumber()
  212        */
  213       public int getColumnNumber() {
  214           return columnNumber;
  215       }
  216   
  217       /**
  218        * Set the columnNumber field on this event locator.
  219        *
  220        * @param _columnNumber the column number
  221        */
  222       public void setColumnNumber( int _columnNumber ) {
  223           this.columnNumber = _columnNumber;
  224       }
  225   
  226       /**
  227        * @see javax.xml.bind.ValidationEventLocator#getObject()
  228        */
  229       public Object getObject() {
  230           return object;
  231       }
  232   
  233       /**
  234        * Set the Object field on this event locator.  Null values are allowed.
  235        *
  236        * @param _object the java content object
  237        */
  238       public void setObject( Object _object ) {
  239           this.object = _object;
  240       }
  241   
  242       /**
  243        * @see javax.xml.bind.ValidationEventLocator#getNode()
  244        */
  245       public Node getNode() {
  246           return node;
  247       }
  248   
  249       /**
  250        * Set the Node field on this event locator.  Null values are allowed.
  251        *
  252        * @param _node the Node
  253        */
  254       public void setNode( Node _node ) {
  255           this.node = _node;
  256       }
  257   
  258       /**
  259        * Returns a string representation of this object in a format
  260        * helpful to debugging.
  261        *
  262        * @see Object#equals(Object)
  263        */
  264       public String toString() {
  265           return MessageFormat.format("[node={0},object={1},url={2},line={3},col={4},offset={5}]",
  266               getNode(),
  267               getObject(),
  268               getURL(),
  269               String.valueOf(getLineNumber()),
  270               String.valueOf(getColumnNumber()),
  271               String.valueOf(getOffset()));
  272       }
  273   }

Home » xml-commons-external-1.4.01-src » javax » xml » bind » helpers » [javadoc | source]