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