Advertisement
Guest User

XmlHandler.java

a guest
Apr 9th, 2011
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. package main;
  2.  
  3. import org.xml.sax.Attributes;
  4. import org.xml.sax.SAXException;
  5. import org.xml.sax.helpers.DefaultHandler;
  6.  
  7. public class XmlHandler extends DefaultHandler {
  8.  
  9.     private String tocParent;
  10.     private Content content;
  11.  
  12.     private boolean inNavMap = false;
  13.     private boolean inNavPoint = false;
  14.     private boolean inNavLabel = false;
  15.     private boolean inText = false;
  16.  
  17.     @Override
  18.     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  19.         if (qName.equalsIgnoreCase("rootfile")) {
  20.             ParsedDataSet.setContentOpfUrl(attributes.getValue("full-path"));
  21.             tocParent = ParsedDataSet.getContentOpfUrl().substring(0, ParsedDataSet.getContentOpfUrl().lastIndexOf("/") + 1);
  22.         }
  23.         if (qName.equalsIgnoreCase("item")) {
  24.             if (attributes.getValue("id").equalsIgnoreCase("ncx")) {
  25.                 ParsedDataSet.setTocUrl(tocParent + attributes.getValue("href"));
  26.             }
  27.         }
  28.  
  29.         // this structure only checks names of children if already in correct
  30.         // parents
  31.         if (qName.equalsIgnoreCase("navMap") || inNavMap == true) {
  32.             inNavMap = true;
  33.             if (qName.equalsIgnoreCase("navPoint") || inNavPoint == true) {
  34.                 inNavPoint = true;
  35.  
  36.                 // all info on each of the chapters/toc points will be placed in
  37.                 // this
  38.                 content = new Content();
  39.  
  40.                 if (qName.equalsIgnoreCase("navLabel") || inNavLabel == true) {
  41.                     inNavLabel = true;
  42.                     if (qName.equalsIgnoreCase("text")) {
  43.                         inText = true;
  44.                     }
  45.                 }
  46.                 if (qName.equalsIgnoreCase("content")) {
  47.                     String contentURL = attributes.getValue("src");
  48.                     String chapterTag = contentURL.substring(contentURL.lastIndexOf("#"), contentURL.length());
  49.                     contentURL = tocParent + contentURL.substring(0, contentURL.lastIndexOf("#"));
  50.  
  51.                     content.setURL(contentURL);
  52.                     content.setTag(chapterTag);
  53.                 }
  54.             }
  55.         }
  56.     }
  57.  
  58.     @Override
  59.     public void characters(char[] ch, int start, int length) throws SAXException {
  60.         if (inText) {
  61.             content.setName(new String(ch, start, length));
  62.         }
  63.     }
  64.  
  65.     @Override
  66.     public void endElement(String uri, String localName, String qName) throws SAXException {
  67.  
  68.         if (qName.equalsIgnoreCase("navMap")) {
  69.             inNavMap = false;
  70.         }
  71.         if (qName.equalsIgnoreCase("navPoint")) {
  72.             inNavPoint = false;
  73.            
  74.             ParsedDataSet.getToc().add(content);
  75.             System.out.println(ParsedDataSet.getToc().get(0).getName());
  76.            
  77.         }
  78.         if (qName.equalsIgnoreCase("navLabel")) {
  79.             inNavLabel = false;
  80.         }
  81.         if (qName.equalsIgnoreCase("text")) {
  82.             inText = false;
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement