001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.books.publisher.impl.publicationprocess;
017:
018: import org.outerj.daisy.util.Constants;
019: import org.outerj.daisy.repository.Version;
020: import org.outerj.daisy.books.publisher.impl.BookInstanceLayout;
021: import org.outerx.daisy.x10Bookstoremeta.ResourcePropertiesDocument;
022: import org.apache.xmlbeans.XmlOptions;
023:
024: import java.util.regex.Matcher;
025: import java.io.InputStream;
026:
027: public class GetDocumentPartTask implements PublicationProcessTask {
028: private final String propertyName;
029: private final String propertyOrigin;
030: private final String partName;
031: private final String saveAs;
032: private final String setProperty;
033:
034: public GetDocumentPartTask(String propertyName,
035: String propertyOrigin, String partName, String saveAs,
036: String setProperty) {
037: this .propertyName = propertyName;
038: this .propertyOrigin = propertyOrigin;
039: this .partName = partName;
040: this .saveAs = saveAs;
041: this .setProperty = setProperty;
042: }
043:
044: public void run(PublicationContext context) throws Exception {
045: String propertyValue;
046: if (propertyOrigin.equals("metadata")) {
047: propertyValue = context.getBookMetadata().get(propertyName);
048: } else if (propertyOrigin.equals("publication")) {
049: propertyValue = context.getProperties().get(propertyName);
050: } else {
051: context
052: .getPublicationLog()
053: .info(
054: "GetDocumentPart: property origin \""
055: + propertyOrigin
056: + "\" is invalid. It should be either 'metadata' or 'publication' (without the quotes).");
057: return;
058: }
059:
060: if (propertyValue == null || propertyValue.length() == 0) {
061: context.getPublicationLog().info(
062: "GetDocumentPart: no property named \""
063: + propertyName + "\" found in "
064: + propertyOrigin);
065: return;
066: }
067:
068: Matcher matcher = Constants.DAISY_LINK_PATTERN
069: .matcher(propertyValue);
070: if (!matcher.matches()) {
071: context.getPublicationLog().info(
072: "GetDocumentPart: property named \"" + propertyName
073: + "\" in " + propertyOrigin
074: + " does not contain a daisy: link but \""
075: + propertyValue + "\".");
076: return;
077: }
078:
079: String documentId = matcher.group(1);
080: String branch = matcher.group(2);
081: if (branch == null || branch.trim().length() == 0)
082: branch = "1";
083: String language = matcher.group(3);
084: if (language == null || language.trim().length() == 0)
085: language = "1";
086:
087: try {
088: Version version = context.getRepository().getDocument(
089: documentId, branch, language, false)
090: .getLiveVersion();
091: if (!version.hasPart(partName)) {
092: context.getPublicationLog().info(
093: "GetDocumentPart: document " + documentId
094: + " as refered to in property \""
095: + propertyName
096: + "\" does not have a part called \""
097: + partName + "\".");
098: return;
099: }
100:
101: String savePath = BookInstanceLayout
102: .getPublicationOutputPath(context
103: .getPublicationOutputName())
104: + saveAs;
105: String mimeType = version.getPart(partName).getMimeType();
106:
107: InputStream is = null;
108: try {
109: is = version.getPart(partName).getDataStream();
110: context.getBookInstance().storeResource(savePath, is);
111: } finally {
112: if (is != null)
113: is.close();
114: }
115:
116: // save metadata with mime type
117: ResourcePropertiesDocument propertiesDocument = ResourcePropertiesDocument.Factory
118: .newInstance();
119: propertiesDocument.addNewResourceProperties().setMimeType(
120: mimeType);
121: XmlOptions xmlOptions = new XmlOptions();
122: xmlOptions.setSavePrettyPrint();
123: xmlOptions.setUseDefaultNamespace();
124: context.getBookInstance().storeResource(
125: savePath + ".meta.xml",
126: propertiesDocument.newInputStream());
127:
128: context.getPublicationLog().info(
129: "GetDocumentPart: resource " + propertyValue
130: + " successfully saved to " + savePath);
131:
132: if (setProperty != null)
133: context.getProperties().put(setProperty, "true");
134: } catch (Throwable e) {
135: context.getPublicationLog().error(
136: "GetDocumentPart: error retrieving resource "
137: + propertyValue, e);
138: }
139: }
140: }
|