Changes for checkstyle 8.32
[policy/apex-pdp.git] / tools / tools-common / src / main / java / org / onap / policy / apex / tools / common / OutputFile.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.tools.common;
23
24 import java.io.BufferedWriter;
25 import java.io.File;
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;
36
37 /**
38  * Standard output file handling and tests.
39  *
40  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
41  */
42 public class OutputFile {
43     // Get a reference to the logger
44     private static final Logger LOGGER = LoggerFactory.getLogger(OutputFile.class);
45
46     /** The output file name. */
47     private final String fileName;
48
49     /** The output file name. */
50     private final boolean overwrite;
51
52     /**
53      * Creates a new object for a given file name.
54      *
55      * @param fileName the file name
56      */
57     public OutputFile(final String fileName) {
58         this(fileName, false);
59     }
60
61     /**
62      * Creates a new object for a given file name.
63      *
64      * @param fileName the file name
65      * @param overwrite if the file already exists, can it be overwritten, or should an error be raised
66      */
67     public OutputFile(final String fileName, final boolean overwrite) {
68         Validate.notBlank(fileName);
69         this.fileName = fileName;
70         this.overwrite = overwrite;
71     }
72
73     /**
74      * Get a File object for this output file.
75      *
76      * @return a File object for this output file
77      */
78     public File toFile() {
79         final Path fp = FileSystems.getDefault().getPath(fileName);
80         return fp.toFile();
81     }
82
83     /**
84      * Get a Writer object for this output file.
85      *
86      * @return a Writer object for this output file
87      */
88     public Writer toWriter() {
89         try {
90             return new BufferedWriter(new FileWriter(toFile()));
91         } catch (final IOException e) {
92             LOGGER.warn("write error", e);
93             return null;
94         }
95     }
96
97     /**
98      * Get a OutputStream object for this output file.
99      *
100      * @return an OutputStream object for this output file
101      */
102     public OutputStream toOutputStream() {
103         try {
104             return new FileOutputStream(toFile());
105         } catch (final IOException e) {
106             LOGGER.warn("stream creation error", e);
107             return null;
108         }
109     }
110
111     /**
112      * Validates the output file. Validation tests for file name being blank, file existing, creation, and finally
113      * can-write.
114      *
115      * @return null on success, an error message on error
116      */
117     public String validate() {
118         final File file = toFile();
119         if (file.exists()) {
120             if (!overwrite) {
121                 return "file already exists";
122             }
123         } else {
124             try {
125                 file.createNewFile();
126             } catch (final IOException e) {
127                 String message = "could not create output file: " + e.getMessage();
128                 LOGGER.warn(message, e);
129                 return message;
130             }
131         }
132
133         if (!file.canWrite()) {
134             return "cannot write to file";
135         }
136
137         return null;
138     }
139 }