Example usage for java.io Writer flush

List of usage examples for java.io Writer flush

Introduction

In this page you can find the example usage for java.io Writer flush.

Prototype

public abstract void flush() throws IOException;

Source Link

Document

Flushes the stream.

Usage

From source file:com.netflix.genie.web.jobs.workflow.impl.JobKickoffTask.java

/**
 * {@inheritDoc}/*ww  w . j  a  v a  2s .com*/
 */
@Override
public void executeTask(@NotNull final Map<String, Object> context) throws GenieException, IOException {
    final long start = System.nanoTime();
    final Set<Tag> tags = Sets.newHashSet();
    try {
        final JobExecutionEnvironment jobExecEnv = (JobExecutionEnvironment) context
                .get(JobConstants.JOB_EXECUTION_ENV_KEY);
        final String jobWorkingDirectory = jobExecEnv.getJobWorkingDir().getCanonicalPath();
        final JobRequest jobRequest = jobExecEnv.getJobRequest();
        final String user = jobRequest.getUser();
        final Writer writer = (Writer) context.get(JobConstants.WRITER_KEY);
        final String jobId = jobRequest.getId()
                .orElseThrow(() -> new GeniePreconditionException("No job id found. Unable to continue."));
        log.info("Starting Job Kickoff Task for job {}", jobId);

        // At this point all contents are written to the run script and we call an explicit flush and close to write
        // the contents to the file before we execute it.
        try {
            writer.flush();
            writer.close();
        } catch (IOException e) {
            throw new GenieServerException("Failed to execute job", e);
        }
        // Create user, if enabled
        if (isUserCreationEnabled) {
            createUser(user, jobRequest.getGroup().orElse(null));
        }
        final List<String> command = new ArrayList<>();

        // If the OS is linux use setsid to launch the process so that the entire process tree
        // is launched in process group id which is the same as the pid of the parent process
        if (SystemUtils.IS_OS_LINUX) {
            command.add("setsid");
        }

        // Set the ownership to the user and run as the user, if enabled
        if (isRunAsUserEnabled) {
            changeOwnershipOfDirectory(jobWorkingDirectory, user);

            // This is needed because the genie.log file is still generated as the user running Genie system.
            makeDirGroupWritable(jobWorkingDirectory + "/genie/logs");
            command.add("sudo");
            command.add("-u");
            command.add(user);
        }

        final String runScript = jobWorkingDirectory + JobConstants.FILE_PATH_DELIMITER
                + JobConstants.GENIE_JOB_LAUNCHER_SCRIPT;
        command.add(runScript);

        // Cannot convert to executor because it does not provide an api to get process id.
        final ProcessBuilder pb = new ProcessBuilder(command).directory(jobExecEnv.getJobWorkingDir())
                .redirectOutput(new File(jobExecEnv.getJobWorkingDir() + JobConstants.GENIE_LOG_PATH))
                .redirectError(new File(jobExecEnv.getJobWorkingDir() + JobConstants.GENIE_LOG_PATH));

        //
        // Check if file can be executed. This is to fix issue where execution of the run script fails because
        // the file may be used by some other program
        //
        canExecute(runScript);
        try {
            final Process process = pb.start();
            final int processId = this.getProcessId(process);
            final Instant timeout = Instant.now().plus(
                    jobRequest.getTimeout().orElse(JobRequest.DEFAULT_TIMEOUT_DURATION), ChronoUnit.SECONDS);
            final JobExecution jobExecution = new JobExecution.Builder(this.hostname).withId(jobId)
                    .withProcessId(processId).withCheckDelay(jobExecEnv.getCommand().getCheckDelay())
                    .withTimeout(timeout).withMemory(jobExecEnv.getMemory()).build();
            context.put(JobConstants.JOB_EXECUTION_DTO_KEY, jobExecution);
        } catch (final IOException ie) {
            throw new GenieServerException("Unable to start command " + String.valueOf(command), ie);
        }
        log.info("Finished Job Kickoff Task for job {}", jobId);
        MetricsUtils.addSuccessTags(tags);
    } catch (final Throwable t) {
        MetricsUtils.addFailureTagsWithException(tags, t);
        throw t;
    } finally {
        this.getRegistry().timer(JOB_KICKOFF_TASK_TIMER_NAME, tags).record(System.nanoTime() - start,
                TimeUnit.NANOSECONDS);
    }
}

From source file:edu.ku.brc.specify.utilapps.SiteGen.java

/**
 * @param dataObj/*from  w w w  .j a va 2  s.  c  o  m*/
 */
protected void processDataObj(final FormDataObjIFace dataObj, final boolean doChildrenSets) {
    //DBTableInfo tblInfo = DBTableIdMgr.getInstance().getByClassName(dataObj.getDataClass().getName());

    StringBuilder sb = new StringBuilder();
    try {
        //sb.append("<html><head><title>"+dataObj.getIdentityTitle()+"</head><body>");
        sb.append("<table>\n");
        for (Field field : dataObj.getClass().getDeclaredFields()) {
            if (field.getName().endsWith("Id")) {
                continue;
            }

            /*String labelName = field.getName();
            DBFieldInfo fi = tblInfo.getFieldByName(field.getName());
            if (fi != null)
            {
            fi.getColumn()
            }*/
            try {
                Object data = getData(field, dataObj);
                if (data != null) {
                    if (!(data instanceof Set<?>)) {

                        sb.append("<tr><td align=\"right\">" + UIHelper.makeNamePretty(field.getName())
                                + ":</td><td nowrap=\"true\">" + formatValue(data) + "</td></tr>\n");
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        sb.append("</table>\n");

        if (doChildrenSets) {
            for (Field field : dataObj.getClass().getDeclaredFields()) {
                try {
                    Object data = getData(field, dataObj);
                    if (data != null) {
                        if (data instanceof Set<?>) {
                            Set<?> set = (Set<?>) data;
                            if (set.size() > 0) {
                                sb.append("<br/>" + UIHelper.makeNamePretty(field.getName()) + "<br/>\n");
                                sb.append("<table width=\"100%\" cellspacing=\"0\" class=\"brdr\">\n");
                                int cnt = 1;
                                for (Object setDataObj : set) {
                                    String fdiStr = formatFDI((FormDataObjIFace) setDataObj);
                                    sb.append("<tr><td class=\"brdr" + ((cnt % 2 == 0) ? "even" : "odd") + "\">"
                                            + fdiStr + "</td></tr>\n");
                                    cnt++;
                                }
                                sb.append("</table>\n");
                            }
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        Writer output = createWriter(dataObj);
        String content = template;
        content = StringUtils.replace(content, "<!-- Title -->", dataObj.getIdentityTitle());
        content = StringUtils.replace(content, "<!-- Content -->", sb.toString());
        output.write(content);
        output.flush();
        output.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    log.info("Done " + makeFileName(dataObj));
    /*
    for (Method method : dataObj.getClass().getMethods())
    {
    if (method.getName().startsWith("get"))
    {
        try
        {
            Object data = method.invoke(dataObj, new Object[]{});
            if (data)
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    }*/

}

From source file:com.consol.citrus.report.HtmlReporter.java

/**
 * Creates the HTML report file//from  w  w w .j av  a2s.c om
 * @param content The String content of the report file
 */
private void createReportFile(String content) {
    Writer fileWriter = null;

    File targetDirectory = new File(OUTPUT_DIRECTORY);
    if (!targetDirectory.exists()) {
        boolean success = targetDirectory.mkdirs();

        if (!success) {
            throw new CitrusRuntimeException("Unable to create folder structure for HTML report");
        }
    }

    try {
        fileWriter = new FileWriter(OUTPUT_DIRECTORY + File.separator + REPORT_FILE_NAME);
        fileWriter.append(content);
        fileWriter.flush();
    } catch (IOException e) {
        log.error("Failed to save HTML test report", e);
    } finally {
        if (fileWriter != null) {
            try {
                fileWriter.close();
            } catch (IOException e) {
                log.error("Error closing HTML report file", e);
            }
        }
    }
}

From source file:com.serphacker.serposcope.db.base.ExportDB.java

public boolean export(Writer writer) throws IOException {
    for (String resource : MigrationDB.DB_SCHEMA_FILES) {
        String sql = new String(ByteStreams.toByteArray(MigrationDB.class.getResourceAsStream(resource)));
        sql = sql.replaceAll("--.*\n", "\n");
        sql = sql.replaceAll("\\s+", " ");
        sql = sql.replaceAll(";\\s*", ";\n");
        writer.append(sql);/*ww w  . j a  v  a2  s  . c o  m*/
        writer.append("\n");
    }

    writer.append("\nSET FOREIGN_KEY_CHECKS=0;\n");
    try (Connection con = ds.getConnection()) {
        for (String table : TABLES) {
            writer.flush();
            try (Statement stmt = con.createStatement()) {
                LOG.info("exporting table {}", table);
                long _start = System.currentTimeMillis();

                stmt.setQueryTimeout(3600 * 24);
                ResultSet rs = stmt.executeQuery("SELECT * FROM `" + table + "`");
                ResultSetMetaData metaData = rs.getMetaData();
                int columns = metaData.getColumnCount();

                String insertStatement = "INSERT INTO `" + table + "` VALUES ";

                StringBuilder stmtBuilder = new StringBuilder(insertStatement);
                while (rs.next()) {

                    StringBuilder entryBuilder = new StringBuilder("(");
                    for (int colIndex = 1; colIndex <= columns; colIndex++) {
                        Object object = rs.getObject(colIndex);
                        String colName = metaData.getColumnName(colIndex);
                        String colClassName = metaData.getColumnClassName(colIndex);
                        String escaped = escape(object, colClassName, colName);
                        entryBuilder.append(escaped);
                        if (colIndex < columns) {
                            entryBuilder.append(',');
                        }
                    }
                    entryBuilder.append("),");

                    if (stmtBuilder.length() != insertStatement.length()
                            && stmtBuilder.length() + entryBuilder.length() > DEFAULT_MAX_ALLOWED_PACKET) {
                        stmtBuilder.setCharAt(stmtBuilder.length() - 1, ';');
                        writer.append(stmtBuilder).append('\n');
                        stmtBuilder = new StringBuilder(insertStatement);
                    }

                    stmtBuilder.append(entryBuilder);
                }

                if (stmtBuilder.length() != insertStatement.length()) {
                    stmtBuilder.setCharAt(stmtBuilder.length() - 1, ';');
                    writer.append(stmtBuilder).append('\n');
                }

                LOG.info("exported table {} in {}", table,
                        DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - _start));
            }
        }
        writer.append("SET FOREIGN_KEY_CHECKS=1;\n");
    } catch (Exception ex) {
        LOG.error("SQL error", ex);
        return false;
    }

    return true;
}

From source file:com.joliciel.frenchTreebank.export.FrenchTreebankXmlWriter.java

public void write(Writer writer, TreebankFile treebankFile) throws TemplateException {
    try {//from   w ww. jav  a2 s. c om
        Configuration cfg = new Configuration();
        // Specify the data source where the template files come from.
        cfg.setClassForTemplateLoading(FrenchTreebankXmlWriter.class, "/com/joliciel/frenchTreebank/export");
        //         cfg.setDirectoryForTemplateLoading(new File("templates"));
        cfg.setCacheStorage(new NullCacheStorage());

        cfg.setObjectWrapper(new DefaultObjectWrapper());

        Map<String, Object> model = new HashMap<String, Object>();
        model.put("file", treebankFile);
        model.put("LOG", LOG);
        Template temp = cfg.getTemplate("xml.ftl");

        temp.process(model, writer);
        writer.flush();
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:com.twinsoft.convertigo.engine.servlets.GenericServlet.java

protected void doRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {
    HttpServletRequestTwsWrapper wrapped_request = new HttpServletRequestTwsWrapper(request);
    request = wrapped_request;//  www. j  a  v  a2s. com

    String baseUrl = getServletBaseUrl(request);

    if ((baseUrl.indexOf("/projects/") != -1) || (baseUrl.indexOf("/webclipper/") != -1)) {
        long t0 = System.currentTimeMillis();
        try {
            String encoded = request.getParameter(Parameter.RsaEncoded.getName());
            if (encoded != null) {
                String query = Engine.theApp.rsaManager.decrypt(encoded, request.getSession());
                wrapped_request.clearParameters();
                wrapped_request.addQuery(query);
            }

            Object result = processRequest(request);

            response.addHeader("Expires", "-1");

            if (getCacheControl(request).equals("false"))
                HeaderName.CacheControl.addHeader(response,
                        "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");

            /**
             * Disabled since #253 : Too much HTML Connector cookies in
             * response header make a tomcat exception
             * http://sourceus.twinsoft.fr/ticket/253 cookies must be in xml
             * if wanted, not in headers
             * 
             * Vector cookies = (Vector)
             * request.getAttribute("convertigo.cookies"); for (int i=0;
             * i<cookies.size(); i++) { String sCookie =
             * (String)cookies.elementAt(i);
             * response.addHeader("Set-Cookie", sCookie);
             * Engine.logContext.trace("[GenericServlet] Set-Cookie: " +
             * sCookie); }
             */

            String trSessionId = (String) request.getAttribute("sequence.transaction.sessionid");
            if ((trSessionId != null) && (!trSessionId.equals(""))) {
                response.setHeader("Transaction-JSessionId", trSessionId);
            }

            String requested_content_type = request.getParameter(Parameter.ContentType.getName());
            String content_type = getContentType(request);
            if (requested_content_type != null && !requested_content_type.equals(content_type)) {
                Engine.logEngine.debug("(GenericServlet) Override Content-Type requested to change : "
                        + content_type + " to " + requested_content_type);
                content_type = requested_content_type;
            } else {
                requested_content_type = null;
            }

            response.setContentType(content_type);
            if (content_type.startsWith("text")) {
                String charset = (String) request.getAttribute("convertigo.charset");
                if (charset != null && charset.length() > 0) {
                    response.setCharacterEncoding(charset);
                }
            }

            try {

                if (result != null) {

                    Boolean b = (Boolean) request.getAttribute("convertigo.isErrorDocument");
                    if (b.booleanValue()) {
                        Requester requester = getRequester();
                        boolean bThrowHTTP500 = false;

                        if (requester instanceof WebServiceServletRequester) {
                            bThrowHTTP500 = Boolean.parseBoolean(EnginePropertiesManager.getProperty(
                                    EnginePropertiesManager.PropertyName.THROW_HTTP_500_SOAP_FAULT));
                        } else if (requester instanceof ServletRequester) {
                            bThrowHTTP500 = Boolean.parseBoolean(EnginePropertiesManager
                                    .getProperty(EnginePropertiesManager.PropertyName.THROW_HTTP_500));
                        }

                        if (bThrowHTTP500) {
                            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                            Engine.logEngine.debug("(GenericServlet) Requested HTTP 500 status code");
                        }
                    } else {
                        applyCustomStatus(request, response);
                    }

                    if (result instanceof AttachmentDetails) {
                        AttachmentDetails attachment = (AttachmentDetails) result;
                        byte[] data = attachment.getData();
                        String contentType = attachment.getContentType();

                        if (requested_content_type != null) {
                            contentType = requested_content_type;
                        }

                        String name = attachment.getName();

                        HeaderName.ContentType.setHeader(response, contentType);
                        HeaderName.ContentLength.setHeader(response, "" + data.length);
                        HeaderName.ContentDisposition.setHeader(response, "attachment; filename=" + name);

                        applyCustomHeaders(request, response);

                        OutputStream out = response.getOutputStream();
                        out.write(data);
                        out.flush();
                    } else if (result instanceof byte[]) {
                        if (requested_content_type != null) {
                            response.setContentType(requested_content_type);
                        } else {
                            response.setContentType(getContentType(request));
                            response.setCharacterEncoding((String) request.getAttribute("convertigo.charset"));
                        }
                        HeaderName.ContentLength.addHeader(response, "" + ((byte[]) result).length);

                        applyCustomHeaders(request, response);

                        OutputStream out = response.getOutputStream();
                        out.write((byte[]) result);
                        out.flush();
                    } else {
                        String sResult = "";
                        if (result instanceof String) {
                            sResult = (String) result;
                        } else if (result instanceof Document) {
                            sResult = XMLUtils.prettyPrintDOM((Document) result);
                        } else if (result instanceof SOAPMessage) {
                            sResult = SOAPUtils.toString((SOAPMessage) result,
                                    (String) request.getAttribute("convertigo.charset"));
                        }

                        applyCustomHeaders(request, response);

                        Writer writer = response.getWriter();
                        writer.write(sResult);
                        writer.flush();
                    }
                } else {
                    applyCustomHeaders(request, response);

                    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                }
            } catch (IOException e) {
                // The connection has probably been reset by peer
                Engine.logContext
                        .warn("[GenericServlet] The connection has probably been reset by peer (IOException): "
                                + e.getMessage());
            } finally {
                // Supervision mode
                String supervision = request.getParameter(Parameter.Supervision.getName());
                if (supervision != null) {
                    Engine.logContext
                            .debug("[GenericServlet] Supervision mode => invalidating HTTP session in 30s.");
                    removeSession(request, 30);
                }

                // Removes context and session when finished
                // Note: case of context.requireEndOfContext has been set in
                // scope
                if (request.getAttribute("convertigo.requireEndOfContext") != null) {
                    removeContext(request);
                    removeSession(request, 1);
                }

                // Removes context when finished
                String removeContextParam = request.getParameter(Parameter.RemoveContext.getName());
                if (removeContextParam == null) {
                    // case of a mother sequence (context is removed by
                    // default)
                    Boolean removeContextAttr = (Boolean) request
                            .getAttribute("convertigo.context.removalRequired");
                    if ((removeContextAttr != null) && removeContextAttr.booleanValue()) {
                        removeContext(request);
                    }
                } else {
                    // other cases (remove context if exist __removeContext
                    // or __removeContext=true/false)
                    if (!("false".equals(removeContextParam))) {
                        removeContext(request);
                    }
                }

                // Removes session when finished
                String removeSessionParam = request.getParameter(Parameter.RemoveSession.getName());
                if (removeSessionParam != null) {
                    // __removeSession or __removeSession=true/false
                    // or __removeSession=xx (where xx is a number of seconds)
                    if (!("false".equals(removeSessionParam))) {
                        int interval = 1;
                        try {
                            interval = Integer.parseInt(removeSessionParam, 10);
                        } catch (Exception e) {
                        }
                        removeSession(request, interval);
                    }
                }

            }
        } catch (Exception e) {
            Engine.logContext.error("Unable to process the request!", e);
            processException(request, response, e);
        } finally {
            long t1 = System.currentTimeMillis();
            Engine.theApp.pluginsManager.fireHttpServletRequestEnd(request, t0, t1);
        }
    } else {
        // Not a valid Convertigo invocation URL, use retrieve as static
        // resource
        handleStaticData(request, response);
        return;
    }
}

From source file:com.joliciel.talismane.machineLearning.perceptron.PerceptronClassifactionModelTrainerImpl.java

void prepareData(ClassificationEventStream eventStream) {
    try {/*from w ww .j a  v a  2 s  .co m*/
        eventFile = File.createTempFile("events", "txt");
        eventFile.deleteOnExit();
        Writer eventWriter = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(eventFile), "UTF-8"));
        while (eventStream.hasNext()) {
            ClassificationEvent corpusEvent = eventStream.next();
            PerceptronEvent event = new PerceptronEvent(corpusEvent, params);
            event.write(eventWriter);
        }
        eventWriter.flush();
        eventWriter.close();

        if (cutoff > 1) {
            params.initialiseCounts();
            File originalEventFile = eventFile;
            Scanner scanner = new Scanner(
                    new BufferedReader(new InputStreamReader(new FileInputStream(eventFile), "UTF-8")));

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                PerceptronEvent event = new PerceptronEvent(line);
                for (int featureIndex : event.getFeatureIndexes()) {
                    params.getFeatureCounts()[featureIndex]++;
                }
            }
            scanner.close();

            if (LOG.isDebugEnabled()) {
                int[] cutoffCounts = new int[21];
                for (int count : params.getFeatureCounts()) {
                    for (int i = 1; i < 21; i++) {
                        if (count >= i) {
                            cutoffCounts[i]++;
                        }
                    }
                }
                LOG.debug("Feature counts:");
                for (int i = 1; i < 21; i++) {
                    LOG.debug("Cutoff " + i + ": " + cutoffCounts[i]);
                }
            }
            PerceptronModelParameters cutoffParams = new PerceptronModelParameters();
            int[] newIndexes = cutoffParams.initialise(params, cutoff);
            decisionMaker = new PerceptronDecisionMaker<T>(cutoffParams, decisionFactory);
            scanner = new Scanner(
                    new BufferedReader(new InputStreamReader(new FileInputStream(eventFile), "UTF-8")));

            eventFile = File.createTempFile("eventsCutoff", "txt");
            eventFile.deleteOnExit();
            Writer eventCutoffWriter = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(eventFile), "UTF-8"));
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                PerceptronEvent oldEvent = new PerceptronEvent(line);
                PerceptronEvent newEvent = new PerceptronEvent(oldEvent, newIndexes);
                newEvent.write(eventCutoffWriter);
            }
            eventCutoffWriter.flush();
            eventCutoffWriter.close();
            params = cutoffParams;
            originalEventFile.delete();
        }

        params.initialiseWeights();
        totalFeatureWeights = new double[params.getFeatureCount()][params.getOutcomeCount()];
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:ru.histone.staticrender.StaticRender.java

public void renderSite(final Path srcDir, final Path dstDir) {
    log.info("Running StaticRender for srcDir={}, dstDir={}", srcDir.toString(), dstDir.toString());
    Path contentDir = srcDir.resolve("content/");
    final Path layoutDir = srcDir.resolve("layouts/");

    FileVisitor<Path> layoutVisitor = new SimpleFileVisitor<Path>() {
        @Override/*from  www  . j a  va 2 s.c  o  m*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (file.toString().endsWith("." + TEMPLATE_FILE_EXTENSION)) {
                ArrayNode ast = null;
                try {
                    ast = histone.parseTemplateToAST(new FileReader(file.toFile()));
                } catch (HistoneException e) {
                    throw new RuntimeException("Error parsing histone template:" + e.getMessage(), e);
                }

                final String fileName = file.getFileName().toString();
                String layoutId = fileName.substring(0,
                        fileName.length() - TEMPLATE_FILE_EXTENSION.length() - 1);
                layouts.put(layoutId, ast);
                if (log.isDebugEnabled()) {
                    log.debug("Layout found id='{}', file={}", layoutId, file);
                } else {
                    log.info("Layout found id='{}'", layoutId);
                }
            } else {
                final Path relativeFileName = srcDir.resolve("layouts").relativize(Paths.get(file.toUri()));
                final Path resolvedFile = dstDir.resolve(relativeFileName);
                if (!resolvedFile.getParent().toFile().exists()) {
                    Files.createDirectories(resolvedFile.getParent());
                }
                Files.copy(Paths.get(file.toUri()), resolvedFile, StandardCopyOption.REPLACE_EXISTING,
                        LinkOption.NOFOLLOW_LINKS);
            }
            return FileVisitResult.CONTINUE;
        }
    };

    FileVisitor<Path> contentVisitor = new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

            Scanner scanner = new Scanner(file, "UTF-8");
            scanner.useDelimiter("-----");

            String meta = null;
            StringBuilder content = new StringBuilder();

            if (!scanner.hasNext()) {
                throw new RuntimeException("Wrong format #1:" + file.toString());
            }

            if (scanner.hasNext()) {
                meta = scanner.next();
            }

            if (scanner.hasNext()) {
                content.append(scanner.next());
                scanner.useDelimiter("\n");
            }

            while (scanner.hasNext()) {
                final String next = scanner.next();
                content.append(next);

                if (scanner.hasNext()) {
                    content.append("\n");
                }
            }

            Map<String, String> metaYaml = (Map<String, String>) yaml.load(meta);

            String layoutId = metaYaml.get("layout");

            if (!layouts.containsKey(layoutId)) {
                throw new RuntimeException(MessageFormat.format("No layout with id='{0}' found", layoutId));
            }

            final Path relativeFileName = srcDir.resolve("content").relativize(Paths.get(file.toUri()));
            final Path resolvedFile = dstDir.resolve(relativeFileName);
            if (!resolvedFile.getParent().toFile().exists()) {
                Files.createDirectories(resolvedFile.getParent());
            }
            Writer output = new FileWriter(resolvedFile.toFile());
            ObjectNode context = jackson.createObjectNode();
            ObjectNode metaNode = jackson.createObjectNode();
            context.put("content", content.toString());
            context.put("meta", metaNode);
            for (String key : metaYaml.keySet()) {
                if (!key.equalsIgnoreCase("content")) {
                    metaNode.put(key, metaYaml.get(key));
                }
            }

            try {
                histone.evaluateAST(layoutDir.toUri().toString(), layouts.get(layoutId), context, output);
                output.flush();
            } catch (HistoneException e) {
                throw new RuntimeException("Error evaluating content: " + e.getMessage(), e);
            } finally {
                output.close();
            }

            return FileVisitResult.CONTINUE;
        }
    };

    try {
        Files.walkFileTree(layoutDir, layoutVisitor);
        Files.walkFileTree(contentDir, contentVisitor);
    } catch (Exception e) {
        throw new RuntimeException("Error during site render", e);
    }
}

From source file:ChannelToWriter.java

/**
 * Read bytes from the specified channel, decode them using the specified
 * Charset, and write the resulting characters to the specified writer
 *///from w  ww  .  j a va 2  s  .  c  o  m
public static void copy(ReadableByteChannel channel, Writer writer, Charset charset) throws IOException {
    // Get and configure the CharsetDecoder we'll use
    CharsetDecoder decoder = charset.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.IGNORE);
    decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);

    // Get the buffers we'll use, and the backing array for the CharBuffer.
    ByteBuffer bytes = ByteBuffer.allocateDirect(2 * 1024);
    CharBuffer chars = CharBuffer.allocate(2 * 1024);
    char[] array = chars.array();

    while (channel.read(bytes) != -1) { // Read from channel until EOF
        bytes.flip(); // Switch to drain mode for decoding
        // Decode the byte buffer into the char buffer.
        // Pass false to indicate that we're not done.
        decoder.decode(bytes, chars, false);

        // Put the char buffer into drain mode, and write its contents
        // to the Writer, reading them from the backing array.
        chars.flip();
        writer.write(array, chars.position(), chars.remaining());

        // Discard all bytes we decoded, and put the byte buffer back into
        // fill mode. Since all characters were output, clear that buffer.
        bytes.compact(); // Discard decoded bytes
        chars.clear(); // Clear the character buffer
    }

    // At this point there may still be some bytes in the buffer to decode
    // So put the buffer into drain mode call decode() a final time, and
    // finish with a flush().
    bytes.flip();
    decoder.decode(bytes, chars, true); // True means final call
    decoder.flush(chars); // Flush any buffered chars
    // Write these final chars (if any) to the writer.
    chars.flip();
    writer.write(array, chars.position(), chars.remaining());
    writer.flush();
}

From source file:com.joliciel.talismane.machineLearning.features.DynamicSourceCodeBuilderImpl.java

@Override
public Feature<T, ?> getFeature() {
    if (isDynamic) {
        // this feature implemented addDynamicSourceCode
        // construct the class code surrounding the checkInternal method

        if (LOG.isDebugEnabled())
            LOG.debug("Building class for " + rootFeature.getName());

        this.indentation = 0;
        indentString = "";

        String packageName = this.rootFeature.getClass().getPackage().getName() + ".runtime";
        String className = this.rootFeature.getClass().getSimpleName() + "_" + dynamiser.nextClassIndex();
        String qualifiedName = packageName + "." + className;
        Class<?> outcomeType = this.dynamiser.getOutcomeType(rootFeature);
        String returnType = outcomeType.getSimpleName();
        String contextType = this.dynamiser.getContextClass().getSimpleName();

        this.appendToClass("package " + packageName + ";");

        imports.add(Feature.class);
        imports.add(FeatureResult.class);
        imports.add(RuntimeEnvironment.class);

        imports.add(this.dynamiser.getContextClass());

        imports.add(this.rootFeature.getFeatureType());

        Class<?> parentClass = null;
        String checkMethodSuffix = "";
        if (AbstractCachableFeature.class.isAssignableFrom(rootFeature.getClass())) {
            parentClass = AbstractCachableFeature.class;
            checkMethodSuffix = "Internal";
        } else if (AbstractMonitorableFeature.class.isAssignableFrom(rootFeature.getClass())) {
            parentClass = AbstractMonitorableFeature.class;
            checkMethodSuffix = "Internal";
        } else {/*  w  w  w . j  av  a2 s  . c  o  m*/
            parentClass = AbstractFeature.class;
        }
        imports.add(parentClass);

        for (Feature<T, ?> classLevelFeature : classLevelFeatures.values()) {
            imports.add(classLevelFeature.getFeatureType());
        }

        Set<String> importNames = new TreeSet<String>();
        for (Class<?> oneImport : imports) {
            if (!oneImport.getName().startsWith("java.lang."))
                importNames.add(oneImport.getName());
        }

        for (String importName : importNames) {
            this.appendToClass("import " + importName + ";");
        }

        String implementedInterface = this.rootFeature.getFeatureType().getSimpleName();
        if (this.rootFeature.getFeatureType().getTypeParameters().length > 0)
            implementedInterface += "<" + contextType + ">";

        this.appendToClass("public final class " + className + " extends " + parentClass.getSimpleName() + "<"
                + contextType + ", " + returnType + "> implements " + implementedInterface + " {");
        this.indent();

        for (Entry<String, Feature<T, ?>> classLevelFeatureEntry : classLevelFeatures.entrySet()) {
            String argumentName = classLevelFeatureEntry.getKey();
            Feature<T, ?> argument = classLevelFeatureEntry.getValue();
            String argumentType = argument.getFeatureType().getSimpleName();

            String argumentNameInitialCaps = Character.toUpperCase(argumentName.charAt(0))
                    + argumentName.substring(1);

            if (argument.getFeatureType().getTypeParameters().length == 0) {
                this.appendToClass("private " + argumentType + " " + argumentName + ";");
                this.appendToClass("public " + argumentType + " get" + argumentNameInitialCaps + "() { return "
                        + argumentName + "; }");
                this.appendToClass("public void set" + argumentNameInitialCaps + "(" + argumentType
                        + " value) { " + argumentName + "=value; }");
            } else {
                this.appendToClass("private " + argumentType + "<" + contextType + "> " + argumentName + ";");
                this.appendToClass("public " + argumentType + "<" + contextType + "> get"
                        + argumentNameInitialCaps + "() { return " + argumentName + "; }");
                this.appendToClass("public void set" + argumentNameInitialCaps + "(" + argumentType + "<"
                        + contextType + "> value) { " + argumentName + "=value; }");
            }
        }

        this.appendToClass("public FeatureResult<" + returnType + "> check" + checkMethodSuffix + "("
                + contextType + " context, RuntimeEnvironment env) {");
        this.indent();

        this.classBuilder.append(methodBuilder);

        this.outdent();
        this.appendToClass("}");

        this.appendToClass("@SuppressWarnings({ \"rawtypes\" })");
        this.appendToClass("@Override");
        this.appendToClass("public Class<? extends Feature> getFeatureType() {");
        this.indent();
        this.appendToClass("return " + this.rootFeature.getFeatureType().getSimpleName() + ".class;");
        this.outdent();
        this.appendToClass("}");

        this.outdent();
        this.appendToClass("}");

        if (LOG.isTraceEnabled()) {
            // write the class to the temp directory if trace is enabled
            try {
                File tempFile = File.createTempFile(className + "_", ".java");

                Writer tempFileWriter = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(tempFile, false), "UTF8"));
                tempFileWriter.write(this.classBuilder.toString());
                tempFileWriter.flush();
                tempFileWriter.close();
            } catch (IOException ioe) {
                LogUtils.logError(LOG, ioe);
            }
        }

        DynamicCompiler compiler = this.dynamiser.getCompiler();

        List<String> optionList = new ArrayList<String>();
        //         optionList.add("-Xlint:unchecked");
        //         optionList.add("-verbose");

        @SuppressWarnings("unchecked")
        Class<Feature<T, ?>> newFeatureClass = (Class<Feature<T, ?>>) compiler.compile(qualifiedName,
                this.classBuilder, optionList);

        // instantiate the feature
        Feature<T, ?> newFeature = null;
        try {
            newFeature = newFeatureClass.newInstance();
        } catch (InstantiationException e) {
            LogUtils.logError(LOG, e);
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            LogUtils.logError(LOG, e);
            throw new RuntimeException(e);
        }

        // assign the dependencies
        for (Entry<String, Feature<T, ?>> classLevelFeatureEntry : classLevelFeatures.entrySet()) {
            String argumentName = classLevelFeatureEntry.getKey();
            Feature<T, ?> argument = classLevelFeatureEntry.getValue();
            String argumentNameInitialCaps = Character.toUpperCase(argumentName.charAt(0))
                    + argumentName.substring(1);

            Method method;

            try {
                method = newFeature.getClass().getMethod("set" + argumentNameInitialCaps,
                        argument.getFeatureType());
            } catch (SecurityException e) {
                LogUtils.logError(LOG, e);
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                LogUtils.logError(LOG, e);
                throw new RuntimeException(e);
            }

            try {
                method.invoke(newFeature, argument);
            } catch (IllegalArgumentException e) {
                LogUtils.logError(LOG, e);
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                LogUtils.logError(LOG, e);
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                LogUtils.logError(LOG, e);
                throw new RuntimeException(e);
            }
        }
        newFeature.setName(rootFeature.getName());
        newFeature.setCollectionName(rootFeature.getCollectionName());

        return newFeature;
    } else {
        // this feature didn't implement addDynamicSourceCode - dynamise its arguments
        if (LOG.isDebugEnabled())
            LOG.debug("No dynamic code for " + rootFeature.getName());

        this.dynamise(rootFeature);
        return rootFeature;
    }
}