Search in sources :

Example 86 with Writer

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);
    }
}
Also used : Path(java.nio.file.Path) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 87 with 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);
    }
}
Also used : Liquibase(liquibase.Liquibase) FileResource(org.apache.tools.ant.types.resources.FileResource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BuildException(org.apache.tools.ant.BuildException) LiquibaseException(liquibase.exception.LiquibaseException) IOException(java.io.IOException) Contexts(liquibase.Contexts) Writer(java.io.Writer)

Example 88 with 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);
    }
}
Also used : Liquibase(liquibase.Liquibase) FileResource(org.apache.tools.ant.types.resources.FileResource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LiquibaseException(liquibase.exception.LiquibaseException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) Contexts(liquibase.Contexts) Writer(java.io.Writer)

Example 89 with 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;
}
Also used : AnnotationFieldKeySorter(org.apache.camel.component.salesforce.api.dto.AnnotationFieldKeySorter) TreeMarshallingStrategy(com.thoughtworks.xstream.core.TreeMarshallingStrategy) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) XppDriver(com.thoughtworks.xstream.io.xml.XppDriver) XStream(com.thoughtworks.xstream.XStream) FieldDictionary(com.thoughtworks.xstream.converters.reflection.FieldDictionary) DateTimeConverter(org.apache.camel.component.salesforce.api.utils.DateTimeConverter) NoNameCoder(com.thoughtworks.xstream.io.naming.NoNameCoder) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Writer(java.io.Writer) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 90 with Writer

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);
        }
    }
}
Also used : SObjectField(org.apache.camel.component.salesforce.api.dto.SObjectField) VelocityContext(org.apache.velocity.VelocityContext) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Template(org.apache.velocity.Template)

Aggregations

Writer (java.io.Writer)1410 OutputStreamWriter (java.io.OutputStreamWriter)554 IOException (java.io.IOException)474 StringWriter (java.io.StringWriter)333 File (java.io.File)306 FileOutputStream (java.io.FileOutputStream)213 BufferedWriter (java.io.BufferedWriter)202 FileWriter (java.io.FileWriter)196 PrintWriter (java.io.PrintWriter)175 OutputStream (java.io.OutputStream)139 Test (org.junit.Test)120 InputStreamReader (java.io.InputStreamReader)81 Reader (java.io.Reader)74 BufferedReader (java.io.BufferedReader)72 ByteArrayOutputStream (java.io.ByteArrayOutputStream)70 ArrayList (java.util.ArrayList)66 HashMap (java.util.HashMap)66 Map (java.util.Map)65 InputStream (java.io.InputStream)61 Properties (java.util.Properties)40