com.googlecode.aaw.badgerfish.SAXHandler Maven / Gradle / Ivy
The newest version!
/**
* Copyright © 2012, 2013 dr. ir. Jeroen M. Valk
*
* This file is part of Badgerfish CPX. Badgerfish CPX is free software:
* you can redistribute it and/or modify it under the terms of the GNU Lesser
* General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version. Badgerfish
* CPX is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU Lesser General Public License for more details. You
* should have received a copy of the GNU Lesser General Public License along
* with Badgerfish CPX. If not, see .
*/
package com.googlecode.aaw.badgerfish;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.googlecode.aaw.arche.Arche;
public abstract class SAXHandler extends DefaultHandler {
static final private Arche arche = Arche.getArche(SAXHandler.class);
static final private Logger logger = (Logger) arche.getInstance();
private final List ancestors = new ArrayList();
private final List builders = new ArrayList();
private int depth = 0;
abstract protected void onDocumentStart();
abstract protected void onDocumentEnd();
abstract protected void onElementStart(String tagname);
abstract protected void onElementEnd(String tagname, String content);
abstract protected void onElement(String tagname, String value);
protected final int getDepth() {
return depth;
}
protected final String getAncestor(int index) {
return ancestors.get(index);
}
@Override
public final void startDocument() throws SAXException {
logger.info("Parsing...");
onDocumentStart();
}
@Override
public final void endDocument() throws SAXException {
logger.info("DONE");
onDocumentEnd();
}
@Override
public final void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
int i = ancestors.size();
if (i-- > 0) {
String tagname = ancestors.get(i);
if (tagname != null) {
ancestors.set(i, null);
logger.debug("<" + tagname + ">");
onElementStart(tagname);
++depth;
}
}
ancestors.add(qName);
builders.add(new StringBuilder());
}
@Override
public final void endElement(String uri, String localName, String qName)
throws SAXException {
int i = ancestors.size();
assert i > 0;
String tagname = ancestors.remove(--i);
StringBuilder builder = builders.remove(i);
if (tagname == null) {
logger.debug("" + qName + ">");
--depth;
assert depth == 0;
onElementEnd(qName, builder.toString());
} else {
assert tagname != null && tagname.equals(qName);
logger.debug("<" + qName + "/>");
onElement(qName, builder.toString());
}
}
@Override
public final void characters(char[] ch, int start, int length)
throws SAXException {
int i = builders.size();
assert i > 0;
builders.get(--i).append(ch, start, length);
}
}