Update of /cvsroot/webmacro/webmacro/src/org/webmacro/engine In directory sc8-pr-cvs1:/tmp/cvs-serv6675/src/org/webmacro/engine Modified Files: Argument.java Block.java BlockBuilder.java BuildContext.java BuildException.java Builder.java ConstantPropertyVariable.java CrankyEvaluationExceptionHandler.java DebugEvaluationExceptionHandler.java DefaultEvaluationExceptionHandler.java EncodeFilter.java EscapeFilter.java EvaluationExceptionHandler.java Expression.java FileTemplate.java FilterManager.java FunctionCall.java FunctionCallBuilder.java FunctionVariable.java GlobalVariable.java IntrospectionException.java IntrospectionUtils.java ListBuilder.java MacroAdapter.java MacroBuildContext.java MacroBuilder.java MacroDefinition.java MacroPropertyVariable.java MapBuilder.java MethodWrapper.java NullBuilder.java NullParser.java ParamBuilder.java ParseException.java Parser.java ParserProvider.java PropertyMethodBuilder.java PropertyOperatorCache.java PropertyVariable.java QuotedStringBuilder.java SilenceFilter.java SimplePropertyVariable.java StaticClassWrapper.java StreamTemplate.java StringMacroAdapter.java StringTemplate.java TemplateDumper.java TestObject.java TextParser.java UndefinedMacro.java Variable.java VariableBuilder.java VoidMacro.java WMTemplate.java Log Message: If this doesn't drive our SF "activity" stats through the roof, I don't know what will. Mass re-formatting of all code, following (to the best of my interpertation) the Sun (w/ jakarta tweaks) coding style guidelines defined here: http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html and here: http://jakarta.apache.org/turbine/common/code-standards.html I did this reformatting with IDEA 3.0.5, and I am going to commit the style configuration for IDEA (if I can find it). Index: Argument.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/engine/Argument.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Argument.java 11 Jun 2002 17:43:21 -0000 1.6 --- Argument.java 12 Jun 2003 00:47:45 -0000 1.7 *************** *** 31,71 **** * term. */ ! public final class Argument { ! private String _name; ! private Object _value; ! /** ! * Create a new predicate ! */ ! public Argument(String name, Object value) { ! _name = name; ! _value = value; ! } ! /** ! * Return the action code for this predicate ! */ ! final public String getName() { ! return _name; ! } ! /** ! * Return the object on which this predicate operates ! */ ! final public Object getValue() { ! return _value; ! } ! /** ! * Make sure that _value is not a builder! Not a public method, ! * used by Directivebuilder and other things that build Argument ! * lists. ! */ ! final void build(BuildContext bc) throws BuildException { ! if (_value instanceof Builder) { ! _value = ((Builder) _value).build(bc); ! } ! } } --- 31,77 ---- * term. */ ! public final class Argument ! { ! private String _name; ! private Object _value; ! /** ! * Create a new predicate ! */ ! public Argument (String name, Object value) ! { ! _name = name; ! _value = value; ! } ! /** ! * Return the action code for this predicate ! */ ! final public String getName () ! { ! return _name; ! } ! /** ! * Return the object on which this predicate operates ! */ ! final public Object getValue () ! { ! return _value; ! } ! /** ! * Make sure that _value is not a builder! Not a public method, ! * used by Directivebuilder and other things that build Argument ! * lists. ! */ ! final void build (BuildContext bc) throws BuildException ! { ! if (_value instanceof Builder) ! { ! _value = ((Builder) _value).build(bc); ! } ! } } Index: Block.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/engine/Block.java,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** Block.java 18 Dec 2002 06:21:41 -0000 1.23 --- Block.java 12 Jun 2003 00:47:45 -0000 1.24 *************** *** 24,90 **** package org.webmacro.engine; - import java.io.*; - import java.util.*; - import org.webmacro.*; import org.webmacro.util.Encoder; /** * A Block is essentially a Macro[] that knows how to write itself * out as a String. */ ! final public class Block implements Macro, Visitable { ! private final String[] _strings; ! private final Macro[] _macros; ! private final int[] _lineNos, _colNos; ! private final Encoder.Block _block; ! private final int _length; ! private final int _remainder; ! private String _name; ! /** ! * A Block must be constructed from a BlockBuilder. The format ! * of a block is: String (Macro String)* ! * and the constructor expects to receive two arrays matching ! * this structure. The output of the block will be the first ! * string, followed by the first macro, followed by the second ! * string, followed by the second macro, etc., and terminated ! * by the final string. ! */ ! protected Block(String name, String[] strings, Macro[] macros, ! int lineNos[], int colNos[]) { ! _name = name; ! _strings = strings; ! _macros = macros; ! _lineNos = lineNos; ! _colNos = colNos; ! _length = _macros.length; ! _remainder = 10 - _length % 10; ! // we'll use this to encode our strings for output to the user ! _block = new Encoder.Block(_strings); ! } ! /** ! * Interpret the directive and write it out, using the values in ! * the supplied context as appropriate. ! * <p> ! * @exception PropertyException if required data was missing from context ! * @exception IOException if we could not successfully write to out ! */ ! final public void write(final FastWriter out, final Context context) ! throws PropertyException, IOException { ! final byte[][] bcontent = out.getEncoder().encode(_block); ! byte[] b; ! Context.TemplateEvaluationContext teC = context.getTemplateEvaluationContext(); ! String oldName = teC._templateName; ! teC._templateName = _name; ! int i = 0; ! switch (_remainder) { ! case 1: b = bcontent[i]; out.write(b, 0, b.length); --- 24,151 ---- package org.webmacro.engine; import org.webmacro.*; import org.webmacro.util.Encoder; + import java.io.IOException; + import java.util.List; + /** * A Block is essentially a Macro[] that knows how to write itself * out as a String. */ ! final public class Block implements Macro, Visitable ! { ! private final String[] _strings; ! private final Macro[] _macros; ! private final int[] _lineNos, _colNos; ! private final Encoder.Block _block; ! private final int _length; ! private final int _remainder; ! private String _name; ! /** ! * A Block must be constructed from a BlockBuilder. The format ! * of a block is: String (Macro String)* ! * and the constructor expects to receive two arrays matching ! * this structure. The output of the block will be the first ! * string, followed by the first macro, followed by the second ! * string, followed by the second macro, etc., and terminated ! * by the final string. ! */ ! protected Block (String name, String[] strings, Macro[] macros, ! int lineNos[], int colNos[]) ! { ! _name = name; ! _strings = strings; ! _macros = macros; ! _lineNos = lineNos; ! _colNos = colNos; ! _length = _macros.length; ! _remainder = 10 - _length % 10; ! // we'll use this to encode our strings for output to the user ! _block = new Encoder.Block(_strings); ! } ! /** ! * Interpret the directive and write it out, using the values in ! * the supplied context as appropriate. ! * <p> ! * @exception PropertyException if required data was missing from context ! * @exception IOException if we could not successfully write to out ! */ ! final public void write (final FastWriter out, final Context context) ! throws PropertyException, IOException ! { ! final byte[][] bcontent = out.getEncoder().encode(_block); ! byte[] b; ! Context.TemplateEvaluationContext teC = context.getTemplateEvaluationContext(); ! String oldName = teC._templateName; ! teC._templateName = _name; ! int i = 0; ! switch (_remainder) ! { ! case 1: ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! case 2: ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! case 3: ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! case 4: ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! case 5: ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! case 6: ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! case 7: ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! case 8: ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! case 9: ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! } ! ! while (i < _length) ! { b = bcontent[i]; out.write(b, 0, b.length); *************** *** 92,96 **** teC._columnNo = this.getColNo(i); _macros[i++].write(out, context); - case 2: b = bcontent[i]; out.write(b, 0, b.length); --- 153,156 ---- *************** *** 98,102 **** teC._columnNo = this.getColNo(i); _macros[i++].write(out, context); - case 3: b = bcontent[i]; out.write(b, 0, b.length); --- 158,161 ---- *************** *** 104,108 **** teC._columnNo = this.getColNo(i); _macros[i++].write(out, context); - case 4: b = bcontent[i]; out.write(b, 0, b.length); --- 163,166 ---- *************** *** 110,114 **** teC._columnNo = this.getColNo(i); _macros[i++].write(out, context); - case 5: b = bcontent[i]; out.write(b, 0, b.length); --- 168,171 ---- *************** *** 116,120 **** teC._columnNo = this.getColNo(i); _macros[i++].write(out, context); - case 6: b = bcontent[i]; out.write(b, 0, b.length); --- 173,176 ---- *************** *** 122,126 **** teC._columnNo = this.getColNo(i); _macros[i++].write(out, context); - case 7: b = bcontent[i]; out.write(b, 0, b.length); --- 178,181 ---- *************** *** 128,132 **** teC._columnNo = this.getColNo(i); _macros[i++].write(out, context); - case 8: b = bcontent[i]; out.write(b, 0, b.length); --- 183,186 ---- *************** *** 134,138 **** teC._columnNo = this.getColNo(i); _macros[i++].write(out, context); - case 9: b = bcontent[i]; out.write(b, 0, b.length); --- 188,191 ---- *************** *** 140,308 **** teC._columnNo = this.getColNo(i); _macros[i++].write(out, context); ! } ! ! while (i < _length) { ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! } ! b = bcontent[_length]; ! out.write(b, 0, b.length); ! teC._templateName = oldName; ! } ! public String getTemplateName() { ! return _name; ! } ! public void setTemplateName(String name) { ! _name = name; ! } ! public int getLineNo(int i) { ! return (_lineNos != null && i>=0 && _lineNos.length > i) ? _lineNos[i] : 0; ! } ! public int getColNo(int i) { ! return (_colNos != null && i>=0 && _colNos.length > i) ? _colNos[i] : 0; ! } ! private static class BlockIterator implements BlockBuilder.BlockIterator { ! private int i = 0; ! private boolean doneString = false, done = false; ! private String[] strings; ! private Macro[] macros; ! private Block block; ! public BlockIterator(String[] strings, Macro[] macros, Block b) { ! this.strings = strings; ! this.macros = macros; ! this.block = b; ! } ! public boolean hasNext() { ! return !done; ! } ! public String getName() { ! return block.getTemplateName(); ! } ! public int getLineNo() { ! return block.getLineNo(i - 1); ! } ! public int getColNo() { ! return block.getColNo(i - 1); ! } ! public void remove() { ! throw new UnsupportedOperationException(); ! } ! public Object next() { ! if (doneString) { ! doneString = false; ! return macros[i++]; ! } ! else { ! if (i == strings.length - 1) ! done = true; ! doneString = true; ! return strings[i]; ! } ! } ! } ! public BlockBuilder.BlockIterator getBlockIterator() { ! return new BlockIterator(_strings, _macros, this); ! } ! final void appendTo(List l) { ! final int len = _macros.length; ! for (int i = 0; i < _macros.length; i++) { ! l.add(_strings[i]); ! l.add(_macros[i]); ! } ! l.add(_strings[len]); ! } ! final public void accept(TemplateVisitor v) { ! v.beginBlock(); ! final int len = _macros.length; ! for (int i = 0; i < len; i++) { ! v.visitString(_strings[i]); ! v.visitMacro(_macros[i]); ! } ! v.visitString(_strings[len]); ! v.endBlock(); ! } ! /** ! * same as out but returns a String ! * <p> ! * @exception PropertyException if required data was missing from context ! */ ! final public Object evaluate(Context context) throws PropertyException { ! try { ! FastWriter fw = FastWriter.getInstance(context.getBroker()); ! write(fw, context); ! String ret = fw.toString(); ! fw.close(); ! return ret; ! } ! catch (IOException e) { ! context.getBroker().getLog("engine", "parsing and template execution").error("StringWriter threw an IOException!", e); ! return null; ! } ! } } --- 193,335 ---- teC._columnNo = this.getColNo(i); _macros[i++].write(out, context); ! b = bcontent[i]; ! out.write(b, 0, b.length); ! teC._lineNo = this.getLineNo(i); ! teC._columnNo = this.getColNo(i); ! _macros[i++].write(out, context); ! } ! b = bcontent[_length]; ! out.write(b, 0, b.length); ! teC._templateName = oldName; ! } ! public String getTemplateName () ! { ! return _name; ! } ! public void setTemplateName (String name) ! { ! _name = name; ! } ! public int getLineNo (int i) ! { ! return (_lineNos != null && i >= 0 && _lineNos.length > i) ? _lineNos[i] : 0; ! } ! public int getColNo (int i) ! { ! return (_colNos != null && i >= 0 && _colNos.length > i) ? _colNos[i] : 0; ! } ! private static class BlockIterator implements BlockBuilder.BlockIterator ! { ! private int i = 0; ! private boolean doneString = false, done = false; ! private String[] strings; ! private Macro[] macros; ! private Block block; ! public BlockIterator (String[] strings, Macro[] macros, Block b) ! { ! this.strings = strings; ! this.macros = macros; ! this.block = b; ! } ! public boolean hasNext () ! { ! return !done; ! } ! public String getName () ! { ! return block.getTemplateName(); ! } ! public int getLineNo () ! { ! return block.getLineNo(i - 1); ! } ! public int getColNo () ! { ! return block.getColNo(i - 1); ! } ! public void remove () ! { ! throw new UnsupportedOperationException(); ! } ! public Object next () ! { ! if (doneString) ! { ! doneString = false; ! return macros[i++]; ! } ! else ! { ! if (i == strings.length - 1) ! done = true; ! doneString = true; ! return strings[i]; ! } ! } ! } ! public BlockBuilder.BlockIterator getBlockIterator () ! { ! return new BlockIterator(_strings, _macros, this); ! } ! final void appendTo (List l) ! { ! final int len = _macros.length; ! for (int i = 0; i < _macros.length; i++) ! { ! l.add(_strings[i]); ! l.add(_macros[i]); ! } ! l.add(_strings[len]); ! } ! final public void accept (TemplateVisitor v) ! { ! v.beginBlock(); ! final int len = _macros.length; ! for (int i = 0; i < len; i++) ! { ! v.visitString(_strings[i]); ! v.visitMacro(_macros[i]); ! } ! v.visitString(_strings[len]); ! v.endBlock(); ! } ! /** ! * same as out but returns a String ! * <p> ! * @exception PropertyException if required data was missing from context ! */ ! final public Object evaluate (Context context) throws PropertyException ! { ! try ! { ! FastWriter fw = FastWriter.getInstance(context.getBroker()); ! write(fw, context); ! String ret = fw.toString(); ! fw.close(); ! return ret; ! } ! catch (IOException e) ! { ! context.getBroker().getLog("engine", "parsing and template execution").error("StringWriter threw an IOException!", e); ! return null; ! } ! } } Index: BlockBuilder.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/engine/BlockBuilder.java,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** BlockBuilder.java 17 Dec 2002 06:01:28 -0000 1.18 --- BlockBuilder.java 12 Jun 2003 00:47:45 -0000 1.19 *************** *** 24,31 **** package org.webmacro.engine; - import java.util.*; - - import org.webmacro.Macro; import org.webmacro.Context; /** --- 24,33 ---- package org.webmacro.engine; import org.webmacro.Context; + import org.webmacro.Macro; + + import java.util.ArrayList; + import java.util.Iterator; + import java.util.Stack; /** *************** *** 35,218 **** * all of the other directives, strings, etc. that can be in a template. */ ! public class BlockBuilder implements Builder { ! ! private static final int INITIAL_SIZE = 64; ! private static Macro[] mArray = new Macro[0]; ! private static String[] sArray = new String[0]; ! private ArrayList elements = new ArrayList(); ! private int[] lineNos = new int[INITIAL_SIZE]; ! private int[] colNos = new int[INITIAL_SIZE]; ! private String name = "unknown"; ! public BlockBuilder() { ! } ! public BlockBuilder(String name) { ! this.name = name; ! } ! public interface BlockIterator extends Iterator { ! public String getName(); ! public int getLineNo(); ! public int getColNo(); ! } ! public class BBIterator implements BlockIterator { ! private int size, i; ! public BBIterator() { ! size = elements.size(); ! i = 0; ! } ! public String getName() { ! return name; ! } ! public boolean hasNext() { ! return (i < size); ! } ! public Object next() { ! return elements.get(i++); ! } ! public int getLineNo() { ! return lineNos[i - 1]; ! } ! public int getColNo() { ! return colNos[i - 1]; ! } ! public void remove() { ! throw new UnsupportedOperationException(); ! } ! } ! final public Object build(BuildContext bc) throws BuildException { ! ArrayList strings = new ArrayList((elements.size())); ! ArrayList macros = new ArrayList((elements.size())); ! int[] ln = new int[elements.size()]; ! int[] cn = new int[elements.size()]; ! Stack iterStack = new Stack(); ! StringBuffer s = new StringBuffer(); ! Context.TemplateEvaluationContext tec = bc.getTemplateEvaluationContext(); ! // flatten everything and view the content as being: ! // string (macro string)* string ! // store that as an array of strings and an array of ! // Macro objects and create a block. ! BlockIterator iter = new BBIterator(); ! while (iter.hasNext()) { ! Object o = iter.next(); ! if (o instanceof Builder) { ! // track line/column numbers in the build context ! // so that bc.getCurrentLocation() stays current ! tec._templateName = iter.getName(); ! tec._lineNo = iter.getLineNo(); ! tec._columnNo = iter.getColNo(); ! try { ! o = ((Builder) o).build(bc); ! } catch (BuildException be) { ! // restore line/column info to what it was before ! // we tried to build the block ! tec._templateName = iter.getName(); ! tec._lineNo = iter.getLineNo(); ! tec._columnNo = iter.getColNo(); ! // and rethrow the exception ! throw be; ! } ! } ! if (o instanceof Block) { ! iterStack.push(iter); ! iter = ((Block) o).getBlockIterator(); ! } ! else { ! if (o instanceof Macro) { ! strings.add(s.toString()); ! s = new StringBuffer(); ! // do not reuse StringBuffer, ! // otherwise all strings will contain char[] of max length!! ! macros.add(o); ! // Now deal with the line numbers ! int size = macros.size(); ! if (ln.length < size) { ! ln = resizeIntArray(ln, ln.length * 2); ! cn = resizeIntArray(cn, cn.length * 2); ! } ! ln[size - 1] = iter.getLineNo(); ! cn[size - 1] = iter.getColNo(); } ! else if (o != null) { ! s.append(o.toString()); } ! } ! while (!iter.hasNext() && !iterStack.empty()) ! iter = (BlockIterator) iterStack.pop(); ! } ! strings.add(s.toString()); ! Macro finalMacros[] = (Macro[]) macros.toArray(mArray); ! String finalStrings[] = (String[]) strings.toArray(sArray); ! int finalLines[] = resizeIntArray(ln, macros.size()); ! int finalCols[] = resizeIntArray(cn, macros.size()); ! return new Block(name, finalStrings, finalMacros, finalLines, finalCols); ! } ! private static int[] resizeIntArray(int[] ia, int size) { ! int[] temp = new int[size]; ! System.arraycopy(ia, 0, temp, 0, Math.min(ia.length, size)); ! return temp; ! } ! // Methods that look like Vector methods ! public void addElement(Object o) { ! elements.add(o); ! } ! public void addElement(Object o, int lineNo, int colNo) { ! elements.add(o); ! int size = elements.size(); ! if (lineNos.length < size) { ! lineNos = resizeIntArray(lineNos, Math.max(lineNos.length * 2, ! size + INITIAL_SIZE)); ! colNos = resizeIntArray(colNos, Math.max(colNos.length * 2, ! size + INITIAL_SIZE)); ! } ! lineNos[size - 1] = lineNo; ! colNos[size - 1] = colNo; ! } ! public int size() { ! return elements.size(); ! } ! public void remove(int i) { ! elements.remove(i); ! } ! public Object elementAt(int i) { ! return elements.get(i); ! } ! public Object setElementAt(Object o, int i) { ! return elements.set(i, o); ! } } --- 37,251 ---- * all of the other directives, strings, etc. that can be in a template. */ ! public class BlockBuilder implements Builder ! { ! private static final int INITIAL_SIZE = 64; ! private static Macro[] mArray = new Macro[0]; ! private static String[] sArray = new String[0]; ! private ArrayList elements = new ArrayList(); ! private int[] lineNos = new int[INITIAL_SIZE]; ! private int[] colNos = new int[INITIAL_SIZE]; ! private String name = "unknown"; ! public BlockBuilder () ! { ! } ! public BlockBuilder (String name) ! { ! this.name = name; ! } ! public interface BlockIterator extends Iterator ! { ! public String getName (); ! public int getLineNo (); ! public int getColNo (); ! } ! public class BBIterator implements BlockIterator ! { ! private int size, i; ! public BBIterator () ! { ! size = elements.size(); ! i = 0; ! } ! public String getName () ! { ! return name; ! } ! public boolean hasNext () ! { ! return (i < size); ! } ! public Object next () ! { ! return elements.get(i++); ! } ! public int getLineNo () ! { ! return lineNos[i - 1]; ! } ! public int getColNo () ! { ! return colNos[i - 1]; ! } ! public void remove () ! { ! throw new UnsupportedOperationException(); ! } ! } ! final public Object build (BuildContext bc) throws BuildException ! { ! ArrayList strings = new ArrayList((elements.size())); ! ArrayList macros = new ArrayList((elements.size())); ! int[] ln = new int[elements.size()]; ! int[] cn = new int[elements.size()]; ! Stack iterStack = new Stack(); ! StringBuffer s = new StringBuffer(); ! Context.TemplateEvaluationContext tec = bc.getTemplateEvaluationContext(); ! // flatten everything and view the content as being: ! // string (macro string)* string ! // store that as an array of strings and an array of ! // Macro objects and create a block. ! BlockIterator iter = new BBIterator(); ! while (iter.hasNext()) ! { ! Object o = iter.next(); ! if (o instanceof Builder) ! { ! // track line/column numbers in the build context ! // so that bc.getCurrentLocation() stays current ! tec._templateName = iter.getName(); ! tec._lineNo = iter.getLineNo(); ! tec._columnNo = iter.getColNo(); ! try ! { ! o = ((Builder) o).build(bc); ! } ! catch (BuildException be) ! { ! // restore line/column info to what it was before ! // we tried to build the block ! tec._templateName = iter.getName(); ! tec._lineNo = iter.getLineNo(); ! tec._columnNo = iter.getColNo(); ! // and rethrow the exception ! throw be; ! } ! } ! if (o instanceof Block) ! { ! iterStack.push(iter); ! iter = ((Block) o).getBlockIterator(); } ! else ! { ! if (o instanceof Macro) ! { ! strings.add(s.toString()); ! s = new StringBuffer(); ! // do not reuse StringBuffer, ! // otherwise all strings will contain char[] of max length!! ! macros.add(o); ! ! // Now deal with the line numbers ! int size = macros.size(); ! if (ln.length < size) ! { ! ln = resizeIntArray(ln, ln.length * 2); ! cn = resizeIntArray(cn, cn.length * 2); ! } ! ln[size - 1] = iter.getLineNo(); ! cn[size - 1] = iter.getColNo(); ! } ! else if (o != null) ! { ! s.append(o.toString()); ! } } ! while (!iter.hasNext() && !iterStack.empty()) ! iter = (BlockIterator) iterStack.pop(); ! } ! strings.add(s.toString()); ! Macro finalMacros[] = (Macro[]) macros.toArray(mArray); ! String finalStrings[] = (String[]) strings.toArray(sArray); ! int finalLines[] = resizeIntArray(ln, macros.size()); ! int finalCols[] = resizeIntArray(cn, macros.size()); ! return new Block(name, finalStrings, finalMacros, finalLines, finalCols); ! } ! private static int[] resizeIntArray (int[] ia, int size) ! { ! int[] temp = new int[size]; ! System.arraycopy(ia, 0, temp, 0, Math.min(ia.length, size)); ! return temp; ! } ! // Methods that look like Vector methods ! public void addElement (Object o) ! { ! elements.add(o); ! } ! public void addElement (Object o, int lineNo, int colNo) ! { ! elements.add(o); ! int size = elements.size(); ! if (lineNos.length < size) ! { ! lineNos = resizeIntArray(lineNos, Math.max(lineNos.length * 2, ! size + INITIAL_SIZE)); ! colNos = resizeIntArray(colNos, Math.max(colNos.length * 2, ! size + INITIAL_SIZE)); ! } ! lineNos[size - 1] = lineNo; ! colNos[size - 1] = colNo; ! } ! public int size () ! { ! return elements.size(); ! } ! public void remove (int i) ! { ! elements.remove(i); ! } ! public Object elementAt (int i) ! { ! return elements.get(i); ! } ! public Object setElementAt (Object o, int i) ! { ! return elements.set(i, o); ! } } Index: BuildContext.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/engine/BuildContext.java,v retrieving revision 1.25 retrieving revision 1.26 diff -C2 -d -r1.25 -r1.26 *** BuildContext.java 21 May 2003 23:01:46 -0000 1.25 --- BuildContext.java 12 Jun 2003 00:47:45 -0000 1.26 *************** *** 24,31 **** package org.webmacro.engine; - import java.util.*; - import org.webmacro.*; /** --- 24,32 ---- package org.webmacro.engine; import org.webmacro.*; + import java.util.HashMap; + import java.util.Map; + /** *************** *** 39,207 **** * your keys, to avoid conflicting with keys inserted by someone else. */ ! public class BuildContext extends Context { ! private final Map _types = new HashMap(); ! private final Map _macros = new HashMap(); ! private final FilterManager _filters = new FilterManager(); ! public BuildContext(Broker b) { ! super(b, false); ! } ! public final Parser getParser(String pname) ! throws NotFoundException { ! try { ! return (Parser) getBroker().get("parser", pname); ! } ! catch (NotFoundException e) { ! throw e; ! } ! catch (ResourceException e) { ! throw new NotFoundException(e.toString(), e); ! } ! } ! /** ! * Find out whether the named variable is a tool, local variable, ! * or property variable. ! */ ! public Object getVariableType(String name) { ! Object ret = _types.get(name); ! return (ret == null) ? Variable.PROPERTY_TYPE : ret; ! } ! /** ! * Declare whether the named variable is to be treated as a tool, ! * local variable, or property variable type. ! */ ! public void setVariableType(String name, Object type) { ! if (name == null) { ! return; ! } ! if (type == null) { ! _types.remove(name); ! } ! else { ! _types.put(name, type); ! } ! } ! /** ! * Register a new filter, adding it to the chain for the supplied name. ! * The name is either a top level property name or * to mean "all". ! * @param var the top level property name that is being filtered ! * @param ft the Filter which will handle this property ! */ ! public void addFilter(Variable var, Filter ft) { ! _filters.addFilter(var, ft); ! } ! /** ! * Clear all the filtered for the supplied name. Cleaing * clears ! * only global filters, leaving filters for specific properties. ! */ ! public void clearFilters(Variable var) { ! _filters.clearFilters(var); ! } ! /** ! * Get the filter that applies to a specific variable. Returning ! * null from this method means that the entire variable should ! * be dropped from the output since it's been filtered to null. ! * @return the Macro to be used to filter it, or null ! */ ! public Macro getFilterMacro(Variable v) { ! return _filters.getMacro(v); ! } ! /** ! * Add a MacroDefinition to the build context ! */ ! public void putMacro(String name, MacroDefinition macro) { ! _macros.put(name, macro); ! } ! /** ! * Search for a MacroDefinition in the build context ! */ ! public MacroDefinition getMacro(String name) { ! return (MacroDefinition) _macros.get(name); ! } ! /** ! * Add #macros and #params from the specified Template to ! * this bulid context ! */ ! public void mergeConstants(Template t) { ! Map macros = t.getMacros(); ! Map params = t.getParameters(); ! if (macros != null) ! _macros.putAll(macros); ! if (params != null) ! super.putAll(params); ! } ! /** ! * Return the map of MacroDefinitions ! */ ! public Map getMacros() { ! return _macros; ! } ! /** ! * Create a variable (or resolve a constant at build time) ! * Used by various build() routines ! */ ! Object resolveVariableReference(Object names[], boolean filtered) ! throws BuildException { ! Object v = null; ! if (names.length < 1) ! throw new BuildException("Variable with name of length zero!"); ! Object c[] = new Object[names.length]; ! for (int i = 0; i < c.length; i++) { ! c[i] = (names[i] instanceof Builder) ? ! ((Builder) names[i]).build(this) : names[i]; ! } ! String firstName = c[0].toString(); ! Object type = null; ! if (c[0] instanceof FunctionCall){ ! type = FunctionVariable.TYPE; ! } ! else { ! type = getVariableType(firstName); ! } ! if (type == Variable.PROPERTY_TYPE) { ! if (containsKey(firstName)) { ! Object expansion = get(firstName); ! if (expansion instanceof Macro) { ! v = (c.length == 1) ? expansion ! : new MacroPropertyVariable((Macro) expansion, c); } ! else { ! v = (c.length == 1) ? expansion ! : new ConstantPropertyVariable(expansion, c); } ! } ! else { ! v = (c.length == 1) ! ? (Object) new SimplePropertyVariable(c) ! : (Object) new PropertyVariable(c); ! } ! } ! else if (type == Variable.LOCAL_TYPE) { ! v = new GlobalVariable(c); ! } ! else if (type == FunctionVariable.TYPE) { ! v = new FunctionVariable(c); ! } ! else { ! throw new BuildException("Unrecognized Variable Type: " + type); ! } ! return (filtered && v instanceof Variable) ! ? getFilterMacro((Variable) v) : v; ! } } --- 40,238 ---- * your keys, to avoid conflicting with keys inserted by someone else. */ ! public class BuildContext extends Context ! { ! private final Map _types = new HashMap(); ! private final Map _macros = new HashMap(); ! private final FilterManager _filters = new FilterManager(); ! public BuildContext (Broker b) ! { ! super(b, false); ! } ! public final Parser getParser (String pname) ! throws NotFoundException ! { ! try ! { ! return (Parser) getBroker().get("parser", pname); ! } ! catch (NotFoundException e) ! { ! throw e; ! } ! catch (ResourceException e) ! { ! throw new NotFoundException(e.toString(), e); ! } ! } ! /** ! * Find out whether the named variable is a tool, local variable, ! * or property variable. ! */ ! public Object getVariableType (String name) ! { ! Object ret = _types.get(name); ! return (ret == null) ? Variable.PROPERTY_TYPE : ret; ! } ! /** ! * Declare whether the named variable is to be treated as a tool, ! * local variable, or property variable type. ! */ ! public void setVariableType (String name, Object type) ! { ! if (name == null) ! { ! return; ! } ! if (type == null) ! { ! _types.remove(name); ! } ! else ! { ! _types.put(name, type); ! } ! } ! /** ! * Register a new filter, adding it to the chain for the supplied name. ! * The name is either a top level property name or * to mean "all". ! * @param var the top level property name that is being filtered ! * @param ft the Filter which will handle this property ! */ ! public void addFilter (Variable var, Filter ft) ! { ! _filters.addFilter(var, ft); ! } ! /** ! * Clear all the filtered for the supplied name. Cleaing * clears ! * only global filters, leaving filters for specific properties. ! */ ! public void clearFilters (Variable var) ! { ! _filters.clearFilters(var); ! } ! /** ! * Get the filter that applies to a specific variable. Returning ! * null from this method means that the entire variable should ! * be dropped from the output since it's been filtered to null. ! * @return the Macro to be used to filter it, or null ! */ ! public Macro getFilterMacro (Variable v) ! { ! return _filters.getMacro(v); ! } ! /** ! * Add a MacroDefinition to the build context ! */ ! public void putMacro (String name, MacroDefinition macro) ! { ! _macros.put(name, macro); ! } ! /** ! * Search for a MacroDefinition in the build context ! */ ! public MacroDefinition getMacro (String name) ! { ! return (MacroDefinition) _macros.get(name); ! } ! /** ! * Add #macros and #params from the specified Template to ! * this bulid context ! */ ! public void mergeConstants (Template t) ! { ! Map macros = t.getMacros(); ! Map params = t.getParameters(); ! if (macros != null) ! _macros.putAll(macros); ! if (params != null) ! super.putAll(params); ! } ! /** ! * Return the map of MacroDefinitions ! */ ! public Map getMacros () ! { ! return _macros; ! } ! /** ! * Create a variable (or resolve a constant at build time) ! * Used by various build() routines ! */ ! Object resolveVariableReference (Object names[], boolean filtered) ! throws BuildException ! { ! Object v = null; ! if (names.length < 1) ! throw new BuildException("Variable with name of length zero!"); ! Object c[] = new Object[names.length]; ! for (int i = 0; i < c.length; i++) ! { ! c[i] = (names[i] instanceof Builder) ? ! ((Builder) names[i]).build(this) : names[i]; ! } ! String firstName = c[0].toString(); ! Object type = null; ! if (c[0] instanceof FunctionCall) ! { ! type = FunctionVariable.TYPE; ! } ! else ! { ! type = getVariableType(firstName); ! } ! if (type == Variable.PROPERTY_TYPE) ! { ! if (containsKey(firstName)) ! { ! Object expansion = get(firstName); ! if (expansion instanceof Macro) ! { ! v = (c.length == 1) ? expansion ! : new MacroPropertyVariable((Macro) expansion, c); ! } ! else ! { ! v = (c.length == 1) ? expansion ! : new ConstantPropertyVariable(expansion, c); ! } } ! else ! { ! v = (c.length == 1) ! ? (Object) new SimplePropertyVariable(c) ! : (Object) new PropertyVariable(c); } ! } ! else if (type == Variable.LOCAL_TYPE) ! { ! v = new GlobalVariable(c); ! } ! else if (type == FunctionVariable.TYPE) ! { ! v = new FunctionVariable(c); ! } ! else ! { ! throw new BuildException("Unrecognized Variable Type: " + type); ! } ! return (filtered && v instanceof Variable) ! ? getFilterMacro((Variable) v) : v; ! } } Index: BuildException.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/engine/BuildException.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** BuildException.java 11 Jun 2002 17:43:21 -0000 1.5 --- BuildException.java 12 Jun 2003 00:47:45 -0000 1.6 *************** *** 26,38 **** import org.webmacro.TemplateException; ! public class BuildException extends TemplateException { ! public BuildException(String message) { ! super(message); ! } ! public BuildException(String reason, Throwable e) { ! super(reason, e); ! } } --- 26,41 ---- import org.webmacro.TemplateException; ! public class BuildException extends TemplateException ! { ! public BuildException (String message) ! { ! super(message); ! } ! public BuildException (String reason, Throwable e) ! { ! super(reason, e); ! } } Index: Builder.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/engine/Builder.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Builder.java 11 Jun 2002 17:43:21 -0000 1.4 --- Builder.java 12 Jun 2003 00:47:45 -0000 1.5 *************** *** 25,32 **** ! public interface Builder { ! public Object build(BuildContext pc) ! throws BuildException; } --- 25,33 ---- ! public interface Builder ! { ! public Object build (BuildContext pc) ! throws BuildException; } Index: ConstantPropertyVariable.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/engine/ConstantPropertyVariable.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ConstantPropertyVariable.java 11 Jun 2002 17:43:21 -0000 1.2 --- ConstantPropertyVariable.java 12 Jun 2003 00:47:45 -0000 1.3 *************** *** 33,79 **** * @since 1.1 */ ! public class ConstantPropertyVariable extends Variable { ! private Object value; ! /** ! * No special initialization ! */ ! ConstantPropertyVariable(Object value, Object names[]) { ! super(names); ! this.value = value; ! } ! /** ! * Look up my value in the corresponding Map, possibly using introspection, ! * and return it ! * @exception PropertyException If the property does not exist ! */ ! public final Object getValue(Context context) ! throws PropertyException { ! if (value == null) ! throw new PropertyException.NullValueException(_names[0].toString()); ! else ! return context.getBroker() ! ._propertyOperators.getProperty(context, value, _names, 1); ! } ! /** ! * Look up my the value of this variable in the specified Map, possibly ! * using introspection, and set it to the supplied value. ! * @exception PropertyException If the property does not exist ! */ ! public final void setValue(Context context, Object newValue) ! throws PropertyException { ! throw new PropertyException("Cannot set properties of a constant"); ! } ! /** ! * Return a string representation naming the variable for ! * debugging purposes. ! */ ! public final String toString() { ! return "constant-property:" + _vname; ! } } --- 33,84 ---- * @since 1.1 */ ! public class ConstantPropertyVariable extends Variable ! { ! private Object value; ! /** ! * No special initialization ! */ ! ConstantPropertyVariable (Object value, Object names[]) ! { ! super(names); ! this.value = value; ! } ! /** ! * Look up my value in the corresponding Map, possibly using introspection, ! * and return it ! * @exception PropertyException If the property does not exist ! */ ! public final Object getValue (Context context) ! throws PropertyException ! { ! if (value == null) ! throw new PropertyException.NullValueException(_names[0].toString()); ! else ! return context.getBroker() ! ._propertyOperators.getProperty(context, value, _names, 1); ! } ! /** ! * Look up my the value of this variable in the specified Map, possibly ! * using introspection, and set it to the supplied value. ! * @exception PropertyException If the property does not exist ! */ ! public final void setValue (Context context, Object newValue) ! throws PropertyException ! { ! throw new PropertyException("Cannot set properties of a constant"); ! } ! /** ! * Return a string representation naming the variable for ! * debugging purposes. ! */ ! public final String toString () ! { ! return "constant-property:" + _vname; ! } } Index: CrankyEvaluationExceptionHandler.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/engine/CrankyEvaluationExceptionHandler.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** CrankyEvaluationExceptionHandler.java 11 Jun 2002 17:43:21 -0000 1.7 --- CrankyEvaluationExceptionHandler.java 12 Jun 2003 00:47:45 -0000 1.8 *************** *** 41,121 **** */ ! public class CrankyEvaluationExceptionHandler implements EvaluationExceptionHandler { ! private Log _log; ! public CrankyEvaluationExceptionHandler() { ! } ! public CrankyEvaluationExceptionHandler(Broker b) { ! init(b, b.getSettings()); ! } ! public void init(Broker b, Settings config) { ! _log = b.getLog("engine"); ! } ! public void evaluate(Variable variable, Context context, Exception problem) ! throws PropertyException { ! // if it's a PropertyException set the current context location ! if (problem instanceof PropertyException) { ! ((PropertyException) problem) ! .setContextLocation(context.getCurrentLocation()); ! } ! else { ! // else, wrap it ! problem = new PropertyException("Error evaluating $" ! + variable.getVariableName(), ! problem, ! context.getCurrentLocation()); ! } ! // log it ! if (_log != null) ! _log.warning("Error evaluating $" + variable.getVariableName(), problem); ! // and rethrow it ! throw (PropertyException) problem; ! } ! public String expand(Variable variable, Context context, Exception problem) ! throws PropertyException { ! // if it's a PropertyException set the current context location ! if (problem instanceof PropertyException) { ! ((PropertyException) problem) ! .setContextLocation(context.getCurrentLocation()); ! } ! else { ! // else, wrap it ! problem = new PropertyException("Error expanding $" ! + variable.getVariableName(), ! problem, ! context.getCurrentLocation()); ! } ! // log it ! if (_log != null) ! _log.warning("Error expanding $" + variable.getVariableName(), problem); ! // and rethrow it ! throw (PropertyException) problem; ! } ! public String warningString(String warningText) throws PropertyException { ! throw new PropertyException("Evaluation warning: " + warningText); ! } ! public String errorString(String errorText) throws PropertyException { ! throw new PropertyException("Evaluation error: " + errorText); ! } } --- 41,133 ---- */ ! public class CrankyEvaluationExceptionHandler implements EvaluationExceptionHandler ! { ! private Log _log; ! public CrankyEvaluationExceptionHandler () ! { ! } ! public CrankyEvaluationExceptionHandler (Broker b) ! { ! init(b, b.getSettings()); ! } ! public void init (Broker b, Settings config) ! { ! _log = b.getLog("engine"); ! } ! public void evaluate (Variable variable, Context context, Exception problem) ! throws PropertyException ! { ! // if it's a PropertyException set the current context location ! if (problem instanceof PropertyException) ! { ! ((PropertyException) problem) ! .setContextLocation(context.getCurrentLocation()); ! } ! else ! { ! // else, wrap it ! problem = new PropertyException("Error evaluating $" ! + variable.getVariableName(), ! problem, ! context.getCurrentLocation()); ! } ! // log it ! if (_log != null) ! _log.warning("Error evaluating $" + variable.getVariableName(), problem); ! // and rethrow it ! throw (PropertyException) problem; ! } ! public String expand (Variable variable, Context context, Exception problem) ! throws PropertyException ! { ! // if it's a PropertyException set the current context location ! if (problem instanceof PropertyException) ! { ! ((PropertyException) problem) ! .setContextLocation(context.getCurrentLocation()); ! } ! else ! { ! // else, wrap it ! problem = new PropertyException("Error expanding $" ! + variable.getVariableName(), ! problem, ! context.getCurrentLocation()); ! } ! // log it ! if (_log != null) ! _log.warning("Error expanding $" + variable.getVariableName(), problem); ! // and rethrow it ! throw (PropertyException) problem; ! } ! public String warningString (String warningText) throws PropertyException ! { ! throw new PropertyException("Evaluation warning: " + warningText); ! } ! public String errorString (String errorText) throws PropertyException ! { ! throw new PropertyException("Evaluation error: " + errorText); ! } } Index: DebugEvaluationExceptionHandler.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/engine/DebugEvaluationExceptionHandler.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** DebugEvaluationExceptionHandler.java 11 Nov 2002 19:22:37 -0000 1.7 --- DebugEvaluationExceptionHandler.java 12 Jun 2003 00:47:45 -0000 1.8 *************** *** 62,67 **** package org.webmacro.engine; - import java.util.*; - import org.webmacro.Broker; import org.webmacro.Context; --- 62,65 ---- *************** *** 70,144 **** import org.webmacro.util.Settings; ! public class DebugEvaluationExceptionHandler implements EvaluationExceptionHandler { ! private Log _log; ! public DebugEvaluationExceptionHandler() { ! } ! public DebugEvaluationExceptionHandler(Broker b) { ! init(b, b.getSettings()); ! } ! public void init(Broker b, Settings config) { ! _log = b.getLog("engine"); ! } ! public void evaluate(Variable variable, Context context, Exception problem) throws PropertyException { ! handleError(variable, context, problem); ! } ! public String expand(Variable variable, Context context, Exception problem) throws PropertyException { ! return handleError(variable, context, problem); ! } - private String handleError(Variable variable, Context context, Exception problem) throws PropertyException { - String strError; ! ArrayList arlErrors = null; ! PropertyException propEx = null; ! if ( problem instanceof PropertyException ) { ! propEx = (PropertyException) problem; ! } ! else { ! propEx = new PropertyException("Error expanding $" + variable.getVariableName() ); ! } ! propEx.setContextLocation(context.getCurrentLocation()); ! strError = propEx.getMessage(); ! if ( ( context.containsKey( "WMERROR" ) ) && ( context.get( "WMERROR" ) instanceof ArrayList ) ) { ! arlErrors = (ArrayList) context.get("WMERROR"); ! } ! else { ! arlErrors = new ArrayList(); ! context.put("WMERROR", arlErrors); ! } ! if ( strError.lastIndexOf( "\r\n" ) >= 0 ) { ! strError = strError.substring( strError.lastIndexOf( "\r\n" ) ); ! strError = strError.trim(); ! } ! if ( !arlErrors.contains( strError ) ) { ! arlErrors.add(strError); ! } ! if (_log != null) { ! _log.warning(strError, propEx); ! } ! // and rethrow it ! throw propEx; ! } ! public String warningString(String strText) throws PropertyException { ! throw new PropertyException("Evaluation warning: " + strText); ! } ! public String errorString(String strText) throws PropertyException { ! throw new PropertyException("Evaluation error: " + strText); ! } } --- 68,160 ---- import org.webmacro.util.Settings; ! import java.util.ArrayList; ! public class DebugEvaluationExceptionHandler implements EvaluationExceptionHandler ! { ! private Log _log; ! public DebugEvaluationExceptionHandler () ! { ! } ! public DebugEvaluationExceptionHandler (Broker b) ! { ! init(b, b.getSettings()); ! ... [truncated message content] |