1 16 package org.outerj.daisy.linkextraction; 17 18 import org.xml.sax.helpers.DefaultHandler ; 19 import org.xml.sax.Attributes ; 20 import org.xml.sax.SAXException ; 21 import org.xml.sax.ContentHandler ; 22 import org.outerj.daisy.repository.Part; 23 24 public class DaisyHtmlLinkExtractor extends AbstractLinkExtractor { 25 public ContentHandler getContentHandler(Part part, LinkCollector linkCollector, long defaultBranchId, long defaultLanguageId) { 26 return new DaisyHtmlLinkExtractionHandler(linkCollector); 27 } 28 29 public class DaisyHtmlLinkExtractionHandler extends DefaultHandler { 30 private int includeLevel = -1; 31 private StringBuffer includeBuffer; 32 private LinkCollector linkCollector; 33 34 public DaisyHtmlLinkExtractionHandler(LinkCollector linkCollector) { 35 this.linkCollector = linkCollector; 36 } 37 38 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 39 if (namespaceURI.equals("")) { 40 if (includeLevel > -1) { 41 includeLevel++; 42 } else if (localName.equals("a")) { 43 String href = atts.getValue("href"); 44 linkCollector.addLink(LinkType.INLINE, href); 45 } else if (localName.equals("img")) { 46 String src = atts.getValue("src"); 47 linkCollector.addLink(LinkType.IMAGE, src); 48 } else if (localName.equals("pre")) { 49 String clazz = atts.getValue("class"); 50 if (clazz != null && clazz.equals("include")) { 51 includeLevel = 0; 52 includeBuffer = new StringBuffer (); 53 } 54 } 55 } 56 } 57 58 public void endElement(String uri, String localName, String qName) throws SAXException { 59 if (includeLevel > -1) { 60 includeLevel--; 61 if (includeLevel == -1) { 62 linkCollector.addLink(LinkType.INCLUDE, includeBuffer.toString().trim()); 63 } 64 } 65 } 66 67 public void characters(char ch[], int start, int length) throws SAXException { 68 if (includeLevel > -1) { 69 includeBuffer.append(ch, start, length); 70 } 71 } 72 } 73 } 74 | Popular Tags |