KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > publicationprocess > GetDocumentPartTask


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.publicationprocess;
17
18 import org.outerj.daisy.util.Constants;
19 import org.outerj.daisy.repository.Version;
20 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout;
21 import org.outerx.daisy.x10Bookstoremeta.ResourcePropertiesDocument;
22 import org.apache.xmlbeans.XmlOptions;
23
24 import java.util.regex.Matcher JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 public class GetDocumentPartTask implements PublicationProcessTask {
28     private final String JavaDoc propertyName;
29     private final String JavaDoc propertyOrigin;
30     private final String JavaDoc partName;
31     private final String JavaDoc saveAs;
32     private final String JavaDoc setProperty;
33
34     public GetDocumentPartTask(String JavaDoc propertyName, String JavaDoc propertyOrigin, String JavaDoc partName, String JavaDoc saveAs, String JavaDoc setProperty) {
35         this.propertyName = propertyName;
36         this.propertyOrigin = propertyOrigin;
37         this.partName = partName;
38         this.saveAs = saveAs;
39         this.setProperty = setProperty;
40     }
41
42     public void run(PublicationContext context) throws Exception JavaDoc {
43         String JavaDoc propertyValue;
44         if (propertyOrigin.equals("metadata")) {
45             propertyValue = (String JavaDoc)context.getBookMetadata().get(propertyName);
46         } else if (propertyOrigin.equals("publication")) {
47             propertyValue = (String JavaDoc)context.getProperties().get(propertyName);
48         } else {
49             context.getPublicationLog().info("GetDocumentPart: property origin \"" + propertyOrigin + "\" is invalid. It should be either 'metadata' or 'publication' (without the quotes).");
50             return;
51         }
52
53
54         if (propertyValue == null || propertyValue.length() == 0) {
55             context.getPublicationLog().info("GetDocumentPart: no property named \"" + propertyName + "\" found in " + propertyOrigin);
56             return;
57         }
58
59         Matcher JavaDoc matcher = Constants.DAISY_LINK_PATTERN.matcher(propertyValue);
60         if (!matcher.matches()) {
61             context.getPublicationLog().info("GetDocumentPart: property named \"" + propertyName + "\" in " + propertyOrigin + " does not contain a daisy: link but \"" + propertyValue + "\".");
62             return;
63         }
64
65         long documentId = Long.parseLong(matcher.group(1));
66         String JavaDoc branch = matcher.group(3);
67         if (branch == null || branch.trim().length() == 0)
68             branch = "1";
69         String JavaDoc language = matcher.group(5);
70         if (language == null || language.trim().length() == 0)
71             language = "1";
72
73         try {
74             Version version = context.getRepository().getDocument(documentId, branch, language, false).getLiveVersion();
75             if (!version.hasPart(partName)) {
76                 context.getPublicationLog().info("GetDocumentPart: document " + documentId + " as refered to in property \"" + propertyName + "\" does not have a part called \"" + partName + "\".");
77                 return;
78             }
79
80             String JavaDoc savePath = BookInstanceLayout.getPublicationOutputPath(context.getPublicationOutputName()) + saveAs;
81             String JavaDoc mimeType = version.getPart(partName).getMimeType();
82
83             InputStream JavaDoc is = null;
84             try {
85                 is = version.getPart(partName).getDataStream();
86                 context.getBookInstance().storeResource(savePath, is);
87             } finally {
88                 if (is != null)
89                     is.close();
90             }
91
92             // save metadata with mime type
93
ResourcePropertiesDocument propertiesDocument = ResourcePropertiesDocument.Factory.newInstance();
94             propertiesDocument.addNewResourceProperties().setMimeType(mimeType);
95             XmlOptions xmlOptions = new XmlOptions();
96             xmlOptions.setSavePrettyPrint();
97             xmlOptions.setUseDefaultNamespace();
98             context.getBookInstance().storeResource(savePath + ".meta.xml", propertiesDocument.newInputStream());
99
100             context.getPublicationLog().info("GetDocumentPart: resource " + propertyValue + " successfully saved to " + savePath);
101
102             if (setProperty != null)
103                 context.getProperties().put(setProperty, "true");
104         } catch (Throwable JavaDoc e) {
105             context.getPublicationLog().error("GetDocumentPart: error retrieving resource " + propertyValue, e);
106         }
107     }
108 }
109
Popular Tags