1 16 package org.outerj.daisy.books.publisher.impl.dataretrieval; 17 18 import org.xml.sax.Attributes ; 19 import org.xml.sax.SAXException ; 20 import org.xml.sax.ContentHandler ; 21 import org.xml.sax.Locator ; 22 import org.outerj.daisy.util.Constants; 23 import org.outerj.daisy.books.publisher.impl.VersionKey; 24 import org.outerj.daisy.repository.variant.Branch; 25 import org.outerj.daisy.repository.variant.Language; 26 import org.outerj.daisy.repository.variant.VariantManager; 27 import org.outerj.daisy.repository.RepositoryException; 28 import org.outerj.daisy.repository.Repository; 29 import org.outerj.daisy.repository.VariantKey; 30 import org.outerj.daisy.repository.Document; 31 import org.outerj.daisy.publisher.Publisher; 32 import org.outerj.daisy.publisher.BlobInfo; 33 import org.outerj.daisy.books.store.BookInstance; 34 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout; 35 import org.apache.cocoon.xml.AttributesImpl; 36 import org.outerx.daisy.x10Bookstoremeta.ResourcePropertiesDocument; 37 38 import java.util.regex.Matcher ; 39 import java.io.InputStream ; 40 41 public class ImageHandler implements ContentHandler { 42 private long currentDocumentId = -1; 43 private long currentDocBranchId = -1; 44 private long currentDocLanguageId = -1; 45 private VariantManager variantManager; 46 private Repository repository; 47 private ContentHandler consumer; 48 private BookInstance bookInstance; 49 private BookDependencies bookDependencies; 50 51 public ImageHandler(Repository repository, BookInstance bookInstance, BookDependencies bookDependencies, ContentHandler consumer) { 52 this.repository = repository; 53 this.variantManager = repository.getVariantManager(); 54 this.consumer = consumer; 55 this.bookInstance = bookInstance; 56 this.bookDependencies = bookDependencies; 57 } 58 59 public void setConsumer(ContentHandler consumer) { 60 this.consumer = consumer; 61 } 62 63 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 64 if (uri.equals(Constants.DAISY_NAMESPACE) && localName.equals("document")) { 65 currentDocumentId = Long.parseLong(attributes.getValue("id")); 66 currentDocBranchId = Long.parseLong(attributes.getValue("branchId")); 67 currentDocLanguageId = Long.parseLong(attributes.getValue("languageId")); 68 } else if (uri.equals("") && localName.equals("img")) { 69 if (currentDocumentId == -1) 70 throw new SAXException ("ImageHandler: encountered img tag outside the context of a document."); 71 72 String src = attributes.getValue("src"); 73 if (src != null && !src.equals("")) { 74 Matcher matcher = Constants.DAISY_LINK_PATTERN.matcher(src); 75 if (matcher.matches()) { 76 long documentId = Long.parseLong(matcher.group(1)); 77 78 String branchString = matcher.group(3); 79 String languageString = matcher.group(5); 80 String versionString = matcher.group(7); 81 82 long branchId; 83 long languageId; 84 long versionId = -1; 86 if (branchString == null || branchString.length() == 0) { 87 branchId = currentDocBranchId; 88 } else { 89 if (Character.isDigit(branchString.charAt(0))) { 90 branchId = Long.parseLong(branchString); 91 } else { 92 Branch branch; 93 try { 94 branch = variantManager.getBranchByName(branchString, false); 95 } catch (RepositoryException e) { 96 throw new RuntimeException (e); 97 } 98 branchId = branch.getId(); 99 } 100 } 101 102 if (languageString == null || languageString.length() == 0) { 103 languageId = currentDocLanguageId; 104 } else { 105 if (Character.isDigit(languageString.charAt(0))) { 106 languageId = Long.parseLong(languageString); 107 } else { 108 Language language; 109 try { 110 language = variantManager.getLanguageByName(languageString, false); 111 } catch (RepositoryException e) { 112 throw new RuntimeException (e); 113 } 114 languageId = language.getId(); 115 } 116 } 117 118 if (versionString != null) { 119 if (versionString.equals("LAST")) { 120 versionId = -2; } else { 122 versionId = Long.parseLong(versionString); 123 } 124 } 125 126 try { 127 Document document = repository.getDocument(documentId, branchId, languageId, false); 128 if (versionId == -1) 129 versionId = document.getLiveVersionId(); 130 else if (versionId == -2) 131 versionId = document.getLastVersionId(); 132 133 VersionKey imageKey = new VersionKey(documentId, branchId, languageId, versionId); 134 String storePath = fetchImage(imageKey); 135 AttributesImpl newAttrs = new AttributesImpl(attributes); 136 newAttrs.addAttribute("", "bookStorePath", "bookStorePath", "CDATA", storePath); 137 attributes = newAttrs; 138 bookDependencies.addDependency(imageKey); 139 } catch (Exception e) { 140 throw new SAXException ("ImageHandler: error trying to download and store image data.", e); 141 } 142 } 143 } 144 } 145 consumer.startElement(uri, localName, qName, attributes); 146 } 147 148 private String fetchImage(VersionKey imageKey) throws Exception { 149 Publisher publisher = (Publisher)repository.getExtension("Publisher"); 150 VariantKey imageVariantKey = new VariantKey(imageKey.getDocumentId(), imageKey.getBranchId(), imageKey.getLanguageId()); 151 String storePath = BookInstanceLayout.getImageStorePath(imageKey.getDocumentId(), imageKey.getBranchId(), imageKey.getLanguageId(), imageKey.getVersionId()); 152 if (!bookInstance.exists(storePath)) { 153 BlobInfo blobInfo = publisher.getBlobInfo(imageVariantKey, String.valueOf(imageKey.getVersionId()), "ImageData"); 154 try { 155 InputStream is = blobInfo.getInputStream(); 156 bookInstance.storeResource(storePath, is); 158 159 ResourcePropertiesDocument propertiesDocument = ResourcePropertiesDocument.Factory.newInstance(); 160 propertiesDocument.addNewResourceProperties().setMimeType(blobInfo.getMimeType()); 161 bookInstance.storeResourceProperties(storePath, propertiesDocument); 162 } finally { 163 blobInfo.dispose(); 164 } 165 } 166 return storePath; 167 } 168 169 public void endElement(String uri, String localName, String qName) throws SAXException { 170 if (uri.equals(Constants.DAISY_NAMESPACE) && localName.equals("document")) { 171 currentDocumentId = -1; 172 } 173 consumer.endElement(uri, localName, qName); 174 } 175 176 public void endDocument() throws SAXException { 177 consumer.endDocument(); 178 } 179 180 public void startDocument() throws SAXException { 181 consumer.startDocument(); 182 } 183 184 public void characters(char ch[], int start, int length) throws SAXException { 185 consumer.characters(ch, start, length); 186 } 187 188 public void ignorableWhitespace(char ch[], int start, int length) throws SAXException { 189 consumer.ignorableWhitespace(ch, start, length); 190 } 191 192 public void endPrefixMapping(String prefix) throws SAXException { 193 consumer.endPrefixMapping(prefix); 194 } 195 196 public void skippedEntity(String name) throws SAXException { 197 consumer.skippedEntity(name); 198 } 199 200 public void setDocumentLocator(Locator locator) { 201 consumer.setDocumentLocator(locator); 202 } 203 204 public void processingInstruction(String target, String data) throws SAXException { 205 consumer.processingInstruction(target, data); 206 } 207 208 public void startPrefixMapping(String prefix, String uri) throws SAXException { 209 consumer.startPrefixMapping(prefix, uri); 210 } 211 } 212 | Popular Tags |