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