Second part of onap rename
[appc.git] / appc-client / code-generator / src / main / java / org / onap / appc / tools / generator / impl / CodeGenWriter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.tools.generator.impl;
26
27 import java.io.*;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 class CodeGenWriter extends Writer {
35
36     private FileWriter fileWriter;
37     private boolean delimiterBeginFound;
38     private Path basePath;
39     private String outPath;
40     private boolean deleteFile;
41     private static final String DELIMITER = "__";
42     private Pattern pattern;
43
44
45     CodeGenWriter(String destination) throws IOException {
46         super(destination);
47         fileWriter = new FileWriter(destination);
48         basePath = Paths.get(destination);
49         delimiterBeginFound = false;
50         outPath = "";
51         deleteFile = false;
52         pattern = Pattern.compile(DELIMITER);
53     }
54
55     @Override
56     public void write(char[] cbuf, int off, int len) throws IOException {
57         String bufferStr = new String(cbuf).substring(off, off + len);
58         Matcher matcher = pattern.matcher(bufferStr);
59
60         boolean isMatch = matcher.find();
61         if (!isMatch) {
62             if (!delimiterBeginFound) {
63                 fileWriter.write(cbuf, off, len);
64             }
65             else {
66                 outPath += bufferStr;
67             }
68         }
69         else {
70             if (!delimiterBeginFound) {
71                 delimiterBeginFound = true;
72             }
73             else {
74                 deleteFile = true;
75                 Path fileName = getNewFileName();
76                 Files.createDirectories(fileName.getParent());
77                 openNewFileWriter(fileName.toString());
78                 delimiterBeginFound = false;
79                 outPath = "";
80             }
81         }
82     }
83
84     @Override
85     public void flush() throws IOException {
86         fileWriter.flush();
87     }
88
89     @Override
90     public void close() throws IOException {
91         fileWriter.close();
92         if (deleteFile) {
93             Files.deleteIfExists(basePath);
94         }
95     }
96
97     private Path getNewFileName() {
98         String newRelativePath = this.outPath.replace(".", File.separator);
99         return Paths.get(basePath.getParent().toString(), newRelativePath + ".java");
100     }
101
102     private void openNewFileWriter(String fileName) throws IOException {
103         flush();
104         close();
105         fileWriter = new FileWriter(fileName);
106     }
107 }