KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > importer > ACPImportPackageHandler


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.importer;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.zip.ZipEntry JavaDoc;
28 import java.util.zip.ZipFile JavaDoc;
29
30 import org.alfresco.service.cmr.view.ImportPackageHandler;
31 import org.alfresco.service.cmr.view.ImporterException;
32
33
34 /**
35  * Handler for importing Repository content from zip package file
36  *
37  * @author David Caruana
38  */

39 public class ACPImportPackageHandler
40     implements ImportPackageHandler
41 {
42     
43     protected File JavaDoc file;
44     protected ZipFile JavaDoc zipFile;
45     protected String JavaDoc dataFileEncoding;
46     
47
48     /**
49      * Constuct Handler
50      *
51      * @param sourceDir source directory
52      * @param packageDir relative directory within source to place exported content
53      */

54     public ACPImportPackageHandler(File JavaDoc zipFile, String JavaDoc dataFileEncoding)
55     {
56         this.file = zipFile;
57         this.dataFileEncoding = dataFileEncoding;
58     }
59
60     /* (non-Javadoc)
61      * @see org.alfresco.service.cmr.view.ImportPackageHandler#startImport()
62      */

63     public void startImport()
64     {
65         log("Importing from zip file " + file.getAbsolutePath());
66         try
67         {
68             zipFile = new ZipFile JavaDoc(file);
69         }
70         catch(IOException JavaDoc e)
71         {
72             throw new ImporterException("Failed to read zip file due to " + e.getMessage(), e);
73         }
74     }
75     
76     /* (non-Javadoc)
77      * @see org.alfresco.service.cmr.view.ImportPackageHandler#getDataStream()
78      */

79     public Reader JavaDoc getDataStream()
80     {
81         try
82         {
83             // find xml meta-data file
84
ZipEntry JavaDoc xmlMetaDataEntry = null;
85             
86             // TODO: First, locate xml meta-data file by name
87

88             // Scan the zip entries one by one (the slow approach)
89
Enumeration JavaDoc entries = zipFile.entries();
90             while(entries.hasMoreElements())
91             {
92                 ZipEntry JavaDoc entry = (ZipEntry JavaDoc)entries.nextElement();
93                 if (!entry.isDirectory())
94                 {
95                     // Locate xml file in root of .acp
96
String JavaDoc entryName = entry.getName();
97                     if (entryName.endsWith(".xml") && entryName.indexOf('/') == -1 && entryName.indexOf('\\') == -1)
98                     {
99                         if (xmlMetaDataEntry != null)
100                         {
101                             throw new ImporterException("Failed to find unique xml meta-data file within .acp package - multiple xml meta-data files exist.");
102                         }
103                         xmlMetaDataEntry = entry;
104                     }
105                 }
106             }
107
108             // oh dear, there's no data file
109
if (xmlMetaDataEntry == null)
110             {
111                 throw new ImporterException("Failed to find xml meta-data file within .acp package");
112             }
113             
114             // open the meta-data xml file
115
InputStream JavaDoc dataStream = zipFile.getInputStream(xmlMetaDataEntry);
116             Reader JavaDoc inputReader = (dataFileEncoding == null) ? new InputStreamReader JavaDoc(dataStream) : new InputStreamReader JavaDoc(dataStream, dataFileEncoding);
117             return new BufferedReader JavaDoc(inputReader);
118         }
119         catch(UnsupportedEncodingException JavaDoc e)
120         {
121             throw new ImporterException("Encoding " + dataFileEncoding + " is not supported");
122         }
123         catch(IOException JavaDoc e)
124         {
125             throw new ImporterException("Failed to open xml meta-data file within .acp package due to " + e.getMessage());
126         }
127     }
128     
129     /* (non-Javadoc)
130      * @see org.alfresco.service.cmr.view.ImportStreamHandler#importStream(java.lang.String)
131      */

132     public InputStream JavaDoc importStream(String JavaDoc content)
133     {
134         ZipEntry JavaDoc zipEntry = zipFile.getEntry(content);
135         if (zipEntry == null)
136         {
137             // Note: for some reason, when modifying a zip archive the path seperator changes
138
// TODO: Need to investigate further as to why and whether this workaround is enough
139
content = content.replace('\\', '/');
140             zipEntry = zipFile.getEntry(content);
141             if (zipEntry == null)
142             {
143                 throw new ImporterException("Failed to find content " + content + " within zip package");
144             }
145         }
146         
147         try
148         {
149             return zipFile.getInputStream(zipEntry);
150         }
151         catch (IOException JavaDoc e)
152         {
153             throw new ImporterException("Failed to open content " + content + " within zip package due to " + e.getMessage(), e);
154         }
155     }
156     
157     /* (non-Javadoc)
158      * @see org.alfresco.service.cmr.view.ImportPackageHandler#endImport()
159      */

160     public void endImport()
161     {
162     }
163     
164     /**
165      * Log Import Message
166      *
167      * @param message message to log
168      */

169     protected void log(String JavaDoc message)
170     {
171     }
172     
173 }
174
175
Popular Tags