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 import org.outerj.daisy.repository.RepositoryException; 24 25 public class NavigationLinkExtractor extends AbstractLinkExtractor { 26 public ContentHandler getContentHandler(Part part, LinkCollector linkCollector, long defaultBranchId, long defaultLanguageId) { 27 return new NavigationLinkExtractionHandler(linkCollector, defaultBranchId, defaultLanguageId); 28 } 29 30 public class NavigationLinkExtractionHandler extends DefaultHandler { 31 private final LinkCollector linkCollector; 32 private final long defaultBranchId; 33 private final long defaultLanguageId; 34 private static final String NAVIGATION_NS = "http://outerx.org/daisy/1.0#navigationspec"; 35 36 public NavigationLinkExtractionHandler(LinkCollector linkCollector, long defaultBranchId, long defaultLanguageId) { 37 this.linkCollector = linkCollector; 38 this.defaultBranchId = defaultBranchId; 39 this.defaultLanguageId = defaultLanguageId; 40 } 41 42 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 43 if ((localName.equals("doc") || localName.equals("import")) && uri.equals(NAVIGATION_NS)) { 44 String idString = attributes.getValue(localName.equals("doc") ? "id" : "docId"); 45 long id; 46 if (idString != null) { 47 try { 48 id = Long.parseLong(idString); 49 String branch = attributes.getValue("branch"); 50 long branchId = defaultBranchId; 51 if (branch != null) 52 branchId = linkCollector.getBranchId(branch); 53 String language = attributes.getValue("language"); 54 long languageId = defaultLanguageId; 55 if (language != null) 56 languageId = linkCollector.getLanguageId(language); 57 linkCollector.addLink(LinkType.OTHER, id, branchId, languageId, -1); 58 } catch (NumberFormatException e) { 59 } catch (RepositoryException e) { 61 } 63 } 64 } 65 } 66 } 67 } 68 | Popular Tags |