2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.tools.generator.impl;
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;
36 class CodeGenWriter extends Writer {
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;
47 CodeGenWriter(String destination) throws IOException {
49 fileWriter = new FileWriter(destination);
50 basePath = Paths.get(destination);
51 delimiterBeginFound = false;
54 pattern = Pattern.compile(DELIMITER);
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);
62 boolean isMatch = matcher.find();
64 if (!delimiterBeginFound) {
65 fileWriter.write(cbuf, off, len);
72 if (!delimiterBeginFound) {
73 delimiterBeginFound = true;
77 Path fileName = getNewFileName();
78 Files.createDirectories(fileName.getParent());
79 openNewFileWriter(fileName.toString());
80 delimiterBeginFound = false;
87 public void flush() throws IOException {
92 public void close() throws IOException {
95 Files.deleteIfExists(basePath);
99 private Path getNewFileName() {
100 String newRelativePath = this.outPath.replace(".", File.separator);
101 return Paths.get(basePath.getParent().toString(), newRelativePath + ".java");
104 private void openNewFileWriter(String fileName) throws IOException {
107 fileWriter = new FileWriter(fileName);