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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
package com.jclark.xml.sax;
import com.jclark.xml.parse.*;
import com.jclark.xml.parse.base.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.InputStream;
import java.io.Reader;
import java.io.IOException;
import java.util.Enumeration;
import org.xml.sax.SAXException;
/**
* An implementation of SAX 1.0 on top of the com.jclark.xml.parse package.
* Note that:
* <UL>
* <LI>the <code>Locator</code> will provide information only for
* <code>startElement</code> and <code>processingInstruction</code>
* events;
* <LI>the line and column number returned by <code>Locator</code>
* will correspond to the first character of the document event
* not the character following the document event as specified by SAX;
* <LI>neither a <code>Locator</code> nor a <code>SAXParseException</code>
* will provide information about an entity's public identifier;
* <LI>the <code>Locator</code> object will be an instance of
* <code>com.jclark.xml.sax.Locator</code> which extends
* <code>org.xml.sax.Locator</code>;
* <LI>the only kind of error that is reported is a fatal error.
* </UL>
* @see Locator
* @version $Revision: 1.16 $ $Date: 1998/12/28 08:12:30 $
*/
public class Driver extends ApplicationImpl
implements org.xml.sax.Parser, EntityManager, org.xml.sax.AttributeList, Locator {
private org.xml.sax.EntityResolver entityResolver;
private org.xml.sax.DocumentHandler documentHandler;
private org.xml.sax.ErrorHandler errorHandler;
private org.xml.sax.DTDHandler dtdHandler;
private Parser parser = new ParserImpl();
private StartElementEvent event;
private LocatedEvent locatedEvent;
private static final int INIT_DATA_BUF_SIZE = 80;
private char[] dataBuf = new char[INIT_DATA_BUF_SIZE];
private int dataBufUsed = 0;
private final static int UNKNOWN_INDEX = -2;
private int idAttributeIndex;
public Driver() {
org.xml.sax.HandlerBase handler = new org.xml.sax.HandlerBase();
this.documentHandler = handler;
this.dtdHandler = handler;
this.errorHandler = handler;
this.entityResolver = handler;
parser.setApplication(this);
parser.setEntityManager(this);
}
public void setEntityResolver(org.xml.sax.EntityResolver resolver) {
this.entityResolver = resolver;
}
public void setDocumentHandler(org.xml.sax.DocumentHandler handler) {
this.documentHandler = handler;
}
public void setDTDHandler(org.xml.sax.DTDHandler handler) {
this.dtdHandler = handler;
}
public void setErrorHandler(org.xml.sax.ErrorHandler handler) {
this.errorHandler = handler;
}
public void setLocale(java.util.Locale locale) {
parser.setLocale(locale);
}
public void parse(String systemId) throws SAXException, IOException {
parse(new org.xml.sax.InputSource(systemId));
}
public void parse(org.xml.sax.InputSource in) throws SAXException, IOException {
documentHandler.setDocumentLocator(this);
try {
parser.parseDocument(openInputSource(in));
}
catch (WrapperException e) {
throw e.getWrapped();
}
catch (NotWellFormedException e) {
errorHandler.fatalError(
new org.xml.sax.SAXParseException(e.getMessageWithoutLocation(),
null,
e.getEntityLocation(),
e.getLineNumber(),
e.getColumnNumber()));
}
catch (ApplicationException e) {
throw (SAXException)e.getException();
}
}
public void startDocument() throws SAXException {
documentHandler.startDocument();
}
public void startElement(StartElementEvent event) throws SAXException {
flushData();
this.event = event;
this.locatedEvent = event;
this.idAttributeIndex = UNKNOWN_INDEX;
documentHandler.startElement(event.getName(), this);
this.locatedEvent = null;
}
public void characterData(CharacterDataEvent event) {
int need = event.getLengthMax() + dataBufUsed;
if (need > dataBuf.length) {
int newLength = dataBuf.length << 1;
while (need > newLength)
newLength <<= 1;
char[] tem = dataBuf;
dataBuf = new char[newLength];
if (dataBufUsed > 0)
System.arraycopy(tem, 0, dataBuf, 0, dataBufUsed);
}
dataBufUsed += event.copyChars(dataBuf, dataBufUsed);
}
private final void flushData() throws SAXException {
if (dataBufUsed > 0) {
documentHandler.characters(dataBuf, 0, dataBufUsed);
dataBufUsed = 0;
}
}
public void endElement(EndElementEvent event) throws SAXException {
flushData();
documentHandler.endElement(event.getName());
}
public void processingInstruction(ProcessingInstructionEvent event) throws SAXException {
flushData();
this.locatedEvent = event;
documentHandler.processingInstruction(event.getName(),
event.getInstruction());
this.locatedEvent = null;
}
public void endProlog(EndPrologEvent event) throws SAXException {
if (dtdHandler == null)
return;
DTD dtd = event.getDTD();
for (Enumeration enum = dtd.entityNames(DTD.NOTATION);
enum.hasMoreElements(); ) {
String name = (String)enum.nextElement();
Entity entity = dtd.getEntity(DTD.NOTATION, name);
String systemId = entity.getSystemId();
if (systemId != null) {
try {
systemId = new URL(entity.getBase(), systemId).toString();
}
catch (MalformedURLException e) { }
}
dtdHandler.notationDecl(name, entity.getPublicId(), systemId);
}
for (Enumeration enum = dtd.entityNames(DTD.GENERAL_ENTITY);
enum.hasMoreElements();) {
String name = (String)enum.nextElement();
Entity entity = dtd.getEntity(DTD.GENERAL_ENTITY, name);
String notationName = entity.getNotationName();
if (notationName != null) {
String systemId = entity.getSystemId();
if (systemId != null) {
try {
systemId = new URL(entity.getBase(), systemId).toString();
}
catch (MalformedURLException e) { }
}
dtdHandler.unparsedEntityDecl(name,
entity.getPublicId(),
systemId,
notationName);
}
}
}
public void endDocument() throws SAXException {
flushData();
documentHandler.endDocument();
}
public OpenEntity open(String systemId, URL baseURL, String publicId) throws IOException {
if (entityResolver != null) {
try {
String s = systemId;
try {
s = new URL(baseURL, systemId).toString();
}
catch (MalformedURLException e) { }
org.xml.sax.InputSource inputSource
= entityResolver.resolveEntity(publicId, s);
if (inputSource != null)
return openInputSource(inputSource);
}
catch (SAXException e) {
throw new WrapperException(e);
}
}
URL url = new URL(baseURL, systemId);
return new OpenEntity(url.openStream(),
url.toString(),
url,
null);
}
private OpenEntity openInputSource(org.xml.sax.InputSource inputSource) throws IOException {
Reader reader = inputSource.getCharacterStream();
String encoding;
InputStream in;
if (reader != null) {
in = new ReaderInputStream(reader);
encoding = "UTF-16";
}
else {
in = inputSource.getByteStream();
encoding = inputSource.getEncoding();
}
String systemId = inputSource.getSystemId();
URL url = null;
if (in == null) {
if (systemId == null)
return null;
url = new URL(systemId);
in = url.openStream();
}
else if (systemId != null) {
try {
url = new URL(systemId);
}
catch (MalformedURLException e) { }
}
else
systemId = "(internal)";
return new OpenEntity(in, systemId, url, encoding);
}
public int getLength() {
return event.getAttributeCount();
}
public String getName(int i) {
return event.getAttributeName(i);
}
public String getValue(int i) {
return event.getAttributeValue(i);
}
public String getValue(String name) {
return event.getAttributeValue(name);
}
public String getType(int i) {
if (idAttributeIndex == UNKNOWN_INDEX)
idAttributeIndex = event.getIdAttributeIndex();
return i == idAttributeIndex ? "ID" : "CDATA";
}
public String getType(String name) {
if (idAttributeIndex == UNKNOWN_INDEX)
idAttributeIndex = event.getIdAttributeIndex();
if (idAttributeIndex >= 0
&& event.getAttributeName(idAttributeIndex).equals(name))
return "ID";
return "CDATA";
}
public String getSystemId() {
if (locatedEvent == null)
return null;
return locatedEvent.getLocation().getEntityLocation();
}
public URL getURL() {
if (locatedEvent == null)
return null;
return locatedEvent.getLocation().getEntityBase();
}
public String getPublicId() {
return null;
}
public int getLineNumber() {
if (locatedEvent == null)
return -1;
return locatedEvent.getLocation().getLineNumber();
}
public int getColumnNumber() {
if (locatedEvent == null)
return -1;
int col = locatedEvent.getLocation().getColumnNumber();
if (col < 0)
return col;
return col + 1;
}
public long getByteIndex() {
if (locatedEvent == null)
return -1;
return locatedEvent.getLocation().getByteIndex();
}
}
|