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.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;
34 class CodeGenWriter extends Writer {
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;
45 CodeGenWriter(String destination) throws IOException {
47 fileWriter = new FileWriter(destination);
48 basePath = Paths.get(destination);
49 delimiterBeginFound = false;
52 pattern = Pattern.compile(DELIMITER);
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);
60 boolean isMatch = matcher.find();
62 if (!delimiterBeginFound) {
63 fileWriter.write(cbuf, off, len);
70 if (!delimiterBeginFound) {
71 delimiterBeginFound = true;
75 Path fileName = getNewFileName();
76 Files.createDirectories(fileName.getParent());
77 openNewFileWriter(fileName.toString());
78 delimiterBeginFound = false;
85 public void flush() throws IOException {
90 public void close() throws IOException {
93 Files.deleteIfExists(basePath);
97 private Path getNewFileName() {
98 String newRelativePath = this.outPath.replace(".", File.separator);
99 return Paths.get(basePath.getParent().toString(), newRelativePath + ".java");
102 private void openNewFileWriter(String fileName) throws IOException {
105 fileWriter = new FileWriter(fileName);