1 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 ; 25 import java.io.InputStream ; 26 27 public class GetDocumentPartTask implements PublicationProcessTask { 28 private final String propertyName; 29 private final String propertyOrigin; 30 private final String partName; 31 private final String saveAs; 32 private final String setProperty; 33 34 public GetDocumentPartTask(String propertyName, String propertyOrigin, String partName, String saveAs, String 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 { 43 String propertyValue; 44 if (propertyOrigin.equals("metadata")) { 45 propertyValue = (String )context.getBookMetadata().get(propertyName); 46 } else if (propertyOrigin.equals("publication")) { 47 propertyValue = (String )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 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 branch = matcher.group(3); 67 if (branch == null || branch.trim().length() == 0) 68 branch = "1"; 69 String 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 savePath = BookInstanceLayout.getPublicationOutputPath(context.getPublicationOutputName()) + saveAs; 81 String mimeType = version.getPart(partName).getMimeType(); 82 83 InputStream 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 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 e) { 105 context.getPublicationLog().error("GetDocumentPart: error retrieving resource " + propertyValue, e); 106 } 107 } 108 } 109 | Popular Tags |