1 17 package org.alfresco.repo.importer; 18 19 import java.io.BufferedReader ; 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.io.Reader ; 25 import java.io.UnsupportedEncodingException ; 26 import java.util.Enumeration ; 27 import java.util.zip.ZipEntry ; 28 import java.util.zip.ZipFile ; 29 30 import org.alfresco.service.cmr.view.ImportPackageHandler; 31 import org.alfresco.service.cmr.view.ImporterException; 32 33 34 39 public class ACPImportPackageHandler 40 implements ImportPackageHandler 41 { 42 43 protected File file; 44 protected ZipFile zipFile; 45 protected String dataFileEncoding; 46 47 48 54 public ACPImportPackageHandler(File zipFile, String dataFileEncoding) 55 { 56 this.file = zipFile; 57 this.dataFileEncoding = dataFileEncoding; 58 } 59 60 63 public void startImport() 64 { 65 log("Importing from zip file " + file.getAbsolutePath()); 66 try 67 { 68 zipFile = new ZipFile (file); 69 } 70 catch(IOException e) 71 { 72 throw new ImporterException("Failed to read zip file due to " + e.getMessage(), e); 73 } 74 } 75 76 79 public Reader getDataStream() 80 { 81 try 82 { 83 ZipEntry xmlMetaDataEntry = null; 85 86 88 Enumeration entries = zipFile.entries(); 90 while(entries.hasMoreElements()) 91 { 92 ZipEntry entry = (ZipEntry )entries.nextElement(); 93 if (!entry.isDirectory()) 94 { 95 String 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 if (xmlMetaDataEntry == null) 110 { 111 throw new ImporterException("Failed to find xml meta-data file within .acp package"); 112 } 113 114 InputStream dataStream = zipFile.getInputStream(xmlMetaDataEntry); 116 Reader inputReader = (dataFileEncoding == null) ? new InputStreamReader (dataStream) : new InputStreamReader (dataStream, dataFileEncoding); 117 return new BufferedReader (inputReader); 118 } 119 catch(UnsupportedEncodingException e) 120 { 121 throw new ImporterException("Encoding " + dataFileEncoding + " is not supported"); 122 } 123 catch(IOException e) 124 { 125 throw new ImporterException("Failed to open xml meta-data file within .acp package due to " + e.getMessage()); 126 } 127 } 128 129 132 public InputStream importStream(String content) 133 { 134 ZipEntry zipEntry = zipFile.getEntry(content); 135 if (zipEntry == null) 136 { 137 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 e) 152 { 153 throw new ImporterException("Failed to open content " + content + " within zip package due to " + e.getMessage(), e); 154 } 155 } 156 157 160 public void endImport() 161 { 162 } 163 164 169 protected void log(String message) 170 { 171 } 172 173 } 174 175 | Popular Tags |