2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (c) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.tools.common;
24 import java.io.BufferedWriter;
26 import java.io.FileOutputStream;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.io.Writer;
31 import java.nio.file.FileSystems;
32 import java.nio.file.Path;
34 import org.apache.commons.lang3.StringUtils;
35 import org.apache.commons.lang3.Validate;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Standard output file handling and tests.
42 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
44 public class OutputFile {
45 // Get a reference to the logger
46 private static final Logger LOGGER = LoggerFactory.getLogger(OutputFile.class);
48 /** The output file name. */
49 private final String fileName;
51 /** The output file name. */
52 private final boolean overwrite;
55 * Creates a new object for a given file name.
57 * @param fileName the file name
59 public OutputFile(final String fileName) {
60 this(fileName, false);
64 * Creates a new object for a given file name.
66 * @param fileName the file name
67 * @param overwrite if the file already exists, can it be overwritten, or should an error be raised
69 public OutputFile(final String fileName, final boolean overwrite) {
70 Validate.notBlank(fileName);
71 this.fileName = fileName;
72 this.overwrite = overwrite;
76 * Get a File object for this output file.
78 * @return a File object for this output file
80 public File toFile() {
81 final Path fp = FileSystems.getDefault().getPath(fileName);
86 * Get a Writer object for this output file.
88 * @return a Writer object for this output file
90 public Writer toWriter() {
92 return new BufferedWriter(new FileWriter(toFile()));
93 } catch (final IOException e) {
94 LOGGER.warn("write error", e);
100 * Get a OutputStream object for this output file.
102 * @return an OutputStream object for this output file
104 public OutputStream toOutputStream() {
106 return new FileOutputStream(toFile());
107 } catch (final IOException e) {
108 LOGGER.warn("stream creation error", e);
114 * Validates the output file. Validation tests for file name being blank, file existing, creation, and finally
117 * @return null on success, an error message on error
119 public String validate() {
120 final File file = toFile();
123 return "file already exists";
127 file.createNewFile();
128 } catch (final IOException e) {
129 String message = "could not create output file: " + e.getMessage();
130 LOGGER.warn(message, e);
135 if (!file.canWrite()) {
136 return "cannot write to file";