From 2bb92b0afce283b68a25fd7fb4b6da952d3393cc Mon Sep 17 00:00:00 2001 From: Musachy Barroso Date: Mon, 22 Dec 2008 18:32:23 +0000 Subject: [PATCH] WW-2933 Move Java Templates plugin out of the sandbox git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@728738 13f79535-47bb-0310-9956-ffa450edef68 --- plugins/javatemplates/pom.xml | 59 ++++++ .../apache/struts2/views/java/Attributes.java | 112 ++++++++++++ .../views/java/DefaultTagHandlerFactory.java | 51 ++++++ .../struts2/views/java/DefaultTheme.java | 85 +++++++++ .../views/java/JavaTemplateEngine.java | 67 +++++++ .../struts2/views/java/TagGenerator.java | 28 +++ .../apache/struts2/views/java/TagHandler.java | 62 +++++++ .../struts2/views/java/TagHandlerFactory.java | 26 +++ .../struts2/views/java/TagSerializer.java | 27 +++ .../org/apache/struts2/views/java/Theme.java | 29 +++ .../views/java/XHTMLTagSerializer.java | 72 ++++++++ .../simple/AbstractMessageListHandler.java | 75 ++++++++ .../views/java/simple/AbstractTagHandler.java | 98 ++++++++++ .../views/java/simple/ActionErrorHandler.java | 32 ++++ .../java/simple/ActionMessageHandler.java | 12 ++ .../views/java/simple/AnchorHandler.java | 51 ++++++ .../views/java/simple/CheckboxHandler.java | 45 +++++ .../java/simple/CommonAttributesHandler.java | 41 +++++ .../struts2/views/java/simple/DivHandler.java | 48 +++++ .../views/java/simple/EmptyHandler.java | 32 ++++ .../views/java/simple/FieldErrorHandler.java | 78 ++++++++ .../views/java/simple/FileHandler.java | 49 +++++ .../views/java/simple/FormHandler.java | 55 ++++++ .../views/java/simple/HeadHandler.java | 49 +++++ .../views/java/simple/HiddenHandler.java | 45 +++++ .../views/java/simple/LabelHandler.java | 49 +++++ .../views/java/simple/PasswordHandler.java | 54 ++++++ .../views/java/simple/ResetHandler.java | 60 +++++++ .../java/simple/ScriptingEventsHandler.java | 57 ++++++ .../views/java/simple/SelectHandler.java | 143 +++++++++++++++ .../views/java/simple/SimpleTheme.java | 74 ++++++++ .../views/java/simple/SubmitHandler.java | 98 ++++++++++ .../views/java/simple/TextAreaHandler.java | 54 ++++++ .../views/java/simple/TextFieldHandler.java | 50 ++++++ .../views/java/simple/TokenHandler.java | 53 ++++++ .../src/main/resources/struts-plugin.xml | 11 ++ .../simple/AbstractCommonAttributesTest.java | 33 ++++ .../views/java/simple/AbstractTest.java | 162 +++++++++++++++++ .../views/java/simple/ActionErrorTest.java | 92 ++++++++++ .../views/java/simple/ActionMessageTest.java | 72 ++++++++ .../struts2/views/java/simple/AnchorTest.java | 63 +++++++ .../struts2/views/java/simple/Bean.java | 42 +++++ .../views/java/simple/CheckboxTest.java | 82 +++++++++ .../struts2/views/java/simple/DivTest.java | 61 +++++++ .../views/java/simple/FieldErrorTest.java | 126 +++++++++++++ .../struts2/views/java/simple/FileTest.java | 66 +++++++ .../struts2/views/java/simple/FormTest.java | 84 +++++++++ .../struts2/views/java/simple/HeadTest.java | 55 ++++++ .../struts2/views/java/simple/HiddenTest.java | 65 +++++++ .../struts2/views/java/simple/LabelTest.java | 66 +++++++ .../views/java/simple/PasswordTest.java | 103 +++++++++++ .../struts2/views/java/simple/ResetTest.java | 86 +++++++++ .../struts2/views/java/simple/SelectTest.java | 144 +++++++++++++++ .../struts2/views/java/simple/SubmitTest.java | 169 ++++++++++++++++++ .../views/java/simple/TextAreaTest.java | 86 +++++++++ .../views/java/simple/TextFieldTest.java | 66 +++++++ .../struts2/views/java/simple/TokenTest.java | 69 +++++++ 57 files changed, 3823 insertions(+) create mode 100644 plugins/javatemplates/pom.xml create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/Attributes.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/DefaultTagHandlerFactory.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/DefaultTheme.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/JavaTemplateEngine.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagGenerator.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagHandlerFactory.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagSerializer.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/Theme.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/XHTMLTagSerializer.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractMessageListHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractTagHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ActionErrorHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ActionMessageHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AnchorHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CheckboxHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CommonAttributesHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/DivHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/EmptyHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FieldErrorHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FileHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FormHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/HeadHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/HiddenHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/LabelHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/PasswordHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ResetHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ScriptingEventsHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SelectHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SimpleTheme.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SubmitHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TextAreaHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TextFieldHandler.java create mode 100644 plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TokenHandler.java create mode 100644 plugins/javatemplates/src/main/resources/struts-plugin.xml create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractCommonAttributesTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ActionErrorTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ActionMessageTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AnchorTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/Bean.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/CheckboxTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/DivTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FieldErrorTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FileTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FormTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/HeadTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/HiddenTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/LabelTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/PasswordTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ResetTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/SelectTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/SubmitTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TextAreaTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TextFieldTest.java create mode 100644 plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TokenTest.java diff --git a/plugins/javatemplates/pom.xml b/plugins/javatemplates/pom.xml new file mode 100644 index 000000000..79745517e --- /dev/null +++ b/plugins/javatemplates/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + org.apache.struts + struts2-plugins + 2.1.3-SNAPSHOT + + org.apache.struts + struts2-javatemplates-plugin + jar + Struts 2 Java Templates Plugin + + + scm:svn:http://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2-javatemplates-plugin/ + scm:svn:https://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2-javatemplates-plugin/ + http://svn.apache.org/viewcvs.cgi/struts/sandbox/trunk/struts2-javatemplates-plugin/ + + + + + junit + junit + test + 3.8.1 + + + org.easymock + easymock + test + 2.2 + + + org.easymock + easymockclassextension + test + 2.2 + + + + + + + true + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + + + + + + + diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/Attributes.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/Attributes.java new file mode 100644 index 000000000..e429948ca --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/Attributes.java @@ -0,0 +1,112 @@ +/* + * $Id$ + * + * 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.views.java; + +import com.opensymphony.xwork2.util.TextUtils; + +import java.util.LinkedHashMap; + +/** + * Map of tag attributes, used for rendering the tags + */ +public class Attributes extends LinkedHashMap { + + public Attributes add(String key, String value) { + return add(key, value, true); + } + + public Attributes add(String key, String value, boolean encode) { + put(key, (encode ? TextUtils.htmlEncode(value) : value)); + return this; + } + + /** + * Add a key/value pair to the attributes only if the value is not null. Value + * is html encoded + * @param attrName attribute name + * @param paramValue value of attribute + * @return this + */ + public Attributes addIfExists(String attrName, Object paramValue) { + return addIfExists(attrName, paramValue, true); + } + + /** + * Add a key/value pair to the attributes only if the value is not null. + * @param attrName attribute name + * @param paramValue value of attribute + * @param encode html encode the value + * @return this + */ + public Attributes addIfExists(String attrName, Object paramValue, boolean encode) { + if (paramValue != null) { + String val = paramValue.toString(); + if (val.trim().length() > 0) + put(attrName, (encode ? TextUtils.htmlEncode(val) : val)); + } + return this; + } + + /** + * Add a key/value pair to the attributes only if the value is not null and is a boolean with a + * value of 'true'. Value is html encoded + * @param attrName attribute name + * @param paramValue value of attribute + * @return this + */ + public Attributes addIfTrue(String attrName, Object paramValue) { + if (paramValue != null) { + if ((paramValue instanceof Boolean && ((Boolean) paramValue).booleanValue()) || + (Boolean.valueOf(paramValue.toString()).booleanValue())) { + put(attrName, attrName); + } + } + return this; + } + + /** + * Add a key/value pair to the attributes, if the value is null, it will be set as an empty string. + * Value is html encoded. + * @param attrName attribute name + * @param paramValue value of attribute + * @return this + */ + public Attributes addDefaultToEmpty(String attrName, Object paramValue) { + return addDefaultToEmpty(attrName, paramValue, true); + } + + /** + * Add a key/value pair to the attributes, if the value is null, it will be set as an empty string. + * @param attrName attribute name + * @param paramValue value of attribute + * @param encode html encode the value + * @return this + */ + public Attributes addDefaultToEmpty(String attrName, Object paramValue, boolean encode) { + if (paramValue != null) { + String val = paramValue.toString(); + put(attrName, (encode ? TextUtils.htmlEncode(val) : val)); + } else { + put(attrName, ""); + } + return this; + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/DefaultTagHandlerFactory.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/DefaultTagHandlerFactory.java new file mode 100644 index 000000000..7e932aaa1 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/DefaultTagHandlerFactory.java @@ -0,0 +1,51 @@ +/* + * $Id$ + * + * 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.views.java; + +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; + +/** + * Default implementation of TagHandlerFactory + */ +public class DefaultTagHandlerFactory implements TagHandlerFactory { + private static final Logger LOG = LoggerFactory.getLogger(DefaultTagHandlerFactory.class); + + private Class tagHandlerClass; + + public DefaultTagHandlerFactory(Class tagHandlerClass) { + this.tagHandlerClass = tagHandlerClass; + } + + public TagHandler create(TagHandler next) { + try { + TagHandler th = (TagHandler) tagHandlerClass.newInstance(); + th.setNext(next); + return th; + } catch (Exception e) { + if (LOG.isErrorEnabled()) + LOG.error("Failed to instantiate tag handler class [#0]", e, tagHandlerClass.getName()); + } + + return null; + } + +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/DefaultTheme.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/DefaultTheme.java new file mode 100644 index 000000000..98ba4c79a --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/DefaultTheme.java @@ -0,0 +1,85 @@ +/* + * $Id$ + * + * 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.views.java; + +import org.apache.struts2.StrutsException; +import org.apache.struts2.components.template.TemplateRenderingContext; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; + +/** + * Default implementation of the theme + */ +public class DefaultTheme implements Theme { + private static final Logger LOG = LoggerFactory.getLogger(DefaultTheme.class); + + private String name; + private Map> handlerFactories; + + protected void setName(String name) { + this.name = name; + } + + protected void setHandlerFactories(Map> handlers) { + this.handlerFactories = handlers; + } + + public String getName() { + return name; + } + + public void renderTag(String tagName, TemplateRenderingContext context) { + if (tagName.endsWith(".java")) { + tagName = tagName.substring(0, tagName.length() - ".java".length()); + } + + List handlers = new ArrayList(); + List factories = handlerFactories.get(tagName); + if (factories == null) { + throw new StrutsException("Unable to find handlers for tag " + tagName); + } + + TagHandler prev = null; + for (int x = factories.size() - 1; x >= 0; x--) { + prev = factories.get(x).create(prev); + prev.setup(context); + handlers.add(0, prev); + } + + TagSerializer ser = (TagSerializer) handlers.get(handlers.size() - 1); + + TagGenerator gen = (TagGenerator) handlers.get(0); + try { + if (LOG.isTraceEnabled()) + LOG.trace("Rendering tag [#0]", tagName); + gen.generate(); + } catch (IOException ex) { + throw new StrutsException("Unable to write tag: " + tagName); + } + } + +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/JavaTemplateEngine.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/JavaTemplateEngine.java new file mode 100644 index 000000000..6cbdf0014 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/JavaTemplateEngine.java @@ -0,0 +1,67 @@ +/* + * $Id$ + * + * 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.views.java; + +import org.apache.struts2.StrutsException; +import org.apache.struts2.components.template.BaseTemplateEngine; +import org.apache.struts2.components.template.Template; +import org.apache.struts2.components.template.TemplateRenderingContext; +import org.apache.struts2.views.java.simple.SimpleTheme; + +import java.util.HashMap; + +/** + * Template engine that renders tags using java implementations + */ +public class JavaTemplateEngine extends BaseTemplateEngine { + + private Themes themes = new Themes() {{ + add(new SimpleTheme()); + }}; + + @Override + protected String getSuffix() { + return "java"; + } + + public void renderTemplate(TemplateRenderingContext templateContext) + throws Exception { + Template t = templateContext.getTemplate(); + Theme theme = themes.get(t.getTheme()); + if (theme == null) { + throw new StrutsException("Cannot render tag [" + t.getName() + "] because theme [" + t.getTheme() + "] was not found."); + } + theme.renderTag(t.getName(), templateContext); + } + + private class Themes { + private HashMap themes = new HashMap(); + + public void add(Theme theme) { + themes.put(theme.getName(), theme); + } + + public Theme get(String name) { + return themes.get(name); + } + } + +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagGenerator.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagGenerator.java new file mode 100644 index 000000000..cbd717314 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagGenerator.java @@ -0,0 +1,28 @@ +/* + * $Id$ + * + * 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.views.java; + +import java.io.IOException; + +public interface TagGenerator extends TagHandler { + + public void generate() throws IOException; +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagHandler.java new file mode 100644 index 000000000..126736383 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagHandler.java @@ -0,0 +1,62 @@ +/* + * $Id$ + * + * 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.views.java; + +import org.apache.struts2.components.template.TemplateRenderingContext; + +import java.io.IOException; + +public interface TagHandler { + + void setNext(TagHandler next); + + void setup(TemplateRenderingContext context); + + /** + * Write a tag openening, with its attributes + * @param name name of the tag + * @param attributes attributes of the tag + * @throws IOException + */ + void start(String name, Attributes attributes) throws IOException; + + /** + * Writes a tag close + * @param name name of the tag + * @throws IOException + */ + void end(String name) throws IOException; + + /** + * Writes to the inner text of a tag. By default the body is html encoded + * @param text tag body. + * @throws IOException + */ + void characters(String text) throws IOException; + + /** + * Writes to the inner text of a tag + * @param text tag body + * @param encode html encode the body + * @throws IOException + */ + void characters(String text, boolean encode) throws IOException; +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagHandlerFactory.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagHandlerFactory.java new file mode 100644 index 000000000..a0d03a621 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagHandlerFactory.java @@ -0,0 +1,26 @@ +/* + * $Id$ + * + * 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.views.java; + +public interface TagHandlerFactory { + + public TagHandler create(TagHandler next); +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagSerializer.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagSerializer.java new file mode 100644 index 000000000..da899ae1b --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/TagSerializer.java @@ -0,0 +1,27 @@ +/* + * $Id$ + * + * 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.views.java; + +/** + * Implementations of this class build the html representation of the tag + */ +public interface TagSerializer extends TagHandler { +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/Theme.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/Theme.java new file mode 100644 index 000000000..f25df200c --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/Theme.java @@ -0,0 +1,29 @@ +/* + * $Id$ + * + * 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.views.java; + +import org.apache.struts2.components.template.TemplateRenderingContext; + +public interface Theme { + public String getName(); + + public void renderTag(String name, TemplateRenderingContext context); +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/XHTMLTagSerializer.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/XHTMLTagSerializer.java new file mode 100644 index 000000000..034c9b6dd --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/XHTMLTagSerializer.java @@ -0,0 +1,72 @@ +/* + * $Id$ + * + * 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.views.java; + +import com.opensymphony.xwork2.util.TextUtils; +import org.apache.struts2.components.template.TemplateRenderingContext; + +import java.io.IOException; +import java.io.Writer; + +/** + * Write tags as XHTML + */ +public class XHTMLTagSerializer implements TagSerializer { + + private Writer writer; + + public void characters(String text) throws IOException { + characters(text, true); + } + + public void characters(String text, boolean encode) throws IOException { + writer.write(encode ? TextUtils.htmlEncode(text) : text); + } + + public void end(String name) throws IOException { + writer.write(""); + } + + public void setNext(TagHandler next) { + } + + public void start(String name, Attributes attrs) throws IOException { + writer.write("<"); + writer.write(name); + if (attrs != null) { + for (String key : attrs.keySet()) { + writer.write(" "); + writer.write(key); + writer.write("=\""); + writer.write(attrs.get(key)); + writer.write("\""); + } + } + + writer.write(">"); + } + + public void setup(TemplateRenderingContext context) { + this.writer = context.getWriter(); + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractMessageListHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractMessageListHandler.java new file mode 100644 index 000000000..d3a4525fa --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractMessageListHandler.java @@ -0,0 +1,75 @@ +/* + * $Id: AbstractTagHandler.java 726340 2008-12-14 02:45:05Z musachy $ + * + * 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.views.java.simple; + +import org.apache.struts2.util.MakeIterator; +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagGenerator; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; + +/** + * Base class for ActionError and ActionMessage + */ +public abstract class AbstractMessageListHandler extends AbstractTagHandler implements TagGenerator { + @Override + public void generate() throws IOException { + Map params = context.getParameters(); + Object errorsObj = findValue(getListExpression()); + + if (errorsObj != null) { + Iterator itt = MakeIterator.convert(errorsObj); + if (itt.hasNext()) { + Attributes attrs = new Attributes(); + attrs.addIfExists("style", params.get("cssStyle")) + .add("class", params.containsKey("cssClass") ? (String) params.get("cssClass") : getDefaultClass()); + start("ul", attrs); + while (itt.hasNext()) { + String error = (String) itt.next(); + + //li for each error + start("li", null); + + //span for error + start("span", null); + characters(error); + end("span"); + end("li"); + + } + end("ul"); + } + } + } + + /* + * Expression used to get list from stack + */ + protected abstract String getListExpression(); + + /* + * default class for UL element + */ + protected abstract String getDefaultClass(); +} + diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractTagHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractTagHandler.java new file mode 100644 index 000000000..8c6e49eb6 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AbstractTagHandler.java @@ -0,0 +1,98 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import com.opensymphony.xwork2.util.TextParseUtil; +import com.opensymphony.xwork2.util.ValueStack; +import org.apache.struts2.components.template.TemplateRenderingContext; +import org.apache.struts2.components.Component; +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagHandler; +import org.apache.struts2.views.util.ContextUtil; + +import java.io.IOException; + +public abstract class AbstractTagHandler implements TagHandler { + + protected TagHandler nextTagHandler; + protected TemplateRenderingContext context; + protected boolean altSyntax; + + public void characters(String text) throws IOException { + characters(text, true); + } + + public void characters(String text, boolean encode) throws IOException { + if (nextTagHandler != null) { + nextTagHandler.characters(text, encode); + } + } + + public void end(String name) throws IOException { + if (nextTagHandler != null) { + nextTagHandler.end(name); + } + + } + + public void setNext(TagHandler next) { + this.nextTagHandler = next; + } + + public void start(String name, Attributes a) throws IOException { + if (nextTagHandler != null) { + nextTagHandler.start(name, a); + } + + } + + public void setup(TemplateRenderingContext context) { + this.context = context; + this.altSyntax = ContextUtil.isUseAltSyntax(context.getStack().getContext()); + processParams(); + } + + protected void processParams() { + } + + protected String findString(String expr) { + return (String) findValue(expr, String.class); + } + + protected Object findValue(String expr) { + if (expr == null) { + return null; + } + + ValueStack stack = context.getStack(); + return stack.findValue(Component.stripExpressionIfAltSyntax(stack, expr)); + } + + private Object findValue(String expr, Class toType) { + ValueStack stack = context.getStack(); + + if (altSyntax && toType == String.class) { + return TextParseUtil.translateVariables('%', expr, stack); + } else { + return stack.findValue(Component.stripExpressionIfAltSyntax(stack, expr), toType); + } + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ActionErrorHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ActionErrorHandler.java new file mode 100644 index 000000000..9fefd9177 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ActionErrorHandler.java @@ -0,0 +1,32 @@ +/* + * $Id: SelectHandler.java 726340 2008-12-14 02:45:05Z musachy $ + * + * 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.views.java.simple; + +public class ActionErrorHandler extends AbstractMessageListHandler { + protected String getListExpression() { + return "actionErrors"; + } + + @Override + protected String getDefaultClass() { + return "errorMessage"; + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ActionMessageHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ActionMessageHandler.java new file mode 100644 index 000000000..144242b09 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ActionMessageHandler.java @@ -0,0 +1,12 @@ +package org.apache.struts2.views.java.simple; + +public class ActionMessageHandler extends AbstractMessageListHandler { + protected String getListExpression() { + return "actionMessages"; + } + + @Override + protected String getDefaultClass() { + return "actionMessage"; + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AnchorHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AnchorHandler.java new file mode 100644 index 000000000..63246f42a --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/AnchorHandler.java @@ -0,0 +1,51 @@ +/* + * $Id: SelectHandler.java 726340 2008-12-14 02:45:05Z musachy $ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagGenerator; + +import java.io.IOException; +import java.util.Map; + +public class AnchorHandler extends AbstractTagHandler implements TagGenerator { + @Override + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attrs = new Attributes(); + + attrs.addIfExists("name", params.get("name")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")) + .addIfExists("href", params.get("href"), false) + .addIfExists("title", params.get("title")) + .addIfExists("tabindex", params.get("tabindex")); + start("a", attrs); + } + + public static class CloseHandler extends AbstractTagHandler implements TagGenerator { + @Override + public void generate() throws IOException { + end("a"); + } + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CheckboxHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CheckboxHandler.java new file mode 100644 index 000000000..6556ff8bc --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CheckboxHandler.java @@ -0,0 +1,45 @@ +package org.apache.struts2.views.java.simple; + +import com.opensymphony.xwork2.util.TextUtils; +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagGenerator; + +import java.io.IOException; +import java.util.Map; + +public class CheckboxHandler extends AbstractTagHandler implements TagGenerator { + @Override + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attrs = new Attributes(); + + String fieldValue = (String) params.get("fieldValue"); + String id = (String) params.get("id"); + String name = (String) params.get("name"); + Object disabled = params.get("disabled"); + + attrs.add("type", "checkbox") + .add("name", name) + .add("value", fieldValue) + .addIfTrue("checked", params.get("nameValue")) + .addIfTrue("readonly", params.get("readonly")) + .addIfTrue("disabled", disabled) + .addIfExists("tabindex", params.get("tabindex")) + .addIfExists("id", id) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")) + .addIfExists("title", params.get("title")); + start("input", attrs); + end("input"); + + //hidden input + attrs = new Attributes(); + attrs.add("type", "hidden") + .add("id", "__checkbox_" + TextUtils.htmlEncode(id)) + .add("name", "__checkbox_" + TextUtils.htmlEncode(name)) + .add("value", "__checkbox_" + TextUtils.htmlEncode(fieldValue)) + .addIfTrue("disabled", disabled); + start("input", attrs); + end("input"); + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CommonAttributesHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CommonAttributesHandler.java new file mode 100644 index 000000000..834abb9a4 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/CommonAttributesHandler.java @@ -0,0 +1,41 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.Attributes; + +import java.io.IOException; + +/** + * Adds attributes from common-attributes.ftl + */ +public class CommonAttributesHandler extends AbstractTagHandler { + + /* (non-Javadoc) + * @see org.apache.struts2.views.java.simple.AbstractTagHandler#start(java.lang.String, org.apache.struts2.views.java.Attributes) + */ + @Override + public void start(String name, Attributes a) throws IOException { + a.addIfExists("accesskey", context.getParameters().get("accesskey")); + super.start(name, a); + } + +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/DivHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/DivHandler.java new file mode 100644 index 000000000..9aa18b4f9 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/DivHandler.java @@ -0,0 +1,48 @@ +/* + * $Id: CommonAttributesHandler.java 726340 2008-12-14 02:45:05Z musachy $ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagGenerator; + +import java.io.IOException; +import java.util.Map; + +public class DivHandler extends AbstractTagHandler implements TagGenerator { + @Override + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attrs = new Attributes(); + attrs.addIfExists("name", params.get("name")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")) + .addIfExists("title", params.get("title")); + start("div", attrs); + } + + public static class CloseHandler extends AbstractTagHandler implements TagGenerator { + @Override + public void generate() throws IOException { + end("div"); + } + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/EmptyHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/EmptyHandler.java new file mode 100644 index 000000000..eb81481bc --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/EmptyHandler.java @@ -0,0 +1,32 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.TagGenerator; + +import java.io.IOException; + +//does nothing +public class EmptyHandler extends AbstractTagHandler implements TagGenerator { + @Override + public void generate() throws IOException { + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FieldErrorHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FieldErrorHandler.java new file mode 100644 index 000000000..27782ccd6 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FieldErrorHandler.java @@ -0,0 +1,78 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagGenerator; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +public class FieldErrorHandler extends AbstractTagHandler implements TagGenerator { + public void generate() throws IOException { + Map params = context.getParameters(); + Map> errors = (Map>) findValue("fieldErrors"); + List fieldErrorFieldNames = (List) params.get("errorFieldNames"); + + if (fieldErrorFieldNames != null && !fieldErrorFieldNames.isEmpty()) { + //wrapping ul + Attributes attrs = new Attributes(); + attrs.addIfExists("style", params.get("cssStyle")) + .add("class", params.containsKey("cssClass") ? (String) params.get("cssClass") : "errorMessage"); + start("ul", attrs); + + //iterate over field error names + for (String fieldErrorFieldName : fieldErrorFieldNames) { + List fieldErrors = errors.get(fieldErrorFieldName); + if (fieldErrors != null) { + for (String fieldError : fieldErrors) { + start("li", null); + start("span", null); + characters(fieldError); + end("span"); + end("li"); + } + } + } + + end("ul"); + } else if (errors != null && !errors.isEmpty()) { + //wrapping ul + Attributes attrs = new Attributes(); + attrs.addIfExists("style", params.get("cssStyle")) + .add("class", params.containsKey("cssClass") ? (String) params.get("cssClass") : "errorMessage"); + start("ul", attrs); + + for (Map.Entry> errorEntry : errors.entrySet()) { + for (String fieldError : errorEntry.getValue()) { + start("li", null); + start("span", null); + characters(fieldError); + end("span"); + end("li"); + } + } + + end("ul"); + } + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FileHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FileHandler.java new file mode 100644 index 000000000..af0db577b --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FileHandler.java @@ -0,0 +1,49 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagGenerator; + +import java.io.IOException; +import java.util.Map; + +public class FileHandler extends AbstractTagHandler implements TagGenerator { + + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes a = new Attributes(); + + a.addDefaultToEmpty("name", params.get("name")) + .add("type", "file") + .addIfExists("size", params.get("size")) + .addIfExists("value", params.get("nameValue"), false) + .addIfTrue("disabled", params.get("disabled")) + .addIfExists("accept", params.get("accept")) + .addIfExists("tabindex", params.get("tabindex")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")) + .addIfExists("title", params.get("title")); + super.start("input", a); + super.end("input"); + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FormHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FormHandler.java new file mode 100644 index 000000000..febf73403 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/FormHandler.java @@ -0,0 +1,55 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagGenerator; + +import java.io.IOException; +import java.util.Map; + +public class FormHandler extends AbstractTagHandler implements TagGenerator { + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attrs = new Attributes(); + + attrs.addIfExists("name", params.get("name")) + .addIfExists("id", params.get("id")) + .addIfExists("onsubmit", params.get("onsubmit")) + .addIfExists("onreset", params.get("onreset")) + .addIfExists("action", params.get("action")) + .addIfExists("target", params.get("target")) + .addIfExists("enctype", params.get("enctype")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")) + .addIfExists("title", params.get("title")) + .addIfExists("accept-charset", params.get("acceptcharset")); + attrs.add("method", params.containsKey("method") ? (String) params.get("method") : "post"); + start("form", attrs); + } + + public static class CloseHandler extends AbstractTagHandler implements TagGenerator { + @Override + public void generate() throws IOException { + end("form"); + } + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/HeadHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/HeadHandler.java new file mode 100644 index 000000000..fcd0fccf3 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/HeadHandler.java @@ -0,0 +1,49 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagGenerator; +import org.apache.struts2.ServletActionContext; + +import java.io.IOException; +import java.util.Map; + +import com.opensymphony.xwork2.ActionContext; + +public class HeadHandler extends AbstractTagHandler implements TagGenerator { + + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attrs = new Attributes(); + attrs.put("type", "text/javascript"); + + String base = ServletActionContext.getRequest().getContextPath(); + StringBuilder sb = new StringBuilder(); + if (base != null) + sb.append(base); + sb.append("/struts/utils.js"); + attrs.put("src", sb.toString()); + + super.start("script", attrs); + super.end("script"); + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/HiddenHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/HiddenHandler.java new file mode 100644 index 000000000..1a5dcd73b --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/HiddenHandler.java @@ -0,0 +1,45 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagGenerator; + +import java.io.IOException; +import java.util.Map; + +public class HiddenHandler extends AbstractTagHandler implements TagGenerator { + + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes a = new Attributes(); + + a.addDefaultToEmpty("name", params.get("name")) + .add("type", "hidden") + .addIfExists("value", params.get("nameValue"), false) + .addIfTrue("disabled", params.get("disabled")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")); + super.start("input", a); + super.end("input"); + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/LabelHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/LabelHandler.java new file mode 100644 index 000000000..f739f4747 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/LabelHandler.java @@ -0,0 +1,49 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.TagGenerator; +import org.apache.struts2.views.java.Attributes; + +import java.io.IOException; +import java.util.Map; + +import com.opensymphony.xwork2.util.TextUtils; + +public class LabelHandler extends AbstractTagHandler implements TagGenerator { + + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes a = new Attributes(); + + a.addDefaultToEmpty("name", params.get("name")) + .addIfExists("for", params.get("for")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")) + .addIfExists("title", params.get("title")); + super.start("label", a); + String value = (String) params.get("nameValue"); + if (TextUtils.stringSet(value)) + characters(value); + super.end("label"); + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/PasswordHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/PasswordHandler.java new file mode 100644 index 000000000..e7011b1ad --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/PasswordHandler.java @@ -0,0 +1,54 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.TagGenerator; +import org.apache.struts2.views.java.Attributes; + +import java.io.IOException; +import java.util.Map; + +public class PasswordHandler extends AbstractTagHandler implements TagGenerator { + + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attrs = new Attributes(); + + Boolean showPassword = (Boolean) params.get("showPassword"); + if (showPassword != null && showPassword) + attrs.addIfExists("value", params.get("nameValue"), false); + + attrs.addDefaultToEmpty("name", params.get("name")) + .add("type", "password") + .addIfExists("size", params.get("size")) + .addIfExists("maxlength", params.get("maxlength")) + .addIfTrue("disabled", params.get("disabled")) + .addIfTrue("readonly", params.get("readonly")) + .addIfExists("tabindex", params.get("tabindex")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")) + .addIfExists("title", params.get("title")); + super.start("input", attrs); + super.end("input"); + } +} + diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ResetHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ResetHandler.java new file mode 100644 index 000000000..0a6e0fe85 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ResetHandler.java @@ -0,0 +1,60 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.TagGenerator; +import org.apache.struts2.views.java.Attributes; + +import java.io.IOException; +import java.util.Map; + +import com.opensymphony.xwork2.util.TextUtils; + +public class ResetHandler extends AbstractTagHandler implements TagGenerator { + + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attrs = new Attributes(); + + boolean isButton = "button".equals(params.get("type")); + + attrs.addDefaultToEmpty("name", params.get("name")) + .add("type", "reset") + .addIfExists("value", params.get("nameValue"), false) + .addIfExists("tabindex", params.get("tabindex")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")); + + if (!isButton) + attrs.addIfExists("title", params.get("title")); + + super.start("input", attrs); + if (isButton) { + String label = (String) params.get("label"); + if (TextUtils.stringSet(label)) + characters(label); + } + + super.end("input"); + } +} + diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ScriptingEventsHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ScriptingEventsHandler.java new file mode 100644 index 000000000..a439676ca --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/ScriptingEventsHandler.java @@ -0,0 +1,57 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.Attributes; + +import java.io.IOException; +import java.util.Map; + +/** + * Adds attributes from scripting-event.ftl + */ +public class ScriptingEventsHandler extends AbstractTagHandler { + + /* (non-Javadoc) + * @see org.apache.struts2.views.java.simple.AbstractTagHandler#start(java.lang.String, org.apache.struts2.views.java.Attributes) + */ + @Override + public void start(String name, Attributes a) throws IOException { + Map params = context.getParameters(); + a.addIfExists("onclick", params.get("onclick")); + a.addIfExists("ondblclick", params.get("ondblclick")); + a.addIfExists("onmousedown", params.get("onmousedown")); + a.addIfExists("onmouseup", params.get("onmouseup")); + a.addIfExists("onmouseover", params.get("onmouseover")); + a.addIfExists("onmousemove", params.get("onmousemove")); + a.addIfExists("onmouseout", params.get("onmouseout")); + a.addIfExists("onfocus", params.get("onfocus")); + a.addIfExists("onblur", params.get("onblur")); + a.addIfExists("onkeypress", params.get("onkeypress")); + a.addIfExists("onkeydown", params.get("onkeydown")); + a.addIfExists("onkeyup", params.get("onkeyup")); + a.addIfExists("onselect", params.get("onselect")); + a.addIfExists("onchange", params.get("onchange")); + + super.start(name, a); + } + +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SelectHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SelectHandler.java new file mode 100644 index 000000000..280dbc612 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SelectHandler.java @@ -0,0 +1,143 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import com.opensymphony.xwork2.util.TextUtils; +import com.opensymphony.xwork2.util.ValueStack; +import org.apache.struts2.components.ListUIBean; +import org.apache.struts2.components.OptGroup; +import org.apache.struts2.util.ContainUtil; +import org.apache.struts2.util.MakeIterator; +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagGenerator; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class SelectHandler extends AbstractTagHandler implements TagGenerator { + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes a = new Attributes(); + + Object value = params.get("nameValue"); + + a.addDefaultToEmpty("name", params.get("name")) + .addIfExists("size", params.get("size")) + .addIfExists("value", value, false) + .addIfTrue("disabled", params.get("disabled")) + .addIfTrue("readonly", params.get("readonly")) + .addIfTrue("multiple", params.get("multiple")) + .addIfExists("tabindex", params.get("tabindex")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")) + .addIfExists("title", params.get("title")); + super.start("select", a); + + //options + + //header + String headerKey = (String) params.get("headerKey"); + String headerValue = (String) params.get("headerValue"); + if (headerKey != null && headerValue != null) { + boolean selected = ContainUtil.contains(value, params.get("headerKey")); + writeOption(headerKey, headerValue, selected); + } + + Object listObj = params.get("list"); + String listKey = (String) params.get("listKey"); + String listValue = (String) params.get("listValue"); + ValueStack stack = this.context.getStack(); + if (listObj != null) { + Iterator itt = MakeIterator.convert(listObj); + while (itt.hasNext()) { + Object item = itt.next(); + stack.push(item); + + //key + Object itemKey = findValue(listKey != null ? listKey : "top"); + String itemKeyStr = TextUtils.noNull(itemKey.toString()); + //value + Object itemValue = findValue(listValue != null ? listValue : "top"); + String itemValueStr = TextUtils.noNull(itemValue.toString()); + + boolean selected = ContainUtil.contains(value, itemKey); + writeOption(itemKeyStr, itemValueStr, selected); + + stack.pop(); + } + } + + //opt group + List listUIBeans = (List) params.get(OptGroup.INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY); + if (listUIBeans != null) { + for (ListUIBean listUIBean : listUIBeans) { + writeOptionGroup(listUIBean, value); + } + } + + super.end("select"); + } + + private void writeOption(String value, String text, boolean selected) throws IOException { + Attributes attrs = new Attributes(); + attrs.addIfExists("value", value) + .addIfTrue("selected", selected); + + start("option", attrs); + characters(text); + end("option"); + } + + private void writeOptionGroup(ListUIBean listUIBean, Object value) throws IOException { + Map params = listUIBean.getParameters(); + Attributes attrs = new Attributes(); + attrs.addIfExists("label", params.get("label")) + .addIfTrue("disabled", params.get("disabled")); + start("optgroup", attrs); + + //options + ValueStack stack = context.getStack(); + Object listObj = params.get("list"); + if (listObj != null) { + Iterator itt = MakeIterator.convert(listObj); + String listKey = (String) params.get("listKey"); + String listValue = (String) params.get("listValue"); + while (itt.hasNext()) { + Object optGroupBean = itt.next(); + stack.push(optGroupBean); + + Object tmpKey = stack.findValue(listKey != null ? listKey : "top"); + String tmpKeyStr = TextUtils.noNull(tmpKey.toString()); + Object tmpValue = stack.findValue(listValue != null ? listValue : "top"); + String tmpValueStr = TextUtils.noNull(tmpValue.toString()); + boolean selected = ContainUtil.contains(value, tmpKeyStr); + writeOption(tmpKeyStr, tmpValueStr, selected); + + stack.pop(); + } + } + + end("optgroup"); + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SimpleTheme.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SimpleTheme.java new file mode 100644 index 000000000..015b577d0 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SimpleTheme.java @@ -0,0 +1,74 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.DefaultTagHandlerFactory; +import org.apache.struts2.views.java.DefaultTheme; +import org.apache.struts2.views.java.TagHandlerFactory; +import org.apache.struts2.views.java.XHTMLTagSerializer; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class SimpleTheme extends DefaultTheme { + + public SimpleTheme() { + setHandlerFactories(new HashMap>() {{ + put("text", new FactoryList(TextFieldHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("textfield", new FactoryList(TextFieldHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("select", new FactoryList(SelectHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("form", new FactoryList(FormHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("form-close", new FactoryList(FormHandler.CloseHandler.class)); + put("a", new FactoryList(AnchorHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("a-close", new FactoryList(AnchorHandler.CloseHandler.class)); + put("checkbox", new FactoryList(CheckboxHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("file", new FactoryList(FileHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("password", new FactoryList(PasswordHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("label", new FactoryList(LabelHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("reset", new FactoryList(ResetHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("submit", new FactoryList(SubmitHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("submit-close", new FactoryList(SubmitHandler.CloseHandler.class)); + put("textarea", new FactoryList(TextAreaHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("actionerror", new FactoryList(ActionErrorHandler.class)); + put("token", new FactoryList(TokenHandler.class)); + put("actionmessage", new FactoryList(ActionMessageHandler.class)); + put("head", new FactoryList(HeadHandler.class)); + put("hidden", new FactoryList(HiddenHandler.class)); + put("fielderror", new FactoryList(FieldErrorHandler.class)); + put("div", new FactoryList(DivHandler.class, ScriptingEventsHandler.class, CommonAttributesHandler.class)); + put("div-close", new FactoryList(DivHandler.CloseHandler.class)); + put("empty", new FactoryList(EmptyHandler.class)); + }}); + setName("simple"); + } + + private class FactoryList extends ArrayList { + public FactoryList(Class... classes) { + super(); + for (Class cls : classes) { + add(new DefaultTagHandlerFactory(cls)); + } + add(new DefaultTagHandlerFactory(XHTMLTagSerializer.class)); + } + } + +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SubmitHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SubmitHandler.java new file mode 100644 index 000000000..b2f90eab5 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/SubmitHandler.java @@ -0,0 +1,98 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.TagGenerator; +import org.apache.struts2.views.java.Attributes; + +import java.io.IOException; +import java.util.Map; + +import com.opensymphony.xwork2.util.TextUtils; + +public class SubmitHandler extends AbstractTagHandler implements TagGenerator { + + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attrs = new Attributes(); + + String type = TextUtils.noNull((String) params.get("type"), "input"); + + if ("button".equals(type)) { + attrs.addIfExists("name", params.get("name")) + .add("type", "submit") + .addIfExists("value", params.get("nameValue"), false) + .addIfTrue("disabled", params.get("disabled")) + .addIfExists("tabindex", params.get("tabindex")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")); + + start("button", attrs); + } else if ("image".equals(type)) { + attrs.addIfExists("src", params.get("src"), false) + .add("type", "image") + .addIfExists("alt", params.get("label")); + + start("input", attrs); + } else { + attrs.addIfExists("name", params.get("name")) + .add("type", "submit") + .addIfExists("value", params.get("nameValue"), false) + .addIfTrue("disabled", params.get("disabled")) + .addIfExists("tabindex", params.get("tabindex")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")); + + start("input", attrs); + } + } + + public static class CloseHandler extends AbstractTagHandler implements TagGenerator { + @Override + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attrs = new Attributes(); + String body = (String) params.get("body"); + + String type = TextUtils.noNull((String) params.get("type"), "input"); + if ("button".equals(type)) { + //button body + if (TextUtils.stringSet(body)) + characters(body, false); + else if (params.containsKey("label")) { + String label = (String) params.get("label"); + if (TextUtils.stringSet(label)) + characters(label, false); + } + end("button"); + } else if ("image".equals(type)) { + if (TextUtils.stringSet(body)) + characters(body, false); + end("input"); + } else + end("input"); + } + } +} + + diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TextAreaHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TextAreaHandler.java new file mode 100644 index 000000000..554868f67 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TextAreaHandler.java @@ -0,0 +1,54 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.TagGenerator; +import org.apache.struts2.views.java.Attributes; + +import java.io.IOException; +import java.util.Map; + +import com.opensymphony.xwork2.util.TextUtils; + +public class TextAreaHandler extends AbstractTagHandler implements TagGenerator { + + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attrs = new Attributes(); + + attrs.addDefaultToEmpty("name", params.get("name")) + .addDefaultToEmpty("cols", params.get("cols")) + .addDefaultToEmpty("rows", params.get("rows")) + .addIfExists("wrap", params.get("wrap")) + .addIfTrue("disabled", params.get("disabled")) + .addIfTrue("readonly", params.get("readonly")) + .addIfExists("tabindex", params.get("tabindex")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")) + .addIfExists("title", params.get("title")); + start("textarea", attrs); + String value = (String) params.get("nameValue"); + if (TextUtils.stringSet(value)) + characters(value); + end("textarea"); + } +} \ No newline at end of file diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TextFieldHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TextFieldHandler.java new file mode 100644 index 000000000..d7cdd7ce4 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TextFieldHandler.java @@ -0,0 +1,50 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.Attributes; +import org.apache.struts2.views.java.TagGenerator; + +import java.io.IOException; +import java.util.Map; + +public class TextFieldHandler extends AbstractTagHandler implements TagGenerator { + + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attr = new Attributes(); + + attr.add("type", "text") + .addDefaultToEmpty("name", params.get("name")) + .addIfExists("size", params.get("size")) + .addIfExists("maxlength", params.get("maxlength")) + .addIfExists("value", params.get("nameValue"), false) + .addIfTrue("disabled", params.get("disabled")) + .addIfTrue("readonly", params.get("readonly")) + .addIfExists("tabindex", params.get("tabindex")) + .addIfExists("id", params.get("id")) + .addIfExists("class", params.get("cssClass")) + .addIfExists("style", params.get("cssStyle")) + .addIfExists("title", params.get("title")); + super.start("input", attr); + super.end("input"); + } +} diff --git a/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TokenHandler.java b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TokenHandler.java new file mode 100644 index 000000000..8b65ce485 --- /dev/null +++ b/plugins/javatemplates/src/main/java/org/apache/struts2/views/java/simple/TokenHandler.java @@ -0,0 +1,53 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.views.java.TagGenerator; +import org.apache.struts2.views.java.Attributes; + +import java.io.IOException; +import java.util.Map; + +public class TokenHandler extends AbstractTagHandler implements TagGenerator { + + public void generate() throws IOException { + Map params = context.getParameters(); + Attributes attrs = new Attributes(); + + //first input + attrs.add("type", "hidden") + .addDefaultToEmpty("name", params.get("tokenNameField"), false) + .addDefaultToEmpty("value", params.get("name")); + + start("input", attrs); + end("input"); + + //second input + attrs = new Attributes(); + attrs.add("type", "hidden") + .addDefaultToEmpty("name", params.get("name"), false) + .addDefaultToEmpty("value", params.get("token")); + + start("input", attrs); + end("input"); + } +} + diff --git a/plugins/javatemplates/src/main/resources/struts-plugin.xml b/plugins/javatemplates/src/main/resources/struts-plugin.xml new file mode 100644 index 000000000..028b89023 --- /dev/null +++ b/plugins/javatemplates/src/main/resources/struts-plugin.xml @@ -0,0 +1,11 @@ + + + + + + + + + diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractCommonAttributesTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractCommonAttributesTest.java new file mode 100644 index 000000000..4b93df858 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractCommonAttributesTest.java @@ -0,0 +1,33 @@ +package org.apache.struts2.views.java.simple; + +import org.apache.struts2.components.UIBean; + +public abstract class AbstractCommonAttributesTest extends AbstractTest { + public void testRenderTextFieldScriptingAttrs() throws Exception { + UIBean tag = getUIBean(); + + applyScriptingAttrs(tag); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + + assertScriptingAttrs(output); + } + + public void testRenderTextFieldCommonAttrs() throws Exception { + UIBean tag = getUIBean(); + + + applyCommonAttrs(tag); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + + assertCommongAttrs(output); + } + +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java new file mode 100644 index 000000000..b49a28ab5 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java @@ -0,0 +1,162 @@ +package org.apache.struts2.views.java.simple; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.conversion.impl.XWorkConverter; +import com.opensymphony.xwork2.inject.Container; +import com.opensymphony.xwork2.util.ValueStack; +import junit.framework.TestCase; +import org.apache.struts2.StrutsConstants; +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.components.Component; +import org.apache.struts2.components.UIBean; +import org.apache.struts2.components.template.Template; +import org.apache.struts2.components.template.TemplateRenderingContext; +import org.easymock.EasyMock; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; +import java.util.Stack; + +public abstract class AbstractTest extends TestCase { + private Map scriptingAttrs = new HashMap(); + private Map commonAttrs = new HashMap(); + + protected SimpleTheme theme; + + protected StringWriter writer; + protected Map map; + + protected Template template; + protected Map stackContext; + protected ValueStack stack; + protected TemplateRenderingContext context; + protected HttpServletRequest request; + protected HttpServletResponse response; + + protected abstract UIBean getUIBean() throws Exception; + + protected abstract String getTagName(); + + @Override + protected void setUp() throws Exception { + super.setUp(); + scriptingAttrs.put("onclick", "onclick_"); + scriptingAttrs.put("ondblclick", "ondblclick_"); + scriptingAttrs.put("onmousedown", "onmousedown_"); + scriptingAttrs.put("onmouseup", "onmouseup_"); + scriptingAttrs.put("onmouseover", "onmouseover_"); + scriptingAttrs.put("onmousemove", "onmousemove_"); + scriptingAttrs.put("onmouseout", "onmouseout_"); + scriptingAttrs.put("onfocus", "onfocus_"); + scriptingAttrs.put("onblur", "onblur_"); + scriptingAttrs.put("onkeypress", "onkeypress_"); + scriptingAttrs.put("onkeydown", "onkeydown_"); + scriptingAttrs.put("onkeyup", "onkeyup_"); + scriptingAttrs.put("onselect", "onselect_"); + scriptingAttrs.put("onchange", "onchange_"); + + commonAttrs.put("accesskey", "accesskey_"); + + theme = new SimpleTheme(); + writer = new StringWriter(); + map = new HashMap(); + + template = org.easymock.classextension.EasyMock.createMock(Template.class); + stack = EasyMock.createNiceMock(ValueStack.class); + setUpStack(); + stackContext = new HashMap(); + + context = new TemplateRenderingContext(template, writer, stack, map, null); + stackContext.put(Component.COMPONENT_STACK, new Stack()); + + request = EasyMock.createNiceMock(HttpServletRequest.class); + EasyMock.expect(request.getContextPath()).andReturn("/some/path").anyTimes(); + response = EasyMock.createNiceMock(HttpServletResponse.class); + + EasyMock.expect(stack.getContext()).andReturn(stackContext).anyTimes(); + + Container container = EasyMock.createNiceMock(Container.class); + XWorkConverter converter = new ConverterEx(); + EasyMock.expect(container.getInstance(String.class, StrutsConstants.STRUTS_TAG_ALTSYNTAX)).andReturn("true").anyTimes(); + EasyMock.expect(container.getInstance(XWorkConverter.class)).andReturn(converter).anyTimes(); + stackContext.put(ActionContext.CONTAINER, container); + + + EasyMock.replay(request); + EasyMock.replay(stack); + EasyMock.replay(container); + + ActionContext.setContext(new ActionContext(stackContext)); + ServletActionContext.setRequest(request); + } + + protected static String s(String input) { + return input.replaceAll("'", "\""); + } + + protected void expectFind(String expr, Class toClass, Object returnVal) { + EasyMock.expect(stack.findValue(expr, toClass)).andReturn(returnVal); + } + + protected void expectFind(String expr, Object returnVal) { + EasyMock.expect(stack.findValue(expr)).andReturn(returnVal); + } + + protected void setUpStack() { + //TODO setup a config with stack and all..for real + } + + protected void applyScriptingAttrs(UIBean bean) { + bean.setOnclick(scriptingAttrs.get("onclick")); + bean.setOndblclick(scriptingAttrs.get("ondblclick")); + bean.setOnmousedown(scriptingAttrs.get("onmousedown")); + bean.setOnmouseup(scriptingAttrs.get("onmouseup")); + bean.setOnmouseover(scriptingAttrs.get("onmouseover")); + bean.setOnmousemove(scriptingAttrs.get("onmousemove")); + bean.setOnmouseout(scriptingAttrs.get("onmouseout")); + bean.setOnfocus(scriptingAttrs.get("onfocus")); + bean.setOnblur(scriptingAttrs.get("onblur")); + bean.setOnkeypress(scriptingAttrs.get("onkeypress")); + bean.setOnkeydown(scriptingAttrs.get("onkeydown")); + bean.setOnkeyup(scriptingAttrs.get("onkeyup")); + bean.setOnselect(scriptingAttrs.get("onselect")); + bean.setOnchange(scriptingAttrs.get("onchange")); + } + + protected void applyCommonAttrs(UIBean bean) { + bean.setAccesskey("accesskey_"); + } + + protected void assertScriptingAttrs(String str) { + for (Map.Entry entry : scriptingAttrs.entrySet()) { + String substr = entry.getKey() + "=\"" + entry.getValue() + "\""; + assertTrue("String [" + substr + "] was not found in [" + str + "]", str.indexOf(substr) >= 0); + } + } + + protected void assertCommongAttrs(String str) { + for (Map.Entry entry : commonAttrs.entrySet()) { + String substr = entry.getKey() + "=\"" + entry.getValue() + "\""; + assertTrue("String [" + substr + "] was not found in [" + str + "]", str.indexOf(substr) >= 0); + } + } + + protected Object doFindValue(String expr, Class toType) { + Object val = stack.findValue(expr); + + if (toType == String.class) + return val == null ? expr : val; + else + return val == null ? null : val; + } + + //XWorkConverter doesnt have a public onstructor (the one with parameters will require mor config) + public class ConverterEx extends XWorkConverter { + public ConverterEx() { + + } + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ActionErrorTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ActionErrorTest.java new file mode 100644 index 000000000..22eb6d6bd --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ActionErrorTest.java @@ -0,0 +1,92 @@ +/* + * $Id: Bean.java 726216 2008-12-13 14:58:16Z musachy $ + * + * 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.views.java.simple; + +import org.apache.struts2.components.ActionError; +import org.apache.struts2.components.Anchor; +import org.apache.struts2.components.UIBean; + +import java.util.ArrayList; +import java.util.List; + +public class ActionErrorTest extends AbstractTest { + private ActionError tag; + private List errors; + + public void testRenderActionError() { + tag.setCssClass("class"); + tag.setCssStyle("style"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s("
  • this clas is bad
  • baaaaad
"); + assertEquals(expected, output); + } + + public void testRenderActionErrorWithoutCssClass() { + tag.setCssStyle("style"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s("
  • this clas is bad
  • baaaaad
"); + assertEquals(expected, output); + } + + public void testRenderActionErrorNoErrors() { + this.errors.clear(); + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + assertEquals("", output); + } + + @Override + protected void setUp() throws Exception { + this.errors = new ArrayList(); + this.errors.add("this clas is bad"); + this.errors.add("baaaaad"); + + //errors are needed to setup stack + super.setUp(); + this.tag = new ActionError(stack, request, response); + } + + @Override + protected void setUpStack() { + super.setUpStack(); + expectFind("actionErrors", this.errors); + } + + @Override + protected UIBean getUIBean() { + return new Anchor(stack, request, response); + } + + @Override + protected String getTagName() { + return "actionerror"; + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ActionMessageTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ActionMessageTest.java new file mode 100644 index 000000000..920697848 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ActionMessageTest.java @@ -0,0 +1,72 @@ +package org.apache.struts2.views.java.simple; + +import org.apache.struts2.components.ActionError; +import org.apache.struts2.components.UIBean; + +import java.util.ArrayList; +import java.util.List; + +public class ActionMessageTest extends AbstractTest { + private ActionError tag; + private List errors; + + public void testRenderActionError() { + tag.setCssClass("class"); + tag.setCssStyle("style"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s("
  • this clas is bad
  • baaaaad
"); + assertEquals(expected, output); + } + + public void testRenderActionErrorWithoutCssClass() { + tag.setCssStyle("style"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s("
  • this clas is bad
  • baaaaad
"); + assertEquals(expected, output); + } + + public void testRenderActionErrorNoErrors() { + this.errors.clear(); + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + assertEquals("", output); + } + + @Override + protected void setUp() throws Exception { + this.errors = new ArrayList(); + this.errors.add("this clas is bad"); + this.errors.add("baaaaad"); + + //errors are needed to setup stack + super.setUp(); + this.tag = new ActionError(stack, request, response); + } + + @Override + protected void setUpStack() { + super.setUpStack(); + expectFind("actionMessages", this.errors); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "actionmessage"; + } +} + diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AnchorTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AnchorTest.java new file mode 100644 index 000000000..d8520c4cc --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AnchorTest.java @@ -0,0 +1,63 @@ +/* + * $Id: Bean.java 726216 2008-12-13 14:58:16Z musachy $ + * + * 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.views.java.simple; + +import org.apache.struts2.components.Anchor; +import org.apache.struts2.components.UIBean; + +public class AnchorTest extends AbstractCommonAttributesTest { + private Anchor tag; + + public void testRenderAnchor() { + tag.setName("name_"); + tag.setDisabled("true"); + tag.setTabindex("1"); + tag.setId("id_"); + tag.setCssClass("class"); + tag.setCssStyle("style"); + tag.setTitle("title"); + tag.setHref("http://sometest.com?ab=10"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + theme.renderTag(getTagName() + "-close", context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new Anchor(stack, request, response); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "a"; + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/Bean.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/Bean.java new file mode 100644 index 000000000..f481372bc --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/Bean.java @@ -0,0 +1,42 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +public class Bean { + private int intField; + private String stringField; + + public int getIntField() { + return intField; + } + + public void setIntField(int intField) { + this.intField = intField; + } + + public String getStringField() { + return stringField; + } + + public void setStringField(String stringField) { + this.stringField = stringField; + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/CheckboxTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/CheckboxTest.java new file mode 100644 index 000000000..06356400c --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/CheckboxTest.java @@ -0,0 +1,82 @@ +/* + * $Id: Bean.java 726216 2008-12-13 14:58:16Z musachy $ + * + * 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.views.java.simple; + +import org.apache.struts2.components.Checkbox; +import org.apache.struts2.components.UIBean; + +public class CheckboxTest extends AbstractCommonAttributesTest { + private Checkbox tag; + + public void testRenderCheckbox() { + tag.setName("name_"); + tag.setDisabled("true"); + tag.setTabindex("1"); + tag.setId("id_"); + tag.setCssClass("class"); + tag.setCssStyle("style"); + tag.setTitle("title"); + tag.setFieldValue("xyz"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderCheckboxWithNameValue() { + tag.setName("name_"); + tag.setValue("%{someValue}"); + tag.setDisabled("true"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + @Override + protected void setUpStack() { + super.setUpStack(); + + expectFind("someValue", Boolean.class, true); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + tag = new Checkbox(stack, request, response); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "checkbox"; + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/DivTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/DivTest.java new file mode 100644 index 000000000..ff978954e --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/DivTest.java @@ -0,0 +1,61 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.Div; +import org.apache.struts2.components.UIBean; + +public class DivTest extends AbstractCommonAttributesTest { + private Div tag; + + public void testRenderAnchor() { + tag.setName("name_"); + tag.setDisabled("true"); + tag.setId("id_"); + tag.setCssClass("class"); + tag.setCssStyle("style"); + tag.setTitle("title"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + theme.renderTag(getTagName() + "-close", context); + String output = writer.getBuffer().toString(); + String expected = s("
"); + assertEquals(expected, output); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new Div(stack, request, response); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "div"; + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FieldErrorTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FieldErrorTest.java new file mode 100644 index 000000000..9728c5742 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FieldErrorTest.java @@ -0,0 +1,126 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.FieldError; +import org.apache.struts2.components.UIBean; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.LinkedHashMap; + +public class FieldErrorTest extends AbstractTest { + private FieldError tag; + private Map> errors; + private List fieldNames; + + public void testRenderFieldError() { + tag.setCssClass("class"); + tag.setCssStyle("style"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s("
  • not good
  • bad
  • bad to the bone
"); + assertEquals(expected, output); + } + + public void testRenderFieldErrorWithoutCssClass() { + tag.setCssStyle("style"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s("
  • not good
  • bad
  • bad to the bone
"); + assertEquals(expected, output); + } + + public void testRenderFieldErrorWithoutFieldName() { + this.fieldNames.clear(); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s("
  • not good
  • bad
  • bad to the bone
"); + assertEquals(expected, output); + } + + public void testRenderFieldErrorWithoutOneFieldName() { + tag.setFieldName("field1"); + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s("
  • not good
  • bad
"); + assertEquals(expected, output); + } + + public void testRenderActionErrorNoErrors() { + this.errors.clear(); + this.fieldNames.clear(); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + assertEquals("", output); + } + + @Override + protected void setUp() throws Exception { + this.errors = new LinkedHashMap>() { + { + put("field1", Arrays.asList("not good", "bad")); + put("field2", Arrays.asList("bad to the bone")); + } + }; + this.fieldNames = new ArrayList(); + this.fieldNames.add("field1"); + this.fieldNames.add("field2"); + + //errors are needed to setup stack + super.setUp(); + this.tag = new FieldError(stack, request, response); + } + + @Override + protected void setUpStack() { + super.setUpStack(); + expectFind("fieldErrorFieldNames", this.fieldNames); + expectFind("fieldErrors", this.errors); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "fielderror"; + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FileTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FileTest.java new file mode 100644 index 000000000..1050b48b3 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FileTest.java @@ -0,0 +1,66 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.File; +import org.apache.struts2.components.UIBean; + +public class FileTest extends AbstractCommonAttributesTest { + private File tag; + + public void testRenderTextField() { + tag.setName("name"); + tag.setValue("val1"); + tag.setSize("10"); + tag.setDisabled("true"); + tag.setAccept("accept_"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new File(stack, request, response); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "file"; + } +} + diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FormTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FormTest.java new file mode 100644 index 000000000..af8f303c9 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/FormTest.java @@ -0,0 +1,84 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.Form; +import org.apache.struts2.components.UIBean; +import org.apache.struts2.components.UrlRenderer; +import org.easymock.EasyMock; + +public class FormTest extends AbstractCommonAttributesTest { + private Form tag; + + public void testRenderForm() { + tag.setName("name_"); + tag.setDisabled("true"); + tag.setTabindex("1"); + tag.setId("id_"); + tag.setCssClass("class_"); + tag.setCssStyle("style_"); + tag.setTitle("title"); + tag.setAcceptcharset("charset_"); + tag.setAction("action_"); + tag.setOnsubmit("submit"); + tag.setOnreset("reset"); + tag.setTarget("target_"); + tag.setEnctype("enc"); + tag.setMethod("post"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + theme.renderTag(getTagName() + "-close", context); + String output = writer.getBuffer().toString(); + String expected = s("
"); + assertEquals(expected, output); + } + + public void testDefaultMethod() { + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + theme.renderTag(getTagName() + "-close", context); + String output = writer.getBuffer().toString(); + String expected = s("
"); + assertEquals(expected, output); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "form"; + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + tag = new Form(stack, request, response); + UrlRenderer renderer = EasyMock.createNiceMock(UrlRenderer.class); + EasyMock.replay(renderer); + tag.setUrlRenderer(renderer); + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/HeadTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/HeadTest.java new file mode 100644 index 000000000..a7057f36b --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/HeadTest.java @@ -0,0 +1,55 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.Head; +import org.apache.struts2.components.UIBean; + +public class HeadTest extends AbstractTest { + private Head tag; + + public void testRenderTextField() { + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new Head(stack, request, response); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "head"; + } +} + + diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/HiddenTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/HiddenTest.java new file mode 100644 index 000000000..9758df672 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/HiddenTest.java @@ -0,0 +1,65 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.Hidden; +import org.apache.struts2.components.UIBean; + +public class HiddenTest extends AbstractTest { + private Hidden tag; + + public void testRenderHidden() { + tag.setName("name"); + tag.setValue("val1"); + tag.setDisabled("true"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new Hidden(stack, request, response); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "hidden"; + } +} + + diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/LabelTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/LabelTest.java new file mode 100644 index 000000000..faa9e6c41 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/LabelTest.java @@ -0,0 +1,66 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.Hidden; +import org.apache.struts2.components.UIBean; +import org.apache.struts2.components.Label; + +public class LabelTest extends AbstractCommonAttributesTest { + private Label tag; + + public void testRenderLabel() { + tag.setName("name"); + tag.setValue("val1"); + tag.setId("id1"); + tag.setFor("for_"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new Label(stack, request, response); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "label"; + } +} + + + diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/PasswordTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/PasswordTest.java new file mode 100644 index 000000000..3d3b6dcea --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/PasswordTest.java @@ -0,0 +1,103 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.File; +import org.apache.struts2.components.UIBean; +import org.apache.struts2.components.Password; +import org.easymock.EasyMock; + +public class PasswordTest extends AbstractCommonAttributesTest { + private Password tag; + private boolean showPassword; + + public void testRenderPassword() throws Exception { + this.showPassword = false; + super.setUp(); + this.tag = new Password(stack, request, response); + + tag.setName("name"); + tag.setValue("val1"); + tag.setSize("10"); + tag.setDisabled("true"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderPasswordShowIt() throws Exception { + this.showPassword = true; + super.setUp(); + this.tag = new Password(stack, request, response); + + tag.setName("name"); + tag.setValue("val1"); + tag.setSize("10"); + tag.setDisabled("true"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + tag.setShowPassword("%{'true'}"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + @Override + protected void setUp() throws Exception { + //dont call base setup + } + + @Override + protected void setUpStack() { + expectFind("'true'", Boolean.class, true); + } + + @Override + protected UIBean getUIBean() throws Exception { + super.setUp(); + this.tag = new Password(stack, request, response); + return tag; + } + + @Override + protected String getTagName() { + return "password"; + } +} + + diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ResetTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ResetTest.java new file mode 100644 index 000000000..a218744cd --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/ResetTest.java @@ -0,0 +1,86 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.File; +import org.apache.struts2.components.UIBean; +import org.apache.struts2.components.Reset; + +public class ResetTest extends AbstractCommonAttributesTest { + private Reset tag; + + public void testRenderResetButton() { + tag.setName("name"); + tag.setValue("val1"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + tag.setType("button"); + tag.setLabel("some label"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s("some label"); + assertEquals(expected, output); + } + + public void testRenderResetNoType() { + tag.setName("name"); + tag.setValue("val1"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + tag.setLabel("some label"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new Reset(stack, request, response); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "reset"; + } +} + + diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/SelectTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/SelectTest.java new file mode 100644 index 000000000..bfe7e0360 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/SelectTest.java @@ -0,0 +1,144 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.Select; +import org.apache.struts2.components.UIBean; + +import java.util.Arrays; +import java.util.HashMap; + +public class SelectTest extends AbstractCommonAttributesTest { + private Bean bean1; + private Select tag; + + public void testRenderSelect() { + tag.setName("name_"); + tag.setSize("10"); + tag.setDisabled("true"); + tag.setMultiple("true"); + tag.setTabindex("1"); + tag.setId("id_"); + tag.setCssClass("class"); + tag.setCssStyle("style"); + tag.setTitle("title"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderSelectWithHeader() { + tag.setList("%{{'key0', 'key1'}}"); + tag.setHeaderKey("%{'key0'}"); + tag.setHeaderValue("%{'val'}"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderSelectWithOptions() { + tag.setList("%{list}"); + tag.setListKey("intField"); + tag.setListValue("stringField"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderSelectWithMapOptions() { + tag.setList("%{#{'key0' : 'val'}}"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderSelectWithOptionSelected() { + tag.setList("%{list}"); + tag.setListKey("intField"); + tag.setListValue("stringField"); + tag.setValue("%{'1'}"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "select"; + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new Select(stack, request, response); + } + + @Override + protected void setUpStack() { + super.setUpStack(); + bean1 = new Bean(); + bean1.setIntField(1); + bean1.setStringField("val"); + + + expectFind("'key0'", String.class, "key0"); + expectFind("'key1'", String.class, "key1"); + expectFind("'val'", String.class, "val"); + expectFind("'val1'", String.class, "val1"); + expectFind("'1'", "1"); + expectFind("list", Arrays.asList(bean1)); + expectFind("key", "key0"); + expectFind("value", "val"); + expectFind("#{'key0' : 'val'}", new HashMap() { + { + put("key0", "val"); + } + }); + + expectFind("intField", 1); + expectFind("stringField", "val"); + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/SubmitTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/SubmitTest.java new file mode 100644 index 000000000..d58503ea2 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/SubmitTest.java @@ -0,0 +1,169 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.File; +import org.apache.struts2.components.UIBean; +import org.apache.struts2.components.Submit; + +public class SubmitTest extends AbstractCommonAttributesTest { + private Submit tag; + + public void testRenderButtonWithBody() { + tag.setName("name"); + tag.setValue("val1"); + tag.setDisabled("true"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + tag.setLabel("some label"); + tag.setType("button"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + + tag.addParameter("body", "hey hey hey, here I go now"); + map.clear(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName() + "-close", context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderButtonWithLabel() { + tag.setName("name"); + tag.setValue("val1"); + tag.setDisabled("true"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + tag.setLabel("Just as soon as I belong, than its time I disappear"); + tag.setType("button"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + theme.renderTag(getTagName() + "-close", context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderButtonSubmit() { + tag.setName("name"); + tag.setValue("val1"); + tag.setDisabled("true"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + tag.setLabel("label"); + tag.setType("input"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + theme.renderTag(getTagName() + "-close", context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderButtonWithoutType() { + tag.setName("name"); + tag.setValue("val1"); + tag.setDisabled("true"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + tag.setLabel("label"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + theme.renderTag(getTagName() + "-close", context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderButtonImage() { + tag.setSrc("http://somesource/image.gif"); + tag.setLabel("alt text"); + tag.setType("image"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + theme.renderTag(getTagName() + "-close", context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderButtonImageWithBody() { + tag.setSrc("http://somesource/image.gif"); + tag.setLabel("alt text"); + tag.setType("image"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + map.clear(); + tag.setType("image"); + tag.addParameter("body", "hey hey hey, here I go now"); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName() + "-close", context); + String output = writer.getBuffer().toString(); + String expected = s("hey hey hey, here I go now"); + assertEquals(expected, output); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new Submit(stack, request, response); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "submit"; + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TextAreaTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TextAreaTest.java new file mode 100644 index 000000000..aa4e2267c --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TextAreaTest.java @@ -0,0 +1,86 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.TextField; +import org.apache.struts2.components.UIBean; +import org.apache.struts2.components.TextArea; + +public class TextAreaTest extends AbstractCommonAttributesTest { + private TextArea tag; + + public void testRenderTextArea() { + tag.setName("name"); + tag.setValue("val1"); + tag.setDisabled("true"); + tag.setReadonly("true"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + tag.setRows("1"); + tag.setCols("2"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + public void testRenderTextAreaDefaults() { + tag.setValue("val1"); + tag.setDisabled("true"); + tag.setReadonly("true"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new TextArea(stack, request, response); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "textarea"; + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TextFieldTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TextFieldTest.java new file mode 100644 index 000000000..7371fa1b4 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TextFieldTest.java @@ -0,0 +1,66 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.TextField; +import org.apache.struts2.components.UIBean; + +public class TextFieldTest extends AbstractCommonAttributesTest { + private TextField tag; + + public void testRenderTextField() { + tag.setName("name"); + tag.setValue("val1"); + tag.setSize("10"); + tag.setMaxlength("11"); + tag.setDisabled("true"); + tag.setReadonly("true"); + tag.setTabindex("1"); + tag.setId("id1"); + tag.setCssClass("class1"); + tag.setCssStyle("style1"); + tag.setTitle("title"); + + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + String expected = s(""); + assertEquals(expected, output); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new TextField(stack, request, response); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "textfield"; + } +} diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TokenTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TokenTest.java new file mode 100644 index 000000000..3de0c5900 --- /dev/null +++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/TokenTest.java @@ -0,0 +1,69 @@ +/* + * $Id$ + * + * 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.views.java.simple; + +import org.apache.struts2.components.TextField; +import org.apache.struts2.components.UIBean; +import org.apache.struts2.components.Token; +import com.opensymphony.xwork2.ActionContext; + +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Pattern; + +public class TokenTest extends AbstractTest { + private Token tag; + + public void testRenderTokenTag() { + tag.setName("name"); + tag.setValue("val1"); + + tag.evaluateParams(); + map.putAll(tag.getParameters()); + theme.renderTag(getTagName(), context); + String output = writer.getBuffer().toString(); + + + //token id is random + String pattern = s(""); + assertTrue(Pattern.matches(pattern, output)); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + this.tag = new Token(stack, request, response); + + Map map = new HashMap(); + map.put(ActionContext.SESSION, new HashMap()); + ActionContext.setContext(new ActionContext(map)); + } + + @Override + protected UIBean getUIBean() { + return tag; + } + + @Override + protected String getTagName() { + return "token"; + } +}