Methods Summary |
---|
void | append(java.lang.String xml)Appends part of an XML document. This parser will parse the given XML to
the extent possible and dispatch to the appropriate methods.
try {
append(this.pointer, xml, false);
} catch (ExpatException e) {
throw new ParseException(e.getMessage(), this.locator);
}
|
private native void | append(int pointer, java.lang.String xml, boolean isFinal)
|
void | append(char[] xml, int offset, int length)Appends part of an XML document. This parser will parse the given XML to
the extent possible and dispatch to the appropriate methods.
try {
append(this.pointer, xml, offset, length);
} catch (ExpatException e) {
throw new ParseException(e.getMessage(), this.locator);
}
|
private native void | append(int pointer, char[] xml, int offset, int length)
|
void | append(byte[] xml)Appends part of an XML document. This parser will parse the given XML to
the extent possible and dispatch to the appropriate methods.
append(xml, 0, xml.length);
|
void | append(byte[] xml, int offset, int length)Appends part of an XML document. This parser will parse the given XML to
the extent possible and dispatch to the appropriate methods.
try {
append(this.pointer, xml, offset, length);
} catch (ExpatException e) {
throw new ParseException(e.getMessage(), this.locator);
}
|
private native void | append(int pointer, byte[] xml, int offset, int length)
|
org.xml.sax.Attributes | cloneAttributes()Clones the current attributes so they can be used outside of
startElement().
if (!inStartElement) {
throw new IllegalStateException(OUTSIDE_START_ELEMENT);
}
if (attributeCount == 0) {
return ClonedAttributes.EMPTY;
}
int clonePointer
= cloneAttributes(this.attributePointer, this.attributeCount);
return new ClonedAttributes(pointer, clonePointer, attributeCount);
|
private static native int | cloneAttributes(int pointer, int attributeCount)
|
private int | column()Gets the current column number within the XML file.
return column(this.pointer);
|
private static native int | column(int pointer)
|
void | comment(char[] text, int length)
LexicalHandler lexicalHandler = xmlReader.lexicalHandler;
if (lexicalHandler != null) {
lexicalHandler.comment(text, 0, length);
}
|
private static native int | createEntityParser(int parentPointer, java.lang.String context, java.lang.String encoding)Creates a native entity parser.
|
void | endCdata()
LexicalHandler lexicalHandler = xmlReader.lexicalHandler;
if (lexicalHandler != null) {
lexicalHandler.endCDATA();
}
|
private void | endDocument()
ContentHandler contentHandler;
contentHandler = xmlReader.contentHandler;
if (contentHandler != null) {
contentHandler.endDocument();
}
|
void | endDtd()
LexicalHandler lexicalHandler = xmlReader.lexicalHandler;
if (lexicalHandler != null) {
lexicalHandler.endDTD();
}
|
void | endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
ContentHandler contentHandler = xmlReader.contentHandler;
if (contentHandler != null) {
contentHandler.endElement(uri, localName, qName);
}
|
void | endNamespace(java.lang.String prefix)
ContentHandler contentHandler = xmlReader.contentHandler;
if (contentHandler != null) {
contentHandler.endPrefixMapping(prefix);
}
|
protected synchronized void | finalize()
if (this.pointer != 0) {
release(this.pointer);
this.pointer = 0;
}
|
void | finish()Indicate that we're finished parsing.
try {
append(this.pointer, "", true);
} catch (ExpatException e) {
throw new ParseException(e.getMessage(), this.locator);
}
|
void | handleExternalEntity(java.lang.String context, java.lang.String publicId, java.lang.String systemId)Handles an external entity.
EntityResolver entityResolver = xmlReader.entityResolver;
if (entityResolver == null) {
return;
}
/*
* The spec. is terribly under-specified here. It says that if the
* systemId is a URL, we should try to resolve it, but it doesn't
* specify how to tell whether or not the systemId is a URL let alone
* how to resolve it.
*
* Other implementations do various insane things. We try to keep it
* simple: if the systemId parses as a URI and it's relative, we try to
* resolve it against the parent document's systemId. If anything goes
* wrong, we go with the original systemId. If crazybob had designed
* the API, he would have left all resolving to the EntityResolver.
*/
if (this.systemId != null) {
try {
URI systemUri = new URI(systemId);
if (!systemUri.isAbsolute() && !systemUri.isOpaque()) {
// It could be relative (or it may not be a URI at all!)
URI baseUri = new URI(this.systemId);
systemUri = baseUri.resolve(systemUri);
// Replace systemId w/ resolved URI
systemId = systemUri.toString();
}
} catch (Exception e) {
Logger.getLogger(ExpatParser.class.getName()).log(Level.INFO,
"Could not resolve '" + systemId + "' relative to"
+ " '" + this.systemId + "' at " + locator, e);
}
}
InputSource inputSource = entityResolver.resolveEntity(
publicId, systemId);
if (inputSource == null) {
/*
* The spec. actually says that we should try to treat systemId
* as a URL and download and parse its contents here, but an
* entity resolver can easily accomplish the same by returning
* new InputSource(systemId).
*
* Downloading external entities by default would result in several
* unwanted DTD downloads, not to mention pose a security risk
* when parsing untrusted XML (http://tinyurl.com/56ggrk),
* so we just do nothing instead. This also enables the user to
* opt out of entity parsing when using
* {@link org.xml.sax.helpers.DefaultHandler}, something that
* wouldn't be possible otherwise.
*/
return;
}
String encoding = pickEncoding(inputSource);
int pointer = createEntityParser(this.pointer, context, encoding);
try {
EntityParser entityParser = new EntityParser(encoding, xmlReader,
pointer, inputSource.getPublicId(),
inputSource.getSystemId());
parseExternalEntity(entityParser, inputSource);
} finally {
releaseParser(pointer);
}
|
private native int | initialize(java.lang.String encoding, boolean namespacesEnabled)Initializes native resources.
|
private int | line()Gets the current line number within the XML file.
staticInitialize("");
return line(this.pointer);
|
private static native int | line(int pointer)
|
static java.io.InputStream | openUrl(java.lang.String url)Opens an InputStream for the given URL.
try {
URLConnection urlConnection = new URL(url).openConnection();
urlConnection.setConnectTimeout(TIMEOUT);
urlConnection.setReadTimeout(TIMEOUT);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(false);
return urlConnection.getInputStream();
} catch (Exception e) {
IOException ioe = new IOException("Couldn't open " + url);
ioe.initCause(e);
throw ioe;
}
|
void | parseDocument(java.io.InputStream in)Parses an XML document from the given input stream.
startDocument();
parseFragment(in);
finish();
endDocument();
|
void | parseDocument(java.io.Reader in)Parses an XML Document from the given reader.
startDocument();
parseFragment(in);
finish();
endDocument();
|
private void | parseExternalEntity(org.apache.harmony.xml.ExpatParser entityParser, org.xml.sax.InputSource inputSource)Parses the the external entity provided by the input source.
/*
* Expat complains if the external entity isn't wrapped with a root
* element so we add one and ignore it later on during parsing.
*/
// Try the character stream.
Reader reader = inputSource.getCharacterStream();
if (reader != null) {
try {
entityParser.append("<externalEntity>");
entityParser.parseFragment(reader);
entityParser.append("</externalEntity>");
} finally {
// TODO: Don't eat original exception when close() throws.
reader.close();
}
return;
}
// Try the byte stream.
InputStream in = inputSource.getByteStream();
if (in != null) {
try {
entityParser.append("<externalEntity>"
.getBytes(entityParser.encoding));
entityParser.parseFragment(in);
entityParser.append("</externalEntity>"
.getBytes(entityParser.encoding));
} finally {
// TODO: Don't eat original exception when close() throws.
in.close();
}
return;
}
// Make sure we use the user-provided systemId.
String systemId = inputSource.getSystemId();
if (systemId == null) {
// TODO: We could just try our systemId here.
throw new ParseException("No input specified.", locator);
}
// Try the system id.
in = openUrl(systemId);
try {
entityParser.append("<externalEntity>"
.getBytes(entityParser.encoding));
entityParser.parseFragment(in);
entityParser.append("</externalEntity>"
.getBytes(entityParser.encoding));
} finally {
in.close();
}
|
private void | parseFragment(java.io.Reader in)Parses XML from the given Reader.
char[] buffer = new char[BUFFER_SIZE / 2];
int length;
while ((length = in.read(buffer)) != -1) {
try {
append(this.pointer, buffer, 0, length);
} catch (ExpatException e) {
throw new ParseException(e.getMessage(), locator);
}
}
|
private void | parseFragment(java.io.InputStream in)Parses XML from the given input stream.
byte[] buffer = new byte[BUFFER_SIZE];
int length;
while ((length = in.read(buffer)) != -1) {
try {
append(this.pointer, buffer, 0, length);
} catch (ExpatException e) {
throw new ParseException(e.getMessage(), this.locator);
}
}
|
private java.lang.String | pickEncoding(org.xml.sax.InputSource inputSource)Picks an encoding for an external entity. Defaults to UTF-8.
Reader reader = inputSource.getCharacterStream();
if (reader != null) {
return CHARACTER_ENCODING;
}
String encoding = inputSource.getEncoding();
return encoding == null ? DEFAULT_ENCODING : encoding;
|
void | processingInstruction(java.lang.String target, java.lang.String data)
ContentHandler contentHandler = xmlReader.contentHandler;
if (contentHandler != null) {
contentHandler.processingInstruction(target, data);
}
|
private native void | release(int pointer)Releases all native objects.
|
private static native void | releaseParser(int pointer)Releases native parser only.
|
void | startCdata()
LexicalHandler lexicalHandler = xmlReader.lexicalHandler;
if (lexicalHandler != null) {
lexicalHandler.startCDATA();
}
|
private void | startDocument()
ContentHandler contentHandler = xmlReader.contentHandler;
if (contentHandler != null) {
contentHandler.setDocumentLocator(this.locator);
contentHandler.startDocument();
}
|
void | startDtd(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
LexicalHandler lexicalHandler = xmlReader.lexicalHandler;
if (lexicalHandler != null) {
lexicalHandler.startDTD(name, publicId, systemId);
}
|
void | startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, int attributePointer, int attributeCount)Called at the start of an element.
ContentHandler contentHandler = xmlReader.contentHandler;
if (contentHandler == null) {
return;
}
try {
inStartElement = true;
this.attributePointer = attributePointer;
this.attributeCount = attributeCount;
contentHandler.startElement(
uri, localName, qName, this.attributes);
}
finally {
inStartElement = false;
this.attributeCount = -1;
this.attributePointer = 0;
}
|
void | startNamespace(java.lang.String prefix, java.lang.String uri)
ContentHandler contentHandler = xmlReader.contentHandler;
if (contentHandler != null) {
contentHandler.startPrefixMapping(prefix, uri);
}
|
private static native void | staticInitialize(java.lang.String emptyString)Initialize static resources.
|
void | text(char[] text, int length)
ContentHandler contentHandler = xmlReader.contentHandler;
if (contentHandler != null) {
contentHandler.characters(text, 0, length);
}
|