| 1 16 package org.outerj.daisy.books.publisher.impl.dataretrieval; 17 18 import org.xml.sax.ContentHandler ; 19 import org.xml.sax.SAXException ; 20 import org.xml.sax.Attributes ; 21 import org.outerj.daisy.repository.Repository; 22 import org.outerj.daisy.repository.VariantKey; 23 import org.outerj.daisy.util.Constants; 24 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout; 25 import org.outerj.daisy.books.publisher.impl.VersionKey; 26 import org.outerj.daisy.books.publisher.impl.util.AbstractContentHandler; 27 import org.outerj.daisy.books.store.BookInstance; 28 import org.outerj.daisy.publisher.Publisher; 29 import org.outerj.daisy.publisher.BlobInfo; 30 import org.outerx.daisy.x10Bookstoremeta.ResourcePropertiesDocument; 31 import org.apache.cocoon.xml.AttributesImpl; 32 33 import java.io.InputStream ; 34 35 39 public class PartDownloadHandler extends AbstractContentHandler { 40 private long currentDocumentId = -1; 41 private long currentDocBranchId = -1; 42 private long currentDocLanguageId = -1; 43 private long currentDataVersionId; 44 private long currentDocumentTypeId; 45 private String currentDocumentTypeName; 46 private Repository repository; 47 private PartDecider partDecider; 48 private BookInstance bookInstance; 49 50 public PartDownloadHandler(PartDecider partDecider, BookInstance bookInstance, Repository repository, ContentHandler consumer) { 51 super(consumer); 52 this.repository = repository; 53 this.partDecider = partDecider; 54 this.bookInstance = bookInstance; 55 } 56 57 public void startElement(String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException { 58 if (namespaceURI.equals(Constants.DAISY_NAMESPACE)) { 59 if (localName.equals("document")) { 60 currentDocumentId = Long.parseLong(attributes.getValue("id")); 61 currentDocBranchId = Long.parseLong(attributes.getValue("branchId")); 62 currentDocLanguageId = Long.parseLong(attributes.getValue("languageId")); 63 currentDataVersionId = Long.parseLong(attributes.getValue("dataVersionId")); 64 currentDocumentTypeId = Long.parseLong(attributes.getValue("typeId")); 65 currentDocumentTypeName = attributes.getValue("typeName"); 66 } else if (localName.equals("part")) { 67 if (currentDocumentId == -1) 68 throw new SAXException ("PartDownloadHandler: encountered a part outside the context of a document."); 69 70 long partTypeId = Long.parseLong(attributes.getValue("typeId")); 71 String partTypeName = attributes.getValue("name"); 72 boolean needsPart = partDecider.needsPart(currentDocumentTypeId, currentDocumentTypeName, partTypeId, partTypeName, 73 attributes.getValue("mimeType"), attributes.getValue("fileName"), 74 Long.parseLong(attributes.getValue("size")), new VariantKey(currentDocumentId, currentDocBranchId, 75 currentDocLanguageId), currentDataVersionId, repository); 76 77 if (needsPart) { 78 String storePath; 80 try { 81 storePath = fetchResource(new VersionKey(currentDocumentId, currentDocBranchId, currentDocLanguageId, currentDataVersionId), partTypeId); 82 } catch (Exception e) { 83 throw new SAXException ("PartDownloadHandler: error trying to download and store part data.", e); 84 } 85 AttributesImpl newAttrs = new AttributesImpl(attributes); 88 newAttrs.addAttribute("", "bookStorePath", "bookStorePath", "CDATA", storePath); 89 attributes = newAttrs; 90 } 91 } 92 } 93 super.startElement(namespaceURI, localName, qName, attributes); 94 } 95 96 private String fetchResource(VersionKey resourceKey, long partTypeId) throws Exception { 97 Publisher publisher = (Publisher)repository.getExtension("Publisher"); 98 VariantKey imageVariantKey = new VariantKey(resourceKey.getDocumentId(), resourceKey.getBranchId(), resourceKey.getLanguageId()); 99 String storePath = BookInstanceLayout.getResourceStorePath(resourceKey.getDocumentId(), resourceKey.getBranchId(), resourceKey.getLanguageId(), resourceKey.getVersionId(), partTypeId); 100 if (!bookInstance.exists(storePath)) { 101 BlobInfo blobInfo = publisher.getBlobInfo(imageVariantKey, String.valueOf(resourceKey.getVersionId()), String.valueOf(partTypeId)); 102 try { 103 InputStream is = blobInfo.getInputStream(); 104 bookInstance.storeResource(storePath, is); 106 107 ResourcePropertiesDocument propertiesDocument = ResourcePropertiesDocument.Factory.newInstance(); 108 propertiesDocument.addNewResourceProperties().setMimeType(blobInfo.getMimeType()); 109 bookInstance.storeResourceProperties(storePath, propertiesDocument); 110 } finally { 111 blobInfo.dispose(); 112 } 113 } 114 return storePath; 115 } 116 } 117 | Popular Tags |