public int write(STWriter out) throws IOException { Interpreter interp = new Interpreter(groupThatCreatedThisInstance, impl.nativeGroup.errMgr, false); return interp.exec(out, this); }
/** Execute template self and return how many characters it wrote to out */ public int exec(STWriter out, ST self) { pushScope(self); try { setDefaultArguments(out, self); return _exec(out, self); } catch (Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); pw.flush(); errMgr.runTimeError(this, self, current_ip, ErrorType.INTERNAL_ERROR, "internal error: "+sw.toString()); return 0; } finally { popScope(); } }
/** Return the first attribute if multiple valued or the attribute * itself if single-valued. Used in <names:first()> */ public Object first(Object v) { if ( v==null ) return null; Object r = v; v = convertAnythingIteratableToIterator(v); if ( v instanceof Iterator ) { Iterator it = (Iterator)v; if ( it.hasNext() ) { r = it.next(); } } return r; }
public List<InterpEvent> getEvents(Locale locale, int lineWidth) { StringWriter out = new StringWriter(); STWriter wr = new AutoIndentWriter(out); wr.setLineWidth(lineWidth); Interpreter interp = new Interpreter(groupThatCreatedThisInstance, locale, true); interp.exec(wr, this); // render and track events return interp.getEvents(); }
int ip = 0; while ( ip < self.impl.codeSize ) { if ( trace || debug ) trace(self, ip); short opcode = code[ip]; load_str(self,ip); ip += Bytecode.OPND_SIZE_IN_BYTES; break; case Bytecode.INSTR_LOAD_ATTR : nameIndex = getShort(code, ip); ip += Bytecode.OPND_SIZE_IN_BYTES; name = self.impl.strings[nameIndex]; try { o = getAttribute(self, name); if ( o==ST.EMPTY_ATTR ) o = null; break; case Bytecode.INSTR_LOAD_LOCAL: int valueIndex = getShort(code, ip); ip += Bytecode.OPND_SIZE_IN_BYTES; o = self.locals[valueIndex]; break; case Bytecode.INSTR_LOAD_PROP : nameIndex = getShort(code, ip); ip += Bytecode.OPND_SIZE_IN_BYTES; o = operands[sp--]; name = self.impl.strings[nameIndex]; operands[++sp] = getObjectProperty(out, self, o, name);
/** Execute template {@code self} and return how many characters it wrote to {@code out}. * * @return the number of characters written to {@code out} */ public int exec(STWriter out, InstanceScope scope) { final ST self = scope.st; if ( trace ) System.out.println("exec("+self.getName()+")"); try { setDefaultArguments(out, scope); return _exec(out, scope); } catch (Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); pw.flush(); errMgr.runTimeError(this, scope, ErrorType.INTERNAL_ERROR, "internal error: "+sw.toString()); return 0; } }
/** * Find an attribute via dynamic scoping up enclosing scope chain. Only look * for a dictionary definition if the attribute is not found, so attributes * sent in to a template override dictionary names. * <p> * Return {@link ST#EMPTY_ATTR} if found definition but no value.</p> */ public Object getAttribute(InstanceScope scope, String name) { InstanceScope current = scope; while ( current!=null ) { ST p = current.st; FormalArgument localArg = null; if ( p.impl.formalArguments!=null ) localArg = p.impl.formalArguments.get(name); if ( localArg!=null ) { Object o = p.locals[localArg.index]; return o; } current = current.parent; // look up enclosing scope chain } // got to root scope and no definition, try dictionaries in group and up final ST self = scope.st; STGroup g = self.impl.nativeGroup; Object o = getDictionary(g, name); if ( o!=null ) return o; // not found, report unknown attr throw new STNoSuchAttributeException(name, scope); }
/** If an instance of x is enclosed in a y which is in a z, return * a String of these instance names in order from topmost to lowest; * here that would be "[z y x]". */ public static String getEnclosingInstanceStackString(InstanceScope scope) { List<ST> templates = getEnclosingInstanceStack(scope, true); StringBuilder buf = new StringBuilder(); int i = 0; for (ST st : templates) { if ( i>0 ) buf.append(" "); buf.append(st.getName()); i++; } return buf.toString(); }
void passthru(ST self, String templateName, Map<String,Object> attrs) { CompiledST c = group.lookupTemplate(templateName); if ( c==null ) return; // will get error later for (FormalArgument arg : c.formalArguments.values()) { // if not already set by user, set to value from outer scope if ( !attrs.containsKey(arg.name) ) { //System.out.println("arg "+arg.name+" missing"); try { Object o = getAttribute(self, arg.name); // If the attribute exists but there is no value and // the formal argument has no default value, make it null. if ( o==ST.EMPTY_ATTR && arg.defaultValueToken==null ) { attrs.put(arg.name, null); } // Else, the attribute has an existing value, set arg. else if ( o!=ST.EMPTY_ATTR ) { attrs.put(arg.name, o); } } catch (STNoSuchAttributeException nsae) { // if no such attribute exists for arg.name, set parameter // if no default value if ( arg.defaultValueToken==null ) { attrs.put(arg.name, null); } } } } }
if ( attr!=null ) exprs.set(i, convertAnythingToIterator(scope, attr));
int ip = 0; while ( ip < self.impl.codeSize ) { if ( trace || debug ) trace(scope, ip); short opcode = code[ip]; load_str(self,ip); ip += Bytecode.OPND_SIZE_IN_BYTES; break; case Bytecode.INSTR_LOAD_ATTR : nameIndex = getShort(code, ip); ip += Bytecode.OPND_SIZE_IN_BYTES; name = self.impl.strings[nameIndex]; try { o = getAttribute(scope, name); if ( o==ST.EMPTY_ATTR ) o = null; break; case Bytecode.INSTR_LOAD_LOCAL: int valueIndex = getShort(code, ip); ip += Bytecode.OPND_SIZE_IN_BYTES; o = self.locals[valueIndex]; break; case Bytecode.INSTR_LOAD_PROP : nameIndex = getShort(code, ip); ip += Bytecode.OPND_SIZE_IN_BYTES; o = operands[sp--]; name = self.impl.strings[nameIndex]; operands[++sp] = getObjectProperty(out, scope, o, name);
/** Execute template {@code self} and return how many characters it wrote to {@code out}. * * @return the number of characters written to {@code out} */ public int exec(STWriter out, InstanceScope scope) { final ST self = scope.st; if ( trace ) System.out.println("exec("+self.getName()+")"); try { setDefaultArguments(out, scope); return _exec(out, scope); } catch (Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); pw.flush(); errMgr.runTimeError(this, scope, ErrorType.INTERNAL_ERROR, "internal error: "+sw.toString()); return 0; } }
public List<InterpEvent> getEvents(Locale locale, int lineWidth) { StringWriter out = new StringWriter(); STWriter wr = new AutoIndentWriter(out); wr.setLineWidth(lineWidth); Interpreter interp = new Interpreter(groupThatCreatedThisInstance, locale, true); InstanceScope scope = new InstanceScope(null, this); interp.exec(wr, scope); // render and track events return interp.getEvents(); }
/** * Find an attribute via dynamic scoping up enclosing scope chain. Only look * for a dictionary definition if the attribute is not found, so attributes * sent in to a template override dictionary names. * <p> * Return {@link ST#EMPTY_ATTR} if found definition but no value.</p> */ public Object getAttribute(InstanceScope scope, String name) { InstanceScope current = scope; while ( current!=null ) { ST p = current.st; FormalArgument localArg = null; if ( p.impl.formalArguments!=null ) localArg = p.impl.formalArguments.get(name); if ( localArg!=null ) { Object o = p.locals[localArg.index]; return o; } current = current.parent; // look up enclosing scope chain } // got to root scope and no definition, try dictionaries in group and up final ST self = scope.st; STGroup g = self.impl.nativeGroup; Object o = getDictionary(g, name); if ( o!=null ) return o; // not found, report unknown attr throw new STNoSuchAttributeException(name, scope); }
/** * If an instance of <i>x</i> is enclosed in a <i>y</i> which is in a * <i>z</i>, return a {@code String} of these instance names in order from * topmost to lowest; here that would be {@code [z y x]}. */ public static String getEnclosingInstanceStackString(InstanceScope scope) { List<ST> templates = getEnclosingInstanceStack(scope, true); StringBuilder buf = new StringBuilder(); int i = 0; for (ST st : templates) { if ( i>0 ) buf.append(" "); buf.append(st.getName()); i++; } return buf.toString(); }
Object o = getAttribute(scope, arg.name);
if ( attr!=null ) exprs.set(i, convertAnythingToIterator(scope, attr));
int ip = 0; while ( ip < self.impl.codeSize ) { if ( trace || debug ) trace(scope, ip); short opcode = code[ip]; load_str(self,ip); ip += Bytecode.OPND_SIZE_IN_BYTES; break; case Bytecode.INSTR_LOAD_ATTR : nameIndex = getShort(code, ip); ip += Bytecode.OPND_SIZE_IN_BYTES; name = self.impl.strings[nameIndex]; try { o = getAttribute(scope, name); if ( o==ST.EMPTY_ATTR ) o = null; break; case Bytecode.INSTR_LOAD_LOCAL: int valueIndex = getShort(code, ip); ip += Bytecode.OPND_SIZE_IN_BYTES; o = self.locals[valueIndex]; break; case Bytecode.INSTR_LOAD_PROP : nameIndex = getShort(code, ip); ip += Bytecode.OPND_SIZE_IN_BYTES; o = operands[sp--]; name = self.impl.strings[nameIndex]; operands[++sp] = getObjectProperty(out, scope, o, name);
public int write(STWriter out, Locale locale) { Interpreter interp = new Interpreter(groupThatCreatedThisInstance, locale, impl.nativeGroup.errMgr, false); return interp.exec(out, this); }
/** Execute template {@code self} and return how many characters it wrote to {@code out}. * * @return the number of characters written to {@code out} */ public int exec(STWriter out, InstanceScope scope) { final ST self = scope.st; if ( trace ) System.out.println("exec("+self.getName()+")"); try { setDefaultArguments(out, scope); return _exec(out, scope); } catch (Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); pw.flush(); errMgr.runTimeError(this, scope, ErrorType.INTERNAL_ERROR, "internal error: "+sw.toString()); return 0; } }