@Test(groups = "fast") public void testCheckQueries() throws Exception { final STGroup kombucha = new STGroupFile(this.getClass().getResource("/org/killbill/billing/util/dao/Kombucha.sql.stg"), "UTF-8", '<', '>'); Assert.assertEquals(kombucha.getInstanceOf("isItTimeForKombucha").render(), "select hour(current_timestamp(0)) < 24 as is_time;"); assertPattern(kombucha.getInstanceOf("getById").render(), "select\r?\n" + " t.record_id\r?\n" + ", t.id\r?\n" + "and t.tenant_record_id = :tenantRecordId\r?\n" + ";"); assertPattern(kombucha.getInstanceOf("getByRecordId").render(), "select\r?\n" + " t.record_id\r?\n" + ", t.id\r?\n" +
private static STGroup readStringTemplateGroup(ClassLoader classLoader, String path) { try { URL resource = classLoader.getResource(path); STGroupFile group = new STGroupFile(resource, StandardCharsets.UTF_8.name(), '<', '>'); group.load(); return group; } catch (Exception e) { throw new RuntimeException("Unable to read StringTemplate group file at " + path + " on classpath", e); } } }
this.templateFile = new STGroupFile(this.url, "UTF-8", '<', '>'); this.templateFile.setListener(this.stErrorListener); this.templateFile.registerRenderer(String.class, new IntegerOrStringRenderer()); if (this.templateFile.isDefined("fileName")) FileUtils.createDirectory(targetPath); boolean hasTwoPrimitives = this.templateFile.isDefined("hasTwoPrimitives") && Boolean.valueOf(this.templateFile.getInstanceOf("hasTwoPrimitives").render()); boolean skipBoolean = this.templateFile.isDefined("skipBoolean") && Boolean.valueOf(this.templateFile.getInstanceOf("skipBoolean").render()); boolean skipBooleanKeys = this.templateFile.isDefined("skipBooleanKeys") && Boolean.valueOf(this.templateFile.getInstanceOf("skipBooleanKeys").render());
private ST findTemplate(String templateName) { ST template = this.templateFile.getInstanceOf(templateName); if (template == null) { throw new RuntimeException("Could not find template " + templateName + " in " + this.templateFile.getFileName()); } return template; }
private String renderMethod(Method method, boolean offsets) { ST m = jTemplates.getInstanceOf("f2jImplMethod"); m.add("returns", method.getReturnType()); m.add("method", method.getName()); m.add("paramTypes", getNetlibJavaParameterTypes(method, offsets)); m.add("paramNames", getNetlibJavaParameterNames(method, offsets)); m.add("impl", method.getDeclaringClass().getCanonicalName() + "." + method.getName()); m.add("calls", getF2jJavaParameters(method, offsets)); if (method.getReturnType().equals(Void.TYPE)) m.add("return", ""); return m.render(); }
public static void main(String[] args) throws IOException { if (args.length != 1) { System.err.println("Usage: java org.jf.dexlib2.AccessorTestGenerator <output_file>"); } URL stgUrl = AccessorTestGenerator.class.getClassLoader().getResource("AccessorTest.stg"); STGroupFile stg = new STGroupFile(stgUrl, "utf-8", '<', '>'); ST fileSt = stg.getInstanceOf("file"); fileSt.add("types", types); PrintWriter w = null; try { w = new PrintWriter(new BufferedWriter(new FileWriter(args[0]))); w.print(fileSt.render()); } finally { if (w != null) { w.close(); } } } }
public static void test3() throws IOException { String templates = "main() ::= <<\n" + "Foo: <{bar};format=\"lower\">\n" + ">>\n"; String tmpdir = System.getProperty("java.io.tmpdir"); writeFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(tmpdir+"/"+"t.stg"); ST st = group.getInstanceOf("main"); st.inspect(); }
STGroupFile stg = new STGroupFile("MyTemplate.stg"); ST templ = stg.getInstanceOf("myTemplate"); templ.add("f", new MyClass());
private void setTest() { this.isTest = this.templateFile.getInstanceOf("isTest") == null ? false : Boolean.valueOf(this.templateFile.getInstanceOf("isTest").render()); }
public void loadPrecRuleTemplates() { String templateGroupFile = "org/antlr/v4/tool/templates/LeftRecursiveRules.stg"; recRuleTemplates = new STGroupFile(templateGroupFile); if ( !recRuleTemplates.isDefined("recRule") ) { tool.errMgr.toolError(ErrorType.MISSING_CODE_GEN_TEMPLATES, "LeftRecursiveRules"); } // use codegen to get correct language templates; that's it though CodeGenerator gen = new CodeGenerator(tool, null, language); codegenTemplates = gen.getTemplates(); }
public void execute() { STGroup targetGroup = new STGroupFile(runtimeTemplates.getPath()); targetGroup.registerModelAdaptor(STGroup.class, new STGroupModelAdaptor()); targetGroup.registerRenderer(String.class, new StringRenderer(), true); targetGroup.defineDictionary("escape", new JavaEscapeStringMap()); targetGroup.defineDictionary("lines", new LinesStringMap()); targetGroup.defineDictionary("strlen", new StrlenStringMap()); String rootFolder = "org/antlr/v4/test/runtime/templates"; generateCodeForFoldersInIndex(targetGroup, rootFolder); }
public BuilderGenerator() { URL url = BuilderGenerator.class.getResource("/stringtemplates/builder-source.stg"); this.templateGroup = new STGroupFile(url, "UTF-8", '<', '>'); this.templateGroup.load(); if(!this.templateGroup.isDefined(SOURCE_TEMPLATE_NAME)) { throw new IllegalStateException("StringTemplate resource does not define 'builder_source' template"); } }
public void loadDependencyTemplates() { if (templates != null) return; String fileName = "org/antlr/v4/tool/templates/depend.stg"; templates = new STGroupFile(fileName, "UTF-8"); }
@Override protected synchronized CompiledST load(String name) { if ( !alreadyLoaded ) load(); return rawGetTemplate(name); }
@Override public boolean isDictionary(String name) { if ( !alreadyLoaded ) load(); return super.isDictionary(name); }
@Override public synchronized void load() { if ( alreadyLoaded ) return; alreadyLoaded = true; // do before actual load to say we're doing it // no prefix since this group file is the entire group, nothing lives // beneath it. if ( verbose ) System.out.println("loading group file "+url.toString()); loadGroupFile("/", url.toString()); if ( verbose ) System.out.println("found "+templates.size()+" templates in "+url.toString()+" = "+templates.keySet()); }
public STGroupFile(String fileName, char delimiterStartChar, char delimiterStopChar) { super(delimiterStartChar, delimiterStopChar); if ( !fileName.endsWith(GROUP_FILE_EXTENSION) ) { throw new IllegalArgumentException("Group file names must end in .stg: "+fileName); } //try { File f = new File(fileName); if ( f.exists() ) { try { url = f.toURI().toURL(); } catch (MalformedURLException e) { throw new STException("can't load group file "+fileName, e); } if ( verbose ) System.out.println("STGroupFile(" + fileName + ") == file "+f.getAbsolutePath()); } else { // try in classpath url = getURL(fileName); if ( url==null ) { throw new IllegalArgumentException("No such group file: "+ fileName); } if ( verbose ) System.out.println("STGroupFile(" + fileName + ") == url "+url); } this.fileName = fileName; }
private ST getTemplate(Method method, boolean offsets) { ST m = jTemplates.getInstanceOf("nativeImplMethod" + (offsets ? "_offsets" : "")); if (unsupported != null && method.getName().matches(unsupported)) { if (extending == null) m = jTemplates.getInstanceOf("unsupportedMethod"); else return null; } return m; }