Home | History | Annotate | Download | only in helpers
      1 // SAX default implementation for Locator.
      2 // http://www.saxproject.org
      3 // No warranty; no copyright -- use this as you will.
      4 // $Id: LocatorImpl.java,v 1.6 2002/01/30 20:52:27 dbrownell Exp $
      5 
      6 package org.xml.sax.helpers;
      7 
      8 import org.xml.sax.Locator;
      9 
     10 
     11 /**
     12  * Provide an optional convenience implementation of Locator.
     13  *
     14  * <blockquote>
     15  * <em>This module, both source code and documentation, is in the
     16  * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
     17  * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
     18  * for further information.
     19  * </blockquote>
     20  *
     21  * <p>This class is available mainly for application writers, who
     22  * can use it to make a persistent snapshot of a locator at any
     23  * point during a document parse:</p>
     24  *
     25  * <pre>
     26  * Locator locator;
     27  * Locator startloc;
     28  *
     29  * public void setLocator (Locator locator)
     30  * {
     31  *         // note the locator
     32  *   this.locator = locator;
     33  * }
     34  *
     35  * public void startDocument ()
     36  * {
     37  *         // save the location of the start of the document
     38  *         // for future use.
     39  *   Locator startloc = new LocatorImpl(locator);
     40  * }
     41  *</pre>
     42  *
     43  * <p>Normally, parser writers will not use this class, since it
     44  * is more efficient to provide location information only when
     45  * requested, rather than constantly updating a Locator object.</p>
     46  *
     47  * @since SAX 1.0
     48  * @author David Megginson
     49  * @version 2.0.1 (sax2r2)
     50  * @see org.xml.sax.Locator Locator
     51  */
     52 public class LocatorImpl implements Locator
     53 {
     54 
     55 
     56     /**
     57      * Zero-argument constructor.
     58      *
     59      * <p>This will not normally be useful, since the main purpose
     60      * of this class is to make a snapshot of an existing Locator.</p>
     61      */
     62     public LocatorImpl ()
     63     {
     64     }
     65 
     66 
     67     /**
     68      * Copy constructor.
     69      *
     70      * <p>Create a persistent copy of the current state of a locator.
     71      * When the original locator changes, this copy will still keep
     72      * the original values (and it can be used outside the scope of
     73      * DocumentHandler methods).</p>
     74      *
     75      * @param locator The locator to copy.
     76      */
     77     public LocatorImpl (Locator locator)
     78     {
     79     setPublicId(locator.getPublicId());
     80     setSystemId(locator.getSystemId());
     81     setLineNumber(locator.getLineNumber());
     82     setColumnNumber(locator.getColumnNumber());
     83     }
     84 
     85 
     86 
     87     ////////////////////////////////////////////////////////////////////
     89     // Implementation of org.xml.sax.Locator
     90     ////////////////////////////////////////////////////////////////////
     91 
     92 
     93     /**
     94      * Return the saved public identifier.
     95      *
     96      * @return The public identifier as a string, or null if none
     97      *         is available.
     98      * @see org.xml.sax.Locator#getPublicId
     99      * @see #setPublicId
    100      */
    101     public String getPublicId ()
    102     {
    103     return publicId;
    104     }
    105 
    106 
    107     /**
    108      * Return the saved system identifier.
    109      *
    110      * @return The system identifier as a string, or null if none
    111      *         is available.
    112      * @see org.xml.sax.Locator#getSystemId
    113      * @see #setSystemId
    114      */
    115     public String getSystemId ()
    116     {
    117     return systemId;
    118     }
    119 
    120 
    121     /**
    122      * Return the saved line number (1-based).
    123      *
    124      * @return The line number as an integer, or -1 if none is available.
    125      * @see org.xml.sax.Locator#getLineNumber
    126      * @see #setLineNumber
    127      */
    128     public int getLineNumber ()
    129     {
    130     return lineNumber;
    131     }
    132 
    133 
    134     /**
    135      * Return the saved column number (1-based).
    136      *
    137      * @return The column number as an integer, or -1 if none is available.
    138      * @see org.xml.sax.Locator#getColumnNumber
    139      * @see #setColumnNumber
    140      */
    141     public int getColumnNumber ()
    142     {
    143     return columnNumber;
    144     }
    145 
    146 
    147 
    148     ////////////////////////////////////////////////////////////////////
    150     // Setters for the properties (not in org.xml.sax.Locator)
    151     ////////////////////////////////////////////////////////////////////
    152 
    153 
    154     /**
    155      * Set the public identifier for this locator.
    156      *
    157      * @param publicId The new public identifier, or null
    158      *        if none is available.
    159      * @see #getPublicId
    160      */
    161     public void setPublicId (String publicId)
    162     {
    163     this.publicId = publicId;
    164     }
    165 
    166 
    167     /**
    168      * Set the system identifier for this locator.
    169      *
    170      * @param systemId The new system identifier, or null
    171      *        if none is available.
    172      * @see #getSystemId
    173      */
    174     public void setSystemId (String systemId)
    175     {
    176     this.systemId = systemId;
    177     }
    178 
    179 
    180     /**
    181      * Set the line number for this locator (1-based).
    182      *
    183      * @param lineNumber The line number, or -1 if none is available.
    184      * @see #getLineNumber
    185      */
    186     public void setLineNumber (int lineNumber)
    187     {
    188     this.lineNumber = lineNumber;
    189     }
    190 
    191 
    192     /**
    193      * Set the column number for this locator (1-based).
    194      *
    195      * @param columnNumber The column number, or -1 if none is available.
    196      * @see #getColumnNumber
    197      */
    198     public void setColumnNumber (int columnNumber)
    199     {
    200     this.columnNumber = columnNumber;
    201     }
    202 
    203 
    204 
    205     ////////////////////////////////////////////////////////////////////
    207     // Internal state.
    208     ////////////////////////////////////////////////////////////////////
    209 
    210     private String publicId;
    211     private String systemId;
    212     private int lineNumber;
    213     private int columnNumber;
    214 
    215 }
    216 
    217 // end of LocatorImpl.java
    218