001// SAX default implementation for Locator. 002// No warranty; no copyright -- use this as you will. 003// $Id: LocatorImpl.java,v 1.1 2001/03/05 21:40:06 jstrachan Exp $ 004 005package org.xml.sax.helpers; 006 007import org.xml.sax.Locator; 008 009 010/** 011 * Provide an optional convenience implementation of Locator. 012 * 013 * <blockquote> 014 * <em>This module, both source code and documentation, is in the 015 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em> 016 * </blockquote> 017 * 018 * <p>This class is available mainly for application writers, who 019 * can use it to make a persistent snapshot of a locator at any 020 * point during a document parse:</p> 021 * 022 * <pre> 023 * Locator locator; 024 * Locator startloc; 025 * 026 * public void setLocator (Locator locator) 027 * { 028 * // note the locator 029 * this.locator = locator; 030 * } 031 * 032 * public void startDocument () 033 * { 034 * // save the location of the start of the document 035 * // for future use. 036 * Locator startloc = new LocatorImpl(locator); 037 * } 038 *</pre> 039 * 040 * <p>Normally, parser writers will not use this class, since it 041 * is more efficient to provide location information only when 042 * requested, rather than constantly updating a Locator object.</p> 043 * 044 * @since SAX 1.0 045 * @author David Megginson, 046 * <a href="mailto:sax@megginson.com">sax@megginson.com</a> 047 * @version 2.0 048 * @see org.xml.sax.Locator Locator 049 */ 050public class LocatorImpl implements Locator 051{ 052 053 054 /** 055 * Zero-argument constructor. 056 * 057 * <p>This will not normally be useful, since the main purpose 058 * of this class is to make a snapshot of an existing Locator.</p> 059 */ 060 public LocatorImpl () 061 { 062 } 063 064 065 /** 066 * Copy constructor. 067 * 068 * <p>Create a persistent copy of the current state of a locator. 069 * When the original locator changes, this copy will still keep 070 * the original values (and it can be used outside the scope of 071 * DocumentHandler methods).</p> 072 * 073 * @param locator The locator to copy. 074 */ 075 public LocatorImpl (Locator locator) 076 { 077 setPublicId(locator.getPublicId()); 078 setSystemId(locator.getSystemId()); 079 setLineNumber(locator.getLineNumber()); 080 setColumnNumber(locator.getColumnNumber()); 081 } 082 083 084 085 //////////////////////////////////////////////////////////////////// 086 // Implementation of org.xml.sax.Locator 087 //////////////////////////////////////////////////////////////////// 088 089 090 /** 091 * Return the saved public identifier. 092 * 093 * @return The public identifier as a string, or null if none 094 * is available. 095 * @see org.xml.sax.Locator#getPublicId 096 * @see #setPublicId 097 */ 098 public String getPublicId () 099 { 100 return publicId; 101 } 102 103 104 /** 105 * Return the saved system identifier. 106 * 107 * @return The system identifier as a string, or null if none 108 * is available. 109 * @see org.xml.sax.Locator#getSystemId 110 * @see #setSystemId 111 */ 112 public String getSystemId () 113 { 114 return systemId; 115 } 116 117 118 /** 119 * Return the saved line number (1-based). 120 * 121 * @return The line number as an integer, or -1 if none is available. 122 * @see org.xml.sax.Locator#getLineNumber 123 * @see #setLineNumber 124 */ 125 public int getLineNumber () 126 { 127 return lineNumber; 128 } 129 130 131 /** 132 * Return the saved column number (1-based). 133 * 134 * @return The column number as an integer, or -1 if none is available. 135 * @see org.xml.sax.Locator#getColumnNumber 136 * @see #setColumnNumber 137 */ 138 public int getColumnNumber () 139 { 140 return columnNumber; 141 } 142 143 144 145 //////////////////////////////////////////////////////////////////// 146 // Setters for the properties (not in org.xml.sax.Locator) 147 //////////////////////////////////////////////////////////////////// 148 149 150 /** 151 * Set the public identifier for this locator. 152 * 153 * @param publicId The new public identifier, or null 154 * if none is available. 155 * @see #getPublicId 156 */ 157 public void setPublicId (String publicId) 158 { 159 this.publicId = publicId; 160 } 161 162 163 /** 164 * Set the system identifier for this locator. 165 * 166 * @param systemId The new system identifier, or null 167 * if none is available. 168 * @see #getSystemId 169 */ 170 public void setSystemId (String systemId) 171 { 172 this.systemId = systemId; 173 } 174 175 176 /** 177 * Set the line number for this locator (1-based). 178 * 179 * @param lineNumber The line number, or -1 if none is available. 180 * @see #getLineNumber 181 */ 182 public void setLineNumber (int lineNumber) 183 { 184 this.lineNumber = lineNumber; 185 } 186 187 188 /** 189 * Set the column number for this locator (1-based). 190 * 191 * @param columnNumber The column number, or -1 if none is available. 192 * @see #getColumnNumber 193 */ 194 public void setColumnNumber (int columnNumber) 195 { 196 this.columnNumber = columnNumber; 197 } 198 199 200 201 //////////////////////////////////////////////////////////////////// 202 // Internal state. 203 //////////////////////////////////////////////////////////////////// 204 205 private String publicId; 206 private String systemId; 207 private int lineNumber; 208 private int columnNumber; 209 210} 211 212// end of LocatorImpl.java