From: Santi B. <san...@us...> - 2008-06-20 11:04:59
|
Update of /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv28239/modules/core/src/com/babeldoc/core/pipeline/stage Modified Files: DecompressionPipelineStage.java Log Message: Decompressor handle multiple entries in a zip file. For each entry createn a new document and enqueue for the next stage. Index: DecompressionPipelineStage.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage/DecompressionPipelineStage.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** DecompressionPipelineStage.java 4 Aug 2007 11:37:14 -0000 1.7 --- DecompressionPipelineStage.java 20 Jun 2008 11:04:53 -0000 1.8 *************** *** 67,70 **** --- 67,71 ---- import com.babeldoc.core.I18n; + import com.babeldoc.core.LogService; import com.babeldoc.core.option.ConfigOption; import com.babeldoc.core.option.IConfigInfo; *************** *** 83,214 **** import java.util.zip.ZipInputStream; - /** * This stage decompresses the data in the pipeline using a specified * compressor. ! * ! * @author bmcdonald ! * @version 1.0 */ public class DecompressionPipelineStage extends PipelineStage { ! /** Constants. */ ! public static final String COMPRESS_TYPE = "compressType"; ! /** ! * Construct with this stages config info. ! */ ! public DecompressionPipelineStage() { ! super(new PipelineStageInfo() { ! public String getName() { ! return "Decompression"; ! } ! public String getDescription() { ! return I18n.get("100270"); ! } ! public Collection getTypeSpecificOptions() { ! ArrayList options = new ArrayList(); ! //add specific options ! options.add(new ConfigOption(COMPRESS_TYPE, ! new ValueListConfigOptionType(new String[] { "gzip", "zip" }), ! null, false, I18n.get("100261"))); ! return options; ! } ! }); ! } ! /** ! * Decompress the input stream to the output stream. The type of compression ! * is configurable to either zip or gzip. ! * ! * @param out the output stream (compressed) ! * @param in the input stream (uncompressed) ! * @param compressType compression type ! * @return Arraylist with filenames ! * @throws IOException ! * @throws PipelineException ! */ ! public static ArrayList decompressData(OutputStream out, InputStream in, ! String compressType) throws IOException, PipelineException { ! //Create a buffer for reading raw bytes from the file. ! byte[] buf = new byte[1024]; ! int len; ! //Read from the file and write to the gzip archive. ! InputStream cis = null; ! ArrayList entryNames = new ArrayList(); ! if ("gzip".equalsIgnoreCase(compressType)) { ! cis = new GZIPInputStream(in); ! while ((len = cis.read(buf)) > -1) { ! out.write(buf, 0, len); ! } ! } else if ("zip".equalsIgnoreCase(compressType)) { ! cis = new ZipInputStream(in); ! ZipEntry entry = null; ! while ((entry = ((ZipInputStream) cis).getNextEntry()) != null) { ! //System.out.println(entry.getName()+":"+entry.getSize()); ! entryNames.add(entry.getName()); ! while ((len = cis.read(buf)) > -1) { ! out.write(buf, 0, len); ! } ! ((ZipInputStream) cis).closeEntry(); ! } ! cis.close(); ! } else { ! throw new PipelineException(I18n.get("100263") + ": " + compressType); ! } ! return entryNames; ! } ! /** ! * pipeline stage processing method. ! * ! * @return array of results ! * @throws PipelineException ! */ ! public PipelineStageResult[] process() throws PipelineException { ! InputStream in = getDocument().getInputStream(); ! ByteArrayOutputStream baos = new ByteArrayOutputStream(getDocument() ! .getBytes().length * 2); ! ArrayList entryNames = new ArrayList(); ! try { ! entryNames = decompressData(baos, in, this.getOptions(COMPRESS_TYPE)); ! } catch (IOException e) { ! throw new PipelineException(I18n.get("100271"), e); ! } ! byte[] data = baos.toByteArray(); ! PipelineDocument newDocument = new PipelineDocument(this.getDocument(), data); ! newDocument.setBinary(true); ! newDocument.setName(formatEntryName(entryNames)); ! return super.processHelper(newDocument); ! } ! ! /** ! * ! * Create a name of descompress file ! * ! * @param entryNames Arraylist that it contains the filenames ! * @return string with the filenames ! */ ! private String formatEntryName(ArrayList entryNames){ ! String entryName = ""; ! for (int i=0; i< entryNames.size(); i++){ ! if(i==0) entryName = entryNames.get(i).toString(); else entryName = entryName + "_" + entryNames.get(i).toString(); ! } ! return entryName; ! } } --- 84,298 ---- import java.util.zip.ZipInputStream; /** * This stage decompresses the data in the pipeline using a specified * compressor. ! * If the data is in zip compress type and contains multiple entries, each entry is ! * feeded to a new document. ! * ! * @author bmcdonald, santibegue ! * @version 1.1 */ public class DecompressionPipelineStage extends PipelineStage { ! /** Constants. */ ! public static final String COMPRESS_TYPE = "compressType"; ! /** ! * Construct with this stages config info. ! */ ! public DecompressionPipelineStage() { ! super(new PipelineStageInfo() { ! public String getName() { ! return "Decompression"; ! } ! public String getDescription() { ! return I18n.get("100270"); ! } ! public Collection getTypeSpecificOptions() { ! ArrayList options = new ArrayList(); ! // add specific options ! options.add(new ConfigOption(COMPRESS_TYPE, ! new ValueListConfigOptionType(new String[] { "gzip", ! "zip" }), null, false, I18n.get("100261"))); ! return options; ! } ! }); ! } ! /** ! * Decompress the input stream to the output stream. The type of compression ! * is configurable to either zip or gzip. ! * ! * @param out ! * the output stream (compressed) ! * @param in ! * the input stream (uncompressed) ! * @param compressType ! * compression type ! * @return Arraylist with filenames ! * @throws IOException ! * @throws PipelineException ! */ ! public static ArrayList decompressData(OutputStream out, InputStream in, ! String compressType) throws IOException, PipelineException { ! // Create a buffer for reading raw bytes from the file. ! byte[] buf = new byte[1024]; ! int len; ! // Read from the file and write to the gzip archive. ! InputStream cis = null; ! ArrayList entryNames = new ArrayList(); ! if ("gzip".equalsIgnoreCase(compressType)) { ! //TODO: test this type of compressor. ! cis = new GZIPInputStream(in); ! while ((len = cis.read(buf)) > -1) { ! out.write(buf, 0, len); ! } ! } else if ("zip".equalsIgnoreCase(compressType)) { ! cis = new ZipInputStream(in); ! ZipEntry entry = null; ! while ((entry = ((ZipInputStream) cis).getNextEntry()) != null) { ! // System.out.println(entry.getName()+":"+entry.getSize()); ! entryNames.add(entry.getName()); ! while ((len = cis.read(buf)) > -1) { ! out.write(buf, 0, len); ! } ! ((ZipInputStream) cis).closeEntry(); ! } ! cis.close(); ! } else { ! throw new PipelineException(I18n.get("100263") + ": " ! + compressType); ! } ! return entryNames; ! } ! /** ! * pipeline stage processing method. ! * ! * @return array of results ! * @throws PipelineException ! */ ! public PipelineStageResult[] process() throws PipelineException { ! InputStream in = getDocument().getInputStream(); ! ByteArrayOutputStream baos = new ByteArrayOutputStream(getDocument() ! .getBytes().length * 2); ! ArrayList entryNames = new ArrayList(); ! // Array per desar els resultats ! ArrayList results = null; ! PipelineStageResult[] psr = null; ! try { ! // entryNames = decompressData(baos, in, ! // this.getOptions(COMPRESS_TYPE)); ! results = serializeDecompressData(baos, in, this.getOptions(COMPRESS_TYPE), entryNames); ! ! LogService.getInstance().logDebug("ZIP contains " + results.size() + " documents."); ! ! if (results.size() != entryNames.size()) { ! LogService.getInstance().logWarn("Number of documents(" + results.size()+ ") in zip differs from number of names(" + entryNames.size() + ")"); ! } ! if ( results.isEmpty()) { ! return super.processHelper(); ! } else { ! psr = new PipelineStageResult[results.size()]; ! for (int i = 0; i < results.size() && i < entryNames.size(); i++) { ! ArrayList newAtribute = new ArrayList(); ! psr[i] = processHelper((PipelineDocument)results.get(i))[0]; ! psr[i].getDocument().put("entryName", entryNames.get(i)); ! } ! ! return psr; ! } ! } catch (Exception e) { ! throw new PipelineException(I18n.get("100271"), e); ! } ! ! // byte[] data = baos.toByteArray(); ! // PipelineDocument newDocument = new ! // PipelineDocument(this.getDocument(), data); ! // newDocument.setBinary(true); ! // newDocument.setName(formatEntryName(entryNames)); ! // ! // return super.processHelper(newDocument); ! } ! ! private ArrayList serializeDecompressData(ByteArrayOutputStream baos, ! InputStream in, String options, ArrayList entryNames) ! throws Exception { ! // Create a buffer for reading raw bytes from the file. ! byte[] buf = new byte[1024]; ! int len; ! ! // Read from the file and write to the gzip archive. ! InputStream cis = null; ! ! //Store results in an ArrayList, each result is an entry. ! ArrayList results = new ArrayList(); ! ! ! if ("gzip".equalsIgnoreCase(options)) { ! cis = new GZIPInputStream(in); ! ! while ((len = cis.read(buf)) > -1) { ! baos.reset(); ! baos.write(buf, 0, len); ! } ! PipelineDocument newDoc = new PipelineDocument(baos.toByteArray()); ! results.add(newDoc); ! ! } else if ("zip".equalsIgnoreCase(options)) { ! cis = new ZipInputStream(in); ! ! ZipEntry entry = null; ! ! while ((entry = ((ZipInputStream) cis).getNextEntry()) != null) { ! // System.out.println(entry.getName()+":"+entry.getSize()); ! baos.reset(); ! entryNames.add(entry.getName()); ! while ((len = cis.read(buf)) > -1) { ! baos.write(buf, 0, len); ! } ! ! ((ZipInputStream) cis).closeEntry(); ! PipelineDocument newDoc = new PipelineDocument(baos.toByteArray()); ! results.add(newDoc); ! ! } ! ! cis.close(); ! } else { ! throw new PipelineException(I18n.get("100263") + ": " + options); ! } ! return results; ! } ! ! /** ! * ! * Create a name of descompress file ! * ! * @param entryNames ! * Arraylist that it contains the filenames ! * @return string with the filenames ! */ ! private String formatEntryName(ArrayList entryNames) { ! String entryName = ""; ! for (int i = 0; i < entryNames.size(); i++) { ! if (i == 0) entryName = entryNames.get(i).toString(); else entryName = entryName + "_" + entryNames.get(i).toString(); ! } ! return entryName; ! } } |