kinman 2003/02/13 11:46:11 Modified: jasper2/src/share/org/apache/jasper/compiler Generator.java PageInfo.java TagPluginManager.java jasper2/src/share/org/apache/jasper/compiler/tagplugin TagPluginContext.java jasper2/src/share/org/apache/jasper/tagplugins/jstl ForEach.java Log: - JSTL plugin now handles arrays. - Add generatedeclaration to the interface to allow method declarations. Revision Changes Path 1.163 +18 -5 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java Index: Generator.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java,v retrieving revision 1.162 retrieving revision 1.163 diff -u -r1.162 -r1.163 --- Generator.java 11 Feb 2003 21:43:58 -0000 1.162 +++ Generator.java 13 Feb 2003 19:46:10 -0000 1.163 @@ -167,6 +167,19 @@ out.printMultiLn(new String(n.getText())); out.println(); } + + // Custom Tags may contain declarations from tag plugins. + public void visit(Node.CustomTag n) throws JasperException { + if (n.useTagPlugin()) { + if (n.getAtSTag() != null) { + n.getAtSTag().visit(this); + } + visitBody(n); + if (n.getAtETag() != null) { + n.getAtETag().visit(this); + } + } + } } out.println(); @@ -2896,8 +2909,8 @@ */ private void genCommonPostamble() { // Append any methods that were generated - out.print(methodsBuffer.toString()); - + out.printMultiLn(methodsBuffer.toString()); + // Append the helper class if( fragmentHelperClass.isUsed() ) { fragmentHelperClass.generatePostamble(); 1.20 +17 -3 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/PageInfo.java Index: PageInfo.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/PageInfo.java,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- PageInfo.java 12 Feb 2003 23:44:22 -0000 1.19 +++ PageInfo.java 13 Feb 2003 19:46:11 -0000 1.20 @@ -117,6 +117,7 @@ private boolean hasJspRoot = false; private Vector includePrelude; private Vector includeCoda; + private Vector pluginDcls; // Id's for tagplugin declarations PageInfo(BeanRepository beanRepository) { @@ -126,10 +127,23 @@ this.dependants = new Vector(); this.includePrelude = new Vector(); this.includeCoda = new Vector(); + this.pluginDcls = new Vector(); // Enter standard imports for(int i = 0; i < Constants.STANDARD_IMPORTS.length; i++) imports.add(Constants.STANDARD_IMPORTS[i]); + } + + /** + * Check if the plugin ID has been previously declared. Make a not + * that this Id is now declared. + * @return true if Id has been declared. + */ + public boolean isPluginDeclared(String id) { + if (pluginDcls.contains(id)) + return true; + pluginDcls.add(id); + return false; } public void addImports(List imports) { 1.13 +10 -3 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/TagPluginManager.java Index: TagPluginManager.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/TagPluginManager.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- TagPluginManager.java 12 Feb 2003 02:40:43 -0000 1.12 +++ TagPluginManager.java 13 Feb 2003 19:46:11 -0000 1.13 @@ -216,6 +216,13 @@ pageInfo.addImport(imp); } + public void generateDeclaration(String id, String text) { + if (pageInfo.isPluginDeclared(id)) { + return; + } + curNodes.add(new Node.Declaration(text, node.getStart(), null)); + } + public void generateJavaSource(String sourceCode) { curNodes.add(new Node.Scriptlet(sourceCode, node.getStart(), null)); 1.8 +15 -3 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/tagplugin/TagPluginContext.java Index: TagPluginContext.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/tagplugin/TagPluginContext.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- TagPluginContext.java 12 Feb 2003 02:22:51 -0000 1.7 +++ TagPluginContext.java 13 Feb 2003 19:46:11 -0000 1.8 @@ -93,6 +93,18 @@ void generateImport(String s); /** + * Generate a declaration in the of the generated class. This can be + * used to declare an innter class, a method, or a class variable. + * @param id An unique ID identifying the declaration. It is not + * part of the declaration, and is used to ensure that the + * declaration will only appear once. If this method is + * invoked with the same id more than once in the translation + * unit, only the first declaration will be taken. + * @param text The text of the declaration. + **/ + void generateDeclaration(String id, String text); + + /** * Generate Java source codes */ void generateJavaSource(String s); 1.5 +175 -19 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/tagplugins/jstl/ForEach.java Index: ForEach.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/tagplugins/jstl/ForEach.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- ForEach.java 12 Feb 2003 02:22:51 -0000 1.4 +++ ForEach.java 13 Feb 2003 19:46:11 -0000 1.5 @@ -65,6 +65,8 @@ public final class ForEach implements TagPlugin { + private boolean hasVar, hasBegin, hasEnd, hasStep; + public void doTag(TagPluginContext ctxt) { String index = null; @@ -75,17 +77,17 @@ return; } + hasVar = ctxt.isAttributeSpecified("var"); + hasBegin = ctxt.isAttributeSpecified("begin"); + hasEnd = ctxt.isAttributeSpecified("end"); + hasStep = ctxt.isAttributeSpecified("step"); + boolean hasItems = ctxt.isAttributeSpecified("items"); if (hasItems) { doCollection(ctxt); return; } - boolean hasVar = ctxt.isAttributeSpecified("var"); - boolean hasBegin = ctxt.isAttributeSpecified("begin"); - boolean hasEnd = ctxt.isAttributeSpecified("end"); - boolean hasStep = ctxt.isAttributeSpecified("step"); - // We must have a begin and end attributes index = ctxt.getTemporaryVariableName(); ctxt.generateJavaSource("for (int " + index + " = "); @@ -117,12 +119,9 @@ * The pseudo code is: */ private void doCollection(TagPluginContext ctxt) { - boolean hasVar = ctxt.isAttributeSpecified("var"); - boolean hasBegin = ctxt.isAttributeSpecified("begin"); - boolean hasEnd = ctxt.isAttributeSpecified("end"); - boolean hasStep = ctxt.isAttributeSpecified("step"); ctxt.generateImport("java.util.*"); + generateIterators(ctxt); String itemsV = ctxt.getTemporaryVariableName(); ctxt.generateJavaSource("Object " + itemsV + "= "); @@ -151,10 +150,38 @@ ctxt.generateJavaSource(";"); } - ctxt.generateJavaSource("if (" + itemsV + " instanceof Collection) {"); String iterV = ctxt.getTemporaryVariableName(); - ctxt.generateJavaSource("Iterator " + iterV + " = "); - ctxt.generateJavaSource("((Collection)" + itemsV + ").iterator();"); + ctxt.generateJavaSource("Iterator " + iterV + " = null;"); + // Object[] + ctxt.generateJavaSource("if (" + itemsV + " instanceof Object[])"); + ctxt.generateJavaSource(iterV + "=toIterator((Object[])" + itemsV + ");"); + // boolean[] + ctxt.generateJavaSource("else if (" + itemsV + " instanceof boolean[])"); + ctxt.generateJavaSource(iterV + "=toIterator((boolean[])" + itemsV + ");"); + // byte[] + ctxt.generateJavaSource("else if (" + itemsV + " instanceof byte[])"); + ctxt.generateJavaSource(iterV + "=toIterator((byte[])" + itemsV + ");"); + // char[] + ctxt.generateJavaSource("else if (" + itemsV + " instanceof char[])"); + ctxt.generateJavaSource(iterV + "=toIterator((char[])" + itemsV + ");"); + // short[] + ctxt.generateJavaSource("else if (" + itemsV + " instanceof short[])"); + ctxt.generateJavaSource(iterV + "=toIterator((short[])" + itemsV + ");"); + // int[] + ctxt.generateJavaSource("else if (" + itemsV + " instanceof int[])"); + ctxt.generateJavaSource(iterV + "=toIterator((int[])" + itemsV + ");"); + // long[] + ctxt.generateJavaSource("else if (" + itemsV + " instanceof long[])"); + ctxt.generateJavaSource(iterV + "=toIterator((long[])" + itemsV + ");"); + // float[] + ctxt.generateJavaSource("else if (" + itemsV + " instanceof float[])"); + ctxt.generateJavaSource(iterV + "=toIterator((float[])" + itemsV + ");"); + // double[] + ctxt.generateJavaSource("else if (" + itemsV + " instanceof double[])"); + ctxt.generateJavaSource(iterV + "=toIterator((double[])" + itemsV + ");"); + + ctxt.generateJavaSource("else if (" + itemsV + " instanceof Collection)"); + ctxt.generateJavaSource(iterV + "=((Collection)" + itemsV + ").iterator();"); if (hasBegin) { String tV = ctxt.getTemporaryVariableName(); @@ -165,12 +192,10 @@ } ctxt.generateJavaSource("while (" + iterV + ".hasNext()){"); - String nextV = ctxt.getTemporaryVariableName(); - ctxt.generateJavaSource("Object " + nextV + " = " + iterV + ".next();"); if (hasVar) { ctxt.generateJavaSource("pageContext.setAttribute("); ctxt.generateAttribute("var"); - ctxt.generateJavaSource(", " + nextV + ");"); + ctxt.generateJavaSource(", " + iterV + ".next());"); } ctxt.generateBody(); @@ -199,6 +224,137 @@ ctxt.generateJavaSource("break;"); } ctxt.generateJavaSource("}"); // while - ctxt.generateJavaSource("}"); // if + } + + /** + * Generate iterators for data types supported in items + */ + private void generateIterators(TagPluginContext ctxt) { + + // Object[] + ctxt.generateDeclaration("ObjectArrayIterator", + "private Iterator toIterator(final Object[] a){\n" + + " return (new Iterator() {\n" + + " int index=0;\n" + + " public boolean hasNext() {\n" + + " return index < a.length;}\n" + + " public Object next() {\n" + + " return a[index++];}\n" + + " public void remove() {}\n" + + " });\n" + + "}" + ); + + // boolean[] + ctxt.generateDeclaration("booleanArrayIterator", + "private Iterator toIterator(final boolean[] a){\n" + + " return (new Iterator() {\n" + + " int index=0;\n" + + " public boolean hasNext() {\n" + + " return index < a.length;}\n" + + " public Object next() {\n" + + " return new Boolean(a[index++]);}\n" + + " public void remove() {}\n" + + " });\n" + + "}" + ); + + // byte[] + ctxt.generateDeclaration("byteArrayIterator", + "private Iterator toIterator(final byte[] a){\n" + + " return (new Iterator() {\n" + + " int index=0;\n" + + " public boolean hasNext() {\n" + + " return index < a.length;}\n" + + " public Object next() {\n" + + " return new Byte(a[index++]);}\n" + + " public void remove() {}\n" + + " });\n" + + "}" + ); + + // char[] + ctxt.generateDeclaration("charArrayIterator", + "private Iterator toIterator(final char[] a){\n" + + " return (new Iterator() {\n" + + " int index=0;\n" + + " public boolean hasNext() {\n" + + " return index < a.length;}\n" + + " public Object next() {\n" + + " return new Character(a[index++]);}\n" + + " public void remove() {}\n" + + " });\n" + + "}" + ); + + // short[] + ctxt.generateDeclaration("shortArrayIterator", + "private Iterator toIterator(final short[] a){\n" + + " return (new Iterator() {\n" + + " int index=0;\n" + + " public boolean hasNext() {\n" + + " return index < a.length;}\n" + + " public Object next() {\n" + + " return new Short(a[index++]);}\n" + + " public void remove() {}\n" + + " });\n" + + "}" + ); + + // int[] + ctxt.generateDeclaration("intArrayIterator", + "private Iterator toIterator(final int[] a){\n" + + " return (new Iterator() {\n" + + " int index=0;\n" + + " public boolean hasNext() {\n" + + " return index < a.length;}\n" + + " public Object next() {\n" + + " return new Integer(a[index++]);}\n" + + " public void remove() {}\n" + + " });\n" + + "}" + ); + + // long[] + ctxt.generateDeclaration("longArrayIterator", + "private Iterator toIterator(final long[] a){\n" + + " return (new Iterator() {\n" + + " int index=0;\n" + + " public boolean hasNext() {\n" + + " return index < a.length;}\n" + + " public Object next() {\n" + + " return new Long(a[index++]);}\n" + + " public void remove() {}\n" + + " });\n" + + "}" + ); + + // float[] + ctxt.generateDeclaration("floatArrayIterator", + "private Iterator toIterator(final float[] a){\n" + + " return (new Iterator() {\n" + + " int index=0;\n" + + " public boolean hasNext() {\n" + + " return index < a.length;}\n" + + " public Object next() {\n" + + " return new Float(a[index++]);}\n" + + " public void remove() {}\n" + + " });\n" + + "}" + ); + + // double[] + ctxt.generateDeclaration("doubleArrayIterator", + "private Iterator toIterator(final double[] a){\n" + + " return (new Iterator() {\n" + + " int index=0;\n" + + " public boolean hasNext() {\n" + + " return index < a.length;}\n" + + " public Object next() {\n" + + " return new Double(a[index++]);}\n" + + " public void remove() {}\n" + + " });\n" + + "}" + ); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]