@Override public void render(PetriNet pn, Writer writer) throws RenderException, IOException { verifyNet(pn); STGroup group = new STGroupFile("uniol/apt/io/renderer/impl/BagginsPN.stg", '$', '$'); ST pnTemplate = group.getInstanceOf("pn"); pnTemplate.add("name", pn.getName()); // Handle places pnTemplate.add("places", pn.getPlaces()); List<Place> placeList = new ArrayList<>(pn.getPlaces()); // Handle transitions (and arcs) for (Transition t : pn.getTransitions()) { List<IdWeightPair> preset = new ArrayList<>(); List<IdWeightPair> postset = new ArrayList<>(); for (Flow e : t.getPresetEdges()) { preset.add(new IdWeightPair(placeList.indexOf(e.getSource()), -e.getWeight())); } for (Flow e : t.getPostsetEdges()) { postset.add(new IdWeightPair(placeList.indexOf(e.getTarget()), e.getWeight())); } pnTemplate.addAggr("transitions.{transition, preset, postset}", t, preset, postset); } pnTemplate.write(new AutoIndentWriter(writer), new ThrowingErrorListener()); }
// output dir is a function of where the original file/module was located. @Override protected void write( @NotNull ModuleIdentifier moduleIdentifier, @NotNull ST code, @NotNull String outputFileName) { try { Writer w = compiler.getOutputFileWriter( moduleIdentifier, outputFileName, new Function<String, File>() { @Override public File apply(String s) { Path p = moduleIdentifier.getPathRelativeToRootDir(); String outputDir = compiler.outputDirectory; if (compiler.outputDirectory.equals(".")) outputDir = "out"; return new File(outputDir, p.getParent().toString()); } }); STWriter wr = new AutoIndentWriter(w); wr.setLineWidth(80); code.write(wr); w.close(); } catch (IOException ioe) { compiler.errMgr.toolError(ErrorKind.CANNOT_WRITE_FILE, ioe, outputFileName); } }
@Test public void testNewlineNormalizationInTemplateStringPC() throws Exception { ST st = new ST("Foo\r\n" + "Bar\n"); StringWriter sw = new StringWriter(); st.write(new AutoIndentWriter(sw, "\r\n")); // force \r\n as newline String result = sw.toString(); String expecting = "Foo\r\nBar\r\n"; // expect \r\n in output assertEquals(expecting, result); }
@Test public void testLineBreakNoWhiteSpace() throws Exception { ST st = new ST("Foo <\\\\>" + newline + "bar\n"); StringWriter sw = new StringWriter(); st.write(new AutoIndentWriter(sw, "\n")); // force \n as newline String result = sw.toString(); String expecting = "Foo bar\n"; assertEquals(expecting, result); }
@Test public void testNewlineNormalizationInAttribute() throws Exception { ST st = new ST("Foo\r\n" + "<name>\n"); st.add("name", "a\nb\r\nc"); StringWriter sw = new StringWriter(); st.write(new AutoIndentWriter(sw, "\n")); // force \n as newline String result = sw.toString(); String expecting = "Foo\na\nb\nc\n"; // expect \n in output assertEquals(expecting, result); }
/** * Render the fully-initialized template into the given Writer. The line width will be set to * {@value #LINE_WIDTH}. The Writer will be closed by this method. */ protected void renderTemplate(ST template, Writer out) throws IOException { AutoIndentWriter writer = new AutoIndentWriter(out); writer.setLineWidth(LINE_WIDTH); template.write(writer); out.close(); }