List of usage examples for java.io Writer append
public Writer append(char c) throws IOException
From source file:org.apache.mahout.utils.clustering.ClusterDumper.java
public void printClusters(String[] dictionary) throws Exception { Configuration conf = new Configuration(); if (this.termDictionary != null) { if ("text".equals(dictionaryFormat)) { dictionary = VectorHelper.loadTermDictionary(new File(this.termDictionary)); } else if ("sequencefile".equals(dictionaryFormat)) { dictionary = VectorHelper.loadTermDictionary(conf, this.termDictionary); } else {/*from w w w . j a va 2s . c o m*/ throw new IllegalArgumentException("Invalid dictionary format"); } } Writer writer; boolean shouldClose; if (this.outputFile == null) { shouldClose = false; writer = new OutputStreamWriter(System.out, Charsets.UTF_8); } else { shouldClose = true; if (outputFile.getName().startsWith("s3n://")) { Path p = outputPath; FileSystem fs = FileSystem.get(p.toUri(), conf); writer = new OutputStreamWriter(fs.create(p), Charsets.UTF_8); } else { Files.createParentDirs(outputFile); writer = Files.newWriter(this.outputFile, Charsets.UTF_8); } } ClusterWriter clusterWriter = createClusterWriter(writer, dictionary); try { long numWritten = clusterWriter.write(new SequenceFileDirValueIterable<ClusterWritable>( new Path(seqFileDir, "part-*"), PathType.GLOB, conf)); writer.flush(); if (runEvaluation) { HadoopUtil.delete(conf, new Path("tmp/representative")); int numIters = 5; RepresentativePointsDriver.main(new String[] { "--input", seqFileDir.toString(), "--output", "tmp/representative", "--clusteredPoints", pointsDir.toString(), "--distanceMeasure", measure.getClass().getName(), "--maxIter", String.valueOf(numIters) }); conf.set(RepresentativePointsDriver.DISTANCE_MEASURE_KEY, measure.getClass().getName()); conf.set(RepresentativePointsDriver.STATE_IN_KEY, "tmp/representative/representativePoints-" + numIters); ClusterEvaluator ce = new ClusterEvaluator(conf, seqFileDir); writer.append("\n"); writer.append("Inter-Cluster Density: ").append(String.valueOf(ce.interClusterDensity())) .append("\n"); writer.append("Intra-Cluster Density: ").append(String.valueOf(ce.intraClusterDensity())) .append("\n"); CDbwEvaluator cdbw = new CDbwEvaluator(conf, seqFileDir); writer.append("CDbw Inter-Cluster Density: ").append(String.valueOf(cdbw.interClusterDensity())) .append("\n"); writer.append("CDbw Intra-Cluster Density: ").append(String.valueOf(cdbw.intraClusterDensity())) .append("\n"); writer.append("CDbw Separation: ").append(String.valueOf(cdbw.separation())).append("\n"); writer.flush(); } log.info("Wrote {} clusters", numWritten); } finally { if (shouldClose) { Closeables.close(clusterWriter, false); } else { if (clusterWriter instanceof GraphMLClusterWriter) { clusterWriter.close(); } } } }
From source file:org.apache.hadoop.hbase.mob.filecompactions.PartitionedMobFileCompactor.java
/** * Compacts a partition of selected small mob files and all the del files in a batch. * @param request The compaction request. * @param partition A compaction partition. * @param table The current table.//w w w .j a va2 s . c o m * @param filesToCompact The files to be compacted. * @param batch The number of mob files to be compacted in a batch. * @param bulkloadPathOfPartition The directory where the bulkload column of the current * partition is saved. * @param bulkloadColumnPath The directory where the bulkload files of current partition * are saved. * @param newFiles The paths of new mob files after compactions. * @throws IOException */ private void compactMobFilesInBatch(PartitionedMobFileCompactionRequest request, CompactionPartition partition, HTable table, List<StoreFile> filesToCompact, int batch, Path bulkloadPathOfPartition, Path bulkloadColumnPath, List<Path> newFiles) throws IOException { // open scanner to the selected mob files and del files. StoreScanner scanner = createScanner(filesToCompact, ScanType.COMPACT_DROP_DELETES); // the mob files to be compacted, not include the del files. List<StoreFile> mobFilesToCompact = filesToCompact.subList(0, batch); // Pair(maxSeqId, cellsCount) Pair<Long, Long> fileInfo = getFileInfo(mobFilesToCompact); // open writers for the mob files and new ref store files. Writer writer = null; Writer refFileWriter = null; Path filePath = null; Path refFilePath = null; long mobCells = 0; try { writer = MobUtils.createWriter(conf, fs, column, partition.getPartitionId().getDate(), tempPath, Long.MAX_VALUE, column.getCompactionCompression(), partition.getPartitionId().getStartKey(), compactionCacheConfig); filePath = writer.getPath(); byte[] fileName = Bytes.toBytes(filePath.getName()); // create a temp file and open a writer for it in the bulkloadPath refFileWriter = MobUtils.createRefFileWriter(conf, fs, column, bulkloadColumnPath, fileInfo.getSecond().longValue(), compactionCacheConfig); refFilePath = refFileWriter.getPath(); List<Cell> cells = new ArrayList<Cell>(); boolean hasMore = false; ScannerContext scannerContext = ScannerContext.newBuilder().setBatchLimit(compactionKVMax).build(); do { hasMore = scanner.next(cells, scannerContext); for (Cell cell : cells) { // TODO remove this after the new code are introduced. KeyValue kv = KeyValueUtil.ensureKeyValue(cell); // write the mob cell to the mob file. writer.append(kv); // write the new reference cell to the store file. KeyValue reference = MobUtils.createMobRefKeyValue(kv, fileName, tableNameTag); refFileWriter.append(reference); mobCells++; } cells.clear(); } while (hasMore); } finally { // close the scanner. scanner.close(); // append metadata to the mob file, and close the mob file writer. closeMobFileWriter(writer, fileInfo.getFirst(), mobCells); // append metadata and bulkload info to the ref mob file, and close the writer. closeRefFileWriter(refFileWriter, fileInfo.getFirst(), request.selectionTime); } if (mobCells > 0) { // commit mob file MobUtils.commitFile(conf, fs, filePath, mobFamilyDir, compactionCacheConfig); // bulkload the ref file bulkloadRefFile(table, bulkloadPathOfPartition, filePath.getName()); newFiles.add(new Path(mobFamilyDir, filePath.getName())); } else { // remove the new files // the mob file is empty, delete it instead of committing. deletePath(filePath); // the ref file is empty, delete it instead of committing. deletePath(refFilePath); } // archive the old mob files, do not archive the del files. try { closeStoreFileReaders(mobFilesToCompact); MobUtils.removeMobFiles(conf, fs, tableName, mobTableDir, column.getName(), mobFilesToCompact); } catch (IOException e) { LOG.error("Failed to archive the files " + mobFilesToCompact, e); } }
From source file:com.github.jknack.handlebars.internal.Block.java
@SuppressWarnings("unchecked") @Override//from w w w .j a v a 2 s . co m protected void merge(final Context context, final Writer writer) throws IOException { if (body == null) { return; } final String helperName; Helper<Object> helper = helper(name); Template template = body; final Object childContext; Context currentScope = context; if (helper == null) { childContext = transform(context.get(name)); if (inverted) { helperName = UnlessHelper.NAME; } else if (childContext instanceof Iterable) { helperName = EachHelper.NAME; } else if (childContext instanceof Boolean) { helperName = IfHelper.NAME; } else if (childContext instanceof Lambda) { helperName = WithHelper.NAME; template = Lambdas.compile(handlebars, (Lambda<Object, Object>) childContext, context, template, startDelimiter, endDelimiter); } else { helperName = WithHelper.NAME; currentScope = Context.newContext(context, childContext); } // A built-in helper might be override it. helper = handlebars.helper(helperName); } else { helperName = name; childContext = transform(determineContext(context)); } Options options = new Options.Builder(handlebars, helperName, TagType.SECTION, currentScope, template) .setInverse(inverse == null ? Template.EMPTY : inverse).setParams(params(currentScope)) .setHash(hash(context)).build(); options.data(Context.PARAM_SIZE, this.params.size()); CharSequence result = helper.apply(childContext, options); if (!isEmpty(result)) { writer.append(result); } }
From source file:org.exoplatform.ecm.webui.form.UIDialogForm.java
public void processRenderAction() throws Exception { WebuiRequestContext context = WebuiRequestContext.getCurrentInstance(); Writer writer = context.getWriter(); writer.append("<div class=\"uiAction uiActionBorder\">"); String[] listAction = getActions(); ResourceBundle res = context.getApplicationResourceBundle(); String actionLabel;/*from w w w .j av a 2 s . c om*/ String link; for (String action : listAction) { try { actionLabel = res.getString(getName() + ".action." + action); } catch (MissingResourceException e) { actionLabel = action; } link = event(action); writer.append("<button type=\"button\" class=\"btn\" onclick =\"").append(link).append("\">") .append(actionLabel).append("</button>"); } writer.append("</div>"); }
From source file:org.kuali.rice.krad.uif.freemarker.FreeMarkerInlineRenderUtils.java
/** * Render a KRAD component template inline. * //w ww . j av a 2 s . co m * <p> * This method originated as template.ftl, and supercedes the previous content of that template. * </p> * * @param env The FreeMarker environment. * @param component The component to render a template for. * @param body The nested body. * @param componentUpdate True if this is an update, false for full view. * @param includeSrc True to include the template source in the environment when rendering, * false to skip inclusion. * @param tmplParms Additional parameters to pass to the template macro. * @throws TemplateException If FreeMarker rendering fails. * @throws IOException If rendering is interrupted due to an I/O error. */ public static void renderTemplate(Environment env, Component component, String body, boolean componentUpdate, boolean includeSrc, Map<String, TemplateModel> tmplParms) throws TemplateException, IOException { String dataJsScripts = ""; String templateJsScripts = ""; if (component == null) { return; } String s; Writer out = env.getOut(); if ((component.isRender() && (!component.isRetrieveViaAjax() || componentUpdate)) || (component.getProgressiveRender() != null && !component.getProgressiveRender().equals("") && !component.isProgressiveRenderViaAJAX() && !component.isProgressiveRenderAndRefresh())) { if (StringUtils.hasText(s = component.getPreRenderContent())) { out.write(s); } if (component.isSelfRendered()) { out.write(component.getRenderedHtmlOutput()); } else { if (includeSrc) { env.include(component.getTemplate(), env.getTemplate().getEncoding(), true); } Macro fmMacro = component.getTemplateName() == null ? null : (Macro) env.getMainNamespace().get(component.getTemplateName()); if (fmMacro == null) { // force inclusion of the source to see if we can get the macro env.include(component.getTemplate(), env.getTemplate().getEncoding(), true); fmMacro = component.getTemplateName() == null ? null : (Macro) env.getCurrentNamespace().get(component.getTemplateName()); // if still missing throw an exception if (fmMacro == null) { throw new TemplateException("No macro found using " + component.getTemplateName(), env); } } Map<String, Object> args = new java.util.HashMap<String, Object>(); args.put(component.getComponentTypeName(), component); if (tmplParms != null) { args.putAll(tmplParms); } if (StringUtils.hasText(body)) { args.put("body", body); } InlineTemplateUtils.invokeMacro(env, fmMacro, args, null); } if (StringUtils.hasText(s = component.getEventHandlerScript())) { templateJsScripts += s; } if (StringUtils.hasText(s = component.getScriptDataAttributesJs())) { dataJsScripts += s; } if (StringUtils.hasText(s = component.getPostRenderContent())) { out.append(s); } } if (componentUpdate || UifConstants.ViewStatus.RENDERED.equals(component.getViewStatus())) { renderScript(dataJsScripts, component, UifConstants.RoleTypes.DATA_SCRIPT, out); renderScript(templateJsScripts, component, null, out); return; } String methodToCallOnRefresh = component.getMethodToCallOnRefresh(); if (!StringUtils.hasText(methodToCallOnRefresh)) { methodToCallOnRefresh = ""; } if ((!component.isRender() && (component.isProgressiveRenderViaAJAX() || component.isProgressiveRenderAndRefresh() || component.isDisclosedByAction() || component.isRefreshedByAction())) || component.isRetrieveViaAjax()) { out.write("<span id=\""); out.write(component.getId()); out.write("\" data-role=\"placeholder\" class=\"uif-placeholder " + component.getStyleClassesAsString() + "\"></span>"); } if (StringUtils.hasText(component.getProgressiveRender())) { for (String cName : component.getProgressiveDisclosureControlNames()) { templateJsScripts += "var condition = function(){return (" + component.getProgressiveDisclosureConditionJs() + ");};setupProgressiveCheck('" + StringEscapeUtils.escapeJavaScript(cName) + "', '" + component.getId() + "', condition," + component.isProgressiveRenderAndRefresh() + ", '" + methodToCallOnRefresh + "');"; } templateJsScripts += "hiddenInputValidationToggle('" + component.getId() + "');"; } if (StringUtils.hasText(component.getConditionalRefresh())) { for (String cName : component.getConditionalRefreshControlNames()) { templateJsScripts += "var condition = function(){return (" + component.getConditionalRefreshConditionJs() + ");};setupRefreshCheck('" + StringEscapeUtils.escapeJavaScript(cName) + "', '" + component.getId() + "', condition,'" + methodToCallOnRefresh + "');"; } } List<String> refreshWhenChanged = component.getRefreshWhenChangedPropertyNames(); if (refreshWhenChanged != null) { for (String cName : refreshWhenChanged) { templateJsScripts += "setupOnChangeRefresh('" + StringEscapeUtils.escapeJavaScript(cName) + "', '" + component.getId() + "','" + methodToCallOnRefresh + "');"; } } renderScript(dataJsScripts, component, UifConstants.RoleTypes.DATA_SCRIPT, out); renderScript(templateJsScripts, component, null, out); renderTooltip(component, out); }
From source file:nl.systemsgenetics.eqtlinteractionanalyser.eqtlinteractionanalyser.TestEQTLDatasetForInteractions.java
private void correctDosageDirectionForQtl(File snpsToSwapFile, ExpressionDataset datasetGenotypes, ExpressionDataset datasetExpression) throws IOException { //double[] mainEQTLCorr = new double[datasetGenotypes.nrProbes]; if (snpsToSwapFile != null) { System.out.println("Enforcing for every eQTL that the genotype dosage is swapped based on: " + snpsToSwapFile.getAbsolutePath()); HashSet<String> snpsToSwap = new HashSet<String>(); BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(snpsToSwapFile), "UTF-8")); String line;/*from ww w . j a v a 2 s. co m*/ while ((line = reader.readLine()) != null) { snpsToSwap.add(line); } reader.close(); for (int snp = 0; snp < datasetGenotypes.nrProbes; snp++) { if (snpsToSwap.contains(datasetGenotypes.probeNames[snp])) { for (int s = 0; s < datasetGenotypes.nrSamples; s++) { datasetGenotypes.rawData[snp][s] = 2 - datasetGenotypes.rawData[snp][s]; } } //mainEQTLCorr[snp] = corr; } } else { System.out.println( "Enforcing for every eQTL that the genotype dosage positively correlated with gene expression levels:"); Writer writer = new BufferedWriter(new FileWriter(outputDir + "/swappedDosages.txt")); for (int snp = 0; snp < datasetGenotypes.nrProbes; snp++) { double corr = JSci.maths.ArrayMath.correlation(datasetGenotypes.rawData[snp], datasetExpression.rawData[snp]); //System.out.println(datasetExpression.probeNames[snp] + "\t" + snp + "\t" + corr); if (corr < 0) { corr = -corr; for (int s = 0; s < datasetGenotypes.nrSamples; s++) { datasetGenotypes.rawData[snp][s] = 2 - datasetGenotypes.rawData[snp][s]; } writer.append(datasetGenotypes.probeNames[snp]); writer.append('\n'); } //mainEQTLCorr[snp] = corr; } writer.close(); } }
From source file:com.fizzed.rocker.compiler.JavaGenerator.java
private void createSourceTemplate(TemplateModel model, Writer w) throws GeneratorException, IOException { if (model.getOptions().getPostProcessing() != null) { // allow post-processors to transform the model try {//from w ww. ja va2 s.co m model = postProcess(model); } catch (PostProcessorException ppe) { throw new GeneratorException("Error during post-processing of model.", ppe); } } // Used to register any withstatements we encounter, so we can generate all dynamic consumers at the end. final WithStatementConsumerGenerator withStatementConsumerGenerator = new WithStatementConsumerGenerator(); // simple increment to help create unique var names int varCounter = -1; if (model.getPackageName() != null && !model.getPackageName().equals("")) { w.append("package ").append(model.getPackageName()).append(";").append(CRLF); } // imports regardless of template w.append(CRLF); w.append("import ").append(java.io.IOException.class.getName()).append(";").append(CRLF); w.append("import ").append(com.fizzed.rocker.ForIterator.class.getName()).append(";").append(CRLF); w.append("import ").append(com.fizzed.rocker.RenderingException.class.getName()).append(";").append(CRLF); w.append("import ").append(com.fizzed.rocker.RockerContent.class.getName()).append(";").append(CRLF); w.append("import ").append(com.fizzed.rocker.RockerOutput.class.getName()).append(";").append(CRLF); w.append("import ").append(com.fizzed.rocker.runtime.DefaultRockerTemplate.class.getName()).append(";") .append(CRLF); w.append("import ").append(com.fizzed.rocker.runtime.PlainTextUnloadedClassLoader.class.getName()) .append(";").append(CRLF); // template imports if (model.getImports().size() > 0) { for (JavaImport i : model.getImports()) { w.append("// import ").append(sourceRef(i)).append(CRLF); w.append("import ").append(i.getStatement()).append(";").append(CRLF); } } w.append(CRLF); w.append("/*").append(CRLF); w.append(" * Auto generated code to render template ").append(model.getPackageName().replace('.', '/')) .append("/").append(model.getTemplateName()).append(CRLF); w.append(" * Do not edit this file. Changes will eventually be overwritten by Rocker parser!").append(CRLF); w.append(" */").append(CRLF); int indent = 0; // MODEL CLASS // class definition tab(w, indent).append("public class ").append(model.getName()).append(" extends ") .append(model.getOptions().getExtendsModelClass()).append(" {").append(CRLF); indent++; w.append(CRLF); // static info about this template tab(w, indent).append("static public final ").append(ContentType.class.getCanonicalName()) .append(" CONTENT_TYPE = ").append(ContentType.class.getCanonicalName()).append(".") .append(model.getContentType().toString()).append(";").append(CRLF); tab(w, indent).append("static public final String TEMPLATE_NAME = \"").append(model.getTemplateName()) .append("\";").append(CRLF); tab(w, indent).append("static public final String TEMPLATE_PACKAGE_NAME = \"") .append(model.getPackageName()).append("\";").append(CRLF); tab(w, indent).append("static public final String HEADER_HASH = \"").append(model.createHeaderHash() + "") .append("\";").append(CRLF); // Don't include MODIFIED_AT header when optimized compiler is used since this implicitly disables hot reloading anyhow if (!model.getOptions().getOptimize()) { tab(w, indent).append("static public final long MODIFIED_AT = ").append(model.getModifiedAt() + "") .append("L;").append(CRLF); } tab(w, indent).append("static public final String[] ARGUMENT_NAMES = {"); StringBuilder argNameList = new StringBuilder(); for (Argument arg : model.getArgumentsWithoutRockerBody()) { if (argNameList.length() > 0) { argNameList.append(","); } argNameList.append(" \"").append(arg.getExternalName()).append("\""); } w.append(argNameList).append(" };").append(CRLF); // model arguments as members of model class appendArgumentMembers(model, w, "private", false, indent); // model setters & getters with builder-style pattern // special case for the RockerBody argument which sorta "hides" its getter/setter if (model.getArguments().size() > 0) { for (Argument arg : model.getArguments()) { // setter w.append(CRLF); tab(w, indent).append("public ").append(model.getName()).append(" ").append(arg.getExternalName()) .append("(" + arg.getExternalType()).append(" ").append(arg.getName()).append(") {") .append(CRLF); tab(w, indent + 1).append("this.").append(arg.getName()).append(" = ").append(arg.getName()) .append(";").append(CRLF); tab(w, indent + 1).append("return this;").append(CRLF); tab(w, indent).append("}").append(CRLF); // getter w.append(CRLF); tab(w, indent).append("public ").append(arg.getExternalType()).append(" ") .append(arg.getExternalName()).append("() {").append(CRLF); tab(w, indent + 1).append("return this.").append(arg.getName()).append(";").append(CRLF); tab(w, indent).append("}").append(CRLF); } } w.append(CRLF); // // model "template" static builder // tab(w, indent).append("static public ").append(model.getName()).append(" template("); if (model.getArguments().size() > 0) { int i = 0; // RockerBody is NOT included (it is passed via a closure block in other templates) // so we only care about the other arguments for (Argument arg : model.getArgumentsWithoutRockerBody()) { if (i != 0) { w.append(", "); } w.append(arg.getType()).append(" ").append(arg.getName()); i++; } } w.append(") {").append(CRLF); tab(w, indent + 1).append("return new ").append(model.getName()).append("()"); if (model.getArguments().size() > 0) { int i = 0; for (Argument arg : model.getArgumentsWithoutRockerBody()) { w.append(CRLF); tab(w, indent + 2).append(".").append(arg.getName()).append("(").append(arg.getName()).append(")"); i++; } } w.append(";").append(CRLF); tab(w, indent).append("}").append(CRLF); // // render of model // w.append(CRLF); tab(w, indent).append("@Override").append(CRLF); tab(w, indent).append("protected DefaultRockerTemplate buildTemplate() throws RenderingException {") .append(CRLF); if (model.getOptions().getOptimize()) { // model "template" static builder (not reloading support, fastest performance) tab(w, indent + 1).append("// optimized for performance (via rocker.optimize flag; no auto reloading)") .append(CRLF); tab(w, indent + 1).append("return new Template(this);").append(CRLF); //tab(w, indent+1).append("return template.__render(context);").append(CRLF); } else { tab(w, indent + 1).append( "// optimized for convenience (runtime auto reloading enabled if rocker.reloading=true)") .append(CRLF); // use bootstrap to create underlying template tab(w, indent + 1).append("return ").append(RockerRuntime.class.getCanonicalName()) .append(".getInstance().getBootstrap().template(this.getClass(), this);").append(CRLF); //tab(w, indent+1).append("return template.__render(context);").append(CRLF); } tab(w, indent).append("}").append(CRLF); // // TEMPLATE CLASS // w.append(CRLF); // class definition tab(w, indent).append("static public class Template extends ").append(model.getOptions().getExtendsClass()); w.append(" {").append(CRLF); indent++; // plain text -> map of chunks of text (Java only supports 2-byte length of string constant) LinkedHashMap<String, LinkedHashMap<String, String>> plainTextMap = model .createPlainTextMap(PLAIN_TEXT_CHUNK_LENGTH); if (!plainTextMap.isEmpty()) { w.append(CRLF); for (String plainText : plainTextMap.keySet()) { // include static text as comments in source (limit to 500) tab(w, indent).append("// ") .append(StringUtils.abbreviate(RockerUtil.ESCAPE_JAVA.translate(plainText), 500)) .append(CRLF); for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) { if (this.plainTextStrategy == PlainTextStrategy.STATIC_STRINGS) { tab(w, indent).append("static private final String ").append(chunk.getKey()).append(" = \"") .append(StringEscapeUtils.escapeJava(chunk.getValue())).append("\";").append(CRLF); } else if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) { tab(w, indent).append("static private final byte[] ").append(chunk.getKey()).append(";") .append(CRLF); } } } // generate the static initializer if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) { w.append(CRLF); tab(w, indent).append("static {").append(CRLF); String loaderClassName = unqualifiedClassName(PlainTextUnloadedClassLoader.class); tab(w, indent + 1).append(loaderClassName).append(" loader = ").append(loaderClassName) .append(".tryLoad(").append(model.getName()).append(".class.getClassLoader(), ") .append(model.getName()).append(".class.getName()").append(" + \"$PlainText\", \"") .append(model.getOptions().getTargetCharset()).append("\");").append(CRLF); for (String plainText : plainTextMap.keySet()) { for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) { if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) { tab(w, indent + 1).append(chunk.getKey()).append(" = loader.tryGet(\"") .append(chunk.getKey()).append("\");").append(CRLF); } } } tab(w, indent).append("}").append(CRLF); } } // arguments as members of template class appendArgumentMembers(model, w, "protected", true, indent); w.append(CRLF); // constructor tab(w, indent).append("public Template(").append(model.getName()).append(" model) {").append(CRLF); tab(w, indent + 1).append("super(model);").append(CRLF); tab(w, indent + 1).append("__internal.setCharset(\"").append(model.getOptions().getTargetCharset()) .append("\");").append(CRLF); tab(w, indent + 1).append("__internal.setContentType(CONTENT_TYPE);").append(CRLF); tab(w, indent + 1).append("__internal.setTemplateName(TEMPLATE_NAME);").append(CRLF); tab(w, indent + 1).append("__internal.setTemplatePackageName(TEMPLATE_PACKAGE_NAME);").append(CRLF); // each model argument passed along as well for (Argument arg : model.getArguments()) { tab(w, indent + 1).append("this.").append(arg.getName()).append(" = model.") .append(arg.getExternalName()).append("();").append(CRLF); } tab(w, indent).append("}").append(CRLF); w.append(CRLF); tab(w, indent).append("@Override").append(CRLF); tab(w, indent).append("protected void __doRender() throws IOException, RenderingException {").append(CRLF); // build rendering code int depth = 1; Deque<String> blockEnd = new ArrayDeque<>(); for (TemplateUnit unit : model.getUnits()) { if (unit instanceof Comment) { continue; } // something like // IfBeginBlock // __internal.aboutToExecutePosInSourceTemplate(5, 10); appendCommentAndSourcePositionUpdate(w, depth + indent, unit); if (unit instanceof PlainText) { PlainText plain = (PlainText) unit; LinkedHashMap<String, String> chunks = plainTextMap.get(plain.getText()); for (String chunkName : chunks.keySet()) { tab(w, depth + indent).append("__internal.writeValue(").append(chunkName).append(");") .append(CRLF); } } else if (unit instanceof ValueExpression) { ValueExpression value = (ValueExpression) unit; tab(w, depth + indent).append("__internal.renderValue(").append(value.getExpression()).append(", ") .append("" + value.isNullSafe()).append(");").append(CRLF); } else if (unit instanceof NullTernaryExpression) { NullTernaryExpression nullTernary = (NullTernaryExpression) unit; tab(w, depth + indent).append("{").append(CRLF); tab(w, depth + indent + 1).append("final Object __v = ").append(nullTernary.getLeftExpression()) .append(";").append(CRLF); tab(w, depth + indent + 1).append("if (__v != null) { __internal.renderValue(__v, false); }") .append(CRLF); if (nullTernary.getRightExpression() != null) { tab(w, depth + indent + 1).append("else {__internal.renderValue(") .append(nullTernary.getRightExpression()).append(", true); }").append(CRLF); } tab(w, depth + indent).append("}").append(CRLF); } else if (unit instanceof ValueClosureBegin) { ValueClosureBegin closure = (ValueClosureBegin) unit; tab(w, depth + indent).append("__internal.renderValue(").append(closure.getExpression()) .append(".__body("); // Java 1.8+ use lambda if (isJava8Plus(model)) { w.append("() -> {").append(CRLF); depth++; blockEnd.push("}), false);"); } // Java 1.7- uses anonymous inner class else { w.append("new ").append(unqualifiedClassName(RockerContent.class)).append("() {").append(CRLF); depth++; blockEnd.push("}), false);"); tab(w, depth + indent).append("@Override").append(CRLF); tab(w, depth + indent).append("public void render() throws IOException, RenderingException {") .append(CRLF); depth++; blockEnd.push("}"); } } else if (unit instanceof ValueClosureEnd) { // Java 1.8+ use lambda if (isJava8Plus(model)) { depth--; tab(w, depth + indent).append(blockEnd.pop()).append(" // value closure end ") .append(sourceRef(unit)).append(CRLF); } // Java 1.7- uses anonymous inner class else { depth--; tab(w, depth + indent).append(blockEnd.pop()).append(CRLF); depth--; tab(w, depth + indent).append(blockEnd.pop()).append(" // value closure end ") .append(sourceRef(unit)).append(CRLF); } } else if (unit instanceof ContentClosureBegin) { ContentClosureBegin closure = (ContentClosureBegin) unit; tab(w, depth + indent).append("RockerContent ").append(closure.getIdentifier()).append(" = "); // Java 1.8+ use lambda if (isJava8Plus(model)) { w.append("() -> {").append(CRLF); depth++; blockEnd.push("};"); } // Java 1.7- uses anonymous inner class else { w.append("new ").append(unqualifiedClassName(com.fizzed.rocker.RockerContent.class)) .append("() {").append(CRLF); depth++; blockEnd.push("};"); tab(w, depth + indent).append("@Override").append(CRLF); tab(w, depth + indent).append("public void render() throws IOException, RenderingException {") .append(CRLF); depth++; blockEnd.push("}"); } } else if (unit instanceof ContentClosureEnd) { // Java 1.8+ use lambda if (isJava8Plus(model)) { depth--; tab(w, depth + indent).append(blockEnd.pop()).append(" // content closure end ") .append(sourceRef(unit)).append(CRLF); } // Java 1.7- uses anonymous inner class else { depth--; tab(w, depth + indent).append(blockEnd.pop()).append(CRLF); depth--; tab(w, depth + indent).append(blockEnd.pop()).append(" // content closure end ") .append(sourceRef(unit)).append(CRLF); } } else if (unit instanceof IfBlockBegin) { IfBlockBegin block = (IfBlockBegin) unit; tab(w, depth + indent).append("if ").append(block.getExpression()).append(" {").append(CRLF); blockEnd.push("}"); depth++; } else if (unit instanceof IfBlockElseIf) { final IfBlockElseIf block = (IfBlockElseIf) unit; depth--; // This keeps else-if nicely formatted in generated code. tab(w, depth + indent).append("} else if ").append(block.getExpression()).append(" {").append(CRLF); depth++; } else if (unit instanceof IfBlockElse) { depth--; tab(w, depth + indent).append("} else {").append(" // else ").append(sourceRef(unit)).append(CRLF); depth++; } else if (unit instanceof IfBlockEnd) { depth--; tab(w, depth + indent).append(blockEnd.pop()).append(" // if end ").append(sourceRef(unit)) .append(CRLF); } else if (unit instanceof WithBlockBegin) { WithBlockBegin block = (WithBlockBegin) unit; WithStatement stmt = block.getStatement(); String statementConsumerName = withStatementConsumerGenerator.register(stmt); final List<WithStatement.VariableWithExpression> variables = stmt.getVariables(); if (isJava8Plus(model)) { tab(w, depth + indent) .append(variables.size() == 1 ? qualifiedClassName(WithBlock.class) : WithStatementConsumerGenerator.WITH_BLOCKS_GENERATED_CLASS_NAME) .append(".with("); // All expressions for (int i = 0; i < variables.size(); i++) { final WithStatement.VariableWithExpression var = variables.get(i); if (i > 0) { w.append(", "); } w.append(var.getValueExpression()); } w.append(", ").append(stmt.isNullSafe() + "").append(", ("); for (int i = 0; i < variables.size(); i++) { final WithStatement.VariableWithExpression var = variables.get(i); if (i > 0) { w.append(", "); } w.append(var.getVariable().getName()); } w.append(") -> {").append(CRLF); depth++; blockEnd.push("});"); } else { tab(w, depth + indent) // Note for standard 1 variable with block we use the predefined consumers. // Otherwise we fallback to the generated ones. .append(variables.size() == 1 ? qualifiedClassName(WithBlock.class) : WithStatementConsumerGenerator.WITH_BLOCKS_GENERATED_CLASS_NAME) .append(".with("); // All expressions for (int i = 0; i < variables.size(); i++) { final WithStatement.VariableWithExpression var = variables.get(i); if (i > 0) { w.append(", "); } w.append(var.getValueExpression()); } w.append(", ").append(stmt.isNullSafe() + "").append(", (new ").append(statementConsumerName) .append('<'); // Types for the .with(..) for (int i = 0; i < variables.size(); i++) { final JavaVariable variable = variables.get(i).getVariable(); if (i > 0) { w.append(", "); } w.append(variable.getType()); } w.append(">() {").append(CRLF); tab(w, depth + indent + 1).append("@Override public void accept("); for (int i = 0; i < variables.size(); i++) { final JavaVariable variable = variables.get(i).getVariable(); if (i > 0) { w.append(", "); } w.append("final ").append(variable.toString()); } w.append(") throws IOException {").append(CRLF); depth++; blockEnd.push("}}));"); } } else if (unit instanceof WithBlockElse) { depth--; if (isJava8Plus(model)) { tab(w, depth + indent).append("}, () -> {").append(CRLF); } else { tab(w, depth + indent).append("}}), (new ") .append(qualifiedClassName(WithBlock.Consumer0.class)).append("() { ") .append("@Override public void accept() throws IOException {").append(CRLF); } depth++; } else if (unit instanceof WithBlockEnd) { depth--; tab(w, depth + indent).append(blockEnd.pop()).append(" // with end ").append(sourceRef(unit)) .append(CRLF); } else if (unit instanceof ForBlockBegin) { ForBlockBegin block = (ForBlockBegin) unit; ForStatement stmt = block.getStatement(); // break support via try and catch mechanism (works across lambdas!) tab(w, depth + indent).append("try {").append(CRLF); depth++; if (stmt.getForm() == ForStatement.Form.GENERAL) { // print out raw statement including parentheses tab(w, depth + indent).append("for ").append(block.getExpression()).append(" {").append(CRLF); blockEnd.push("}"); } else if (stmt.getForm() == ForStatement.Form.ENHANCED) { // Java 1.8+ (use lambdas) if (stmt.hasAnyUntypedArguments() && isJava8Plus(model)) { // build list of lambda vars String localVars = ""; for (JavaVariable arg : stmt.getArguments()) { if (localVars.length() != 0) { localVars += ","; } localVars += arg.getName(); } tab(w, depth + indent).append(Java8Iterator.class.getName()).append(".forEach(") .append(stmt.getValueExpression()).append(", (").append(localVars).append(") -> {") .append(CRLF); blockEnd.push("});"); } else { // is the first argument a "ForIterator" ? boolean forIterator = isForIteratorType(stmt.getArguments().get(0).getType()); int collectionCount = (forIterator ? 2 : 1); int mapCount = (forIterator ? 3 : 2); // type and value we are going to iterate thru String iterateeType = null; String valueExpression = null; if (stmt.getArguments().size() == collectionCount) { iterateeType = stmt.getArguments().get(collectionCount - 1).getTypeAsNonPrimitiveType(); valueExpression = stmt.getValueExpression(); } else if (stmt.getArguments().size() == mapCount) { iterateeType = "java.util.Map.Entry<" + stmt.getArguments().get(mapCount - 2).getTypeAsNonPrimitiveType() + "," + stmt.getArguments().get(mapCount - 1).getTypeAsNonPrimitiveType() + ">"; valueExpression = stmt.getValueExpression() + ".entrySet()"; } // create unique variable name for iterator String forIteratorVarName = "__forIterator" + (++varCounter); // ForIterator for collection and make it final to assure nested anonymous // blocks can access it as well. tab(w, depth + indent).append("final ") .append(com.fizzed.rocker.runtime.CollectionForIterator.class.getName()).append("<") .append(iterateeType).append(">").append(" ").append(forIteratorVarName) .append(" = new ") .append(com.fizzed.rocker.runtime.CollectionForIterator.class.getName()).append("<") .append(iterateeType).append(">").append("(").append(valueExpression).append(");") .append(CRLF); // for loop same regardless of map vs. collection tab(w, depth + indent).append("while (").append(forIteratorVarName).append(".hasNext()) {") .append(CRLF); // if forIterator request assign to local var and make it final to assure nested anonymous // blocks can access it as well. if (forIterator) { tab(w, depth + indent + 1).append("final ") .append(com.fizzed.rocker.ForIterator.class.getName()).append(" ") .append(stmt.getArguments().get(0).getName()).append(" = ") .append(forIteratorVarName).append(";").append(CRLF); } if (stmt.getArguments().size() == collectionCount) { // assign item to local var and make it final to assure nested anonymous // blocks can access it as well. tab(w, depth + indent + 1).append("final ") .append(stmt.getArguments().get(collectionCount - 1).toString()).append(" = ") .append(forIteratorVarName).append(".next();").append(CRLF); } else if (stmt.getArguments().size() == mapCount) { // create unique variable name for iterator String entryVarName = "__entry" + (++varCounter); // assign map entry to local var tab(w, depth + indent + 1).append("final ").append(iterateeType).append(" ") .append(entryVarName).append(" = ").append(forIteratorVarName) .append(".next();").append(CRLF); // assign entry to local values make it final to assure nested anonymous // blocks can access it as well. tab(w, depth + indent + 1).append("final ") .append(stmt.getArguments().get(mapCount - 2).toString()).append(" = ") .append(entryVarName).append(".getKey();").append(CRLF); tab(w, depth + indent + 1).append("final ") .append(stmt.getArguments().get(mapCount - 1).toString()).append(" = ") .append(entryVarName).append(".getValue();").append(CRLF); } else { throw new GeneratorException("Unsupported number of arguments for for loop"); } blockEnd.push("}"); } } depth++; // continue support via try and catch mechanism (works across lambdas!) tab(w, depth + indent).append("try {").append(CRLF); depth++; } else if (unit instanceof ForBlockEnd) { depth--; // continue support via try and catch mechanism (works across lambdas!) tab(w, depth + indent).append("} catch (").append(ContinueException.class.getCanonicalName()) .append(" e) {").append(CRLF); tab(w, depth + indent + 1).append("// support for continuing for loops").append(CRLF); tab(w, depth + indent).append("}").append(CRLF); depth--; tab(w, depth + indent).append(blockEnd.pop()).append(" // for end ").append(sourceRef(unit)) .append(CRLF); depth--; // break support via try and catch mechanism (works across lambdas!) tab(w, depth + indent).append("} catch (").append(BreakException.class.getCanonicalName()) .append(" e) {").append(CRLF); tab(w, depth + indent + 1).append("// support for breaking for loops").append(CRLF); tab(w, depth + indent).append("}").append(CRLF); } else if (unit instanceof BreakStatement) { tab(w, depth + indent).append("__internal.throwBreakException();").append(CRLF); } else if (unit instanceof ContinueStatement) { tab(w, depth + indent).append("__internal.throwContinueException();").append(CRLF); } //log.info(" src (@ {}): [{}]", unit.getSourceRef(), unit.getSourceRef().getConsoleFriendlyText()); } // end of render() tab(w, indent).append("}").append(CRLF); indent--; // end of template class tab(w, indent).append("}").append(CRLF); // Generate class with all gathered consumer interfaces for all withblocks withStatementConsumerGenerator.generate(this, w); if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS && !plainTextMap.isEmpty()) { w.append(CRLF); tab(w, indent).append("private static class PlainText {").append(CRLF); w.append(CRLF); for (String plainText : plainTextMap.keySet()) { for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) { tab(w, indent + 1).append("static private final String ").append(chunk.getKey()).append(" = \"") .append(StringEscapeUtils.escapeJava(chunk.getValue())).append("\";").append(CRLF); } } w.append(CRLF); tab(w, indent).append("}").append(CRLF); } w.append(CRLF); w.append("}").append(CRLF); }
From source file:org.netbeans.util.source.minify.MinifyUtil.java
public MinifyFileResult compressHtml(String inputFilename, String outputFilename, MinifyProperty minifyProperty) throws IOException { InputStreamReader in = null;// www .j a v a 2 s . c o m Writer out = null; MinifyFileResult minifyFileResult = new MinifyFileResult(); try { File inputFile = new File(inputFilename); File outputFile = new File(outputFilename); in = new InputStreamReader(new FileInputStream(inputFile), minifyProperty.getCharset()); minifyFileResult.setInputFileSize(inputFile.length()); HtmlCompressor compressor = new HtmlCompressor(); compressor.setRemoveIntertagSpaces(true); compressor.setCompressCss(minifyProperty.isBuildInternalCSSMinify()); //compress inline css compressor.setCompressJavaScript(minifyProperty.isBuildInternalJSMinify()); //compress inline javascript compressor.setYuiJsNoMunge(!minifyProperty.isJsObfuscate()); // compressor.setRemoveQuotes(true); //false may in feature //removes unnecessary tag attribute quotes //compressor.setSimpleDoctype(true); //false may in feature //simplify existing doctype //compressor.setRemoveComments(true); //false may in feature //if false keeps HTML comments (default is true) //compressor.setSimpleBooleanAttributes(true); //false may in feature //remove values from boolean tag attributes //compressor.setPreserveLineBreaks(true); //preserves original line breaks String output = compressor.compress(fromStream(in));//out, minifyProperty.getLineBreakPosition()); in.close(); in = null; out = new OutputStreamWriter(new FileOutputStream(outputFile), minifyProperty.getCharset()); out.write(output); out.flush(); minifyFileResult.setOutputFileSize(outputFile.length()); if (minifyProperty.isAppendLogToFile()) { out.append("\n<!--Size: " + minifyFileResult.getInputFileSize() + "=>" + minifyFileResult.getOutputFileSize() + "Bytes " + "\n Saved " + minifyFileResult.getSavedPercentage() + "%-->"); } out.flush(); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } return minifyFileResult; }
From source file:tudarmstadt.lt.ABSentiment.training.util.ProblemBuilder.java
protected static INDArray classifyTestSet(String inputFile, Model model, Vector<FeatureExtractor> features, String predictionFile, String type, boolean printResult) { InputReader fr;/*from ww w.jav a 2s . com*/ if (inputFile.endsWith("xml")) { fr = new XMLReader(inputFile); } else { fr = new TsvReader(inputFile); } Writer out = null; Writer featureOut = null; try { OutputStream predStream = new FileOutputStream(predictionFile); out = new OutputStreamWriter(predStream, "UTF-8"); if (featureOutputFile != null) { OutputStream vectorStream = new FileOutputStream(featureOutputFile); featureOut = new OutputStreamWriter(vectorStream, "UTF-8"); } } catch (FileNotFoundException | UnsupportedEncodingException e1) { e1.printStackTrace(); System.exit(1); } Feature[] instance; Vector<Feature[]> instanceFeatures; confusionMatrix = new ConfusionMatrix(); String item; for (int j = 0; j < model.getNrClass(); j++) { item = labelLookup.get(Integer.parseInt(model.getLabels()[j] + "")); confusionMatrix.addLabel(item); allLabels.add(item); } ArrayList<double[]> probability = new ArrayList<>(); confusionMatrix.createMatrix(); for (Document doc : fr) { for (Sentence sentence : doc.getSentences()) { int i = 0; preprocessor.processText(sentence.getText()); instanceFeatures = applyFeatures(preprocessor.getCas(), features); Double prediction; instance = combineInstanceFeatures(instanceFeatures); double[] prob_estimates = new double[model.getNrClass()]; prediction = Linear.predictProbability(model, instance, prob_estimates); probability.add(prob_estimates); try { out.write(sentence.getId() + "\t" + sentence.getText() + "\t"); String goldLabel = null; String predictedLabel = labelLookup.get(prediction.intValue()); if (type.compareTo("relevance") == 0) { goldLabel = sentence.getRelevance()[0]; confusionMatrix.updateMatrix(predictedLabel, goldLabel); } else if (type.compareTo("sentiment") == 0) { try { while (i < sentence.getSentiment().length) { goldLabel = sentence.getSentiment()[i++]; confusionMatrix.updateMatrix(predictedLabel, goldLabel); } } catch (NoSuchFieldException e) { } } else if (useCoarseLabels) { out.append(StringUtils.join(sentence.getAspectCategoriesCoarse(), " ")); goldLabel = StringUtils.join(sentence.getAspectCategoriesCoarse(), " "); confusionMatrix.updateMatrix(predictedLabel, goldLabel); } else { out.append(StringUtils.join(sentence.getAspectCategories(), " ")); goldLabel = StringUtils.join(sentence.getAspectCategories(), " "); confusionMatrix.updateMatrix(predictedLabel, goldLabel); } out.append("\t").append(labelLookup.get(prediction.intValue())).append("\n"); } catch (IOException e) { e.printStackTrace(); } if (featureOutputFile != null) { String[] labels = sentence.getAspectCategories(); if (useCoarseLabels) { labels = sentence.getAspectCategoriesCoarse(); } for (String label : labels) { try { assert featureOut != null; for (Feature f : instance) { featureOut.write(" " + f.getIndex() + ":" + f.getValue()); } featureOut.write("\n"); } catch (IOException e) { e.printStackTrace(); } } } } } INDArray classificationProbability = Nd4j.zeros(probability.size(), model.getNrClass()); int j = -1; for (double prob_estimates[] : probability) { classificationProbability.putRow(++j, Nd4j.create(prob_estimates)); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } HashMap<String, Float> recall; HashMap<String, Float> precision; HashMap<String, Float> fMeasure; recall = getRecallForAll(); precision = getPrecisionForAll(); fMeasure = getFMeasureForAll(); if (printResult) { System.out.println("Label" + "\t" + "Recall" + "\t" + "Precision" + "\t" + "F Score"); for (String itemLabel : allLabels) { System.out.println(itemLabel + "\t" + recall.get(itemLabel) + "\t" + precision.get(itemLabel) + "\t" + fMeasure.get(itemLabel)); } printFeatureStatistics(features); printConfusionMatrix(); System.out.println("\n"); System.out.println("True positive : " + getTruePositive()); System.out.println("Accuracy : " + getOverallAccuracy()); System.out.println("Overall Precision : " + getOverallPrecision()); System.out.println("Overall Recall : " + getOverallRecall()); System.out.println("Overall FMeasure : " + getOverallFMeasure()); } return classificationProbability; }
From source file:org.joget.apps.app.service.AppServiceImpl.java
/** * Generate Message Bundle PO file to OutputStream * @param appId/* w ww .ja v a 2s. co m*/ * @param version * @param locale * @param output * @throws IOException */ public void generatePO(String appId, String version, String locale, OutputStream output) throws IOException { Writer writer = new OutputStreamWriter(output, "UTF-8"); try { writer.append("# This file was generated by Kecak Workflow\r\n"); writer.append("# http://www.kecak.net\r\n"); writer.append("msgid \"\"\r\n"); writer.append("msgstr \"\"\r\n"); writer.append("\"Content-Type: text/plain; charset=utf-8\\n\"\r\n"); writer.append("\"Content-Transfer-Encoding: 8bit\\n\"\r\n"); writer.append("\"Project-Id-Version: " + appId + "\\n\"\r\n"); writer.append("\"POT-Creation-Date: \\n\"\r\n"); writer.append("\"PO-Revision-Date: \\n\"\r\n"); writer.append("\"Last-Translator: \\n\"\r\n"); writer.append("\"Language-Team: \\n\"\r\n"); writer.append("\"Language: " + locale + "\\n\"\r\n"); writer.append("\"MIME-Version: 1.0\\n\"\r\n\r\n"); Map<String, String> messages = getMessages(appId, version, locale); for (String key : messages.keySet()) { String value = messages.get(key); writer.append("msgid \"" + key + "\"\r\n"); writer.append("msgstr \"" + value + "\"\r\n"); } } catch (Exception e) { LogUtil.error(AppServiceImpl.class.getName(), e, "Error generating PO file for " + appId); } finally { writer.flush(); writer.close(); } }