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;
33 import org.apache.commons.lang3.Validate;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Standard output file handling and tests.
40 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
42 public class OutputFile {
43 // Get a reference to the logger
44 private static final Logger LOGGER = LoggerFactory.getLogger(OutputFile.class);
46 /** The output file name. */
47 private final String fileName;
49 /** The output file name. */
50 private final boolean overwrite;
53 * Creates a new object for a given file name.
55 * @param fileName the file name
57 public OutputFile(final String fileName) {
58 this(fileName, false);
62 * Creates a new object for a given file name.
64 * @param fileName the file name
65 * @param overwrite if the file already exists, can it be overwritten, or should an error be raised
67 public OutputFile(final String fileName, final boolean overwrite) {
68 Validate.notBlank(fileName);
69 this.fileName = fileName;
70 this.overwrite = overwrite;
74 * Get a File object for this output file.
76 * @return a File object for this output file
78 public File toFile() {
79 final Path fp = FileSystems.getDefault().getPath(fileName);
84 * Get a Writer object for this output file.
86 * @return a Writer object for this output file
88 public Writer toWriter() {
90 return new BufferedWriter(new FileWriter(toFile()));
91 } catch (final IOException e) {
92 LOGGER.warn("write error", e);
98 * Get a OutputStream object for this output file.
100 * @return an OutputStream object for this output file
102 public OutputStream toOutputStream() {
104 return new FileOutputStream(toFile());
105 } catch (final IOException e) {
106 LOGGER.warn("stream creation error", e);
112 * Validates the output file. Validation tests for file name being blank, file existing, creation, and finally
115 * @return null on success, an error message on error
117 public String validate() {
118 final File file = toFile();
121 return "file already exists";
125 file.createNewFile();
126 } catch (final IOException e) {
127 String message = "could not create output file: " + e.getMessage();
128 LOGGER.warn(message, e);
133 if (!file.canWrite()) {
134 return "cannot write to file";