1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 /*
24  * creates new files
25  * checks if they exist or
26  * can be overwritten
27  *
28  */
29 package com.sun.star.tooling.converter;
30 
31 import java.io.File;
32 import java.io.IOException;
33 
34 /**
35  * Creates new files only if the file  does not yet exist
36  * or overwriting is allowed
37  *
38  * @author Christian Schmidt 2005
39  *
40  */
41 public final class FileMaker {
42 
43     /**
44      * Create a new file if overwriting is not alowed
45      * ask if existing files should be overwritten
46      *
47      * @param fileName the files name to overwrite
48      * @param overwrite indicates wether the file can be overwritten
49      * @return the File created from the fileName
50      * @throws IOException
51      */
newFile(String fileName, boolean overwrite)52     public final static File newFile(String fileName, boolean overwrite)
53             throws IOException {
54         File file = new File(fileName);
55         if (file.exists()) {
56             if (!overwrite) {
57                 char c = 0;
58 
59                 System.out.print("Warning: File " + fileName
60                         + " already exist.\n" + "Overwrite (y/n) ? :");
61                 byte[] waste = new byte[10];
62                 System.in.read(waste);
63                 c = (char) waste[0];
64                 if (c == 'y') {
65                     OutputHandler.out("...overwriting " + fileName);
66                 } else {
67                     OutputHandler.out(
68                             "\nPlease set '-o' switch at command line to overwrite.\n\nProgramm Aborted.");
69                     System.exit(-1);
70                 }
71             } else {
72                 OutputHandler.out("...overwriting " + fileName);
73             }
74         } else {
75             OutputHandler.out("...creating new target file " + fileName);
76         }
77         return file;
78     }
79 
80 }