You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by me...@apache.org on 2009/04/15 12:22:08 UTC
svn commit: r765116 [1/2] - in /incubator/click/trunk/click:
extras/src/org/apache/click/extras/control/ framework/src/org/apache/click/
framework/src/org/apache/click/element/
Author: medgar
Date: Wed Apr 15 10:22:08 2009
New Revision: 765116
URL: http://svn.apache.org/viewvc?rev=765116&view=rev
Log:
first post! - formatting changes and checkstyle updates
Modified:
incubator/click/trunk/click/extras/src/org/apache/click/extras/control/DateField.java
incubator/click/trunk/click/extras/src/org/apache/click/extras/control/HiddenList.java
incubator/click/trunk/click/extras/src/org/apache/click/extras/control/TableInlinePaginator.java
incubator/click/trunk/click/framework/src/org/apache/click/ClickServlet.java
incubator/click/trunk/click/framework/src/org/apache/click/Page.java
incubator/click/trunk/click/framework/src/org/apache/click/element/CssImport.java
incubator/click/trunk/click/framework/src/org/apache/click/element/CssStyle.java
incubator/click/trunk/click/framework/src/org/apache/click/element/Element.java
incubator/click/trunk/click/framework/src/org/apache/click/element/JsImport.java
incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java
incubator/click/trunk/click/framework/src/org/apache/click/element/ResourceElement.java
Modified: incubator/click/trunk/click/extras/src/org/apache/click/extras/control/DateField.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/extras/src/org/apache/click/extras/control/DateField.java?rev=765116&r1=765115&r2=765116&view=diff
==============================================================================
--- incubator/click/trunk/click/extras/src/org/apache/click/extras/control/DateField.java (original)
+++ incubator/click/trunk/click/extras/src/org/apache/click/extras/control/DateField.java Wed Apr 15 10:22:08 2009
@@ -65,6 +65,8 @@
* @author Malcolm Edgar
*/
public class DateField extends TextField {
+
+ private static final long serialVersionUID = 1L;
// ----------------------------------------------------- Instance Variables
Modified: incubator/click/trunk/click/extras/src/org/apache/click/extras/control/HiddenList.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/extras/src/org/apache/click/extras/control/HiddenList.java?rev=765116&r1=765115&r2=765116&view=diff
==============================================================================
--- incubator/click/trunk/click/extras/src/org/apache/click/extras/control/HiddenList.java (original)
+++ incubator/click/trunk/click/extras/src/org/apache/click/extras/control/HiddenList.java Wed Apr 15 10:22:08 2009
@@ -31,20 +31,20 @@
* However it can not render multiple values as a same name.
* This control can have multiple values and it renders them as multiple hidden field.
* </p>
- *
+ *
* <h3>HiddenList Examples</h3>
- *
+ *
* <pre class="codeJava">
* HiddenList hiddenList = <span class="kw">new</span> HiddenList(<span class="st">"hiddenList"</span>);
* hiddenList.addValue(<span class="st">"001"</span>);
* hiddenList.addValue(<span class="st">"002"</span>); </pre>
- *
+ *
* This <code>HiddenList</code> would generate following HTML:
- *
+ *
* <pre class="codeHtml">
* <input type="hidden" name="hiddenList" id="form-hiddenList_1" value="001"/>
* <input type="hidden" name="hiddenList" id="form-hiddenList_2" value="002"/> </pre>
- *
+ *
* @author Naoki Takezoe
* @since 2.1.0
*/
@@ -61,7 +61,7 @@
/**
* Create a HiddenList with the given name.
- *
+ *
* @param name the name of the field
*/
public HiddenList(String name) {
@@ -72,7 +72,7 @@
/**
* Set the list of hidden values.
- *
+ *
* @param valueObject a list of Strings
*/
public void setValueObject(Object valueObject) {
@@ -84,7 +84,7 @@
/**
* Returns the list of added values as a <tt>List</tt> of Strings.
- *
+ *
* @return a list of Strings
*/
public Object getValueObject() {
@@ -97,16 +97,16 @@
/**
* This method delegates to {@link #getValueObject()} to return the
* hidden values as a <tt>java.util.List</tt> of Strings.
- *
+ *
* @return a list of Strings
*/
public List getValues() {
return (List) getValueObject();
}
-
+
/**
* Add the given value to this <code>HiddenList</code>.
- *
+ *
* @param value the hidden value to add
*/
public void addValue(String value) {
@@ -120,7 +120,7 @@
*/
public void bindRequestValue() {
String[] values = getContext().getRequestParameterValues(getName());
-
+
if (values != null) {
List list = new ArrayList();
for (int i = 0; i < values.length; i++) {
Modified: incubator/click/trunk/click/extras/src/org/apache/click/extras/control/TableInlinePaginator.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/extras/src/org/apache/click/extras/control/TableInlinePaginator.java?rev=765116&r1=765115&r2=765116&view=diff
==============================================================================
--- incubator/click/trunk/click/extras/src/org/apache/click/extras/control/TableInlinePaginator.java (original)
+++ incubator/click/trunk/click/extras/src/org/apache/click/extras/control/TableInlinePaginator.java Wed Apr 15 10:22:08 2009
@@ -46,6 +46,8 @@
/** Private Control which handles resource bundle properties. */
private AbstractControl paginatorMessages = new AbstractControl() {
+ private static final long serialVersionUID = 1L;
+
/**
* Messages are defined in the resource bundle:
* <tt>org/apache/click/extras/control/TableInlinePaginator.properties</tt>.
Modified: incubator/click/trunk/click/framework/src/org/apache/click/ClickServlet.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/ClickServlet.java?rev=765116&r1=765115&r2=765116&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/ClickServlet.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/ClickServlet.java Wed Apr 15 10:22:08 2009
@@ -23,7 +23,6 @@
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Enumeration;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -38,21 +37,19 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
+import ognl.Ognl;
+import ognl.OgnlException;
+import ognl.TypeConverter;
+
import org.apache.click.service.ConfigService;
import org.apache.click.service.LogService;
import org.apache.click.service.XmlConfigService;
import org.apache.click.util.ClickUtils;
import org.apache.click.util.ErrorPage;
-import org.apache.click.util.Format;
import org.apache.click.util.HtmlStringBuffer;
import org.apache.click.util.PageImports;
import org.apache.click.util.PropertyUtils;
import org.apache.click.util.RequestTypeConverter;
-import org.apache.click.util.SessionMap;
-import ognl.Ognl;
-import ognl.OgnlException;
-import ognl.TypeConverter;
-
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;
Modified: incubator/click/trunk/click/framework/src/org/apache/click/Page.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/Page.java?rev=765116&r1=765115&r2=765116&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/Page.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/Page.java Wed Apr 15 10:22:08 2009
@@ -24,11 +24,9 @@
import java.util.List;
import java.util.Map;
-import org.apache.click.element.Element;
import org.apache.click.util.Format;
import org.apache.click.util.MessagesMap;
import org.apache.click.util.PageImports;
-
import org.apache.commons.lang.StringUtils;
/**
Modified: incubator/click/trunk/click/framework/src/org/apache/click/element/CssImport.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/element/CssImport.java?rev=765116&r1=765115&r2=765116&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/element/CssImport.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/element/CssImport.java Wed Apr 15 10:22:08 2009
@@ -1,255 +1,256 @@
-/*
- * 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.click.element;
-
-import org.apache.click.Context;
-import org.apache.click.util.ClickUtils;
-import org.apache.click.util.HtmlStringBuffer;
-import org.apache.commons.lang.builder.HashCodeBuilder;
-
-/**
- * Provides a Css HEAD element for importing <tt>external</tt> Cascading
- * Stylesheet files using the <link> tag.
- * <p/>
- * Example usage:
- * <pre class="prettyprint">
- * public class MyPage extends Page {
- *
- * public List getHeadElements() {
- * // We use lazy loading to ensure the CSS import is only added the
- * // first time this method is called.
- * if (headElements == null) {
- * // Get the head elements from the super implementation
- * headElements = super.getHeadElements();
- *
- * CssImport cssImport = new CssImport("/css/style.css");
- * headElements.add(cssImport);
- * }
- * return headElements;
- * }
- * } </pre>
- *
- * The <tt>cssImport</tt> instance will be rendered as follows (assuming the
- * context path is <tt>myApp</tt>):
- * <pre class="prettyprint">
- * <link type="text/css" rel="stylesheet" href="/myApp/css/style.css"/> </pre>
- *
- * @author Bob Schellink
- */
-public class CssImport extends ResourceElement {
-
- // ----------------------------------------------------------- Constructors
-
- /**
- * Constructs a new Css import element.
- * <p/>
- * The CssImport {@link #setVersionIndicator(java.lang.String) version indicator}
- * will automatically be set to the
- * {@link ClickUtils#getApplicationResourceVersionIndicator() application version indicator}.
- */
- public CssImport() {
- this(null);
- }
-
- /**
- * Construct a new Css import element with the specified <tt>href</tt>
- * attribute.
- * <p/>
- * The CssImport {@link #setVersionIndicator(java.lang.String) version indicator}
- * will automatically be set to the
- * {@link ClickUtils#getApplicationResourceVersionIndicator() application version indicator}.
- * <p/>
- * <b>Please note</b> if the given <tt>href</tt> begins with a
- * <tt class="wr">"/"</tt> character the href will be prefixed with the web
- * application <tt>context path</tt>.
- *
- * @param href the Css import href attribute
- */
- public CssImport(String href) {
- this(href, true);
- }
-
- /**
- * Construct a new Css import element with the specified <tt>href</tt>
- * attribute.
- * <p/>
- * If useApplicationVersionIndicator is true the
- * CssImport {@link #setVersionIndicator(java.lang.String) version indicator}
- * will automatically be set to the
- * {@link ClickUtils#getApplicationResourceVersionIndicator() application version indicator}.
- * <p/>
- * <b>Please note</b> if the given <tt>href</tt> begins with a
- * <tt class="wr">"/"</tt> character the href will be prefixed with the web
- * application <tt>context path</tt>.
- *
- * @param href the Css import href attribute
- * @param useApplicationVersionIndicator indicates whether the version
- * indicator will automatically be set to the application version indicator
- */
- public CssImport(String href, boolean useApplicationVersionIndicator) {
- this(href, null);
- if (useApplicationVersionIndicator) {
- setVersionIndicator(ClickUtils.getApplicationResourceVersionIndicator());
- }
- }
-
- /**
- * Construct a new Css import element with the specified <tt>href</tt>
- * attribute and version indicator.
- * <p/>
- * <b>Please note</b> if the given <tt>href</tt> begins with a
- * <tt class="wr">"/"</tt> character the href will be prefixed with the web
- * application <tt>context path</tt>.
- *
- * @param href the Css import href attribute
- * @param versionIndicator the version indicator to add to the href path
- */
- public CssImport(String href, String versionIndicator) {
- setHref(href);
- setAttribute("type", "text/css");
- setAttribute("rel", "stylesheet");
- setVersionIndicator(versionIndicator);
- }
-
- // ------------------------------------------------------ Public Properties
-
- /**
- * Returns the Css import HTML tag: <link>.
- *
- * @return the Css import HTML tag: <link>
- */
- public String getTag() {
- return "link";
- }
-
- /**
- * This method always return true because Css import must be unique based on
- * its <tt>href</tt> attribute. In other words the Page HEAD should only
- * contain a single CSS import for the specific <tt>href</tt>.
- *
- * @see ResourceElement#isUnique()
- *
- * @return true because Css import must unique based on its <tt>href</tt>
- * attribute
- */
- public boolean isUnique() {
- return true;
- }
-
- /**
- * Sets the <tt>href</tt> attribute.
- * <p/>
- * If the given <tt>href</tt> begins with a <tt class="wr">"/"</tt> character
- * the href will be prefixed with the web applications <tt>context path</tt>.
- * Note if the given href is already prefixed with the <tt>context path</tt>,
- * Click won't add it a second time.
- *
- * @param href the new href attribute
- */
- public void setHref(String href) {
- if (href != null) {
- if (href.charAt(0) == '/') {
- Context context = getContext();
- String contextPath = context.getRequest().getContextPath();
-
- // Guard against adding duplicate context path
- if (!href.startsWith(contextPath + '/')) {
- HtmlStringBuffer buffer =
- new HtmlStringBuffer(contextPath.length() + href.length());
-
- // Append the context path
- buffer.append(contextPath);
- buffer.append(href);
- href = buffer.toString();
- }
- }
- }
- setAttribute("href", href);
- }
-
- /**
- * Return the <tt>href</tt> attribute.
- *
- * @return the href attribute
- */
- public String getHref() {
- return getAttribute("href");
- }
-
- // --------------------------------------------------------- Public Methods
-
- /**
- * Render the HTML representation of the CssImport element to the specified
- * buffer.
- *
- * @param buffer the buffer to render output to
- */
- public void render(HtmlStringBuffer buffer) {
- // Add version indicator to the href
- setHref(addVersionIndicator(getHref()));
-
- renderConditionalCommentPrefix(buffer);
-
- buffer.elementStart(getTag());
-
- buffer.appendAttribute("id", getId());
- appendAttributes(buffer);
-
- buffer.elementEnd();
-
- renderConditionalCommentSuffix(buffer);
- }
-
- /**
- * @see java.lang.Object#equals(java.lang.Object)
- *
- * @param o the object with which to compare this instance with
- * @return true if the specified object is the same as this object
- */
- public boolean equals(Object o) {
- if (getHref() == null) {
- throw new IllegalStateException("'href' attribute is not defined.");
- }
-
- //1. Use the == operator to check if the argument is a reference to this object.
- if (o == this) {
- return true;
- }
-
- //2. Use the instanceof operator to check if the argument is of the correct type.
- if (!(o instanceof CssImport)) {
- return false;
- }
-
- //3. Cast the argument to the correct type.
- CssImport that = (CssImport) o;
-
- return getHref() == null ? that.getHref() == null
- : getHref().equals(that.getHref());
- }
-
- /**
- * @see java.lang.Object#hashCode()
- *
- * @return a hash code value for this object
- */
- public int hashCode() {
- return new HashCodeBuilder(17, 37).append(getHref()).toHashCode();
- }
-}
+/*
+ * 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.click.element;
+
+import org.apache.click.Context;
+import org.apache.click.util.ClickUtils;
+import org.apache.click.util.HtmlStringBuffer;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+
+/**
+ * Provides a Css HEAD element for importing <tt>external</tt> Cascading
+ * Stylesheet files using the <link> tag.
+ * <p/>
+ * Example usage:
+ * <pre class="prettyprint">
+ * public class MyPage extends Page {
+ *
+ * public List getHeadElements() {
+ * // We use lazy loading to ensure the CSS import is only added the
+ * // first time this method is called.
+ * if (headElements == null) {
+ * // Get the head elements from the super implementation
+ * headElements = super.getHeadElements();
+ *
+ * CssImport cssImport = new CssImport("/css/style.css");
+ * headElements.add(cssImport);
+ * }
+ * return headElements;
+ * }
+ * } </pre>
+ *
+ * The <tt>cssImport</tt> instance will be rendered as follows (assuming the
+ * context path is <tt>myApp</tt>):
+ * <pre class="prettyprint">
+ * <link type="text/css" rel="stylesheet" href="/myApp/css/style.css"/> </pre>
+ *
+ * @author Bob Schellink
+ */
+public class CssImport extends ResourceElement {
+
+ // ----------------------------------------------------------- Constructors
+
+ /**
+ * Constructs a new Css import element.
+ * <p/>
+ * The CssImport {@link #setVersionIndicator(java.lang.String) version indicator}
+ * will automatically be set to the
+ * {@link ClickUtils#getApplicationResourceVersionIndicator() application version indicator}.
+ */
+ public CssImport() {
+ this(null);
+ }
+
+ /**
+ * Construct a new Css import element with the specified <tt>href</tt>
+ * attribute.
+ * <p/>
+ * The CssImport {@link #setVersionIndicator(java.lang.String) version indicator}
+ * will automatically be set to the
+ * {@link ClickUtils#getApplicationResourceVersionIndicator() application version indicator}.
+ * <p/>
+ * <b>Please note</b> if the given <tt>href</tt> begins with a
+ * <tt class="wr">"/"</tt> character the href will be prefixed with the web
+ * application <tt>context path</tt>.
+ *
+ * @param href the Css import href attribute
+ */
+ public CssImport(String href) {
+ this(href, true);
+ }
+
+ /**
+ * Construct a new Css import element with the specified <tt>href</tt>
+ * attribute.
+ * <p/>
+ * If useApplicationVersionIndicator is true the
+ * CssImport {@link #setVersionIndicator(java.lang.String) version indicator}
+ * will automatically be set to the
+ * {@link ClickUtils#getApplicationResourceVersionIndicator() application version indicator}.
+ * <p/>
+ * <b>Please note</b> if the given <tt>href</tt> begins with a
+ * <tt class="wr">"/"</tt> character the href will be prefixed with the web
+ * application <tt>context path</tt>.
+ *
+ * @param href the Css import href attribute
+ * @param useApplicationVersionIndicator indicates whether the version
+ * indicator will automatically be set to the application version indicator
+ */
+ public CssImport(String href, boolean useApplicationVersionIndicator) {
+ this(href, null);
+ if (useApplicationVersionIndicator) {
+ setVersionIndicator(ClickUtils.getApplicationResourceVersionIndicator());
+ }
+ }
+
+ /**
+ * Construct a new Css import element with the specified <tt>href</tt>
+ * attribute and version indicator.
+ * <p/>
+ * <b>Please note</b> if the given <tt>href</tt> begins with a
+ * <tt class="wr">"/"</tt> character the href will be prefixed with the web
+ * application <tt>context path</tt>.
+ *
+ * @param href the Css import href attribute
+ * @param versionIndicator the version indicator to add to the href path
+ */
+ public CssImport(String href, String versionIndicator) {
+ setHref(href);
+ setAttribute("type", "text/css");
+ setAttribute("rel", "stylesheet");
+ setVersionIndicator(versionIndicator);
+ }
+
+ // ------------------------------------------------------ Public Properties
+
+ /**
+ * Returns the Css import HTML tag: <link>.
+ *
+ * @return the Css import HTML tag: <link>
+ */
+ public String getTag() {
+ return "link";
+ }
+
+ /**
+ * This method always return true because Css import must be unique based on
+ * its <tt>href</tt> attribute. In other words the Page HEAD should only
+ * contain a single CSS import for the specific <tt>href</tt>.
+ *
+ * @see ResourceElement#isUnique()
+ *
+ * @return true because Css import must unique based on its <tt>href</tt>
+ * attribute
+ */
+ public boolean isUnique() {
+ return true;
+ }
+
+ /**
+ * Sets the <tt>href</tt> attribute.
+ * <p/>
+ * If the given <tt>href</tt> begins with a <tt class="wr">"/"</tt> character
+ * the href will be prefixed with the web applications <tt>context path</tt>.
+ * Note if the given href is already prefixed with the <tt>context path</tt>,
+ * Click won't add it a second time.
+ *
+ * @param href the new href attribute
+ */
+ public void setHref(String href) {
+ if (href != null) {
+ if (href.charAt(0) == '/') {
+ Context context = getContext();
+ String contextPath = context.getRequest().getContextPath();
+
+ // Guard against adding duplicate context path
+ if (!href.startsWith(contextPath + '/')) {
+ HtmlStringBuffer buffer =
+ new HtmlStringBuffer(contextPath.length() + href.length());
+
+ // Append the context path
+ buffer.append(contextPath);
+ buffer.append(href);
+ href = buffer.toString();
+ }
+ }
+ }
+ setAttribute("href", href);
+ }
+
+ /**
+ * Return the <tt>href</tt> attribute.
+ *
+ * @return the href attribute
+ */
+ public String getHref() {
+ return getAttribute("href");
+ }
+
+ // --------------------------------------------------------- Public Methods
+
+ /**
+ * Render the HTML representation of the CssImport element to the specified
+ * buffer.
+ *
+ * @param buffer the buffer to render output to
+ */
+ public void render(HtmlStringBuffer buffer) {
+ // Add version indicator to the href
+ setHref(addVersionIndicator(getHref()));
+
+ renderConditionalCommentPrefix(buffer);
+
+ buffer.elementStart(getTag());
+
+ buffer.appendAttribute("id", getId());
+ appendAttributes(buffer);
+
+ buffer.elementEnd();
+
+ renderConditionalCommentSuffix(buffer);
+ }
+
+ /**
+ * @see java.lang.Object#equals(java.lang.Object)
+ *
+ * @param o the object with which to compare this instance with
+ * @return true if the specified object is the same as this object
+ */
+ public boolean equals(Object o) {
+ if (getHref() == null) {
+ throw new IllegalStateException("'href' attribute is not defined.");
+ }
+
+ //1. Use the == operator to check if the argument is a reference to this object.
+ if (o == this) {
+ return true;
+ }
+
+ //2. Use the instanceof operator to check if the argument is of the correct type.
+ if (!(o instanceof CssImport)) {
+ return false;
+ }
+
+ //3. Cast the argument to the correct type.
+ CssImport that = (CssImport) o;
+
+ return getHref() == null ? that.getHref() == null
+ : getHref().equals(that.getHref());
+ }
+
+ /**
+ * @see java.lang.Object#hashCode()
+ *
+ * @return a hash code value for this object
+ */
+ public int hashCode() {
+ return new HashCodeBuilder(17, 37).append(getHref()).toHashCode();
+ }
+
+}
Modified: incubator/click/trunk/click/framework/src/org/apache/click/element/CssStyle.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/element/CssStyle.java?rev=765116&r1=765115&r2=765116&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/element/CssStyle.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/element/CssStyle.java Wed Apr 15 10:22:08 2009
@@ -1,461 +1,461 @@
-/*
- * 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.click.element;
-
-import java.util.HashMap;
-import java.util.Map;
-import org.apache.click.Context;
-import org.apache.click.util.HtmlStringBuffer;
-import org.apache.commons.lang.builder.HashCodeBuilder;
-
-/**
- * Provides a Css HEAD element for including <tt>inline</tt> Cascading
- * Stylesheets using the <style> tag.
- * <p/>
- * Example usage:
- *
- * <pre class="prettyprint">
- * public class MyPage extends Page {
- *
- * public List getHeadElements() {
- * // We use lazy loading to ensure the CSS import is only added the
- * // first time this method is called.
- * if (headElements == null) {
- * // Get the header entries from the super implementation
- * headElements = super.getHeadElements();
- *
- * CssStyle cssStyle = new CssStyle("body { font: 12px arial; }");
- * headElements.add(cssStyle);
- * }
- * return headElements;
- * }
- * } </pre>
- *
- * The <tt>cssStyle</tt> instance will render as follows:
- *
- * <pre class="prettyprint">
- * <style type="text/css" rel="stylesheet">
- * body { font: 12px arial; }
- * </style> </pre>
- *
- * Below is an example showing how to render inline CSS from a Velocity
- * template.
- * <p/>
- * First we create a Velocity template <tt>(/css/style-template.css)</tt> which
- * contains the variable <tt>$context</tt> that must be replaced at runtime with
- * the application <tt>context path</tt>:
- *
- * <pre class="prettyprint">
- * .blue {
- * background: #00ff00 url('$context/css/blue.png') no-repeat fixed center;
- * } </pre>
- *
- * Next is the Page implementation:
- *
- * <pre class="prettyprint">
- * public class MyPage extends Page {
- *
- * public List getHeadElements() {
- * // We use lazy loading to ensure the CSS is only added the first time
- * // this method is called.
- * if (headElements == null) {
- * // Get the head elements from the super implementation
- * headElements = super.getHeadElements();
- *
- * Context context = getContext();
- *
- * // Create a default template model to pass to the template
- * Map model = ClickUtils.createTemplateModel(this, context);
- *
- * // Specify the path to CSS template
- * String templatePath = "/css/style-template.css";
- *
- * // Create the inline Css for the given template path and model
- * CssStyle cssStyle = new CssStyle(templatePath, model);
- * headElements.add(cssStyle);
- * }
- * return headElements;
- * }
- * } </pre>
- *
- * The <tt>Css</tt> above will render as follows (assuming the context path is
- * <tt>myApp</tt>):
- *
- * <pre class="prettyprint">
- * <style type="text/css" rel="stylesheet">
- * .blue {
- * background: #00ff00 url('/myApp/css/blue.png') no-repeat fixed center;
- * }
- * </style> </pre>
- *
- * <h3>Character data (CDATA) support</h3>
- *
- * Sometimes it is necessary to wrap <tt>inline</tt> {@link CssStyle Css} in
- * CDATA tags. Two use cases are common for doing this:
- * <ul>
- * <li>For XML parsing: When using Ajax one often send back partial
- * XML snippets to the browser, which is parsed as valid XML. However the XML
- * parser will throw an error if the content contains reserved XML characters
- * such as '&', '<' and '>'. For these situations it is recommended
- * to wrap the style content inside CDATA tags.
- * </li>
- * <li>XHTML validation: if you want to validate your site using an XHTML
- * validator e.g: <a target="_blank" href="http://validator.w3.org/">http://validator.w3.org/</a>.</li>
- * </ul>
- *
- * To wrap the CSS Style content in CDATA tags, set
- * {@link #setCharacterData(boolean)} to true. Below is shown how the Css
- * content would be rendered:
- *
- * <pre class="codeHtml">
- * <style type="text/css">
- * <span style="color:#3F7F5F">/∗<![CDATA[∗/</span>
- *
- * div > p {
- * border: 1px solid black;
- * }
- *
- * <span style="color:#3F7F5F">/∗]]>∗/</span>
- * </style> </pre>
- *
- * Notice the CDATA tags are commented out which ensures older browsers that
- * don't understand the CDATA tag, will ignore it and only process the actual
- * content.
- * <p/>
- * For an overview of XHTML validation and CDATA tags please see
- * <a target="_blank" href="http://javascript.about.com/library/blxhtml.htm">http://javascript.about.com/library/blxhtml.htm</a>.
- *
- * @author Bob Schellink
- */
-public class CssStyle extends ResourceElement {
-
- // -------------------------------------------------------------- Variables
-
- /** A buffer holding the inline Css content. */
- private HtmlStringBuffer content = new HtmlStringBuffer();
-
- /**
- * Indicates if the HeadElement's content should be wrapped in a CDATA tag.
- */
- private boolean characterData = false;
-
- /** The path of the template to render. */
- private String template;
-
- /** The model of the template to render. */
- private Map model;
-
- // ------------------------------------------------------------ Constructor
-
- /**
- * Construct a new Css style element.
- */
- public CssStyle() {
- this(null);
- }
-
- /**
- * Construct a new Css style element with the given content.
- *
- * @param content the Css content
- */
- public CssStyle(String content) {
- if (content != null) {
- this.content.append(content);
- }
- setAttribute("type", "text/css");
- setAttribute("rel", "stylesheet");
- }
-
- /**
- * Construct a new Css style element for the given template path
- * and template model.
- * <p/>
- * When the CssStyle is rendered the template and model will be merged and
- * the result will be rendered together with any CssStyle
- * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
- * <p/>
- *
- * For example:
- * <pre class="prettyprint">
- * public class MyPage extends Page {
- * public void onInit() {
- * Context context = getContext();
- *
- * // Create a default template model
- * Map model = ClickUtils.createTemplateModel(this, context);
- *
- * // Create CssStyle for the given template path and model
- * CssStyle style = new CssStyle("/mypage-template.css", model);
- *
- * // Add style to the Page Head elements
- * getHeadElements().add(style);
- * }
- * } </pre>
- *
- * @param template the path of the template to render
- * @param model the template model
- */
- public CssStyle(String template, Map model) {
- this(null);
- setTemplate(template);
- setModel(model);
- }
-
- // ------------------------------------------------------ Public properties
-
- /**
- * Returns the Css HTML tag: <style>.
- *
- * @return the Css HTML tag: <style>
- */
- public String getTag() {
- return "style";
- }
-
- /**
- * Return the Css content buffer.
- *
- * @return the Css content buffer
- */
- public HtmlStringBuffer getContent() {
- return content;
- }
-
- /**
- * Set the Css content buffer.
- *
- * @param content the new content buffer
- */
- public void setContent(HtmlStringBuffer content) {
- this.content = content;
- }
-
- /**
- * Return true if the CssStyle's content should be wrapped in CDATA tags,
- * false otherwise.
- *
- * @return true if the CssStyle's content should be wrapped in CDATA tags,
- * false otherwise
- */
- public boolean isCharacterData() {
- return characterData;
- }
-
- /**
- * Sets whether the CssStyle's content should be wrapped in CDATA tags or
- * not.
- *
- * @param characterData true indicates that the CssStyle's content should be
- * wrapped in CDATA tags, false otherwise
- */
- public void setCharacterData(boolean characterData) {
- this.characterData = characterData;
- }
-
- /**
- * Return the path of the template to render.
- *
- * @see #setTemplate(java.lang.String)
- *
- * @return the path of the template to render
- */
- public String getTemplate() {
- return template;
- }
-
- /**
- * Set the path of the template to render.
- * <p/>
- * If the {@link #template} property is set, the template and {@link #model}
- * will be merged and the result will be rendered together with any CssStyle
- * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
- *
- * @param template the path of the template to render
- */
- public void setTemplate(String template) {
- this.template = template;
- }
-
- /**
- * Return the model of the {@link #setTemplate(java.lang.String) template}
- * to render.
- *
- * @see #setModel(java.util.Map)
- *
- * @return the model of the template to render
- */
- public Map getModel() {
- return model;
- }
-
- /**
- * Set the model of the template to render.
- * <p/>
- * If the {@link #template} property is set, the template and {@link #model}
- * will be merged and the result will be rendered together with any CssStyle
- * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
- *
- * @param model the model of the template to render
- */
- public void setModel(Map model) {
- this.model = model;
- }
-
- // --------------------------------------------------------- Public Methods
-
- /**
- * Append the given Css string to the content buffer.
- *
- * @param content the CSS string to append to the content buffer
- * @return the Css content buffer
- */
- public HtmlStringBuffer append(String content) {
- return this.content.append(content);
- }
-
- /**
- * Render the HTML representation of the CssStyle element to the specified
- * buffer.
- *
- * @param buffer the buffer to render output to
- */
- public void render(HtmlStringBuffer buffer) {
-
- // Render IE conditional comment if conditional comment was set
- renderConditionalCommentPrefix(buffer);
-
- buffer.elementStart(getTag());
-
- buffer.appendAttribute("id", getId());
- appendAttributes(buffer);
-
- buffer.closeTag();
-
- // Render CDATA tag if necessary
- renderCharacterDataPrefix(buffer);
-
- renderContent(buffer);
-
- renderCharacterDataSuffix(buffer);
-
- buffer.elementEnd(getTag());
-
- renderConditionalCommentSuffix(buffer);
- }
-
- /**
- * @see java.lang.Object#equals(java.lang.Object)
- *
- * @param o the object with which to compare this instance with
- * @return true if the specified object is the same as this object
- */
- public boolean equals(Object o) {
- if (!isUnique()) {
- return super.equals(o);
- }
-
- //1. Use the == operator to check if the argument is a reference to this object.
- if (o == this) {
- return true;
- }
-
- //2. Use the instanceof operator to check if the argument is of the correct type.
- if (!(o instanceof CssStyle)) {
- return false;
- }
-
- //3. Cast the argument to the correct type.
- CssStyle that = (CssStyle) o;
-
- String id = getId();
- String thatId = that.getId();
- return id == null ? thatId == null : id.equals(thatId);
- }
-
- /**
- * @see java.lang.Object#hashCode()
- *
- * @return a hash code value for this object
- */
- public int hashCode() {
- if (!isUnique()) {
- return super.hashCode();
- }
- return new HashCodeBuilder(17, 37).append(getId()).toHashCode();
- }
-
- // ------------------------------------------------------ Protected Methods
-
- /**
- * Render the CssStyle {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}
- * to the specified buffer.
- * <p/>
- * <b>Please note:</b> if the {@link #setTemplate(java.lang.String) template}
- * property is set, this method will merge the {@link #setTemplate(java.lang.String) template}
- * and {@link #setModel(java.util.Map) model} and the result will be
- * rendered, together with the CssStyle
- * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content},
- * to the specified buffer.
- *
- * @param buffer the buffer to append the output to
- */
- protected void renderContent(HtmlStringBuffer buffer) {
- if (getTemplate() != null) {
- Context context = getContext();
-
- Map templateModel = getModel();
- if (templateModel == null) {
- templateModel = new HashMap();
- }
- buffer.append(context.renderTemplate(getTemplate(), templateModel));
-
- }
- buffer.append(getContent());
- }
-
- // ------------------------------------------------ Package Private Methods
-
- /**
- * Render the CDATA tag prefix to the specified buffer if
- * {@link #isCharacterData()} returns true. The default value is
- * <tt>/∗<![CDATA[∗/</tt>.
- *
- * @param buffer buffer to append the conditional comment prefix
- */
- void renderCharacterDataPrefix(HtmlStringBuffer buffer) {
- // Wrap character data in CDATA block
- if (isCharacterData()) {
- buffer.append("/*<![CDATA[*/ ");
- }
- }
-
- /**
- * Render the CDATA tag suffix to the specified buffer if
- * {@link #isCharacterData()} returns true. The default value is
- * <tt>/∗]]>∗/</tt>.
- *
- * @param buffer buffer to append the conditional comment prefix
- */
- void renderCharacterDataSuffix(HtmlStringBuffer buffer) {
- if (isCharacterData()) {
- buffer.append(" /*]]>*/");
- }
- }
-}
+/*
+ * 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.click.element;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.click.Context;
+import org.apache.click.util.HtmlStringBuffer;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+
+/**
+ * Provides a Css HEAD element for including <tt>inline</tt> Cascading
+ * Stylesheets using the <style> tag.
+ * <p/>
+ * Example usage:
+ *
+ * <pre class="prettyprint">
+ * public class MyPage extends Page {
+ *
+ * public List getHeadElements() {
+ * // We use lazy loading to ensure the CSS import is only added the
+ * // first time this method is called.
+ * if (headElements == null) {
+ * // Get the header entries from the super implementation
+ * headElements = super.getHeadElements();
+ *
+ * CssStyle cssStyle = new CssStyle("body { font: 12px arial; }");
+ * headElements.add(cssStyle);
+ * }
+ * return headElements;
+ * }
+ * } </pre>
+ *
+ * The <tt>cssStyle</tt> instance will render as follows:
+ *
+ * <pre class="prettyprint">
+ * <style type="text/css" rel="stylesheet">
+ * body { font: 12px arial; }
+ * </style> </pre>
+ *
+ * Below is an example showing how to render inline CSS from a Velocity
+ * template.
+ * <p/>
+ * First we create a Velocity template <tt>(/css/style-template.css)</tt> which
+ * contains the variable <tt>$context</tt> that must be replaced at runtime with
+ * the application <tt>context path</tt>:
+ *
+ * <pre class="prettyprint">
+ * .blue {
+ * background: #00ff00 url('$context/css/blue.png') no-repeat fixed center;
+ * } </pre>
+ *
+ * Next is the Page implementation:
+ *
+ * <pre class="prettyprint">
+ * public class MyPage extends Page {
+ *
+ * public List getHeadElements() {
+ * // We use lazy loading to ensure the CSS is only added the first time
+ * // this method is called.
+ * if (headElements == null) {
+ * // Get the head elements from the super implementation
+ * headElements = super.getHeadElements();
+ *
+ * Context context = getContext();
+ *
+ * // Create a default template model to pass to the template
+ * Map model = ClickUtils.createTemplateModel(this, context);
+ *
+ * // Specify the path to CSS template
+ * String templatePath = "/css/style-template.css";
+ *
+ * // Create the inline Css for the given template path and model
+ * CssStyle cssStyle = new CssStyle(templatePath, model);
+ * headElements.add(cssStyle);
+ * }
+ * return headElements;
+ * }
+ * } </pre>
+ *
+ * The <tt>Css</tt> above will render as follows (assuming the context path is
+ * <tt>myApp</tt>):
+ *
+ * <pre class="prettyprint">
+ * <style type="text/css" rel="stylesheet">
+ * .blue {
+ * background: #00ff00 url('/myApp/css/blue.png') no-repeat fixed center;
+ * }
+ * </style> </pre>
+ *
+ * <h3>Character data (CDATA) support</h3>
+ *
+ * Sometimes it is necessary to wrap <tt>inline</tt> {@link CssStyle Css} in
+ * CDATA tags. Two use cases are common for doing this:
+ * <ul>
+ * <li>For XML parsing: When using Ajax one often send back partial
+ * XML snippets to the browser, which is parsed as valid XML. However the XML
+ * parser will throw an error if the content contains reserved XML characters
+ * such as '&', '<' and '>'. For these situations it is recommended
+ * to wrap the style content inside CDATA tags.
+ * </li>
+ * <li>XHTML validation: if you want to validate your site using an XHTML
+ * validator e.g: <a target="_blank" href="http://validator.w3.org/">http://validator.w3.org/</a>.</li>
+ * </ul>
+ *
+ * To wrap the CSS Style content in CDATA tags, set
+ * {@link #setCharacterData(boolean)} to true. Below is shown how the Css
+ * content would be rendered:
+ *
+ * <pre class="codeHtml">
+ * <style type="text/css">
+ * <span style="color:#3F7F5F">/∗<![CDATA[∗/</span>
+ *
+ * div > p {
+ * border: 1px solid black;
+ * }
+ *
+ * <span style="color:#3F7F5F">/∗]]>∗/</span>
+ * </style> </pre>
+ *
+ * Notice the CDATA tags are commented out which ensures older browsers that
+ * don't understand the CDATA tag, will ignore it and only process the actual
+ * content.
+ * <p/>
+ * For an overview of XHTML validation and CDATA tags please see
+ * <a target="_blank" href="http://javascript.about.com/library/blxhtml.htm">http://javascript.about.com/library/blxhtml.htm</a>.
+ *
+ * @author Bob Schellink
+ */
+public class CssStyle extends ResourceElement {
+
+ // -------------------------------------------------------------- Variables
+
+ /** A buffer holding the inline Css content. */
+ private HtmlStringBuffer content = new HtmlStringBuffer();
+
+ /**
+ * Indicates if the HeadElement's content should be wrapped in a CDATA tag.
+ */
+ private boolean characterData = false;
+
+ /** The path of the template to render. */
+ private String template;
+
+ /** The model of the template to render. */
+ private Map model;
+
+ // ------------------------------------------------------------ Constructor
+
+ /**
+ * Construct a new Css style element.
+ */
+ public CssStyle() {
+ this(null);
+ }
+
+ /**
+ * Construct a new Css style element with the given content.
+ *
+ * @param content the Css content
+ */
+ public CssStyle(String content) {
+ if (content != null) {
+ this.content.append(content);
+ }
+ setAttribute("type", "text/css");
+ setAttribute("rel", "stylesheet");
+ }
+
+ /**
+ * Construct a new Css style element for the given template path
+ * and template model.
+ * <p/>
+ * When the CssStyle is rendered the template and model will be merged and
+ * the result will be rendered together with any CssStyle
+ * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
+ * <p/>
+ *
+ * For example:
+ * <pre class="prettyprint">
+ * public class MyPage extends Page {
+ * public void onInit() {
+ * Context context = getContext();
+ *
+ * // Create a default template model
+ * Map model = ClickUtils.createTemplateModel(this, context);
+ *
+ * // Create CssStyle for the given template path and model
+ * CssStyle style = new CssStyle("/mypage-template.css", model);
+ *
+ * // Add style to the Page Head elements
+ * getHeadElements().add(style);
+ * }
+ * } </pre>
+ *
+ * @param template the path of the template to render
+ * @param model the template model
+ */
+ public CssStyle(String template, Map model) {
+ this(null);
+ setTemplate(template);
+ setModel(model);
+ }
+
+ // ------------------------------------------------------ Public properties
+
+ /**
+ * Returns the Css HTML tag: <style>.
+ *
+ * @return the Css HTML tag: <style>
+ */
+ public String getTag() {
+ return "style";
+ }
+
+ /**
+ * Return the Css content buffer.
+ *
+ * @return the Css content buffer
+ */
+ public HtmlStringBuffer getContent() {
+ return content;
+ }
+
+ /**
+ * Set the Css content buffer.
+ *
+ * @param content the new content buffer
+ */
+ public void setContent(HtmlStringBuffer content) {
+ this.content = content;
+ }
+
+ /**
+ * Return true if the CssStyle's content should be wrapped in CDATA tags,
+ * false otherwise.
+ *
+ * @return true if the CssStyle's content should be wrapped in CDATA tags,
+ * false otherwise
+ */
+ public boolean isCharacterData() {
+ return characterData;
+ }
+
+ /**
+ * Sets whether the CssStyle's content should be wrapped in CDATA tags or
+ * not.
+ *
+ * @param characterData true indicates that the CssStyle's content should be
+ * wrapped in CDATA tags, false otherwise
+ */
+ public void setCharacterData(boolean characterData) {
+ this.characterData = characterData;
+ }
+
+ /**
+ * Return the path of the template to render.
+ *
+ * @see #setTemplate(java.lang.String)
+ *
+ * @return the path of the template to render
+ */
+ public String getTemplate() {
+ return template;
+ }
+
+ /**
+ * Set the path of the template to render.
+ * <p/>
+ * If the {@link #template} property is set, the template and {@link #model}
+ * will be merged and the result will be rendered together with any CssStyle
+ * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
+ *
+ * @param template the path of the template to render
+ */
+ public void setTemplate(String template) {
+ this.template = template;
+ }
+
+ /**
+ * Return the model of the {@link #setTemplate(java.lang.String) template}
+ * to render.
+ *
+ * @see #setModel(java.util.Map)
+ *
+ * @return the model of the template to render
+ */
+ public Map getModel() {
+ return model;
+ }
+
+ /**
+ * Set the model of the template to render.
+ * <p/>
+ * If the {@link #template} property is set, the template and {@link #model}
+ * will be merged and the result will be rendered together with any CssStyle
+ * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
+ *
+ * @param model the model of the template to render
+ */
+ public void setModel(Map model) {
+ this.model = model;
+ }
+
+ // --------------------------------------------------------- Public Methods
+
+ /**
+ * Append the given Css string to the content buffer.
+ *
+ * @param content the CSS string to append to the content buffer
+ * @return the Css content buffer
+ */
+ public HtmlStringBuffer append(String content) {
+ return this.content.append(content);
+ }
+
+ /**
+ * Render the HTML representation of the CssStyle element to the specified
+ * buffer.
+ *
+ * @param buffer the buffer to render output to
+ */
+ public void render(HtmlStringBuffer buffer) {
+
+ // Render IE conditional comment if conditional comment was set
+ renderConditionalCommentPrefix(buffer);
+
+ buffer.elementStart(getTag());
+
+ buffer.appendAttribute("id", getId());
+ appendAttributes(buffer);
+
+ buffer.closeTag();
+
+ // Render CDATA tag if necessary
+ renderCharacterDataPrefix(buffer);
+
+ renderContent(buffer);
+
+ renderCharacterDataSuffix(buffer);
+
+ buffer.elementEnd(getTag());
+
+ renderConditionalCommentSuffix(buffer);
+ }
+
+ /**
+ * @see java.lang.Object#equals(java.lang.Object)
+ *
+ * @param o the object with which to compare this instance with
+ * @return true if the specified object is the same as this object
+ */
+ public boolean equals(Object o) {
+ if (!isUnique()) {
+ return super.equals(o);
+ }
+
+ //1. Use the == operator to check if the argument is a reference to this object.
+ if (o == this) {
+ return true;
+ }
+
+ //2. Use the instanceof operator to check if the argument is of the correct type.
+ if (!(o instanceof CssStyle)) {
+ return false;
+ }
+
+ //3. Cast the argument to the correct type.
+ CssStyle that = (CssStyle) o;
+
+ String id = getId();
+ String thatId = that.getId();
+ return id == null ? thatId == null : id.equals(thatId);
+ }
+
+ /**
+ * @see java.lang.Object#hashCode()
+ *
+ * @return a hash code value for this object
+ */
+ public int hashCode() {
+ if (!isUnique()) {
+ return super.hashCode();
+ }
+ return new HashCodeBuilder(17, 37).append(getId()).toHashCode();
+ }
+
+ // ------------------------------------------------------ Protected Methods
+
+ /**
+ * Render the CssStyle {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}
+ * to the specified buffer.
+ * <p/>
+ * <b>Please note:</b> if the {@link #setTemplate(java.lang.String) template}
+ * property is set, this method will merge the {@link #setTemplate(java.lang.String) template}
+ * and {@link #setModel(java.util.Map) model} and the result will be
+ * rendered, together with the CssStyle
+ * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content},
+ * to the specified buffer.
+ *
+ * @param buffer the buffer to append the output to
+ */
+ protected void renderContent(HtmlStringBuffer buffer) {
+ if (getTemplate() != null) {
+ Context context = getContext();
+
+ Map templateModel = getModel();
+ if (templateModel == null) {
+ templateModel = new HashMap();
+ }
+ buffer.append(context.renderTemplate(getTemplate(), templateModel));
+
+ }
+ buffer.append(getContent());
+ }
+
+ // ------------------------------------------------ Package Private Methods
+
+ /**
+ * Render the CDATA tag prefix to the specified buffer if
+ * {@link #isCharacterData()} returns true. The default value is
+ * <tt>/∗<![CDATA[∗/</tt>.
+ *
+ * @param buffer buffer to append the conditional comment prefix
+ */
+ void renderCharacterDataPrefix(HtmlStringBuffer buffer) {
+ // Wrap character data in CDATA block
+ if (isCharacterData()) {
+ buffer.append("/*<![CDATA[*/ ");
+ }
+ }
+
+ /**
+ * Render the CDATA tag suffix to the specified buffer if
+ * {@link #isCharacterData()} returns true. The default value is
+ * <tt>/∗]]>∗/</tt>.
+ *
+ * @param buffer buffer to append the conditional comment prefix
+ */
+ void renderCharacterDataSuffix(HtmlStringBuffer buffer) {
+ if (isCharacterData()) {
+ buffer.append(" /*]]>*/");
+ }
+ }
+}
Modified: incubator/click/trunk/click/framework/src/org/apache/click/element/Element.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/element/Element.java?rev=765116&r1=765115&r2=765116&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/element/Element.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/element/Element.java Wed Apr 15 10:22:08 2009
@@ -1,261 +1,261 @@
-/*
- * 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.click.element;
-
-import java.util.HashMap;
-import java.util.Map;
-import org.apache.click.Context;
-import org.apache.click.util.HtmlStringBuffer;
-
-/**
- * Provides a base class for rendering HTML elements, for example
- * JavaScript (<script>) and Cascading Stylesheets
- * (<link> / <style>).
- * <p/>
- * Subclasses should override {@link #getTag()} to return a specific HTML tag.
- *
- * @author Bob Schellink
- */
-public class Element {
-
- // -------------------------------------------------------------- Variables
-
- /** The Element attributes Map. */
- private Map attributes;
-
- // ------------------------------------------------------ Public properties
-
- /**
- * Returns the Element HTML tag, the default value is <tt>null</tt>.
- * <p/>
- * Subclasses should override this method and return the correct tag.
- *
- * @return this Element HTML tag
- */
- public String getTag() {
- return null;
- }
-
- /**
- * Return the HTML attribute with the given name, or null if the
- * attribute does not exist.
- *
- * @param name the name of link HTML attribute
- * @return the link HTML attribute
- */
- public String getAttribute(String name) {
- if (hasAttributes()) {
- return (String) getAttributes().get(name);
- }
- return null;
- }
-
- /**
- * Set the Element attribute with the given attribute name and value.
- *
- * @param name the attribute name
- * @param value the attribute value
- * @throws IllegalArgumentException if name parameter is null
- */
- public void setAttribute(String name, String value) {
- if (name == null) {
- throw new IllegalArgumentException("Null name parameter");
- }
-
- if (value != null) {
- getAttributes().put(name, value);
- } else {
- getAttributes().remove(name);
- }
- }
-
- /**
- * Return the Element attributes Map.
- *
- * @return the Element attributes Map.
- */
- public Map getAttributes() {
- if (attributes == null) {
- attributes = new HashMap();
- }
- return attributes;
- }
-
- /**
- * Return true if the Element has attributes or false otherwise.
- *
- * @return true if the Element has attributes on false otherwise
- */
- public boolean hasAttributes() {
- if (attributes != null) {
- return !attributes.isEmpty();
- } else {
- return false;
- }
- }
-
- /**
- * Returns true if specified attribute is defined, false otherwise.
- *
- * @param name the specified attribute to check
- * @return true if name is a defined attribute
- */
- public boolean hasAttribute(String name) {
- return hasAttributes() && getAttributes().containsKey(name);
- }
-
- /**
- * Return the "id" attribute value or null if no id is defined.
- *
- * @return HTML element identifier attribute "id" value or null if no id
- * is defined
- */
- public String getId() {
- return getAttribute("id");
- }
-
- /**
- * Set the HTML id attribute for the with the given value.
- *
- * @param id the element HTML id attribute value to set
- */
- public void setId(String id) {
- if (id != null) {
- setAttribute("id", id);
- } else {
- getAttributes().remove("id");
- }
- }
-
- // --------------------------------------------------------- Public methods
-
- /**
- * Return the thread local Context.
- *
- * @return the thread local Context
- */
- public Context getContext() {
- return Context.getThreadLocalContext();
- }
-
- /**
- * Render the HTML representation of the Element to the specified buffer.
- * <p/>
- * If {@link #getTag()} returns null, this method will return an empty
- * string.
- *
- * @param buffer the specified buffer to render the Element output to
- */
- public void render(HtmlStringBuffer buffer) {
- if (getTag() == null) {
- return;
- }
- renderTagBegin(getTag(), buffer);
- renderTagEnd(getTag(), buffer);
-
- }
-
- /**
- * Return the HTML string representation of the Element.
- *
- * @return the HTML string representation of the Element
- */
- public String toString() {
- if (getTag() == null) {
- return "";
- }
- HtmlStringBuffer buffer = new HtmlStringBuffer(getElementSizeEst());
- render(buffer);
- return buffer.toString();
- }
-
- // ------------------------------------------------------ Protected Methods
-
- /**
- * Append all the Element attributes to the specified buffer.
- *
- * @param buffer the specified buffer to append all the attributes
- */
- protected void appendAttributes(HtmlStringBuffer buffer) {
- if (hasAttributes()) {
- buffer.appendAttributes(attributes);
- }
- }
-
- // ------------------------------------------------ Package Private Methods
-
- /**
- * Render the specified {@link #getTag() tag} and {@link #getAttributes()}.
- * <p/>
- * <b>Please note:</b> the tag will not be closed by this method. This
- * enables callers of this method to append extra attributes as needed.
- * <p/>
- * For example the result of calling:
- * <pre class="prettyprint">
- * Field field = new TextField("mytext");
- * HtmlStringBuffer buffer = new HtmlStringBuffer();
- * field.renderTagBegin("div", buffer);
- * </pre>
- * will be:
- * <pre class="prettyprint">
- * <div name="mytext" id="mytext"
- * </pre>
- * Note that the tag is not closed.
- *
- * @param tagName the name of the tag to render
- * @param buffer the buffer to append the output to
- */
- void renderTagBegin(String tagName, HtmlStringBuffer buffer) {
- if (tagName == null) {
- throw new IllegalStateException("Tag cannot be null");
- }
-
- buffer.elementStart(tagName);
-
- buffer.appendAttribute("id", getId());
- appendAttributes(buffer);
- }
-
- /**
- * Closes the specified {@link #getTag() tag}.
- *
- * @param tagName the name of the tag to close
- * @param buffer the buffer to append the output to
- */
- void renderTagEnd(String tagName, HtmlStringBuffer buffer) {
- buffer.elementEnd();
- }
-
- /**
- * Return the estimated rendered element size in characters.
- *
- * @return the estimated rendered element size in characters
- */
- int getElementSizeEst() {
- int size = 0;
- if (getTag() != null && hasAttributes()) {
- //length of the markup -> </> == 3
- //1 * tag.length()
- size += 3 + getTag().length();
- //using 20 as an estimate
- size += 20 * getAttributes().size();
- }
- return size;
- }
-}
+/*
+ * 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.click.element;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.click.Context;
+import org.apache.click.util.HtmlStringBuffer;
+
+/**
+ * Provides a base class for rendering HTML elements, for example
+ * JavaScript (<script>) and Cascading Stylesheets
+ * (<link> / <style>).
+ * <p/>
+ * Subclasses should override {@link #getTag()} to return a specific HTML tag.
+ *
+ * @author Bob Schellink
+ */
+public class Element {
+
+ // -------------------------------------------------------------- Variables
+
+ /** The Element attributes Map. */
+ private Map attributes;
+
+ // ------------------------------------------------------ Public properties
+
+ /**
+ * Returns the Element HTML tag, the default value is <tt>null</tt>.
+ * <p/>
+ * Subclasses should override this method and return the correct tag.
+ *
+ * @return this Element HTML tag
+ */
+ public String getTag() {
+ return null;
+ }
+
+ /**
+ * Return the HTML attribute with the given name, or null if the
+ * attribute does not exist.
+ *
+ * @param name the name of link HTML attribute
+ * @return the link HTML attribute
+ */
+ public String getAttribute(String name) {
+ if (hasAttributes()) {
+ return (String) getAttributes().get(name);
+ }
+ return null;
+ }
+
+ /**
+ * Set the Element attribute with the given attribute name and value.
+ *
+ * @param name the attribute name
+ * @param value the attribute value
+ * @throws IllegalArgumentException if name parameter is null
+ */
+ public void setAttribute(String name, String value) {
+ if (name == null) {
+ throw new IllegalArgumentException("Null name parameter");
+ }
+
+ if (value != null) {
+ getAttributes().put(name, value);
+ } else {
+ getAttributes().remove(name);
+ }
+ }
+
+ /**
+ * Return the Element attributes Map.
+ *
+ * @return the Element attributes Map.
+ */
+ public Map getAttributes() {
+ if (attributes == null) {
+ attributes = new HashMap();
+ }
+ return attributes;
+ }
+
+ /**
+ * Return true if the Element has attributes or false otherwise.
+ *
+ * @return true if the Element has attributes on false otherwise
+ */
+ public boolean hasAttributes() {
+ if (attributes != null) {
+ return !attributes.isEmpty();
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Returns true if specified attribute is defined, false otherwise.
+ *
+ * @param name the specified attribute to check
+ * @return true if name is a defined attribute
+ */
+ public boolean hasAttribute(String name) {
+ return hasAttributes() && getAttributes().containsKey(name);
+ }
+
+ /**
+ * Return the "id" attribute value or null if no id is defined.
+ *
+ * @return HTML element identifier attribute "id" value or null if no id
+ * is defined
+ */
+ public String getId() {
+ return getAttribute("id");
+ }
+
+ /**
+ * Set the HTML id attribute for the with the given value.
+ *
+ * @param id the element HTML id attribute value to set
+ */
+ public void setId(String id) {
+ if (id != null) {
+ setAttribute("id", id);
+ } else {
+ getAttributes().remove("id");
+ }
+ }
+
+ // --------------------------------------------------------- Public methods
+
+ /**
+ * Return the thread local Context.
+ *
+ * @return the thread local Context
+ */
+ public Context getContext() {
+ return Context.getThreadLocalContext();
+ }
+
+ /**
+ * Render the HTML representation of the Element to the specified buffer.
+ * <p/>
+ * If {@link #getTag()} returns null, this method will return an empty
+ * string.
+ *
+ * @param buffer the specified buffer to render the Element output to
+ */
+ public void render(HtmlStringBuffer buffer) {
+ if (getTag() == null) {
+ return;
+ }
+ renderTagBegin(getTag(), buffer);
+ renderTagEnd(getTag(), buffer);
+
+ }
+
+ /**
+ * Return the HTML string representation of the Element.
+ *
+ * @return the HTML string representation of the Element
+ */
+ public String toString() {
+ if (getTag() == null) {
+ return "";
+ }
+ HtmlStringBuffer buffer = new HtmlStringBuffer(getElementSizeEst());
+ render(buffer);
+ return buffer.toString();
+ }
+
+ // ------------------------------------------------------ Protected Methods
+
+ /**
+ * Append all the Element attributes to the specified buffer.
+ *
+ * @param buffer the specified buffer to append all the attributes
+ */
+ protected void appendAttributes(HtmlStringBuffer buffer) {
+ if (hasAttributes()) {
+ buffer.appendAttributes(attributes);
+ }
+ }
+
+ // ------------------------------------------------ Package Private Methods
+
+ /**
+ * Render the specified {@link #getTag() tag} and {@link #getAttributes()}.
+ * <p/>
+ * <b>Please note:</b> the tag will not be closed by this method. This
+ * enables callers of this method to append extra attributes as needed.
+ * <p/>
+ * For example the result of calling:
+ * <pre class="prettyprint">
+ * Field field = new TextField("mytext");
+ * HtmlStringBuffer buffer = new HtmlStringBuffer();
+ * field.renderTagBegin("div", buffer);
+ * </pre>
+ * will be:
+ * <pre class="prettyprint">
+ * <div name="mytext" id="mytext"
+ * </pre>
+ * Note that the tag is not closed.
+ *
+ * @param tagName the name of the tag to render
+ * @param buffer the buffer to append the output to
+ */
+ void renderTagBegin(String tagName, HtmlStringBuffer buffer) {
+ if (tagName == null) {
+ throw new IllegalStateException("Tag cannot be null");
+ }
+
+ buffer.elementStart(tagName);
+
+ buffer.appendAttribute("id", getId());
+ appendAttributes(buffer);
+ }
+
+ /**
+ * Closes the specified {@link #getTag() tag}.
+ *
+ * @param tagName the name of the tag to close
+ * @param buffer the buffer to append the output to
+ */
+ void renderTagEnd(String tagName, HtmlStringBuffer buffer) {
+ buffer.elementEnd();
+ }
+
+ /**
+ * Return the estimated rendered element size in characters.
+ *
+ * @return the estimated rendered element size in characters
+ */
+ int getElementSizeEst() {
+ int size = 0;
+ if (getTag() != null && hasAttributes()) {
+ //length of the markup -> </> == 3
+ //1 * tag.length()
+ size += 3 + getTag().length();
+ //using 20 as an estimate
+ size += 20 * getAttributes().size();
+ }
+ return size;
+ }
+}