Skip to content

Instantly share code, notes, and snippets.

@amobiz
Created September 30, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amobiz/5c9cfff0b62f5ad70a6a to your computer and use it in GitHub Desktop.
Save amobiz/5c9cfff0b62f5ad70a6a to your computer and use it in GitHub Desktop.
public void parse (InputSource input) throws IOException, SAXException {
setup();
Reader r = getReader(input);
theContentHandler.startDocument();
theScanner.resetDocumentLocator(input.getPublicId(), input.getSystemId());
if (theScanner instanceof Locator) {
theContentHandler.setDocumentLocator((Locator)theScanner);
}
// hack start
if (namespaces)
// hack end
if (!(theSchema.getURI().equals("")))
theContentHandler.startPrefixMapping(theSchema.getPrefix(),
theSchema.getURI());
theScanner.scan(r, this);
}
public void eof(char[] buff, int offset, int length) throws SAXException {
if (virginStack) rectify(thePCDATA);
while (theStack.next() != null) {
pop();
}
// hack start
if (namespaces)
// hack end
if (!(theSchema.getURI().equals("")))
theContentHandler.endPrefixMapping(theSchema.getPrefix());
theContentHandler.endDocument();
}
private void pop() throws SAXException {
if (theStack == null) return; // empty stack
String name = theStack.name();
String localName = theStack.localName();
String namespace = theStack.namespace();
String prefix = prefixOf(name);
// System.err.println("%% Popping " + name);
if (!namespaces) namespace = localName = "";
theContentHandler.endElement(namespace, localName, name);
// hack start
if (namespaces) {
// hack end
if (foreign(prefix, namespace)) {
theContentHandler.endPrefixMapping(prefix);
// System.err.println("%% Unmapping [" + prefix + "] for elements to " + namespace);
}
Attributes atts = theStack.atts();
for (int i = atts.getLength() - 1; i >= 0; i--) {
String attNamespace = atts.getURI(i);
String attPrefix = prefixOf(atts.getQName(i));
if (foreign(attPrefix, attNamespace)) {
theContentHandler.endPrefixMapping(attPrefix);
// System.err.println("%% Unmapping [" + attPrefix + "] for attributes to " + attNamespace);
}
}
// hack start
}
// hack end
theStack = theStack.next();
}
private void push(Element e) throws SAXException {
String name = e.name();
String localName = e.localName();
String namespace = e.namespace();
String prefix = prefixOf(name);
// System.err.println("%% Pushing " + name);
e.clean();
if (!namespaces) namespace = localName = "";
if (virginStack && localName.equalsIgnoreCase(theDoctypeName)) {
try {
theEntityResolver.resolveEntity(theDoctypePublicId, theDoctypeSystemId);
} catch (IOException ew) { } // Can't be thrown for root I believe.
}
// hack start
if (!namespaces)
// hack end
if (foreign(prefix, namespace)) {
theContentHandler.startPrefixMapping(prefix, namespace);
// System.err.println("%% Mapping [" + prefix + "] for elements to " + namespace);
}
Attributes atts = e.atts();
int len = atts.getLength();
for (int i = 0; i < len; i++) {
String attNamespace = atts.getURI(i);
String attPrefix = prefixOf(atts.getQName(i));
// hack start
if (!namespaces) {
((AttributesImpl)atts).setURI( i, "" );
((AttributesImpl)atts).setLocalName( i, "" );
} else
// hack end
if (foreign(attPrefix, attNamespace)) {
theContentHandler.startPrefixMapping(attPrefix, attNamespace);
// System.err.println("%% Mapping [" + attPrefix + "] for attributes to " + attNamespace);
}
}
theContentHandler.startElement(namespace, localName, name, e.atts());
e.setNext(theStack);
theStack = e;
virginStack = false;
if (CDATAElements && (theStack.flags() & Schema.F_CDATA) != 0) {
theScanner.startCDATA();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment