KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.BufferedInputStream JavaDoc;
29
30 public class CopyResourceTask implements PublicationProcessTask {
31     private final String JavaDoc fromPath;
32     private final String JavaDoc baseToPath;
33
34     public CopyResourceTask(String JavaDoc fromPath, String JavaDoc baseToPath) {
35         this.fromPath = fromPath;
36         this.baseToPath = baseToPath == null ? "" : baseToPath;
37     }
38
39     public void run(PublicationContext context) throws Exception JavaDoc {
40         context.getPublicationLog().info("Running copy resource task, copy from = " + fromPath);
41         String JavaDoc outputPath = BookInstanceLayout.getPublicationOutputPath(context.getPublicationOutputName());
42         File JavaDoc publicationTypeDir = getPublicationTypeDir(context);
43         String JavaDoc toPath = outputPath + baseToPath;
44
45         File JavaDoc fromFile = new File JavaDoc(publicationTypeDir + "/" + fromPath);
46         if (fromFile.isDirectory()) {
47             copyRecursive(fromFile, fromFile.getCanonicalPath(), toPath, context.getBookInstance());
48         } else {
49             InputStream JavaDoc is = null;
50             try {
51                 is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(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 JavaDoc dir, String JavaDoc publicationTypePath, String JavaDoc toPath, BookInstance bookInstance) throws Exception JavaDoc {
61         File JavaDoc[] files = dir.listFiles();
62         for (int i = 0; i < files.length; i++) {
63             File JavaDoc file = files[i];
64             if (file.isDirectory()) {
65                 copyRecursive(file, publicationTypePath, toPath, bookInstance);
66             } else {
67                 InputStream JavaDoc is = null;
68                 try {
69                     String JavaDoc path = toPath + "/" + file.getCanonicalPath().substring(publicationTypePath.length());
70                     is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
71                     bookInstance.storeResource(path, is);
72                 } finally {
73                     if (is != null)
74                         is.close();
75                 }
76             }
77         }
78     }
79
80     private File JavaDoc getPublicationTypeDir(PublicationContext context) throws Exception JavaDoc {
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 JavaDoc("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