KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > dataretrieval > PartDownloadHandler


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.books.publisher.impl.dataretrieval;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.xml.sax.Attributes JavaDoc;
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 JavaDoc;
34
35 /**
36  * Downloads the data of certain parts and stores it in the book instance. The storage
37  * path is left as an attribute on the part element.
38  */

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 JavaDoc currentDocumentTypeName;
46     private Repository repository;
47     private PartDecider partDecider;
48     private BookInstance bookInstance;
49
50     public PartDownloadHandler(PartDecider partDecider, BookInstance bookInstance, Repository repository, ContentHandler JavaDoc consumer) {
51         super(consumer);
52         this.repository = repository;
53         this.partDecider = partDecider;
54         this.bookInstance = bookInstance;
55     }
56
57     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
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 JavaDoc("PartDownloadHandler: encountered a part outside the context of a document.");
69
70                 long partTypeId = Long.parseLong(attributes.getValue("typeId"));
71                 String JavaDoc 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                     // download the part
79
String JavaDoc storePath;
80                     try {
81                         storePath = fetchResource(new VersionKey(currentDocumentId, currentDocBranchId, currentDocLanguageId, currentDataVersionId), partTypeId);
82                     } catch (Exception JavaDoc e) {
83                         throw new SAXException JavaDoc("PartDownloadHandler: error trying to download and store part data.", e);
84                     }
85                     // Note: since this is a part within a requested document, we don't need to add a new
86
// book dependency
87
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 JavaDoc fetchResource(VersionKey resourceKey, long partTypeId) throws Exception JavaDoc {
97         Publisher publisher = (Publisher)repository.getExtension("Publisher");
98         VariantKey imageVariantKey = new VariantKey(resourceKey.getDocumentId(), resourceKey.getBranchId(), resourceKey.getLanguageId());
99         String JavaDoc 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 JavaDoc is = blobInfo.getInputStream();
104                 // InputStream gets closed by storeResource method
105
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