public void rawDefineTemplate(String name, CompiledST code, Token defT) { CompiledST prev = rawGetTemplate(name); if (prev != null) { if (!prev.isRegion) { errMgr.compileTimeError(ErrorType.TEMPLATE_REDEFINITION, null, defT); return; } if (prev.isRegion) { if (code.regionDefType != ST.RegionType.IMPLICIT && prev.regionDefType == ST.RegionType.EMBEDDED) { errMgr.compileTimeError( ErrorType.EMBEDDED_REGION_REDEFINITION, null, defT, getUnMangledTemplateName(name)); return; } else if (code.regionDefType == ST.RegionType.IMPLICIT || prev.regionDefType == ST.RegionType.EXPLICIT) { errMgr.compileTimeError( ErrorType.REGION_REDEFINITION, null, defT, getUnMangledTemplateName(name)); return; } } } code.nativeGroup = this; code.templateDefStartToken = defT; templates.put(name, code); }
/** Make name and alias for target. Replace any previous def of name */ public CompiledST defineTemplateAlias(Token aliasT, Token targetT) { String alias = aliasT.getText(); String target = targetT.getText(); CompiledST targetCode = rawGetTemplate(target); if (targetCode == null) { errMgr.compileTimeError(ErrorType.ALIAS_TARGET_UNDEFINED, null, aliasT, alias, target); return null; } templates.put(alias, targetCode); return targetCode; }
protected ST getEmbeddedInstanceOf( Interpreter interp, ST enclosingInstance, int ip, String name) { if (verbose) System.out.println("getEmbeddedInstanceOf(" + name + ")"); ST st = getInstanceOf(name); if (st == null) { errMgr.runTimeError(interp, enclosingInstance, ip, ErrorType.NO_SUCH_TEMPLATE, name); return createStringTemplateInternally(new CompiledST()); } // this is only called internally. wack any debug ST create events if (trackCreationEvents) { st.debugState.newSTEvent = null; // toss it out } return st; }
/** Load template stream into this group */ public CompiledST loadTemplateFile(String prefix, String fileName, CharStream templateStream) { GroupLexer lexer = new GroupLexer(templateStream); CommonTokenStream tokens = new CommonTokenStream(lexer); GroupParser parser = new GroupParser(tokens); parser.group = this; lexer.group = this; try { parser.templateDef(prefix); } catch (RecognitionException re) { errMgr.groupSyntaxError(ErrorType.SYNTAX_ERROR, fileName, re, re.getMessage()); } String templateName = Misc.getFileNameNoSuffix(fileName); if (prefix != null && prefix.length() > 0) templateName = prefix + "/" + templateName; return rawGetTemplate(templateName); }
/** Load a group file with full path fileName; it's relative to root by prefix. */ public void loadGroupFile(String prefix, String fileName) { // System.out.println("load group file prefix="+prefix+", fileName="+fileName); GroupParser parser = null; try { URL f = new URL(fileName); ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding); GroupLexer lexer = new GroupLexer(fs); fs.name = fileName; CommonTokenStream tokens = new CommonTokenStream(lexer); parser = new GroupParser(tokens); parser.group(this, prefix); } catch (Exception e) { errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName); } }
@Override protected int writeObject(STWriter out, InstanceScope scope, Object o, String[] options) { if (o instanceof ST) { String name = ((ST) o).getName(); name = name.substring(1); if (!name.startsWith("_sub")) { try { out.write("<ST:" + name + ">"); evals.add("<ST:" + name + ">"); int r = super.writeObject(out, scope, o, options); out.write("</ST:" + name + ">"); evals.add("</ST:" + name + ">"); return r; } catch (IOException ioe) { myErrMgrCopy.IOError(scope.st, ErrorType.WRITE_IO_ERROR, ioe); } } } return super.writeObject(out, scope, o, options); }
public CompiledST defineRegion( String enclosingTemplateName, Token regionT, String template, Token templateToken) { String name = regionT.getText(); template = Misc.trimOneStartingNewline(template); template = Misc.trimOneTrailingNewline(template); CompiledST code = compile(getFileName(), enclosingTemplateName, null, template, templateToken); String mangled = getMangledRegionName(enclosingTemplateName, name); if (lookupTemplate(mangled) == null) { errMgr.compileTimeError( ErrorType.NO_SUCH_REGION, templateToken, regionT, enclosingTemplateName, name); return new CompiledST(); } code.name = mangled; code.isRegion = true; code.regionDefType = ST.RegionType.EXPLICIT; code.templateDefStartToken = regionT; rawDefineTemplate(mangled, code, regionT); code.defineArgDefaultValueTemplates(this); code.defineImplicitlyDefinedTemplates(this); return code; }
/** * Import template files, directories, and group files. Priority is given to templates defined in * the current group; this, in effect, provides inheritance. Polymorphism is in effect so that if * an inherited template references template t() then we search for t() in the subgroup first. * * <p>If you specify an absolute file name or directory name, the import statement uses that * directly. If it is not an absolute path, we look that entity up in the directory holding the * group that initiates the import. If file or directory is not in that directory, then we load * using the classpath. * * <p>Templates are loaded on-demand from import dirs. Imported groups are loaded on-demand when * searching for a template. * * <p>The listener of this group is passed to the import group so errors found while loading * imported element are sent to listener of this group. */ public void importTemplates(Token fileNameToken) { String fileName = fileNameToken.getText(); // do nothing upon syntax error if (fileName == null || fileName.equals("<missing STRING>")) return; fileName = Misc.strip(fileName, 1); // System.out.println("import "+fileName); boolean isGroupFile = fileName.endsWith(".stg"); boolean isTemplateFile = fileName.endsWith(".st"); boolean isGroupDir = !(isGroupFile || isTemplateFile); STGroup g = null; File f = new File(fileName); if (f.isAbsolute()) { // load directly if absolute if (isTemplateFile) { g = new STGroup(); g.setListener(this.getListener()); g.loadAbsoluteTemplateFile(fileName); } else if (isGroupFile) { g = new STGroupFile(fileName, delimiterStartChar, delimiterStopChar); g.setListener(this.getListener()); } else if (isGroupDir) { g = new STGroupDir(fileName, delimiterStartChar, delimiterStopChar); g.setListener(this.getListener()); } importTemplates(g); return; } // it's a relative name; search path is working dir, g.stg's dir, CLASSPATH URL thisRoot = getRootDirURL(); URL fileUnderRoot = null; // System.out.println("thisRoot="+thisRoot); try { fileUnderRoot = new URL(thisRoot + "/" + fileName); } catch (MalformedURLException mfe) { errMgr.internalError(null, "can't build URL for " + thisRoot + "/" + fileName, mfe); return; } if (isTemplateFile) { g = new STGroup(); g.setListener(this.getListener()); URL fileURL; if (Misc.urlExists(fileUnderRoot)) fileURL = fileUnderRoot; else fileURL = getURL(fileName); // try CLASSPATH if (fileURL != null) { try { InputStream s = fileURL.openStream(); ANTLRInputStream templateStream = new ANTLRInputStream(s); templateStream.name = fileName; CompiledST code = g.loadTemplateFile("", fileName, templateStream); if (code == null) g = null; } catch (IOException ioe) { errMgr.internalError(null, "can't read from " + fileURL, ioe); g = null; } } else { g = null; } } else if (isGroupFile) { // System.out.println("look for fileUnderRoot: "+fileUnderRoot); if (Misc.urlExists(fileUnderRoot)) { g = new STGroupFile(fileUnderRoot, encoding, delimiterStartChar, delimiterStopChar); g.setListener(this.getListener()); } else { g = new STGroupFile(fileName, delimiterStartChar, delimiterStopChar); g.setListener(this.getListener()); } } else if (isGroupDir) { // System.out.println("try dir "+fileUnderRoot); if (Misc.urlExists(fileUnderRoot)) { g = new STGroupDir(fileUnderRoot, encoding, delimiterStartChar, delimiterStopChar); g.setListener(this.getListener()); } else { // try in CLASSPATH // System.out.println("try dir in CLASSPATH "+fileName); g = new STGroupDir(fileName, delimiterStartChar, delimiterStopChar); g.setListener(this.getListener()); } } if (g == null) { errMgr.compileTimeError(ErrorType.CANT_IMPORT, null, fileNameToken, fileName); } else { importTemplates(g); } }