1 16 package org.outerj.daisy.books.publisher.impl.publicationprocess; 17 18 import org.apache.excalibur.source.Source; 19 import org.apache.excalibur.source.SourceResolver; 20 import org.apache.avalon.framework.service.ServiceManager; 21 import org.outerj.daisy.books.store.BookInstance; 22 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout; 23 import org.outerj.daisy.frontend.components.wikidatasource.WikiDataSource; 24 25 import java.io.File ; 26 import java.io.InputStream ; 27 import java.io.FileInputStream ; 28 import java.io.BufferedInputStream ; 29 30 public class CopyResourceTask implements PublicationProcessTask { 31 private final String fromPath; 32 private final String baseToPath; 33 34 public CopyResourceTask(String fromPath, String baseToPath) { 35 this.fromPath = fromPath; 36 this.baseToPath = baseToPath == null ? "" : baseToPath; 37 } 38 39 public void run(PublicationContext context) throws Exception { 40 context.getPublicationLog().info("Running copy resource task, copy from = " + fromPath); 41 String outputPath = BookInstanceLayout.getPublicationOutputPath(context.getPublicationOutputName()); 42 File publicationTypeDir = getPublicationTypeDir(context); 43 String toPath = outputPath + baseToPath; 44 45 File fromFile = new File (publicationTypeDir + "/" + fromPath); 46 if (fromFile.isDirectory()) { 47 copyRecursive(fromFile, fromFile.getCanonicalPath(), toPath, context.getBookInstance()); 48 } else { 49 InputStream is = null; 50 try { 51 is = new BufferedInputStream (new FileInputStream (fromFile)); 52 context.getBookInstance().storeResource(outputPath + baseToPath, is); 53 } finally { 54 if (is != null) 55 is.close(); 56 } 57 } 58 } 59 60 private void copyRecursive(File dir, String publicationTypePath, String toPath, BookInstance bookInstance) throws Exception { 61 File [] files = dir.listFiles(); 62 for (int i = 0; i < files.length; i++) { 63 File file = files[i]; 64 if (file.isDirectory()) { 65 copyRecursive(file, publicationTypePath, toPath, bookInstance); 66 } else { 67 InputStream is = null; 68 try { 69 String path = toPath + "/" + file.getCanonicalPath().substring(publicationTypePath.length()); 70 is = new BufferedInputStream (new FileInputStream (file)); 71 bookInstance.storeResource(path, is); 72 } finally { 73 if (is != null) 74 is.close(); 75 } 76 } 77 } 78 } 79 80 private File getPublicationTypeDir(PublicationContext context) throws Exception { 81 ServiceManager serviceManager = context.getServiceManager(); 82 SourceResolver sourceResolver = null; 83 Source source = null; 84 try { 85 sourceResolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE); 86 source = sourceResolver.resolveURI("wikidata:/books/publicationtypes/" + context.getPublicationTypeName()); 87 if (!(source instanceof WikiDataSource)) 88 throw new Exception ("Expected a WikiDataSource for the publication type directory."); 89 return ((WikiDataSource)source).getFile(); 90 } finally { 91 if (source == null) 92 sourceResolver.release(source); 93 if (sourceResolver == null) 94 serviceManager.release(sourceResolver); 95 } 96 } 97 } 98 | Popular Tags |