You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by mu...@apache.org on 2009/07/31 20:12:51 UTC
svn commit: r799681 [20/24] - in /struts/sandbox/trunk/struts2-jsp-plugin:
./ src/main/java/org/apache/struts/ src/main/java/org/apache/struts2/
src/main/java/org/apache/struts2/compiler/
src/main/java/org/apache/struts2/jasper/ src/main/java/org/apach...
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/ForTokens.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/ForTokens.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/ForTokens.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/ForTokens.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.struts2.jasper.tagplugins.jstl.core;
+
+import org.apache.struts2.jasper.compiler.tagplugin.TagPlugin;
+import org.apache.struts2.jasper.compiler.tagplugin.TagPluginContext;
+
+public class ForTokens implements TagPlugin {
+
+ public void doTag(TagPluginContext ctxt) {
+ boolean hasVar, hasVarStatus, hasBegin, hasEnd, hasStep;
+
+ //init the flags
+ hasVar = ctxt.isAttributeSpecified("var");
+ hasVarStatus = ctxt.isAttributeSpecified("varStatus");
+ hasBegin = ctxt.isAttributeSpecified("begin");
+ hasEnd = ctxt.isAttributeSpecified("end");
+ hasStep = ctxt.isAttributeSpecified("step");
+
+ if(hasVarStatus){
+ ctxt.dontUseTagPlugin();
+ return;
+ }
+
+ //define all the temp variables' names
+ String itemsName = ctxt.getTemporaryVariableName();
+ String delimsName = ctxt.getTemporaryVariableName();
+ String stName = ctxt.getTemporaryVariableName();
+ String beginName = ctxt.getTemporaryVariableName();
+ String endName = ctxt.getTemporaryVariableName();
+ String stepName = ctxt.getTemporaryVariableName();
+ String index = ctxt.getTemporaryVariableName();
+ String temp = ctxt.getTemporaryVariableName();
+ String tokensCountName = ctxt.getTemporaryVariableName();
+
+ //get the value of the "items" attribute
+ ctxt.generateJavaSource("String " + itemsName + " = (String)");
+ ctxt.generateAttribute("items");
+ ctxt.generateJavaSource(";");
+
+ //get the value of the "delim" attribute
+ ctxt.generateJavaSource("String " + delimsName + " = (String)");
+ ctxt.generateAttribute("delims");
+ ctxt.generateJavaSource(";");
+
+ //new a StringTokenizer Object according to the "items" and the "delim"
+ ctxt.generateJavaSource("java.util.StringTokenizer " + stName + " = " +
+ "new java.util.StringTokenizer(" + itemsName + ", " + delimsName + ");");
+
+ //if "begin" specified, move the token to the "begin" place
+ //and record the begin index. default begin place is 0.
+ ctxt.generateJavaSource("int " + tokensCountName + " = " + stName + ".countTokens();");
+ if(hasBegin){
+ ctxt.generateJavaSource("int " + beginName + " = " );
+ ctxt.generateAttribute("begin");
+ ctxt.generateJavaSource(";");
+ ctxt.generateJavaSource("for(int " + index + " = 0; " + index + " < " + beginName + " && " + stName + ".hasMoreTokens(); " + index + "++, " + stName + ".nextToken()){}");
+ }else{
+ ctxt.generateJavaSource("int " + beginName + " = 0;");
+ }
+
+ //when "end" is specified, if the "end" is more than the last index,
+ //record the end place as the last index, otherwise, record it as "end";
+ //default end place is the last index
+ if(hasEnd){
+ ctxt.generateJavaSource("int " + endName + " = 0;" );
+ ctxt.generateJavaSource("if((" + tokensCountName + " - 1) < ");
+ ctxt.generateAttribute("end");
+ ctxt.generateJavaSource("){");
+ ctxt.generateJavaSource(" " + endName + " = " + tokensCountName + " - 1;");
+ ctxt.generateJavaSource("}else{");
+ ctxt.generateJavaSource(" " + endName + " = ");
+ ctxt.generateAttribute("end");
+ ctxt.generateJavaSource(";}");
+ }else{
+ ctxt.generateJavaSource("int " + endName + " = " + tokensCountName + " - 1;");
+ }
+
+ //get the step value from "step" if specified.
+ //default step value is 1.
+ if(hasStep){
+ ctxt.generateJavaSource("int " + stepName + " = " );
+ ctxt.generateAttribute("step");
+ ctxt.generateJavaSource(";");
+ }else{
+ ctxt.generateJavaSource("int " + stepName + " = 1;");
+ }
+
+ //the loop
+ ctxt.generateJavaSource("for(int " + index + " = " + beginName + "; " + index + " <= " + endName + "; " + index + "++){");
+ ctxt.generateJavaSource(" String " + temp + " = " + stName + ".nextToken();");
+ ctxt.generateJavaSource(" if(((" + index + " - " + beginName + ") % " + stepName + ") == 0){");
+ //if var specified, put the current token into the attribute "var" defines.
+ if(hasVar){
+ String strVar = ctxt.getConstantAttribute("var");
+ ctxt.generateJavaSource(" pageContext.setAttribute(\"" + strVar + "\", " + temp + ");");
+ }
+ ctxt.generateBody();
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource("}");
+ }
+
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/If.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/If.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/If.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/If.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.struts2.jasper.tagplugins.jstl.core;
+
+import org.apache.struts2.jasper.compiler.tagplugin.*;
+
+public final class If implements TagPlugin {
+
+ public void doTag(TagPluginContext ctxt) {
+ String condV = ctxt.getTemporaryVariableName();
+ ctxt.generateJavaSource("boolean " + condV + "=");
+ ctxt.generateAttribute("test");
+ ctxt.generateJavaSource(";");
+ if (ctxt.isAttributeSpecified("var")) {
+ String scope = "PageContext.PAGE_SCOPE";
+ if (ctxt.isAttributeSpecified("scope")) {
+ String scopeStr = ctxt.getConstantAttribute("scope");
+ if ("request".equals(scopeStr)) {
+ scope = "PageContext.REQUEST_SCOPE";
+ } else if ("session".equals(scopeStr)) {
+ scope = "PageContext.SESSION_SCOPE";
+ } else if ("application".equals(scopeStr)) {
+ scope = "PageContext.APPLICATION_SCOPE";
+ }
+ }
+ ctxt.generateJavaSource("_jspx_page_context.setAttribute(");
+ ctxt.generateAttribute("var");
+ ctxt.generateJavaSource(", new Boolean(" + condV + ")," + scope + ");");
+ }
+ ctxt.generateJavaSource("if (" + condV + "){");
+ ctxt.generateBody();
+ ctxt.generateJavaSource("}");
+ }
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Import.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Import.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Import.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Import.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,382 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.struts2.jasper.tagplugins.jstl.core;
+
+import org.apache.struts2.jasper.compiler.tagplugin.TagPlugin;
+import org.apache.struts2.jasper.compiler.tagplugin.TagPluginContext;
+import org.apache.struts2.jasper.tagplugins.jstl.Util;
+
+public class Import implements TagPlugin {
+
+ public void doTag(TagPluginContext ctxt) {
+ boolean hasContext, hasVar, hasScope, hasVarReader, hasCharEncoding;
+
+ //flags
+ hasContext = ctxt.isAttributeSpecified("context");
+ hasVar = ctxt.isAttributeSpecified("var");
+ hasScope = ctxt.isAttributeSpecified("scope");
+ hasVarReader = ctxt.isAttributeSpecified("varReader");
+ hasCharEncoding = ctxt.isAttributeSpecified("charEncoding");
+
+ //variables' names
+ String urlName = ctxt.getTemporaryVariableName();
+ String contextName = ctxt.getTemporaryVariableName();
+ String iauName = ctxt.getTemporaryVariableName(); // is absolute url
+ String urlObjName = ctxt.getTemporaryVariableName(); //URL object
+ String ucName = ctxt.getTemporaryVariableName(); //URLConnection
+ String inputStreamName = ctxt.getTemporaryVariableName();
+ String tempReaderName = ctxt.getTemporaryVariableName();
+ String tempReaderName2 = ctxt.getTemporaryVariableName();
+ String charSetName = ctxt.getTemporaryVariableName();
+ String charEncodingName = ctxt.getTemporaryVariableName();
+ String contentTypeName = ctxt.getTemporaryVariableName();
+ String varReaderName = ctxt.getTemporaryVariableName();
+ String servletContextName = ctxt.getTemporaryVariableName();
+ String servletPathName = ctxt.getTemporaryVariableName();
+ String requestDispatcherName = ctxt.getTemporaryVariableName();
+ String irwName = ctxt.getTemporaryVariableName(); //ImportResponseWrapper name
+ String brName = ctxt.getTemporaryVariableName(); //BufferedReader name
+ String sbName = ctxt.getTemporaryVariableName(); //StringBuffer name
+ String tempStringName = ctxt.getTemporaryVariableName();
+
+ //is absolute url
+ ctxt.generateJavaSource("boolean " + iauName + ";");
+
+ //get the url value
+ ctxt.generateJavaSource("String " + urlName + " = ");
+ ctxt.generateAttribute("url");
+ ctxt.generateJavaSource(";");
+
+ //validate the url
+ ctxt.generateJavaSource("if(" + urlName + " == null || " + urlName + ".equals(\"\")){");
+ ctxt.generateJavaSource(" throw new JspTagException(\"The \\\"url\\\" attribute " +
+ "illegally evaluated to \\\"null\\\" or \\\"\\\" in <import>\");");
+ ctxt.generateJavaSource("}");
+
+ //initialize the is_absolute_url
+ ctxt.generateJavaSource(iauName + " = " +
+ "org.apache.struts2.jasper.tagplugins.jstl.Util.isAbsoluteUrl(" + urlName + ");");
+
+ //validate the context
+ if(hasContext){
+
+ ctxt.generateJavaSource("String " + contextName + " = ");
+ ctxt.generateAttribute("context");
+ ctxt.generateJavaSource(";");
+
+ ctxt.generateJavaSource("if((!" + contextName + ".startsWith(\"/\")) " +
+ "|| (!" + urlName + ".startsWith(\"/\"))){");
+ ctxt.generateJavaSource(" throw new JspTagException" +
+ "(\"In URL tags, when the \\\"context\\\" attribute is specified, " +
+ "values of both \\\"context\\\" and \\\"url\\\" must start with \\\"/\\\".\");");
+ ctxt.generateJavaSource("}");
+
+ }
+
+ //define charset
+ ctxt.generateJavaSource("String " + charSetName + " = null;");
+
+ //if the charEncoding attribute is specified
+ if(hasCharEncoding){
+
+ //initialize the charEncoding
+ ctxt.generateJavaSource("String " + charEncodingName + " = ");
+ ctxt.generateAttribute("charEncoding");
+ ctxt.generateJavaSource(";");
+
+ //assign appropriate value tp the charset
+ ctxt.generateJavaSource("if(null != " + charEncodingName + " " +
+ "&& !" + charEncodingName + ".equals(\"\")){");
+ ctxt.generateJavaSource(" " + charSetName + " = "
+ + charEncodingName + ";");
+ ctxt.generateJavaSource("}");
+ }
+
+ //reshape the url string
+ ctxt.generateJavaSource("if(!"+iauName+"){");
+ ctxt.generateJavaSource(" if(!" + urlName + ".startsWith(\"/\")){");
+ ctxt.generateJavaSource(" String " + servletPathName + " = " +
+ "((HttpServletRequest)pageContext.getRequest()).getServletPath();");
+ ctxt.generateJavaSource(" " + urlName + " = "
+ + servletPathName + ".substring(0," + servletPathName + ".lastIndexOf('/')) + '/' + " + urlName + ";");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource("}");
+
+ //if the varReader attribute specified
+ if(hasVarReader){
+
+ //get the String value of varReader
+ ctxt.generateJavaSource("String " + varReaderName + " = ");
+ ctxt.generateAttribute("varReader");
+ ctxt.generateJavaSource(";");
+
+ //if the url is absolute url
+ ctxt.generateJavaSource("if(" + iauName + "){");
+
+ //get the content of the target
+ ctxt.generateJavaSource(" java.net.URL " + urlObjName + " = new java.net.URL(" + urlName + ");");
+ ctxt.generateJavaSource(" java.net.URLConnection " + ucName + " = "
+ + urlObjName + ".openConnection();");
+ ctxt.generateJavaSource(" java.io.InputStream " + inputStreamName + " = "
+ + ucName + ".getInputStream();");
+
+ ctxt.generateJavaSource(" if(" + charSetName + " == null){");
+ ctxt.generateJavaSource(" String " + contentTypeName + " = "
+ + ucName + ".getContentType();");
+ ctxt.generateJavaSource(" if(null != " + contentTypeName + "){");
+ ctxt.generateJavaSource(" " + charSetName + " = " +
+ "org.apache.struts2.jasper.tagplugins.jstl.Util.getContentTypeAttribute(" + contentTypeName + ", \"charset\");");
+ ctxt.generateJavaSource(" if(" + charSetName + " == null) "
+ + charSetName + " = org.apache.struts2.jasper.tagplugins.jstl.Util.DEFAULT_ENCODING;");
+ ctxt.generateJavaSource(" }else{");
+ ctxt.generateJavaSource(" " + charSetName + " = org.apache.struts2.jasper.tagplugins.jstl.Util.DEFAULT_ENCODING;");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" }");
+
+ if(!hasCharEncoding){
+ ctxt.generateJavaSource(" String " + contentTypeName + " = " + ucName + ".getContentType();");
+ }
+
+ //define the Reader
+ ctxt.generateJavaSource(" java.io.Reader " + tempReaderName + " = null;");
+
+ //initialize the Reader object
+ ctxt.generateJavaSource(" try{");
+ ctxt.generateJavaSource(" " + tempReaderName + " = new java.io.InputStreamReader(" + inputStreamName + ", " + charSetName + ");");
+ ctxt.generateJavaSource(" }catch(Exception ex){");
+ ctxt.generateJavaSource(" " + tempReaderName + " = new java.io.InputStreamReader(" + inputStreamName + ", org.apache.struts2.jasper.tagplugins.jstl.Util.DEFAULT_ENCODING);");
+ ctxt.generateJavaSource(" }");
+
+ //validate the response
+ ctxt.generateJavaSource(" if(" + ucName + " instanceof java.net.HttpURLConnection){");
+ ctxt.generateJavaSource(" int status = ((java.net.HttpURLConnection) " + ucName + ").getResponseCode();");
+ ctxt.generateJavaSource(" if(status < 200 || status > 299){");
+ ctxt.generateJavaSource(" throw new JspTagException(status + \" \" + " + urlName + ");");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" }");
+
+ //set attribute in the page context scope
+ ctxt.generateJavaSource(" pageContext.setAttribute(" + varReaderName + ", " + tempReaderName + ");");
+
+ //if the url is relative
+ ctxt.generateJavaSource("}else{");
+
+ //if the url is relative, http request is needed
+ ctxt.generateJavaSource(" if (!(pageContext.getRequest() instanceof HttpServletRequest " +
+ "&& pageContext.getResponse() instanceof HttpServletResponse)){");
+ ctxt.generateJavaSource(" throw new JspTagException(\"Relative <import> from non-HTTP request not allowed\");");
+ ctxt.generateJavaSource(" }");
+
+ //get the servlet context of the context defined in the context attribute
+ ctxt.generateJavaSource(" ServletContext " + servletContextName + " = null;");
+ if(hasContext){
+ ctxt.generateJavaSource(" if(null != " + contextName + "){");
+ ctxt.generateJavaSource(" " + servletContextName + " = pageContext.getServletContext().getContext(" + contextName + ");" );
+ ctxt.generateJavaSource(" }else{");
+ ctxt.generateJavaSource(" " + servletContextName + " = pageContext.getServletContext();");
+ ctxt.generateJavaSource(" }");
+ }else{
+ ctxt.generateJavaSource(" " + servletContextName + " = pageContext.getServletContext();");
+ }
+
+ //
+ ctxt.generateJavaSource(" if(" + servletContextName + " == null){");
+ if(hasContext){
+ ctxt.generateJavaSource(" throw new JspTagException(\"Unable to get RequestDispatcher for Context: \\\" \"+" + contextName + "+\" \\\" and URL: \\\" \" +" + urlName + "+ \" \\\". Verify values and/or enable cross context access.\");");
+ }else{
+ ctxt.generateJavaSource(" throw new JspTagException(\"Unable to get RequestDispatcher for URL: \\\" \" +" + urlName + "+ \" \\\". Verify values and/or enable cross context access.\");");
+ }
+ ctxt.generateJavaSource(" }");
+
+ //get the request dispatcher
+ ctxt.generateJavaSource(" RequestDispatcher " + requestDispatcherName + " = " + servletContextName + ".getRequestDispatcher(org.apache.struts2.jasper.tagplugins.jstl.Util.stripSession("+urlName+"));");
+ ctxt.generateJavaSource(" if(" + requestDispatcherName + " == null) throw new JspTagException(org.apache.struts2.jasper.tagplugins.jstl.Util.stripSession("+urlName+"));");
+
+ //initialize a ImportResponseWrapper to include the resource
+ ctxt.generateJavaSource(" org.apache.struts2.jasper.tagplugins.jstl.Util.ImportResponseWrapper " + irwName + " = new org.apache.struts2.jasper.tagplugins.jstl.Util.ImportResponseWrapper((HttpServletResponse) pageContext.getResponse());");
+ ctxt.generateJavaSource(" if(" + charSetName + " == null){");
+ ctxt.generateJavaSource(" " + charSetName + " = org.apache.struts2.jasper.tagplugins.jstl.Util.DEFAULT_ENCODING;");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" " + irwName + ".setCharEncoding(" + charSetName + ");");
+ ctxt.generateJavaSource(" try{");
+ ctxt.generateJavaSource(" " + requestDispatcherName + ".include(pageContext.getRequest(), " + irwName + ");");
+ ctxt.generateJavaSource(" }catch(java.io.IOException ex){");
+ ctxt.generateJavaSource(" throw new JspException(ex);");
+ ctxt.generateJavaSource(" }catch(RuntimeException ex){");
+ ctxt.generateJavaSource(" throw new JspException(ex);");
+ ctxt.generateJavaSource(" }catch(ServletException ex){");
+ ctxt.generateJavaSource(" Throwable rc = ex.getRootCause();");
+ ctxt.generateJavaSource(" if (rc == null)");
+ ctxt.generateJavaSource(" throw new JspException(ex);");
+ ctxt.generateJavaSource(" else");
+ ctxt.generateJavaSource(" throw new JspException(rc);");
+ ctxt.generateJavaSource(" }");
+
+ //validate the response status
+ ctxt.generateJavaSource(" if(" + irwName + ".getStatus() < 200 || " + irwName + ".getStatus() > 299){");
+ ctxt.generateJavaSource(" throw new JspTagException(" + irwName + ".getStatus()+\" \" + org.apache.struts2.jasper.tagplugins.jstl.Util.stripSession(" + urlName + "));");
+ ctxt.generateJavaSource(" }");
+
+ //push in the page context
+ ctxt.generateJavaSource(" java.io.Reader " + tempReaderName + " = new java.io.StringReader(" + irwName + ".getString());");
+ ctxt.generateJavaSource(" pageContext.setAttribute(" + varReaderName + ", " + tempReaderName + ");");
+
+ ctxt.generateJavaSource("}");
+
+ //execute the body action
+ ctxt.generateBody();
+
+ //close the reader
+ ctxt.generateJavaSource("java.io.Reader " + tempReaderName2 + " = (java.io.Reader)pageContext.getAttribute(" + varReaderName + ");");
+ ctxt.generateJavaSource("if(" + tempReaderName2 + " != null) " + tempReaderName2 + ".close();");
+ ctxt.generateJavaSource("pageContext.removeAttribute(" + varReaderName + ",1);");
+ }
+
+ //if the varReader is not specified
+ else{
+
+ ctxt.generateJavaSource("pageContext.setAttribute(\"url_without_param\"," + urlName + ");");
+ ctxt.generateBody();
+ ctxt.generateJavaSource(urlName + " = (String)pageContext.getAttribute(\"url_without_param\");");
+ ctxt.generateJavaSource("pageContext.removeAttribute(\"url_without_param\");");
+ String strScope = "page";
+ if(hasScope){
+ strScope = ctxt.getConstantAttribute("scope");
+ }
+ int iScope = Util.getScope(strScope);
+
+ ctxt.generateJavaSource("String " + tempStringName + " = null;");
+
+ ctxt.generateJavaSource("if(" + iauName + "){");
+
+ //get the content of the target
+ ctxt.generateJavaSource(" java.net.URL " + urlObjName + " = new java.net.URL(" + urlName + ");");
+ ctxt.generateJavaSource(" java.net.URLConnection " + ucName + " = " + urlObjName + ".openConnection();");
+ ctxt.generateJavaSource(" java.io.InputStream " + inputStreamName + " = " + ucName + ".getInputStream();");
+ ctxt.generateJavaSource(" java.io.Reader " + tempReaderName + " = null;");
+
+ ctxt.generateJavaSource(" if(" + charSetName + " == null){");
+ ctxt.generateJavaSource(" String " + contentTypeName + " = "
+ + ucName + ".getContentType();");
+ ctxt.generateJavaSource(" if(null != " + contentTypeName + "){");
+ ctxt.generateJavaSource(" " + charSetName + " = " +
+ "org.apache.struts2.jasper.tagplugins.jstl.Util.getContentTypeAttribute(" + contentTypeName + ", \"charset\");");
+ ctxt.generateJavaSource(" if(" + charSetName + " == null) "
+ + charSetName + " = org.apache.struts2.jasper.tagplugins.jstl.Util.DEFAULT_ENCODING;");
+ ctxt.generateJavaSource(" }else{");
+ ctxt.generateJavaSource(" " + charSetName + " = org.apache.struts2.jasper.tagplugins.jstl.Util.DEFAULT_ENCODING;");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" }");
+
+ ctxt.generateJavaSource(" try{");
+ ctxt.generateJavaSource(" " + tempReaderName + " = new java.io.InputStreamReader(" + inputStreamName + "," + charSetName + ");");
+ ctxt.generateJavaSource(" }catch(Exception ex){");
+ //ctxt.generateJavaSource(" throw new JspTagException(ex.toString());");
+ ctxt.generateJavaSource(" " + tempReaderName + " = new java.io.InputStreamReader(" + inputStreamName + ",org.apache.struts2.jasper.tagplugins.jstl.Util.DEFAULT_ENCODING);");
+ ctxt.generateJavaSource(" }");
+
+ //validate the response
+ ctxt.generateJavaSource(" if(" + ucName + " instanceof java.net.HttpURLConnection){");
+ ctxt.generateJavaSource(" int status = ((java.net.HttpURLConnection) " + ucName + ").getResponseCode();");
+ ctxt.generateJavaSource(" if(status < 200 || status > 299){");
+ ctxt.generateJavaSource(" throw new JspTagException(status + \" \" + " + urlName + ");");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" }");
+
+ ctxt.generateJavaSource(" java.io.BufferedReader " + brName + " = new java.io.BufferedReader(" + tempReaderName + ");");
+ ctxt.generateJavaSource(" StringBuffer " + sbName + " = new StringBuffer();");
+ String index = ctxt.getTemporaryVariableName();
+ ctxt.generateJavaSource(" int " + index + ";");
+ ctxt.generateJavaSource(" while(("+index+" = "+brName+".read()) != -1) "+sbName+".append((char)"+index+");");
+ ctxt.generateJavaSource(" " + tempStringName + " = " +sbName + ".toString();");
+
+ ctxt.generateJavaSource("}else{");
+
+ //if the url is relative, http request is needed.
+ ctxt.generateJavaSource(" if (!(pageContext.getRequest() instanceof HttpServletRequest " +
+ "&& pageContext.getResponse() instanceof HttpServletResponse)){");
+ ctxt.generateJavaSource(" throw new JspTagException(\"Relative <import> from non-HTTP request not allowed\");");
+ ctxt.generateJavaSource(" }");
+
+ //get the servlet context of the context defined in the context attribute
+ ctxt.generateJavaSource(" ServletContext " + servletContextName + " = null;");
+ if(hasContext){
+ ctxt.generateJavaSource(" if(null != " + contextName + "){");
+ ctxt.generateJavaSource(" " + servletContextName + " = pageContext.getServletContext().getContext(" + contextName + ");" );
+ ctxt.generateJavaSource(" }else{");
+ ctxt.generateJavaSource(" " + servletContextName + " = pageContext.getServletContext();");
+ ctxt.generateJavaSource(" }");
+ }else{
+ ctxt.generateJavaSource(" " + servletContextName + " = pageContext.getServletContext();");
+ }
+
+ //
+ ctxt.generateJavaSource(" if(" + servletContextName + " == null){");
+ if(hasContext){
+ ctxt.generateJavaSource(" throw new JspTagException(\"Unable to get RequestDispatcher for Context: \\\" \" +" + contextName + "+ \" \\\" and URL: \\\" \" +" + urlName + "+ \" \\\". Verify values and/or enable cross context access.\");");
+ }else{
+ ctxt.generateJavaSource(" throw new JspTagException(\"Unable to get RequestDispatcher for URL: \\\" \" +" + urlName + "+ \" \\\". Verify values and/or enable cross context access.\");");
+ }
+ ctxt.generateJavaSource(" }");
+
+ //get the request dispatcher
+ ctxt.generateJavaSource(" RequestDispatcher " + requestDispatcherName + " = " + servletContextName + ".getRequestDispatcher(org.apache.struts2.jasper.tagplugins.jstl.Util.stripSession("+urlName+"));");
+ ctxt.generateJavaSource(" if(" + requestDispatcherName + " == null) throw new JspTagException(org.apache.struts2.jasper.tagplugins.jstl.Util.stripSession("+urlName+"));");
+
+ //initialize a ImportResponseWrapper to include the resource
+ ctxt.generateJavaSource(" org.apache.struts2.jasper.tagplugins.jstl.Util.ImportResponseWrapper " + irwName + " = new org.apache.struts2.jasper.tagplugins.jstl.Util.ImportResponseWrapper((HttpServletResponse) pageContext.getResponse());");
+ ctxt.generateJavaSource(" if(" + charSetName + " == null){");
+ ctxt.generateJavaSource(" " + charSetName + " = org.apache.struts2.jasper.tagplugins.jstl.Util.DEFAULT_ENCODING;");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" " + irwName + ".setCharEncoding(" + charSetName + ");");
+ ctxt.generateJavaSource(" try{");
+ ctxt.generateJavaSource(" " + requestDispatcherName + ".include(pageContext.getRequest(), " + irwName + ");");
+ ctxt.generateJavaSource(" }catch(java.io.IOException ex){");
+ ctxt.generateJavaSource(" throw new JspException(ex);");
+ ctxt.generateJavaSource(" }catch(RuntimeException ex){");
+ ctxt.generateJavaSource(" throw new JspException(ex);");
+ ctxt.generateJavaSource(" }catch(ServletException ex){");
+ ctxt.generateJavaSource(" Throwable rc = ex.getRootCause();");
+ ctxt.generateJavaSource(" if (rc == null)");
+ ctxt.generateJavaSource(" throw new JspException(ex);");
+ ctxt.generateJavaSource(" else");
+ ctxt.generateJavaSource(" throw new JspException(rc);");
+ ctxt.generateJavaSource(" }");
+
+ //validate the response status
+ ctxt.generateJavaSource(" if(" + irwName + ".getStatus() < 200 || " + irwName + ".getStatus() > 299){");
+ ctxt.generateJavaSource(" throw new JspTagException(" + irwName + ".getStatus()+\" \" + org.apache.struts2.jasper.tagplugins.jstl.Util.stripSession(" + urlName + "));");
+ ctxt.generateJavaSource(" }");
+
+ ctxt.generateJavaSource(" " + tempStringName + " = " + irwName + ".getString();");
+
+ ctxt.generateJavaSource("}");
+
+ if(hasVar){
+ String strVar = ctxt.getConstantAttribute("var");
+ ctxt.generateJavaSource("pageContext.setAttribute(\""+strVar+"\"," + tempStringName + "," + iScope + ");");
+ }else{
+ ctxt.generateJavaSource("pageContext.getOut().print(" + tempStringName + ");");
+ }
+ }
+ }
+
+
+
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Otherwise.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Otherwise.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Otherwise.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Otherwise.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.struts2.jasper.tagplugins.jstl.core;
+
+import org.apache.struts2.jasper.compiler.tagplugin.*;
+
+public final class Otherwise implements TagPlugin {
+
+ public void doTag(TagPluginContext ctxt) {
+
+ // See When.java for the reason whey "}" is need at the beginng and
+ // not at the end.
+ ctxt.generateJavaSource("} else {");
+ ctxt.generateBody();
+ }
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Out.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Out.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Out.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Out.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.struts2.jasper.tagplugins.jstl.core;
+
+import org.apache.struts2.jasper.compiler.tagplugin.TagPlugin;
+import org.apache.struts2.jasper.compiler.tagplugin.TagPluginContext;
+
+
+public final class Out implements TagPlugin {
+
+ public void doTag(TagPluginContext ctxt) {
+
+ //these two data member are to indicate
+ //whether the corresponding attribute is specified
+ boolean hasDefault=false, hasEscapeXml=false;
+ hasDefault = ctxt.isAttributeSpecified("default");
+ hasEscapeXml = ctxt.isAttributeSpecified("escapeXml");
+
+ //strValName, strEscapeXmlName & strDefName are two variables' name
+ //standing for value, escapeXml and default attribute
+ String strValName = ctxt.getTemporaryVariableName();
+ String strDefName = ctxt.getTemporaryVariableName();
+ String strEscapeXmlName = ctxt.getTemporaryVariableName();
+
+ //according to the tag file, the value attribute is mandatory.
+ ctxt.generateJavaSource("String " + strValName + " = null;");
+ ctxt.generateJavaSource("if(");
+ ctxt.generateAttribute("value");
+ ctxt.generateJavaSource("!=null){");
+ ctxt.generateJavaSource(" " + strValName + " = (");
+ ctxt.generateAttribute("value");
+ ctxt.generateJavaSource(").toString();");
+ ctxt.generateJavaSource("}");
+
+ //initiate the strDefName with null.
+ //if the default has been specified, then assign the value to it;
+ ctxt.generateJavaSource("String " + strDefName + " = null;\n");
+ if(hasDefault){
+ ctxt.generateJavaSource("if(");
+ ctxt.generateAttribute("default");
+ ctxt.generateJavaSource(" != null){");
+ ctxt.generateJavaSource(strDefName + " = (");
+ ctxt.generateAttribute("default");
+ ctxt.generateJavaSource(").toString();");
+ ctxt.generateJavaSource("}");
+ }
+
+ //initiate the strEscapeXmlName with true;
+ //if the escapeXml is specified, assign the value to it;
+ ctxt.generateJavaSource("boolean " + strEscapeXmlName + " = true;");
+ if(hasEscapeXml){
+ ctxt.generateJavaSource(strEscapeXmlName + " = Boolean.parseBoolean((");
+ ctxt.generateAttribute("default");
+ ctxt.generateJavaSource(").toString());");
+ }
+
+ //main part.
+ ctxt.generateJavaSource("if(null != " + strValName +"){");
+ ctxt.generateJavaSource(" if(" + strEscapeXmlName + "){");
+ ctxt.generateJavaSource(" " + strValName + " = org.apache.struts2.jasper.tagplugins.jstl.Util.escapeXml(" + strValName + ");");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" out.write(" + strValName + ");");
+ ctxt.generateJavaSource("}else{");
+ ctxt.generateJavaSource(" if(null != " + strDefName + "){");
+ ctxt.generateJavaSource(" if(" + strEscapeXmlName + "){");
+ ctxt.generateJavaSource(" " + strDefName + " = org.apache.struts2.jasper.tagplugins.jstl.Util.escapeXml(" + strDefName + ");");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" out.write(" + strDefName + ");");
+ ctxt.generateJavaSource(" }else{");
+ ctxt.generateBody();
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource("}");
+ }
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Param.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Param.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Param.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Param.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.struts2.jasper.tagplugins.jstl.core;
+
+import org.apache.struts2.jasper.compiler.tagplugin.TagPlugin;
+import org.apache.struts2.jasper.compiler.tagplugin.TagPluginContext;
+
+public class Param implements TagPlugin {
+
+ public void doTag(TagPluginContext ctxt) {
+
+ //don't support the body content
+
+ //define names of all the temp variables
+ String nameName = ctxt.getTemporaryVariableName();
+ String valueName = ctxt.getTemporaryVariableName();
+ String urlName = ctxt.getTemporaryVariableName();
+ String encName = ctxt.getTemporaryVariableName();
+ String index = ctxt.getTemporaryVariableName();
+
+ //if the param tag has no parents, throw a exception
+ TagPluginContext parent = ctxt.getParentContext();
+ if(parent == null){
+ ctxt.generateJavaSource(" throw new JspTagExcption" +
+ "(\"<param> outside <import> or <urlEncode>\");");
+ return;
+ }
+
+ //get the url string before adding this param
+ ctxt.generateJavaSource("String " + urlName + " = " +
+ "(String)pageContext.getAttribute(\"url_without_param\");");
+
+ //get the value of "name"
+ ctxt.generateJavaSource("String " + nameName + " = ");
+ ctxt.generateAttribute("name");
+ ctxt.generateJavaSource(";");
+
+ //if the "name" is null then do nothing.
+ //else add such string "name=value" to the url.
+ //and the url should be encoded
+ ctxt.generateJavaSource("if(" + nameName + " != null && !" + nameName + ".equals(\"\")){");
+ ctxt.generateJavaSource(" String " + valueName + " = ");
+ ctxt.generateAttribute("value");
+ ctxt.generateJavaSource(";");
+ ctxt.generateJavaSource(" if(" + valueName + " == null) " + valueName + " = \"\";");
+ ctxt.generateJavaSource(" String " + encName + " = pageContext.getResponse().getCharacterEncoding();");
+ ctxt.generateJavaSource(" " + nameName + " = java.net.URLEncoder.encode(" + nameName + ", " + encName + ");");
+ ctxt.generateJavaSource(" " + valueName + " = java.net.URLEncoder.encode(" + valueName + ", " + encName + ");");
+ ctxt.generateJavaSource(" int " + index + ";");
+ ctxt.generateJavaSource(" " + index + " = " + urlName + ".indexOf(\'?\');");
+ //if the current param is the first one, add a "?" ahead of it
+ //else add a "&" ahead of it
+ ctxt.generateJavaSource(" if(" + index + " == -1){");
+ ctxt.generateJavaSource(" " + urlName + " = " + urlName + " + \"?\" + " + nameName + " + \"=\" + " + valueName + ";");
+ ctxt.generateJavaSource(" }else{");
+ ctxt.generateJavaSource(" " + urlName + " = " + urlName + " + \"&\" + " + nameName + " + \"=\" + " + valueName + ";");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" pageContext.setAttribute(\"url_without_param\"," + urlName + ");");
+ ctxt.generateJavaSource("}");
+ }
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Redirect.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Redirect.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Redirect.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Redirect.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.struts2.jasper.tagplugins.jstl.core;
+
+import org.apache.struts2.jasper.compiler.tagplugin.TagPlugin;
+import org.apache.struts2.jasper.compiler.tagplugin.TagPluginContext;
+
+public class Redirect implements TagPlugin {
+
+ public void doTag(TagPluginContext ctxt) {
+
+ //flag for the existence of the "context"
+ boolean hasContext = ctxt.isAttributeSpecified("context");
+
+ //names of the temp variables
+ String urlName = ctxt.getTemporaryVariableName();
+ String contextName = ctxt.getTemporaryVariableName();
+ String baseUrlName = ctxt.getTemporaryVariableName();
+ String resultName = ctxt.getTemporaryVariableName();
+ String responseName = ctxt.getTemporaryVariableName();
+
+ //get context
+ ctxt.generateJavaSource("String " + contextName + " = null;");
+ if(hasContext){
+ ctxt.generateJavaSource(contextName + " = ");
+ ctxt.generateAttribute("context");
+ ctxt.generateJavaSource(";");
+ }
+
+ //get the url
+ ctxt.generateJavaSource("String " + urlName + " = ");
+ ctxt.generateAttribute("url");
+ ctxt.generateJavaSource(";");
+
+ //get the raw url according to "url" and "context"
+ ctxt.generateJavaSource("String " + baseUrlName + " = " +
+ "org.apache.struts2.jasper.tagplugins.jstl.Util.resolveUrl(" + urlName + ", " + contextName + ", pageContext);");
+ ctxt.generateJavaSource("pageContext.setAttribute" +
+ "(\"url_without_param\", " + baseUrlName + ");");
+
+ //add params
+ ctxt.generateBody();
+
+ ctxt.generateJavaSource("String " + resultName + " = " +
+ "(String)pageContext.getAttribute(\"url_without_param\");");
+ ctxt.generateJavaSource("pageContext.removeAttribute" +
+ "(\"url_without_param\");");
+
+ //get the response object
+ ctxt.generateJavaSource("HttpServletResponse " + responseName + " = " +
+ "((HttpServletResponse) pageContext.getResponse());");
+
+ //if the url is relative, encode it
+ ctxt.generateJavaSource("if(!org.apache.struts2.jasper.tagplugins.jstl.Util.isAbsoluteUrl(" + resultName + ")){");
+ ctxt.generateJavaSource(" " + resultName + " = "
+ + responseName + ".encodeRedirectURL(" + resultName + ");");
+ ctxt.generateJavaSource("}");
+
+ //do redirect
+ ctxt.generateJavaSource("try{");
+ ctxt.generateJavaSource(" " + responseName + ".sendRedirect(" + resultName + ");");
+ ctxt.generateJavaSource("}catch(java.io.IOException ex){");
+ ctxt.generateJavaSource(" throw new JspTagException(ex.toString(), ex);");
+ ctxt.generateJavaSource("}");
+ }
+
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Remove.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Remove.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Remove.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Remove.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.struts2.jasper.tagplugins.jstl.core;
+
+import org.apache.struts2.jasper.compiler.tagplugin.TagPlugin;
+import org.apache.struts2.jasper.compiler.tagplugin.TagPluginContext;
+import org.apache.struts2.jasper.tagplugins.jstl.Util;
+
+public class Remove implements TagPlugin {
+
+ public void doTag(TagPluginContext ctxt) {
+
+ //scope flag
+ boolean hasScope = ctxt.isAttributeSpecified("scope");
+
+ //the value of the "var"
+ String strVar = ctxt.getConstantAttribute("var");
+
+ //remove attribute from certain scope.
+ //default scope is "page".
+ if(hasScope){
+ int iScope = Util.getScope(ctxt.getConstantAttribute("scope"));
+ ctxt.generateJavaSource("pageContext.removeAttribute(\"" + strVar + "\"," + iScope + ");");
+ }else{
+ ctxt.generateJavaSource("pageContext.removeAttribute(\"" + strVar + "\");");
+ }
+ }
+
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Set.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Set.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Set.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Set.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.struts2.jasper.tagplugins.jstl.core;
+
+import org.apache.struts2.jasper.compiler.tagplugin.TagPlugin;
+import org.apache.struts2.jasper.compiler.tagplugin.TagPluginContext;
+import org.apache.struts2.jasper.tagplugins.jstl.Util;
+
+public class Set implements TagPlugin {
+
+ public void doTag(TagPluginContext ctxt) {
+
+ //the flags to indicate whether the attributes have been specified
+ boolean hasValue = false, hasVar = false, hasScope = false,
+ hasTarget = false;
+
+ //the scope name
+ String strScope;
+ //the id of the scope
+ int iScope;
+
+ //initialize the flags
+ hasValue = ctxt.isAttributeSpecified("value");
+ hasVar = ctxt.isAttributeSpecified("var");
+ hasScope = ctxt.isAttributeSpecified("scope");
+ hasTarget = ctxt.isAttributeSpecified("target");
+
+ //the temp variables name
+ String resultName = ctxt.getTemporaryVariableName();
+ String targetName = ctxt.getTemporaryVariableName();
+ String propertyName = ctxt.getTemporaryVariableName();
+
+ //initialize the "result" which will be assigned to the var or target.property
+ ctxt.generateJavaSource("Object " + resultName + " = null;");
+ if(hasValue){
+ ctxt.generateJavaSource(resultName + " = ");
+ ctxt.generateAttribute("value");
+ ctxt.generateJavaSource(";");
+ }else{
+ ctxt.dontUseTagPlugin();
+ return;
+ }
+
+ //initialize the strScope
+ if(hasScope){
+ strScope = ctxt.getConstantAttribute("scope");
+ }else{
+ strScope = "page";
+ }
+
+ //get the iScope according to the strScope
+ iScope = Util.getScope(strScope);
+
+ //if the attribute var has been specified then assign the result to the var;
+ if(hasVar){
+ String strVar = ctxt.getConstantAttribute("var");
+ ctxt.generateJavaSource("if(null != " + resultName + "){");
+ ctxt.generateJavaSource(" pageContext.setAttribute(\"" + strVar + "\"," + resultName + "," + iScope + ");");
+ ctxt.generateJavaSource("} else {");
+ if(hasScope){
+ ctxt.generateJavaSource(" pageContext.removeAttribute(\"" + strVar + "\"," + iScope + ");");
+ }else{
+ ctxt.generateJavaSource(" pageContext.removeAttribute(\"" + strVar + "\");");
+ }
+ ctxt.generateJavaSource("}");
+
+ //else assign the result to the target.property
+ }else if(hasTarget){
+
+ //generate the temp variable name
+ String pdName = ctxt.getTemporaryVariableName();
+ String successFlagName = ctxt.getTemporaryVariableName();
+ String index = ctxt.getTemporaryVariableName();
+ String methodName = ctxt.getTemporaryVariableName();
+
+ //initialize the property
+ ctxt.generateJavaSource("String " + propertyName + " = null;");
+ ctxt.generateJavaSource("if(");
+ ctxt.generateAttribute("property");
+ ctxt.generateJavaSource(" != null){");
+ ctxt.generateJavaSource(" " + propertyName + " = (");
+ ctxt.generateAttribute("property");
+ ctxt.generateJavaSource(").toString();");
+ ctxt.generateJavaSource("}");
+
+ //initialize the target
+ ctxt.generateJavaSource("Object " + targetName + " = ");
+ ctxt.generateAttribute("target");
+ ctxt.generateJavaSource(";");
+
+ //the target is ok
+ ctxt.generateJavaSource("if(" + targetName + " != null){");
+
+ //if the target is a map, then put the result into the map with the key property
+ ctxt.generateJavaSource(" if(" + targetName + " instanceof java.util.Map){");
+ ctxt.generateJavaSource(" if(null != " + resultName + "){");
+ ctxt.generateJavaSource(" ((java.util.Map) " + targetName + ").put(" + propertyName + "," + resultName + ");");
+ ctxt.generateJavaSource(" }else{");
+ ctxt.generateJavaSource(" ((java.util.Map) " + targetName + ").remove(" + propertyName + ");");
+ ctxt.generateJavaSource(" }");
+
+ //else assign the result to the target.property
+ ctxt.generateJavaSource(" }else{");
+ ctxt.generateJavaSource(" try{");
+
+ //get all the property of the target
+ ctxt.generateJavaSource(" java.beans.PropertyDescriptor " + pdName + "[] = java.beans.Introspector.getBeanInfo(" + targetName + ".getClass()).getPropertyDescriptors();");
+
+ //the success flag is to imply whether the assign is successful
+ ctxt.generateJavaSource(" boolean " + successFlagName + " = false;");
+
+ //find the right property
+ ctxt.generateJavaSource(" for(int " + index + "=0;" + index + "<" + pdName + ".length;" + index + "++){");
+ ctxt.generateJavaSource(" if(" + pdName + "[" + index + "].getName().equals(" + propertyName + ")){");
+
+ //get the "set" method;
+ ctxt.generateJavaSource(" java.lang.reflect.Method " + methodName + " = " + pdName + "[" + index + "].getWriteMethod();");
+ ctxt.generateJavaSource(" if(null == " + methodName + "){");
+ ctxt.generateJavaSource(" throw new JspException(\"No setter method in <set> for property \"+" + propertyName + ");");
+ ctxt.generateJavaSource(" }");
+
+ //invoke the method through the reflection
+ ctxt.generateJavaSource(" if(" + resultName + " != null){");
+ ctxt.generateJavaSource(" " + methodName + ".invoke(" + targetName + ", new Object[]{(" + methodName + ".getParameterTypes()[0]).cast(" + resultName + ")});");
+ ctxt.generateJavaSource(" }else{");
+ ctxt.generateJavaSource(" " + methodName + ".invoke(" + targetName + ", new Object[]{null});");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" " + successFlagName + " = true;");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" if(!" + successFlagName + "){");
+ ctxt.generateJavaSource(" throw new JspException(\"Invalid property in <set>:\"+" + propertyName + ");");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" }");
+
+ //catch the el exception and throw it as a JspException
+ ctxt.generateJavaSource(" catch (IllegalAccessException ex) {");
+ ctxt.generateJavaSource(" throw new JspException(ex);");
+ ctxt.generateJavaSource(" } catch (java.beans.IntrospectionException ex) {");
+ ctxt.generateJavaSource(" throw new JspException(ex);");
+ ctxt.generateJavaSource(" } catch (java.lang.reflect.InvocationTargetException ex) {");
+ ctxt.generateJavaSource(" throw new JspException(ex);");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource(" }");
+ ctxt.generateJavaSource("}else{");
+ ctxt.generateJavaSource(" throw new JspException();");
+ ctxt.generateJavaSource("}");
+ }
+ }
+
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Url.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Url.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Url.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/Url.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.struts2.jasper.tagplugins.jstl.core;
+
+import org.apache.struts2.jasper.compiler.tagplugin.TagPlugin;
+import org.apache.struts2.jasper.compiler.tagplugin.TagPluginContext;
+import org.apache.struts2.jasper.tagplugins.jstl.Util;
+
+public class Url implements TagPlugin {
+
+ public void doTag(TagPluginContext ctxt) {
+
+ //flags
+ boolean hasVar, hasContext, hasScope;
+
+ //init flags
+ hasVar = ctxt.isAttributeSpecified("var");
+ hasContext = ctxt.isAttributeSpecified("context");
+ hasScope = ctxt.isAttributeSpecified("scope");
+
+ //define name of the temp variables
+ String valueName = ctxt.getTemporaryVariableName();
+ String contextName = ctxt.getTemporaryVariableName();
+ String baseUrlName = ctxt.getTemporaryVariableName();
+ String resultName = ctxt.getTemporaryVariableName();
+ String responseName = ctxt.getTemporaryVariableName();
+
+ //get the scope
+ String strScope = "page";
+ if(hasScope){
+ strScope = ctxt.getConstantAttribute("scope");
+ }
+ int iScope = Util.getScope(strScope);
+
+ //get the value
+ ctxt.generateJavaSource("String " + valueName + " = ");
+ ctxt.generateAttribute("value");
+ ctxt.generateJavaSource(";");
+
+ //get the context
+ ctxt.generateJavaSource("String " + contextName + " = null;");
+ if(hasContext){
+ ctxt.generateJavaSource(contextName + " = ");
+ ctxt.generateAttribute("context");
+ ctxt.generateJavaSource(";");
+ }
+
+ //get the raw url
+ ctxt.generateJavaSource("String " + baseUrlName + " = " +
+ "org.apache.struts2.jasper.tagplugins.jstl.Util.resolveUrl(" + valueName + ", " + contextName + ", pageContext);");
+ ctxt.generateJavaSource("pageContext.setAttribute" +
+ "(\"url_without_param\", " + baseUrlName + ");");
+
+ //add params
+ ctxt.generateBody();
+
+ ctxt.generateJavaSource("String " + resultName + " = " +
+ "(String)pageContext.getAttribute(\"url_without_param\");");
+ ctxt.generateJavaSource("pageContext.removeAttribute(\"url_without_param\");");
+
+ //if the url is relative, encode it
+ ctxt.generateJavaSource("if(!org.apache.struts2.jasper.tagplugins.jstl.Util.isAbsoluteUrl(" + resultName + ")){");
+ ctxt.generateJavaSource(" HttpServletResponse " + responseName + " = " +
+ "((HttpServletResponse) pageContext.getResponse());");
+ ctxt.generateJavaSource(" " + resultName + " = "
+ + responseName + ".encodeURL(" + resultName + ");");
+ ctxt.generateJavaSource("}");
+
+ //if "var" is specified, the url string store in the attribute var defines
+ if(hasVar){
+ String strVar = ctxt.getConstantAttribute("var");
+ ctxt.generateJavaSource("pageContext.setAttribute" +
+ "(\"" + strVar + "\", " + resultName + ", " + iScope + ");");
+
+ //if var is not specified, just print out the url string
+ }else{
+ ctxt.generateJavaSource("try{");
+ ctxt.generateJavaSource(" pageContext.getOut().print(" + resultName + ");");
+ ctxt.generateJavaSource("}catch(java.io.IOException ex){");
+ ctxt.generateJavaSource(" throw new JspTagException(ex.toString(), ex);");
+ ctxt.generateJavaSource("}");
+ }
+ }
+
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/When.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/When.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/When.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/core/When.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.struts2.jasper.tagplugins.jstl.core;
+
+import org.apache.struts2.jasper.compiler.tagplugin.*;
+
+public final class When implements TagPlugin {
+
+ public void doTag(TagPluginContext ctxt) {
+ // Get the parent context to determine if this is the first <c:when>
+ TagPluginContext parentContext = ctxt.getParentContext();
+ if (parentContext == null) {
+ ctxt.dontUseTagPlugin();
+ return;
+ }
+
+ if ("true".equals(parentContext.getPluginAttribute("hasBeenHere"))) {
+ ctxt.generateJavaSource("} else if(");
+ // See comment below for the reason we generate the extra "}" here.
+ }
+ else {
+ ctxt.generateJavaSource("if(");
+ parentContext.setPluginAttribute("hasBeenHere", "true");
+ }
+ ctxt.generateAttribute("test");
+ ctxt.generateJavaSource("){");
+ ctxt.generateBody();
+
+ // We don't generate the closing "}" for the "if" here because there
+ // may be whitespaces in between <c:when>'s. Instead we delay
+ // generating it until the next <c:when> or <c:otherwise> or
+ // <c:choose>
+ }
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/tagPlugins.xml
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/tagPlugins.xml?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/tagPlugins.xml (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/tagplugins/jstl/tagPlugins.xml Fri Jul 31 18:12:48 2009
@@ -0,0 +1,63 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<tag-plugins>
+ <tag-plugin>
+ <tag-class>org.apache.taglibs.standard.tag.rt.core.IfTag</tag-class>
+ <plugin-class>org.apache.struts2.jasper.tagplugins.jstl.core.If</plugin-class>
+ </tag-plugin>
+ <tag-plugin>
+ <tag-class>org.apache.taglibs.standard.tag.common.core.ChooseTag</tag-class>
+ <plugin-class>org.apache.struts2.jasper.tagplugins.jstl.core.Choose</plugin-class>
+ </tag-plugin>
+ <tag-plugin>
+ <tag-class>org.apache.taglibs.standard.tag.rt.core.WhenTag</tag-class>
+ <plugin-class>org.apache.struts2.jasper.tagplugins.jstl.core.When</plugin-class>
+ </tag-plugin>
+ <tag-plugin>
+ <tag-class>org.apache.taglibs.standard.tag.common.core.OtherwiseTag</tag-class>
+ <plugin-class>org.apache.struts2.jasper.tagplugins.jstl.core.Otherwise</plugin-class>
+ </tag-plugin>
+ <tag-plugin>
+ <tag-class>org.apache.taglibs.standard.tag.rt.core.ForEachTag</tag-class>
+ <plugin-class>org.apache.struts2.jasper.tagplugins.jstl.core.ForEach</plugin-class>
+ </tag-plugin>
+ <tag-plugin>
+ <tag-class>org.apache.taglibs.standard.tag.rt.core.OutTag</tag-class>
+ <plugin-class>org.apache.struts2.jasper.tagplugins.jstl.core.Out</plugin-class>
+ </tag-plugin>
+ <tag-plugin>
+ <tag-class>org.apache.taglibs.standard.tag.rt.core.SetTag</tag-class>
+ <plugin-class>org.apache.struts2.jasper.tagplugins.jstl.core.Set</plugin-class>
+ </tag-plugin>
+ <tag-plugin>
+ <tag-class>org.apache.taglibs.standard.tag.common.core.RemoveTag</tag-class>
+ <plugin-class>org.apache.struts2.jasper.tagplugins.jstl.core.Remove</plugin-class>
+ </tag-plugin>
+ <tag-plugin>
+ <tag-class>org.apache.taglibs.standard.tag.common.core.CatchTag</tag-class>
+ <plugin-class>org.apache.struts2.jasper.tagplugins.jstl.core.Catch</plugin-class>
+ </tag-plugin>
+ <tag-plugin>
+ <tag-class>org.apache.taglibs.standard.tag.rt.core.ForTokensTag</tag-class>
+ <plugin-class>org.apache.struts2.jasper.tagplugins.jstl.core.ForTokens</plugin-class>
+ </tag-plugin>
+ <tag-plugin>
+ <tag-class>org.apache.taglibs.standard.tag.rt.core.ImportTag</tag-class>
+ <plugin-class>org.apache.struts2.jasper.tagplugins.jstl.core.Import</plugin-class>
+ </tag-plugin>
+</tag-plugins>
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/FastDateFormat.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/FastDateFormat.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/FastDateFormat.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/FastDateFormat.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.struts2.jasper.util;
+
+import java.util.Date;
+
+import java.text.DateFormat;
+import java.text.FieldPosition;
+import java.text.ParsePosition;
+import java.text.SimpleDateFormat;
+
+/**
+ * Fast date formatter that caches recently formatted date information
+ * and uses it to avoid too-frequent calls to the underlying
+ * formatter. Note: breaks fieldPosition param of format(Date,
+ * StringBuffer, FieldPosition). If you care about the field
+ * position, call the underlying DateFormat directly.
+ *
+ * @author Stan Bailes
+ * @author Alex Chaffee
+ */
+public class FastDateFormat extends DateFormat {
+
+ private DateFormat df;
+ private long lastSec = -1;
+ private StringBuffer sb = new StringBuffer();
+ private FieldPosition fp = new FieldPosition(DateFormat.MILLISECOND_FIELD);
+
+ public FastDateFormat(DateFormat df) {
+ this.df = df;
+ }
+
+ public Date parse(String text, ParsePosition pos) {
+ return df.parse(text, pos);
+ }
+
+ /**
+ * Note: breaks functionality of fieldPosition param. Also:
+ * there's a bug in SimpleDateFormat with "S" and "SS", use "SSS"
+ * instead if you want a msec field.
+ */
+ public StringBuffer format(Date date, StringBuffer toAppendTo,
+ FieldPosition fieldPosition) {
+ long dt = date.getTime();
+ long ds = dt / 1000;
+ if (ds != lastSec) {
+ sb.setLength(0);
+ df.format(date, sb, fp);
+ lastSec = ds;
+ } else {
+ // munge current msec into existing string
+ int ms = (int)(dt % 1000);
+ int pos = fp.getEndIndex();
+ int begin = fp.getBeginIndex();
+ if (pos > 0) {
+ if (pos > begin)
+ sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
+ ms /= 10;
+ if (pos > begin)
+ sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
+ ms /= 10;
+ if (pos > begin)
+ sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
+ }
+ }
+ toAppendTo.append(sb.toString());
+ return toAppendTo;
+ }
+
+ public static void main(String[] args) {
+ String format = "yyyy-MM-dd HH:mm:ss.SSS";
+ if (args.length > 0)
+ format = args[0];
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
+ FastDateFormat fdf = new FastDateFormat(sdf);
+ Date d = new Date();
+
+ d.setTime(1);
+ System.out.println(fdf.format(d) + "\t" + sdf.format(d));
+ d.setTime(20);
+ System.out.println(fdf.format(d) + "\t" + sdf.format(d));
+ d.setTime(500);
+ System.out.println(fdf.format(d) + "\t" + sdf.format(d));
+ d.setTime(543);
+ System.out.println(fdf.format(d) + "\t" + sdf.format(d));
+ d.setTime(999);
+ System.out.println(fdf.format(d) + "\t" + sdf.format(d));
+ d.setTime(1050);
+ System.out.println(fdf.format(d) + "\t" + sdf.format(d));
+ d.setTime(2543);
+ System.out.println(fdf.format(d) + "\t" + sdf.format(d));
+ d.setTime(12345);
+ System.out.println(fdf.format(d) + "\t" + sdf.format(d));
+ d.setTime(12340);
+ System.out.println(fdf.format(d) + "\t" + sdf.format(d));
+
+ final int reps = 100000;
+ {
+ long start = System.currentTimeMillis();
+ for (int i = 0; i < reps; i++) {
+ d.setTime(System.currentTimeMillis());
+ fdf.format(d);
+ }
+ long elap = System.currentTimeMillis() - start;
+ System.out.println("fast: " + elap + " elapsed");
+ System.out.println(fdf.format(d));
+ }
+ {
+ long start = System.currentTimeMillis();
+ for (int i = 0; i < reps; i++) {
+ d.setTime(System.currentTimeMillis());
+ sdf.format(d);
+ }
+ long elap = System.currentTimeMillis() - start;
+ System.out.println("slow: " + elap + " elapsed");
+ System.out.println(sdf.format(d));
+ }
+ }
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/Queue.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/Queue.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/Queue.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/Queue.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.struts2.jasper.util;
+
+import java.util.Vector;
+
+/**
+ * A simple FIFO queue class which causes the calling thread to wait
+ * if the queue is empty and notifies threads that are waiting when it
+ * is not empty.
+ *
+ * @author Anil V (akv@eng.sun.com)
+ */
+public class Queue {
+ private Vector vector = new Vector();
+
+ /**
+ * Put the object into the queue.
+ *
+ * @param object the object to be appended to the
+ * queue.
+ */
+ public synchronized void put(Object object) {
+ vector.addElement(object);
+ notify();
+ }
+
+ /**
+ * Pull the first object out of the queue. Wait if the queue is
+ * empty.
+ */
+ public synchronized Object pull() {
+ while (isEmpty())
+ try {
+ wait();
+ } catch (InterruptedException ex) {
+ }
+ return get();
+ }
+
+ /**
+ * Get the first object out of the queue. Return null if the queue
+ * is empty.
+ */
+ public synchronized Object get() {
+ Object object = peek();
+ if (object != null)
+ vector.removeElementAt(0);
+ return object;
+ }
+
+ /**
+ * Peek to see if something is available.
+ */
+ public Object peek() {
+ if (isEmpty())
+ return null;
+ return vector.elementAt(0);
+ }
+
+ /**
+ * Is the queue empty?
+ */
+ public boolean isEmpty() {
+ return vector.isEmpty();
+ }
+
+ /**
+ * How many elements are there in this queue?
+ */
+ public int size() {
+ return vector.size();
+ }
+}
Added: struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/SimplePool.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/SimplePool.java?rev=799681&view=auto
==============================================================================
--- struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/SimplePool.java (added)
+++ struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/jasper/util/SimplePool.java Fri Jul 31 18:12:48 2009
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.struts2.jasper.util;
+
+/**
+ * Simple object pool. Based on ThreadPool and few other classes
+ *
+ * The pool will ignore overflow and return null if empty.
+ *
+ * @author Gal Shachor
+ * @author Costin
+ */
+public final class SimplePool {
+
+ private static final int DEFAULT_SIZE=16;
+
+ /*
+ * Where the threads are held.
+ */
+ private Object pool[];
+
+ private int max;
+ private int current=-1;
+
+ private Object lock;
+
+ public SimplePool() {
+ this.max=DEFAULT_SIZE;
+ this.pool=new Object[max];
+ this.lock=new Object();
+ }
+
+ public SimplePool(int max) {
+ this.max=max;
+ this.pool=new Object[max];
+ this.lock=new Object();
+ }
+
+ /**
+ * Adds the given object to the pool, and does nothing if the pool is full
+ */
+ public void put(Object o) {
+ synchronized( lock ) {
+ if( current < (max-1) ) {
+ current += 1;
+ pool[current] = o;
+ }
+ }
+ }
+
+ /**
+ * Get an object from the pool, null if the pool is empty.
+ */
+ public Object get() {
+ Object item = null;
+ synchronized( lock ) {
+ if( current >= 0 ) {
+ item = pool[current];
+ current -= 1;
+ }
+ }
+ return item;
+ }
+
+ /**
+ * Return the size of the pool
+ */
+ public int getMax() {
+ return max;
+ }
+}