Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- public class XmlHandler extends DefaultHandler {
- private String tocParent;
- private Content content;
- private boolean inNavMap = false;
- private boolean inNavPoint = false;
- private boolean inNavLabel = false;
- private boolean inText = false;
- @Override
- public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
- if (qName.equalsIgnoreCase("rootfile")) {
- ParsedDataSet.setContentOpfUrl(attributes.getValue("full-path"));
- tocParent = ParsedDataSet.getContentOpfUrl().substring(0, ParsedDataSet.getContentOpfUrl().lastIndexOf("/") + 1);
- }
- if (qName.equalsIgnoreCase("item")) {
- if (attributes.getValue("id").equalsIgnoreCase("ncx")) {
- ParsedDataSet.setTocUrl(tocParent + attributes.getValue("href"));
- }
- }
- // this structure only checks names of children if already in correct
- // parents
- if (qName.equalsIgnoreCase("navMap") || inNavMap == true) {
- inNavMap = true;
- if (qName.equalsIgnoreCase("navPoint") || inNavPoint == true) {
- inNavPoint = true;
- // all info on each of the chapters/toc points will be placed in
- // this
- content = new Content();
- if (qName.equalsIgnoreCase("navLabel") || inNavLabel == true) {
- inNavLabel = true;
- if (qName.equalsIgnoreCase("text")) {
- inText = true;
- }
- }
- if (qName.equalsIgnoreCase("content")) {
- String contentURL = attributes.getValue("src");
- String chapterTag = contentURL.substring(contentURL.lastIndexOf("#"), contentURL.length());
- contentURL = tocParent + contentURL.substring(0, contentURL.lastIndexOf("#"));
- content.setURL(contentURL);
- content.setTag(chapterTag);
- }
- }
- }
- }
- @Override
- public void characters(char[] ch, int start, int length) throws SAXException {
- if (inText) {
- content.setName(new String(ch, start, length));
- }
- }
- @Override
- public void endElement(String uri, String localName, String qName) throws SAXException {
- if (qName.equalsIgnoreCase("navMap")) {
- inNavMap = false;
- }
- if (qName.equalsIgnoreCase("navPoint")) {
- inNavPoint = false;
- ParsedDataSet.getToc().add(content);
- System.out.println(ParsedDataSet.getToc().get(0).getName());
- }
- if (qName.equalsIgnoreCase("navLabel")) {
- inNavLabel = false;
- }
- if (qName.equalsIgnoreCase("text")) {
- inText = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement