| /** |
| * ******************************************************************************* |
| * Copyright (c) 2019-2021 Robert Bosch GmbH. |
| * |
| * This program and the accompanying materials are made |
| * available under the terms of the Eclipse Public License 2.0 |
| * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Robert Bosch GmbH - initial API and implementation |
| * ******************************************************************************* |
| */ |
| |
| package org.eclipse.app4mc.transformation.util; |
| |
| import java.io.BufferedWriter; |
| import java.io.File; |
| import java.io.FileWriter; |
| import java.io.IOException; |
| import java.util.HashMap; |
| import java.util.Map.Entry; |
| |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import com.google.inject.Singleton; |
| |
| @Singleton |
| public class OutputBuffer { |
| |
| private static final Logger LOG = LoggerFactory.getLogger(OutputBuffer.class); |
| |
| private final HashMap<String, TypedBuffer> filetypeToBufferMap = new HashMap<>(); |
| |
| private String outputFolder; |
| |
| public void initialize(String outputFolder) { |
| this.outputFolder = outputFolder; |
| } |
| |
| public void configureFiletype(String type, String fileExtension, String header, String footer) { |
| if (type == null || type.isEmpty()) |
| throw new IllegalArgumentException("Illegal type string"); |
| |
| if (filetypeToBufferMap.containsKey(type)) |
| throw new IllegalArgumentException("Type is already configured"); |
| |
| filetypeToBufferMap.put(type, new TypedBuffer(fileExtension, header, footer)); |
| } |
| |
| /** |
| * @return: true if new file handle has been created |
| */ |
| public boolean appendTo(String type, String targetFile, String content) { |
| if (! filetypeToBufferMap.containsKey(type)) |
| throw new IllegalArgumentException("Unknown type"); |
| |
| if (targetFile == null || targetFile.isEmpty()) |
| throw new IllegalArgumentException("Undefined filename"); |
| |
| |
| return filetypeToBufferMap.get(type).appendTo(targetFile, content); |
| } |
| |
| public void finish() { |
| finish(true); |
| } |
| |
| public void finish(boolean createZip) { |
| |
| // Write files to disk |
| for (TypedBuffer typedBuffer : filetypeToBufferMap.values()) { |
| |
| for (Entry<String, StringBuilder> entry : typedBuffer.filenameToBufferMap.entrySet()) { |
| String targetFile = entry.getKey(); |
| StringBuilder buffer = entry.getValue(); |
| |
| File file = getFile(outputFolder, targetFile + typedBuffer.fileExtension); |
| try (FileWriter fw = new FileWriter(file); BufferedWriter writer = new BufferedWriter(fw);) { |
| writer.append(typedBuffer.header); |
| writer.append(buffer); |
| writer.append(typedBuffer.footer); |
| writer.flush(); |
| } catch (IOException e) { |
| LOG.error("Failed to write result file: {}", targetFile); |
| } |
| } |
| } |
| |
| if (createZip) { |
| // Create Zip archive |
| try { |
| FileHelper.zipResult(outputFolder); |
| } catch (Exception e) { |
| LOG.error("Failed to produce result zip archive: ", e); |
| } |
| } |
| } |
| |
| public String getOutputFolder() { |
| return outputFolder; |
| } |
| |
| /** |
| * @return: true if buffer has already been created |
| */ |
| public boolean bufferExists(String type, String targetFile) { |
| TypedBuffer typedBuffer = filetypeToBufferMap.get(type); |
| |
| return typedBuffer != null && typedBuffer.filenameToBufferMap.containsKey(targetFile); |
| } |
| |
| /** |
| * This method will get the File object by creating all the parent directories |
| * of the file |
| */ |
| private File getFile(final String outputFolder, final String targetFile) { |
| final File file = new File(outputFolder, targetFile); |
| final File parentFile = file.getParentFile(); |
| |
| if ( ! parentFile.exists()) |
| parentFile.mkdirs(); |
| |
| return file; |
| } |
| |
| private class TypedBuffer { |
| private final HashMap<String, StringBuilder> filenameToBufferMap = new HashMap<>(); |
| |
| private String fileExtension = ""; |
| private String header = ""; |
| private String footer = ""; |
| |
| public TypedBuffer(String fileExtension, String header, String footer) { |
| super(); |
| if (fileExtension != null) { |
| this.fileExtension = fileExtension; |
| } |
| if (header != null) { |
| this.header = header; |
| } |
| if (footer != null) { |
| this.footer = footer; |
| } |
| } |
| |
| /** |
| * @return: true if new file buffer has been created |
| */ |
| public boolean appendTo(String targetFile, String content) { |
| boolean isNewBuffer = false; |
| if (!filenameToBufferMap.containsKey(targetFile)) { |
| filenameToBufferMap.put(targetFile, new StringBuilder()); |
| isNewBuffer = true; |
| } |
| |
| filenameToBufferMap.get(targetFile).append(content); |
| |
| return isNewBuffer; |
| } |
| |
| } |
| |
| } |