2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.tools.generator.impl;
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;
37 class CodeGenWriter extends Writer {
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;
48 CodeGenWriter(String destination) throws IOException {
50 fileWriter = new FileWriter(destination);
51 basePath = Paths.get(destination);
52 delimiterBeginFound = false;
55 pattern = Pattern.compile(DELIMITER);
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);
63 boolean isMatch = matcher.find();
65 if (!delimiterBeginFound) {
66 fileWriter.write(cbuf, off, len);
73 if (!delimiterBeginFound) {
74 delimiterBeginFound = true;
78 Path fileName = getNewFileName();
79 Files.createDirectories(fileName.getParent());
80 openNewFileWriter(fileName.toString());
81 delimiterBeginFound = false;
88 public void flush() throws IOException {
93 public void close() throws IOException {
96 Files.deleteIfExists(basePath);
100 private Path getNewFileName() {
101 String newRelativePath = this.outPath.replace(".", File.separator);
102 return Paths.get(basePath.getParent().toString(), newRelativePath + ".java");
105 private void openNewFileWriter(String fileName) throws IOException {
108 fileWriter = new FileWriter(fileName);