30ed2cabfd0bb7eeb78902d9bc31b6798243503a
[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.File;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import java.io.Writer;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36
37 class CodeGenWriter extends Writer {
38
39     private FileWriter fileWriter;
40     private boolean delimiterBeginFound;
41     private Path basePath;
42     private String outPath;
43     private boolean deleteFile;
44     private static final String DELIMITER = "__";
45     private Pattern pattern;
46
47
48     CodeGenWriter(String destination) throws IOException {
49         super(destination);
50         fileWriter = new FileWriter(destination);
51         basePath = Paths.get(destination);
52         delimiterBeginFound = false;
53         outPath = "";
54         deleteFile = false;
55         pattern = Pattern.compile(DELIMITER);
56     }
57
58     @Override
59     public void write(char[] cbuf, int off, int len) throws IOException {
60         String bufferStr = new String(cbuf).substring(off, off + len);
61         Matcher matcher = pattern.matcher(bufferStr);
62
63         boolean isMatch = matcher.find();
64         if (!isMatch) {
65             if (!delimiterBeginFound) {
66                 fileWriter.write(cbuf, off, len);
67             }
68             else {
69                 outPath += bufferStr;
70             }
71         }
72         else {
73             if (!delimiterBeginFound) {
74                 delimiterBeginFound = true;
75             }
76             else {
77                 deleteFile = true;
78                 Path fileName = getNewFileName();
79                 Files.createDirectories(fileName.getParent());
80                 openNewFileWriter(fileName.toString());
81                 delimiterBeginFound = false;
82                 outPath = "";
83             }
84         }
85     }
86
87     @Override
88     public void flush() throws IOException {
89         fileWriter.flush();
90     }
91
92     @Override
93     public void close() throws IOException {
94         fileWriter.close();
95         if (deleteFile) {
96             Files.deleteIfExists(basePath);
97         }
98     }
99
100     private Path getNewFileName() {
101         String newRelativePath = this.outPath.replace(".", File.separator);
102         return Paths.get(basePath.getParent().toString(), newRelativePath + ".java");
103     }
104
105     private void openNewFileWriter(String fileName) throws IOException {
106         flush();
107         close();
108         fileWriter = new FileWriter(fileName);
109     }
110 }