public static void main(String[] args) throws Exception { /* You should do this ONLY ONCE in the whole application life-cycle: */ /* Create and adjust the configuration singleton */ Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); cfg.setDirectoryForTemplateLoading(new File("/Users/ian.goldsmith/projects/freemarker")); cfg.setDefaultEncoding("UTF-8"); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); /* * You usually do these for MULTIPLE TIMES in the application * life-cycle: */ /* Create a data-model */ Map message = new HashMap(); message.put( "contentAsXml", freemarker.ext.dom.NodeModel.parse( new File("/Users/ian.goldsmith/projects/freemarker/test.xml"))); Map root = new HashMap(); root.put("message", message); /* Get the template (uses cache internally) */ Template temp = cfg.getTemplate("testxml.ftl"); /* Merge data-model with template */ Writer out = new OutputStreamWriter(System.out); temp.process(root, out); // Note: Depending on what `out` is, you may need to call `out.close()`. // This is usually the case for file output, but not for servlet output. }
/** generate delivery items and give them to the player */ protected void makeDeliveryItems() { Long playerOid = getPlayerOid(); Long bagOid = playerOid; if (Log.loggingDebug) log.debug("makeDeliveryItems: playerOid " + playerOid + ", bagOid + " + bagOid); // Normally the persistence flag is inherited from the enclosing // object, but all we have are OIDs. Assume this is only used // for players and players are always persistent. Template overrideTemplate = new Template(); overrideTemplate.put(Namespace.OBJECT_MANAGER, ObjectManagerClient.TEMPL_PERSISTENT, true); for (String templateName : deliveryItems) { Long itemOid = ObjectManagerClient.generateObject(templateName, overrideTemplate); InventoryClient.addItem(bagOid, playerOid, bagOid, itemOid); } }
@Override public Node execute(ExecutionContext ec, Template tmpl) throws IOException { // {{{ Map<String, StartNamedBlock> blocks = tmpl.getBlocks(); Node n = blocks.get(this.blockName); if (blocks.containsKey(this.blockName)) { return blocks.get(this.blockName).getExecuteNode(); } else { return this.executeNode; } // return this.getNextNode(); } // }}}
@Override protected void writePlans(String dir) { Properties p = new Properties(); p.setProperty("resource.loader", "class"); p.setProperty( "class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); Velocity.init(p); Context context = new VelocityContext(); context.put("plans", plans); Template template = Velocity.getTemplate("pl/poznan/put/transims/demand/matsim/plans.xml.vm"); File planFile = new File(dir + "\\plans.xml"); try (Writer writer = new BufferedWriter(new FileWriter(planFile))) { template.merge(context, writer); } catch (IOException e) { throw new RuntimeException(e); } }
/** This replaces the user input variables in the XML template * */ public String replaceVariablesInString(String xmlStringArg) { String modXmlString = xmlStringArg; Properties prop = new Properties(); for (int i = 0; i < uiList.size(); i++) { UserInput ui = (UserInput) uiList.get(i); XmlUIElement el = (XmlUIElement) uiElementsList.get(i); ui.setValue(el.getValue()); prop.put("$UserInput$" + ui.getID(), el.getValue()); // modXmlString = StringUtil.replaceStringBySpecifiedString(modXmlString,"$UserInput$" + // ui.getID(),el.getValue()); } Template template = null; try { template = new Template(xmlStringArg); // template = PopulateTemplateParams.substituteParams(template, prop, 3); template = PopulateTemplateParams.substituteParams(template, prop); } catch (Exception exc) { exc.printStackTrace(); } // return modXmlString; return template.toString(); }
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Map root = new HashMap(); Connection conn = null; conn = ConFact.getInstance().makeConnection(); String id = req.getParameter("id"); String sql = "select title,description,language_id from film where film_id=" + id + ";"; java.sql.PreparedStatement pst; try { pst = conn.prepareStatement(sql); ResultSet rs = pst.executeQuery(); while (rs.next()) { java.sql.PreparedStatement pst1; String sql1 = "select name from language where language_id=" + rs.getString(3) + ";"; pst1 = conn.prepareStatement(sql1); ResultSet rs1 = pst1.executeQuery(); while (rs1.next()) { Template t = cfg.getTemplate("test.ftl"); root.put("title", rs.getString(1)); root.put("description", rs.getString(2)); root.put("language", rs1.getString(1)); resp.setContentType("text/html; charset=" + t.getEncoding()); Writer out = resp.getWriter(); t.process(root, out); } rs1.close(); } rs.close(); pst.close(); conn.close(); } catch (TemplateException e) { e.printStackTrace(); } catch (SQLException e1) { e1.printStackTrace(); } }
/** * Prepends the value of the specified Template to the begining of this Template. * * @param hout the Template */ public Template prepend(Template hout) { if (hout != null) m_template = new StringBuffer(hout.toString() + m_template.toString()); return this; }
/** * Appends the value of the specified Template to the end of this Template. * * @param hout the Template */ public Template append(Template hout) { if (hout != null) m_template = m_template.append(hout.toString()); return this; }
public void setLastTag(String tag, Template tagvalue) { setLastTag(tag, tagvalue.toString()); }
/** * Constructs a new Template with the specified template. * * @param templ the Template */ public Template(Template templ) { m_template = new StringBuffer(templ.toString()); m_template2 = new StringBuffer(templ.toString()); }