- if (!fileOption.getReplaceEdition()) {
- return new FileInputStream(file);
- }
- ByteArrayOutputStream filteredByteOutputStream = new ByteArrayOutputStream(Math.min(Integer.MAX_VALUE, (int) length[0]));
- ReplacingOutputStream outputStream = new ReplacingOutputStream(filteredByteOutputStream);
- FileInputStream fileInput = new FileInputStream(file);
- outputStream.addReplacement("$[CONTAINER]", "/");
- outputStream.addReplacement("$[EDITION]", String.valueOf(edition));
- outputStream.addReplacement("$[URI]", project.getFinalURI(0));
- for (int index = 1; index <= fileOption.getEditionRange(); index++) {
- outputStream.addReplacement("$[URI+" + index + "]", project.getFinalURI(index));
- outputStream.addReplacement("$[URI+" + index + "]", project.getFinalURI(index));
- }
- StreamCopier.copy(fileInput, outputStream, length[0]);
- outputStream.close();
- filteredByteOutputStream.close();
- byte[] filteredBytes = filteredByteOutputStream.toByteArray();
- length[0] = filteredBytes.length;
- return new ByteArrayInputStream(filteredBytes);
- }
-
- private InputStream createContainerInputStream(Map<String, List<String>> containerFiles, String containerName, int edition, long[] containerLength) throws IOException {
- File tempFile = File.createTempFile("jsite", ".zip");
- tempFile.deleteOnExit();
- FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
- ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
- for (String filename: containerFiles.get(containerName)) {
- File dataFile = new File(project.getLocalPath(), filename);
- if (dataFile.exists()) {
- ZipEntry zipEntry = new ZipEntry(filename);
- long[] fileLength = new long[1];
- InputStream wrappedInputStream = createFileInputStream(filename, project.getFileOption(filename), edition, fileLength);
- zipOutputStream.putNextEntry(zipEntry);
- StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
- zipOutputStream.closeEntry();
- wrappedInputStream.close();
- }
- }
- zipOutputStream.closeEntry();
-
- /* FIXME - create metadata */
- // ZipEntry metadataEntry = new ZipEntry("metadata");
- // zipOutputStream.putNextEntry(metadataEntry);
- // Metadata zipMetadata = new Metadata();
- // for (String filename: containerFiles.get(containerName)) {
- // if (new File(project.getLocalPath(), filename).exists()) {
- // DocumentMetadata zipEntryMetadata = new DocumentMetadata();
- // zipEntryMetadata.setName(filename);
- // zipEntryMetadata.setFormat(project.getFileOption(filename).getMimeType());
- // zipMetadata.addDocument(zipEntryMetadata);
- // }
- // }
- // zipOutputStream.write(zipMetadata.toByteArray());
- // zipOutputStream.closeEntry();
- zipOutputStream.close();
-
- containerLength[0] = tempFile.length();
- return new FileInputStream(tempFile);