use of java.io.Writer in project javapoet by square.
the class JavaFile method writeTo.
/** Writes this to {@code directory} as UTF-8 using the standard directory structure. */
public void writeTo(Path directory) throws IOException {
checkArgument(Files.notExists(directory) || Files.isDirectory(directory), "path %s exists but is not a directory.", directory);
Path outputDirectory = directory;
if (!packageName.isEmpty()) {
for (String packageComponent : packageName.split("\\.")) {
outputDirectory = outputDirectory.resolve(packageComponent);
}
Files.createDirectories(outputDirectory);
}
Path outputPath = outputDirectory.resolve(typeSpec.name + ".java");
try (Writer writer = new OutputStreamWriter(Files.newOutputStream(outputPath), UTF_8)) {
writeTo(writer);
}
}
use of java.io.Writer in project liquibase by liquibase.
the class DatabaseRollbackTask method executeWithLiquibaseClassloader.
@Override
public void executeWithLiquibaseClassloader() throws BuildException {
Writer writer = null;
Liquibase liquibase = getLiquibase();
try {
FileResource outputFile = getOutputFile();
if (rollbackCount != null) {
if (outputFile != null) {
writer = getOutputFileWriter();
liquibase.rollback(rollbackCount, rollbackScript, new Contexts(getContexts()), getLabels(), writer);
} else {
liquibase.rollback(rollbackCount, rollbackScript, new Contexts(getContexts()), getLabels());
}
} else if (rollbackTag != null) {
if (outputFile != null) {
writer = getOutputFileWriter();
liquibase.rollback(rollbackTag, rollbackScript, new Contexts(getContexts()), getLabels(), writer);
} else {
liquibase.rollback(rollbackTag, rollbackScript, new Contexts(getContexts()), getLabels());
}
} else if (rollbackDate != null) {
if (outputFile != null) {
writer = getOutputFileWriter();
liquibase.rollback(rollbackDate, rollbackScript, new Contexts(getContexts()), getLabels(), writer);
} else {
liquibase.rollback(rollbackDate, rollbackScript, new Contexts(getContexts()), getLabels());
}
} else {
throw new BuildException("Unable to rollback database. No count, tag, or date set.");
}
} catch (LiquibaseException e) {
throw new BuildException("Unable to rollback database. " + e.toString(), e);
} catch (UnsupportedEncodingException e) {
throw new BuildException("Unable to generate rollback SQL. Encoding [" + getOutputEncoding() + "] is not supported.", e);
} catch (IOException e) {
throw new BuildException("Unable to generate rollback SQL. Error creating output writer.", e);
} finally {
FileUtils.close(writer);
}
}
use of java.io.Writer in project liquibase by liquibase.
the class DatabaseUpdateTask method executeWithLiquibaseClassloader.
@Override
public void executeWithLiquibaseClassloader() throws BuildException {
Writer writer = null;
Liquibase liquibase = getLiquibase();
try {
FileResource outputFile = getOutputFile();
if (outputFile != null) {
writer = getOutputFileWriter();
liquibase.update(toTag, new Contexts(getContexts()), getLabels(), writer);
} else {
if (dropFirst) {
liquibase.dropAll();
}
liquibase.update(toTag, new Contexts(getContexts()), getLabels());
}
} catch (LiquibaseException e) {
throw new BuildException("Unable to update database. " + e.toString(), e);
} catch (UnsupportedEncodingException e) {
throw new BuildException("Unable to generate update SQL. Encoding [" + getOutputEncoding() + "] is not supported.", e);
} catch (IOException e) {
throw new BuildException("Unable to generate update SQL. Error creating output writer.", e);
} finally {
FileUtils.close(writer);
}
}
use of java.io.Writer in project camel by apache.
the class DefaultCompositeApiClient method configureXStream.
static XStream configureXStream() {
final PureJavaReflectionProvider reflectionProvider = new PureJavaReflectionProvider(new FieldDictionary(new AnnotationFieldKeySorter()));
final XppDriver hierarchicalStreamDriver = new XppDriver(new NoNameCoder()) {
@Override
public HierarchicalStreamWriter createWriter(final Writer out) {
return new CompactWriter(out, getNameCoder());
}
};
final XStream xStream = new XStream(reflectionProvider, hierarchicalStreamDriver);
xStream.aliasSystemAttribute(null, "class");
xStream.ignoreUnknownElements();
XStreamUtils.addDefaultPermissions(xStream);
xStream.registerConverter(new DateTimeConverter());
xStream.setMarshallingStrategy(new TreeMarshallingStrategy());
xStream.processAnnotations(ADDITIONAL_TYPES);
return xStream;
}
use of java.io.Writer in project camel by apache.
the class CamelSalesforceMojo method processDescription.
void processDescription(File pkgDir, SObjectDescription description, GeneratorUtility utility, String generatedDate) throws IOException {
// generate a source file for SObject
final VelocityContext context = new VelocityContext();
context.put("packageName", packageName);
context.put("utility", utility);
context.put("esc", StringEscapeUtils.class);
context.put("desc", description);
context.put("generatedDate", generatedDate);
context.put("useStringsForPicklists", useStringsForPicklists);
final String pojoFileName = description.getName() + JAVA_EXT;
final File pojoFile = new File(pkgDir, pojoFileName);
try (final Writer writer = new OutputStreamWriter(new FileOutputStream(pojoFile), StandardCharsets.UTF_8)) {
final Template pojoTemplate = engine.getTemplate(SOBJECT_POJO_VM, UTF_8);
pojoTemplate.merge(context, writer);
}
if (useOptionals) {
final String optionalFileName = description.getName() + "Optional" + JAVA_EXT;
final File optionalFile = new File(pkgDir, optionalFileName);
try (final Writer writer = new OutputStreamWriter(new FileOutputStream(optionalFile), StandardCharsets.UTF_8)) {
final Template optionalTemplate = engine.getTemplate(SOBJECT_POJO_OPTIONAL_VM, UTF_8);
optionalTemplate.merge(context, writer);
}
}
// write required Enumerations for any picklists
for (SObjectField field : description.getFields()) {
if (utility.isPicklist(field) || utility.isMultiSelectPicklist(field)) {
final String enumName = description.getName() + "_" + utility.enumTypeName(field.getName());
final String enumFileName = enumName + JAVA_EXT;
final File enumFile = new File(pkgDir, enumFileName);
context.put("field", field);
context.put("enumName", enumName);
final Template enumTemplate = engine.getTemplate(SOBJECT_PICKLIST_VM, UTF_8);
try (final Writer writer = new OutputStreamWriter(new FileOutputStream(enumFile), StandardCharsets.UTF_8)) {
enumTemplate.merge(context, writer);
}
}
}
// write the QueryRecords class
final String queryRecordsFileName = "QueryRecords" + description.getName() + JAVA_EXT;
final File queryRecordsFile = new File(pkgDir, queryRecordsFileName);
final Template queryTemplate = engine.getTemplate(SOBJECT_QUERY_RECORDS_VM, UTF_8);
try (final Writer writer = new OutputStreamWriter(new FileOutputStream(queryRecordsFile), StandardCharsets.UTF_8)) {
queryTemplate.merge(context, writer);
}
if (useOptionals) {
// write the QueryRecords Optional class
final String queryRecordsOptionalFileName = "QueryRecords" + description.getName() + "Optional" + JAVA_EXT;
final File queryRecordsOptionalFile = new File(pkgDir, queryRecordsOptionalFileName);
final Template queryRecordsOptionalTemplate = engine.getTemplate(SOBJECT_QUERY_RECORDS_OPTIONAL_VM, UTF_8);
try (final Writer writer = new OutputStreamWriter(new FileOutputStream(queryRecordsOptionalFile), StandardCharsets.UTF_8)) {
queryRecordsOptionalTemplate.merge(context, writer);
}
}
}
Aggregations