
org.jooq.util.GeneratorWriter Maven / Gradle / Ivy
Show all versions of jooq-codegen Show documentation
/**
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jooq.tools.StringUtils;
/**
* A wrapper for a {@link PrintWriter}
*
* This wrapper postpones the actual write to the wrapped {@link PrintWriter}
* until all information about the target Java class is available. This way, the
* import dependencies can be calculated at the end.
*
* @author Lukas Eder
*/
public abstract class GeneratorWriter> {
/**
* A pattern to be used with "list" expressions
*/
private static final Pattern PATTERN_LIST = Pattern.compile(
"\\[" +
"(?:\\[before=([^\\]]+)\\])?" +
"(?:\\[separator=([^\\]]+)\\])?" +
"(?:\\[after=([^\\]]+)\\])?" +
"(?:\\[(.*)\\])" +
"\\]", Pattern.DOTALL);
private final File file;
private final String encoding;
private final StringBuilder sb;
private int indentTabs;
private String tabString = " ";
private boolean newline = true;
protected GeneratorWriter(File file) {
this(file, null);
}
protected GeneratorWriter(File file, String encoding) {
file.getParentFile().mkdirs();
this.file = file;
this.encoding = encoding;
this.sb = new StringBuilder();
}
public void tabString(String string) {
this.tabString = string;
}
public File file() {
return file;
}
@SuppressWarnings("unchecked")
public W print(char value) {
print("" + value);
return (W) this;
}
@SuppressWarnings("unchecked")
public W print(int value) {
print("" + value);
return (W) this;
}
@SuppressWarnings("unchecked")
public W print(String string) {
print(string, new Object[0]);
return (W) this;
}
@SuppressWarnings("unchecked")
public W print(String string, Object... args) {
string = string.replaceAll("\t", tabString);
if (newline && indentTabs > 0) {
for (int i = 0; i < indentTabs; i++)
sb.append(tabString);
newline = false;
indentTabs = 0;
}
if (args.length > 0) {
List