Modified: incubator/click/trunk/click/framework/src/org/apache/click/element/JsImport.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/element/JsImport.java?rev=765116&r1=765115&r2=765116&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/element/JsImport.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/element/JsImport.java Wed Apr 15 10:22:08 2009
@@ -1,252 +1,252 @@
-/*
- * 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 JavaScript HEAD element for importing <tt>external</tt> JavaScript
- * files using the <script> tag.
- * <p/>
- * Example usage:
- * <pre class="prettyprint">
- * public class MyPage extends Page {
- *
- * public List getHeadElements() {
- * // We use lazy loading to ensure the JS 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();
- *
- * JsImport jsImport = new JsImport("/js/js-library.js");
- * headElements.add(jsImport);
- * }
- * return headElements;
- * }
- * } </pre>
- *
- * The <tt>jsImport</tt> instance will be rendered as follows (assuming the context
- * path is <tt>myApp</tt>):
- * <pre class="prettyprint">
- * <script type="text/javascript" href="/myApp/js/js-library.js"></script> </pre>
- *
- * @author Bob Schellink
- */
-public class JsImport extends ResourceElement {
-
- // ----------------------------------------------------------- Constructors
-
- /**
- * Constructs a new JavaScript import element.
- * <p/>
- * The JsImport {@link #setVersionIndicator(java.lang.String) version indicator}
- * will automatically be set to the
- * {@link ClickUtils#getApplicationResourceVersionIndicator() application version indicator}.
- */
- public JsImport() {
- this(null);
- }
-
- /**
- * Construct a new JavaScript import element with the specified
- * <tt>src</tt> attribute.
- * <p/>
- * The JsImport {@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>src</tt> begins with a
- * <tt class="wr">"/"</tt> character the src will be prefixed with the web
- * application <tt>context path</tt>.
- *
- * @param src the JavaScript import src attribute
- */
- public JsImport(String src) {
- this(src, true);
- }
-
- /**
- * Construct a new JavaScript import element with the specified <tt>src</tt>
- * attribute.
- * <p/>
- * If useApplicationVersionIndicator is true the
- * {@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>src</tt> begins with a
- * <tt class="wr">"/"</tt> character the src will be prefixed with the web
- * application <tt>context path</tt>.
- *
- * @param src the JavaScript import src attribute
- * @param useApplicationVersionIndicator indicates whether the version
- * indicator will automatically be set to the application version indicator
- */
- public JsImport(String src, boolean useApplicationVersionIndicator) {
- this(src, null);
- if (useApplicationVersionIndicator) {
- setVersionIndicator(ClickUtils.getApplicationResourceVersionIndicator());
- }
- }
-
- /**
- * Construct a new JavaScript import element with the specified <tt>src</tt>
- * attribute and version indicator.
- * <p/>
- * <b>Please note</b> if the given <tt>src</tt> begins with a
- * <tt class="wr">"/"</tt> character the src will be prefixed with the web
- * application <tt>context path</tt>.
- *
- * @param src the JsImport src attribute
- * @param versionIndicator the version indicator to add to the src path
- */
- public JsImport(String src, String versionIndicator) {
- setSrc(src);
- setAttribute("type", "text/javascript");
- setVersionIndicator(versionIndicator);
- }
-
- // ------------------------------------------------------ Public properties
-
- /**
- * Returns the JavaScript import HTML tag: <script>.
- *
- * @return the JavaScript import HTML tag: <script>
- */
- public String getTag() {
- return "script";
- }
-
- /**
- * This method always return true because a JavaScript import must be unique
- * based on its <tt>src</tt> attribute. In other words the Page HEAD should
- * only contain a single JavaScript import for the specific <tt>src</tt>.
- *
- * @see ResourceElement#isUnique()
- *
- * @return true because JavaScript import must unique based on its
- * <tt>src</tt> attribute
- */
- public boolean isUnique() {
- return true;
- }
-
- /**
- * Sets the <tt>src</tt> attribute.
- * <p/>
- * If the given <tt>src</tt> begins with a <tt class="wr">"/"</tt> character
- * the sr will be prefixed with the web application <tt>context path</tt>.
- * Note if the given src is already prefixed with the <tt>context path</tt>,
- * Click won't add it a second time.
- *
- * @param src the new src attribute
- */
- public void setSrc(String src) {
- if (src != null) {
- if (src.charAt(0) == '/') {
- Context context = getContext();
- String contextPath = context.getRequest().getContextPath();
-
- // Guard against adding duplicate context path
- if (!src.startsWith(contextPath + '/')) {
- HtmlStringBuffer buffer =
- new HtmlStringBuffer(contextPath.length() + src.length());
-
- // Append the context path
- buffer.append(contextPath);
- buffer.append(src);
- src = buffer.toString();
- }
- }
- }
- setAttribute("src", src);
- }
-
- /**
- * Return the <tt>src</tt> attribute.
- *
- * @return the src attribute
- */
- public String getSrc() {
- return getAttribute("src");
- }
-
- // ------------------------------------------------ Package Private Methods
-
- /**
- * Render the HTML representation of the JsImport element to the specified
- * buffer.
- *
- * @param buffer the buffer to render output to
- */
- public void render(HtmlStringBuffer buffer) {
- // Add version indicator to the href
- setSrc(addVersionIndicator(getSrc()));
-
- renderConditionalCommentPrefix(buffer);
-
- buffer.elementStart(getTag());
-
- buffer.appendAttribute("id", getId());
- appendAttributes(buffer);
-
- buffer.closeTag();
-
- 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) {
- //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 JsImport)) {
- return false;
- }
-
- //3. Cast the argument to the correct type.
- JsImport that = (JsImport) o;
-
- return getSrc() == null ? that.getSrc() == null
- : getSrc().equals(that.getSrc());
- }
-
- /**
- * @see java.lang.Object#hashCode()
- *
- * @return a hash code value for this object
- */
- public int hashCode() {
- return new HashCodeBuilder(17, 37).append(getSrc()).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 JavaScript HEAD element for importing <tt>external</tt> JavaScript
+ * files using the <script> tag.
+ * <p/>
+ * Example usage:
+ * <pre class="prettyprint">
+ * public class MyPage extends Page {
+ *
+ * public List getHeadElements() {
+ * // We use lazy loading to ensure the JS 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();
+ *
+ * JsImport jsImport = new JsImport("/js/js-library.js");
+ * headElements.add(jsImport);
+ * }
+ * return headElements;
+ * }
+ * } </pre>
+ *
+ * The <tt>jsImport</tt> instance will be rendered as follows (assuming the context
+ * path is <tt>myApp</tt>):
+ * <pre class="prettyprint">
+ * <script type="text/javascript" href="/myApp/js/js-library.js"></script> </pre>
+ *
+ * @author Bob Schellink
+ */
+public class JsImport extends ResourceElement {
+
+ // ----------------------------------------------------------- Constructors
+
+ /**
+ * Constructs a new JavaScript import element.
+ * <p/>
+ * The JsImport {@link #setVersionIndicator(java.lang.String) version indicator}
+ * will automatically be set to the
+ * {@link ClickUtils#getApplicationResourceVersionIndicator() application version indicator}.
+ */
+ public JsImport() {
+ this(null);
+ }
+
+ /**
+ * Construct a new JavaScript import element with the specified
+ * <tt>src</tt> attribute.
+ * <p/>
+ * The JsImport {@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>src</tt> begins with a
+ * <tt class="wr">"/"</tt> character the src will be prefixed with the web
+ * application <tt>context path</tt>.
+ *
+ * @param src the JavaScript import src attribute
+ */
+ public JsImport(String src) {
+ this(src, true);
+ }
+
+ /**
+ * Construct a new JavaScript import element with the specified <tt>src</tt>
+ * attribute.
+ * <p/>
+ * If useApplicationVersionIndicator is true the
+ * {@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>src</tt> begins with a
+ * <tt class="wr">"/"</tt> character the src will be prefixed with the web
+ * application <tt>context path</tt>.
+ *
+ * @param src the JavaScript import src attribute
+ * @param useApplicationVersionIndicator indicates whether the version
+ * indicator will automatically be set to the application version indicator
+ */
+ public JsImport(String src, boolean useApplicationVersionIndicator) {
+ this(src, null);
+ if (useApplicationVersionIndicator) {
+ setVersionIndicator(ClickUtils.getApplicationResourceVersionIndicator());
+ }
+ }
+
+ /**
+ * Construct a new JavaScript import element with the specified <tt>src</tt>
+ * attribute and version indicator.
+ * <p/>
+ * <b>Please note</b> if the given <tt>src</tt> begins with a
+ * <tt class="wr">"/"</tt> character the src will be prefixed with the web
+ * application <tt>context path</tt>.
+ *
+ * @param src the JsImport src attribute
+ * @param versionIndicator the version indicator to add to the src path
+ */
+ public JsImport(String src, String versionIndicator) {
+ setSrc(src);
+ setAttribute("type", "text/javascript");
+ setVersionIndicator(versionIndicator);
+ }
+
+ // ------------------------------------------------------ Public properties
+
+ /**
+ * Returns the JavaScript import HTML tag: <script>.
+ *
+ * @return the JavaScript import HTML tag: <script>
+ */
+ public String getTag() {
+ return "script";
+ }
+
+ /**
+ * This method always return true because a JavaScript import must be unique
+ * based on its <tt>src</tt> attribute. In other words the Page HEAD should
+ * only contain a single JavaScript import for the specific <tt>src</tt>.
+ *
+ * @see ResourceElement#isUnique()
+ *
+ * @return true because JavaScript import must unique based on its
+ * <tt>src</tt> attribute
+ */
+ public boolean isUnique() {
+ return true;
+ }
+
+ /**
+ * Sets the <tt>src</tt> attribute.
+ * <p/>
+ * If the given <tt>src</tt> begins with a <tt class="wr">"/"</tt> character
+ * the sr will be prefixed with the web application <tt>context path</tt>.
+ * Note if the given src is already prefixed with the <tt>context path</tt>,
+ * Click won't add it a second time.
+ *
+ * @param src the new src attribute
+ */
+ public void setSrc(String src) {
+ if (src != null) {
+ if (src.charAt(0) == '/') {
+ Context context = getContext();
+ String contextPath = context.getRequest().getContextPath();
+
+ // Guard against adding duplicate context path
+ if (!src.startsWith(contextPath + '/')) {
+ HtmlStringBuffer buffer =
+ new HtmlStringBuffer(contextPath.length() + src.length());
+
+ // Append the context path
+ buffer.append(contextPath);
+ buffer.append(src);
+ src = buffer.toString();
+ }
+ }
+ }
+ setAttribute("src", src);
+ }
+
+ /**
+ * Return the <tt>src</tt> attribute.
+ *
+ * @return the src attribute
+ */
+ public String getSrc() {
+ return getAttribute("src");
+ }
+
+ // ------------------------------------------------ Package Private Methods
+
+ /**
+ * Render the HTML representation of the JsImport element to the specified
+ * buffer.
+ *
+ * @param buffer the buffer to render output to
+ */
+ public void render(HtmlStringBuffer buffer) {
+ // Add version indicator to the href
+ setSrc(addVersionIndicator(getSrc()));
+
+ renderConditionalCommentPrefix(buffer);
+
+ buffer.elementStart(getTag());
+
+ buffer.appendAttribute("id", getId());
+ appendAttributes(buffer);
+
+ buffer.closeTag();
+
+ 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) {
+ //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 JsImport)) {
+ return false;
+ }
+
+ //3. Cast the argument to the correct type.
+ JsImport that = (JsImport) o;
+
+ return getSrc() == null ? that.getSrc() == null
+ : getSrc().equals(that.getSrc());
+ }
+
+ /**
+ * @see java.lang.Object#hashCode()
+ *
+ * @return a hash code value for this object
+ */
+ public int hashCode() {
+ return new HashCodeBuilder(17, 37).append(getSrc()).toHashCode();
+ }
+}
Modified: incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java?rev=765116&r1=765115&r2=765116&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.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 HEAD element for including <tt>inline</tt> JavaScript using the
- * <script> tag.
- * <p/>
- * Example usage:
- *
- * <pre class="prettyprint">
- * public class MyPage extends Page {
- *
- * public List getHeadElements() {
- * // We use lazy loading to ensure the JS is only added the
- * // first time this method is called.
- * if (headElements == null) {
- * // Get the head elements from the super implementation
- * headElements = super.getHeadElements();
- *
- * JsScript jsScript = new JsScript("alert('Hello World!);");
- * headElements.add(jsScript);
- * }
- * return headElements;
- * }
- * } </pre>
- *
- * The <tt>jsScript</tt> instance will be rendered as follows:
- *
- * <pre class="prettyprint">
- * <script type="text/javascript">
- * alert('Hello World');
- * </script> </pre>
- *
- * Below is an example showing how to render inline Javascript from a
- * Velocity template.
- * <p/>
- * First we create a Velocity template <tt>(/js/mycorp-template.js)</tt> which
- * contains the variable <tt>$divId</tt> that must be replaced at runtime with
- * the real Div ID attribute:
- *
- * <pre class="prettyprint">
- * hide = function() {
- * var div = document.getElementById('$divId');
- * div.style.display = "none";
- * } </pre>
- *
- * Next is the Page implementation:
- *
- * <pre class="prettyprint">
- * public class MyPage extends Page {
- *
- * public List getHeadElements() {
- * // We use lazy loading to ensure the JS is only added the
- * // first time this method is called.
- * if (headElements == null) {
- * // Get the head elements from the super implementation
- * headElements = super.getHeadElements();
- *
- * // Create a default template model to pass to the template
- * Map model = ClickUtils.createTemplateModel(this, getContext());
- *
- * // Add the id of the div to hide
- * model.put("divId", "myDiv");
- *
- * // Specify the path to the JavaScript template
- * String templatePath = "/js/mycorp-template.js";
- *
- * // Create the inline JavaScript for the given template path and model
- * JsScript jsScript = new JsScript(templatePath, model);
- * headElements.add(jsScript);
- * }
- * return headElements;
- * }
- * } </pre>
- *
- * The <tt>jsScript</tt> instance will render as follows (assuming the context
- * path is <tt>myApp</tt>):
- *
- * <pre class="prettyprint">
- * <script type="text/javascript">
- * hide = function() {
- * var div = document.getElementById('myDiv');
- * div.style.display = "none";
- * }
- * </style> </pre>
- *
- * <h3>Character data (CDATA) support</h3>
- *
- * Sometimes it is necessary to wrap <tt>inline</tt> {@link JsScript JavaScript}
- * 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 script 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 JavaScript content in CDATA tags, set
- * {@link #setCharacterData(boolean)} to true. Below is shown how the JavaScript
- * content would be rendered:
- *
- * <pre class="codeHtml">
- * <script type="text/javascript">
- * <span style="color:#3F7F5F">/∗<![CDATA[∗/</span>
- *
- * if(x < y) alert('Hello');
- *
- * <span style="color:#3F7F5F">/∗]]>∗/</span>
- * </script> </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 JsScript extends ResourceElement {
-
- // -------------------------------------------------------------- Variables
-
- /** A buffer holding the inline JavaScript content. */
- private HtmlStringBuffer content = new HtmlStringBuffer();
-
- /**
- * Indicates if the JsScript'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;
-
- // ----------------------------------------------------------- Constructors
-
- /**
- * Construct a new inline JavaScript element.
- */
- public JsScript() {
- this(null);
- }
-
- /**
- * Construct a new inline JavaScript element with the given content.
- *
- * @param content the JavaScript content
- */
- public JsScript(String content) {
- if (content != null) {
- this.content.append(content);
- }
- setAttribute("type", "text/javascript");
- }
-
- /**
- * Construct a new inline JavaScript element for the given template path
- * and template model.
- * <p/>
- * When the JsScript is rendered the template and model will be merged and
- * the result will be rendered together with any JsScript
- * {@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 JsScript for the given template path and model
- * JsScript script = new JsScript("/mypage-template.js", model);
- *
- * // Add script to the Page Head elements
- * getHeadElements().add(script);
- * }
- * } </pre>
- *
- * @param template the path of the template to render
- * @param model the template model
- */
- public JsScript(String template, Map model) {
- this(null);
- setTemplate(template);
- setModel(model);
- }
-
- // ------------------------------------------------------ Public Properties
-
- /**
- * Returns the JavaScript HTML tag: <script>.
- *
- * @return the JavaScript HTML tag: <script>
- */
- public String getTag() {
- return "script";
- }
-
- /**
- * Return the JavaScript content buffer.
- *
- * @return the JavaScript content buffer
- */
- public HtmlStringBuffer getContent() {
- return content;
- }
-
- /**
- * Set the JavaScript content buffer.
- *
- * @param content the new content buffer
- */
- public void setContent(HtmlStringBuffer content) {
- this.content = content;
- }
-
- /**
- * Return true if the JsScript's content should be wrapped in CDATA tags,
- * false otherwise.
- *
- * @return true if the JsScript's content should be wrapped in CDATA tags,
- * false otherwise
- */
- public boolean isCharacterData() {
- return characterData;
- }
-
- /**
- * Sets whether the JsScript's content should be wrapped in CDATA tags or not.
- *
- * @param characterData true indicates that the JsScript'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 JsScript
- * {@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 JsScript
- * {@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 JavaScript string to the content buffer.
- *
- * @param content the JavaScript string to append to the content
- * buffer
- * @return the JavaScript content buffer
- */
- public HtmlStringBuffer append(String content) {
- return this.content.append(content);
- }
-
- /**
- * Render the HTML representation of the JsScript 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 JsScript)) {
- return false;
- }
-
- //3. Cast the argument to the correct type.
- JsScript that = (JsScript) 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 JsScript {@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 JsScript
- * {@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 HEAD element for including <tt>inline</tt> JavaScript using the
+ * <script> tag.
+ * <p/>
+ * Example usage:
+ *
+ * <pre class="prettyprint">
+ * public class MyPage extends Page {
+ *
+ * public List getHeadElements() {
+ * // We use lazy loading to ensure the JS is only added the
+ * // first time this method is called.
+ * if (headElements == null) {
+ * // Get the head elements from the super implementation
+ * headElements = super.getHeadElements();
+ *
+ * JsScript jsScript = new JsScript("alert('Hello World!);");
+ * headElements.add(jsScript);
+ * }
+ * return headElements;
+ * }
+ * } </pre>
+ *
+ * The <tt>jsScript</tt> instance will be rendered as follows:
+ *
+ * <pre class="prettyprint">
+ * <script type="text/javascript">
+ * alert('Hello World');
+ * </script> </pre>
+ *
+ * Below is an example showing how to render inline Javascript from a
+ * Velocity template.
+ * <p/>
+ * First we create a Velocity template <tt>(/js/mycorp-template.js)</tt> which
+ * contains the variable <tt>$divId</tt> that must be replaced at runtime with
+ * the real Div ID attribute:
+ *
+ * <pre class="prettyprint">
+ * hide = function() {
+ * var div = document.getElementById('$divId');
+ * div.style.display = "none";
+ * } </pre>
+ *
+ * Next is the Page implementation:
+ *
+ * <pre class="prettyprint">
+ * public class MyPage extends Page {
+ *
+ * public List getHeadElements() {
+ * // We use lazy loading to ensure the JS is only added the
+ * // first time this method is called.
+ * if (headElements == null) {
+ * // Get the head elements from the super implementation
+ * headElements = super.getHeadElements();
+ *
+ * // Create a default template model to pass to the template
+ * Map model = ClickUtils.createTemplateModel(this, getContext());
+ *
+ * // Add the id of the div to hide
+ * model.put("divId", "myDiv");
+ *
+ * // Specify the path to the JavaScript template
+ * String templatePath = "/js/mycorp-template.js";
+ *
+ * // Create the inline JavaScript for the given template path and model
+ * JsScript jsScript = new JsScript(templatePath, model);
+ * headElements.add(jsScript);
+ * }
+ * return headElements;
+ * }
+ * } </pre>
+ *
+ * The <tt>jsScript</tt> instance will render as follows (assuming the context
+ * path is <tt>myApp</tt>):
+ *
+ * <pre class="prettyprint">
+ * <script type="text/javascript">
+ * hide = function() {
+ * var div = document.getElementById('myDiv');
+ * div.style.display = "none";
+ * }
+ * </style> </pre>
+ *
+ * <h3>Character data (CDATA) support</h3>
+ *
+ * Sometimes it is necessary to wrap <tt>inline</tt> {@link JsScript JavaScript}
+ * 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 script 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 JavaScript content in CDATA tags, set
+ * {@link #setCharacterData(boolean)} to true. Below is shown how the JavaScript
+ * content would be rendered:
+ *
+ * <pre class="codeHtml">
+ * <script type="text/javascript">
+ * <span style="color:#3F7F5F">/∗<![CDATA[∗/</span>
+ *
+ * if(x < y) alert('Hello');
+ *
+ * <span style="color:#3F7F5F">/∗]]>∗/</span>
+ * </script> </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 JsScript extends ResourceElement {
+
+ // -------------------------------------------------------------- Variables
+
+ /** A buffer holding the inline JavaScript content. */
+ private HtmlStringBuffer content = new HtmlStringBuffer();
+
+ /**
+ * Indicates if the JsScript'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;
+
+ // ----------------------------------------------------------- Constructors
+
+ /**
+ * Construct a new inline JavaScript element.
+ */
+ public JsScript() {
+ this(null);
+ }
+
+ /**
+ * Construct a new inline JavaScript element with the given content.
+ *
+ * @param content the JavaScript content
+ */
+ public JsScript(String content) {
+ if (content != null) {
+ this.content.append(content);
+ }
+ setAttribute("type", "text/javascript");
+ }
+
+ /**
+ * Construct a new inline JavaScript element for the given template path
+ * and template model.
+ * <p/>
+ * When the JsScript is rendered the template and model will be merged and
+ * the result will be rendered together with any JsScript
+ * {@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 JsScript for the given template path and model
+ * JsScript script = new JsScript("/mypage-template.js", model);
+ *
+ * // Add script to the Page Head elements
+ * getHeadElements().add(script);
+ * }
+ * } </pre>
+ *
+ * @param template the path of the template to render
+ * @param model the template model
+ */
+ public JsScript(String template, Map model) {
+ this(null);
+ setTemplate(template);
+ setModel(model);
+ }
+
+ // ------------------------------------------------------ Public Properties
+
+ /**
+ * Returns the JavaScript HTML tag: <script>.
+ *
+ * @return the JavaScript HTML tag: <script>
+ */
+ public String getTag() {
+ return "script";
+ }
+
+ /**
+ * Return the JavaScript content buffer.
+ *
+ * @return the JavaScript content buffer
+ */
+ public HtmlStringBuffer getContent() {
+ return content;
+ }
+
+ /**
+ * Set the JavaScript content buffer.
+ *
+ * @param content the new content buffer
+ */
+ public void setContent(HtmlStringBuffer content) {
+ this.content = content;
+ }
+
+ /**
+ * Return true if the JsScript's content should be wrapped in CDATA tags,
+ * false otherwise.
+ *
+ * @return true if the JsScript's content should be wrapped in CDATA tags,
+ * false otherwise
+ */
+ public boolean isCharacterData() {
+ return characterData;
+ }
+
+ /**
+ * Sets whether the JsScript's content should be wrapped in CDATA tags or not.
+ *
+ * @param characterData true indicates that the JsScript'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 JsScript
+ * {@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 JsScript
+ * {@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 JavaScript string to the content buffer.
+ *
+ * @param content the JavaScript string to append to the content
+ * buffer
+ * @return the JavaScript content buffer
+ */
+ public HtmlStringBuffer append(String content) {
+ return this.content.append(content);
+ }
+
+ /**
+ * Render the HTML representation of the JsScript 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 JsScript)) {
+ return false;
+ }
+
+ //3. Cast the argument to the correct type.
+ JsScript that = (JsScript) 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 JsScript {@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 JsScript
+ * {@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/ResourceElement.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/element/ResourceElement.java?rev=765116&r1=765115&r2=765116&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/element/ResourceElement.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/element/ResourceElement.java Wed Apr 15 10:22:08 2009
@@ -1,358 +1,358 @@
-/*
- * 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.util.HtmlStringBuffer;
-import org.apache.commons.lang.StringUtils;
-
-/**
- * Provides a base class for rendering HEAD resources of an HTML page, for
- * example JavaScript (<script>) and Cascading Stylesheets
- * (<link>/<style>).
- * <p/>
- * Subclasses should override {@link #getTag()} to return a specific HTML tag.
- * <p/>
- * Below are some example Resource elements:
- * <ul>
- * <li>{@link JsImport}, for importing <tt>external</tt> JavaScript using the
- * <script> element.</li>
- * <li>{@link JsScript}, for including <tt>inline</tt> JavaScript using the
- * <script> element.</li>
- * <li>{@link CssImport}, for importing <tt>external</tt> Cascading Stylesheets
- * using the <link> element.</li>
- * <li>{@link CssStyle}, for including <tt>inline</tt> Cascading Stylesheets
- * using the <style> element.</li>
- * </ul>
- *
- * <a name="remove-duplicates"></a>
- * <h3>Remove duplicates</h3>
- * Click will ensure that duplicate Resource elements are removed by checking
- * the {@link #isUnique()} property. Thus if the same Resource is imported
- * mutliple times by the Page or different Controls, only one Resource will be
- * rendered, if {@link #isUnique()} returns <tt>true</tt>.
- * <p/>
- * The rules for defining a unique Resource is as follows:
- * <ul>
- * <li>{@link JsImport} and {@link CssImport} is unique based on the
- * attributes {@link JsImport#getSrc()} and {@link CssImport#getHref()}
- * respectively.</li>
- * <li>{@link JsScript} and {@link CssStyle} is unique if their HTML
- * {@link #setId(java.lang.String) ID} attribute is set. The HTML
- * spec defines that an element's HTML ID must be unique per page.</li>
- * </ul>
- * For example:
- * <pre class="prettyprint">
- * public class MyPage extends Page {
- *
- * public List getHeadElements() {
- * // We use lazy loading to ensure the JavaScript and 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();
- *
- * JsImport jsImport = new JsImport("/js/mylib.js");
- * // Click will ensure the library "/js/mylib.js" is only included
- * // once in the Page
- * headElements.add(jsImport);
- *
- * JsScript jsScript = new JsScript("alert('Hello!');");
- * // Click won't ensure the script is unique because its ID
- * // attribute is not defined
- * headElements.add(jsScript);
- *
- * jsScript = new JsScript("alert('Hello!');");
- * jsScript.setId("my-unique-script-id");
- * // Click will ensure the script is unique because its ID attribute
- * // is defined. Click will remove other scripts with the same ID
- * headElements.add(jsScript);
- *
- * CssImport cssImport = new CssImport("/css/style.css");
- * // Click will ensure the library "/css/style.css" is only
- * // included once in the Page
- * headElements.add(cssImport);
- *
- * CssScript cssScript = new CssScript("body { font-weight: bold; }");
- * cssScript.setId("my-unique-style-id");
- * // Click will ensure the css is unique because its ID attribute
- * // is defined. Click will remove other css styles with the same ID
- * headElements.add(cssScript);
- * }
- * return headElements;
- * }
- * } </pre>
- *
- * <a name="versioning"></a>
- * <h3>Automatic Resource versioning</h3>
- *
- * ResourceElement provides the ability to automatically version elements
- * according to Yahoo Performance Rule: <a target="_blank" href="http://developer.yahoo.com/performance/rules.html#expires">Add an Expires or a Cache-Control Header</a>.
- * This rule recommends adding an expiry header to JavaScript, Css
- * and image resources, which forces the browser to cache the resources. It also
- * suggests <em>versioning</em> the resources so that each new release of the
- * web application renders resources with different paths, forcing the browser
- * to download the new resources.
- * <p/>
- * For detailed information on versioning JavaScript, Css and image resources
- * see the <a href="../../../../extras-api/org/apache/click/extras/filter/PerformanceFilter.html">PerformanceFilter</a>.
- * <p/>
- * To enable versioning of JavaScript, Css and image resources the following
- * conditions must be met:
- * <ul>
- * <li>the {@link org.apache.click.util.ClickUtils#ENABLE_RESOURCE_VERSION}
- * request attribute must be set to <tt>true</tt></li>
- * <li>the application mode must be either "production" or "profile"</li>
- * <li>the {@link org.apache.click.util.ClickUtils#setApplicationVersion(java.lang.String)
- * application version} must be set</li>
- * </ul>
- * <b>Please note:</b> <a href="../../../../extras-api/org/apache/click/extras/filter/PerformanceFilter.html">PerformanceFilter</a>
- * handles the above steps for you.
- *
- * <a name="conditional-comment"></a>
- * <h3>Conditional comment support for Internet Explorer</h3>
- *
- * Sometimes it is necessary to provide additional JavaScript and Css for
- * Internet Explorer because it deviates quite often from the standards.
- * <p/>
- * Conditional comments allows you to wrap the resource in a special comment
- * which only IE understands, meaning other browsers won't process the resource.
- * <p/>
- * You can read more about conditional comments
- * <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx#syntax">here</a>
- * and <a target="_blank" href="http://www.quirksmode.org/css/condcom.html">here</a>
- * <p/>
- * It has to be said that IE7 and up has much better support for Css, thus
- * conditional comments are mostly used for IE6 and below.
- * <pre class="prettyprint">
- * public class MyPage extends Page {
- *
- * public List getHeadElements() {
- * // We use lazy loading to ensure the JavaScript and 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();
- *
- * CssImport cssImport = new CssImport("/css/ie-style.css");
- * // Use one of the predefined conditional comments to target IE6
- * // and below
- * cssImport.setConditionalComment(IE_LESS_THAN_IE7);
- * headElements.add(cssImport);
- *
- * cssImport = new CssImport("/css/ie-style2.css");
- * // Use a custom predefined conditional comments to target only IE6
- * cssImport.setConditionalComment("[if IE 6]");
- * headElements.add(cssImport);
- * }
- * return headElements;
- * }
- * } </pre>
- *
- * ResourceElement contains some predefined Conditional Comments namely
- * {@link #IF_IE}, {@link #IF_LESS_THAN_IE7} and {@link #IF_IE7}.
- *
- * @author Bob Schellink
- */
-public class ResourceElement extends Element {
-
- // -------------------------------------------------------------- Constants
-
- /**
- * A predefined conditional comment to test if browser is IE. Value:
- * <tt>[if IE]</tt>.
- */
- public static final String IF_IE = "[if IE]";
-
- /**
- * A predefined conditional comment to test if browser is less than IE7.
- * Value: <tt>[if lt IE 7]</tt>.
- */
- public static final String IF_LESS_THAN_IE7 = "[if lt IE 7]";
-
- /**
- * A predefined conditional comment to test if browser is IE7. Value:
- * <tt>[if IE 7]</tt>.
- */
- public static final String IF_IE7 = "[if IE 7]";
-
- // -------------------------------------------------------------- Variables
-
- /**
- * The Internet Explorer conditional comment to wrap the Resource with.
- */
- private String conditionalComment;
-
- /**
- * Indicates if Click should ensure the import is unique, default value is
- * <tt>false</tt>.
- */
- private boolean unique = false;
-
- /**
- * The <tt>version indicator</tt> to append to the Resource element.
- */
- private String versionIndicator;
-
- // ------------------------------------------------------ Public properties
-
- /**
- * Return the <tt>version indicator</tt> to be appended to the resource
- * path.
- *
- * @return the <tt>version indicator</tt> to be appended to the resource
- * path.
- */
- public String getVersionIndicator() {
- return versionIndicator;
- }
-
- /**
- * Set the <tt>version indicator</tt> to be appended to the resource path.
- *
- * @param versionIndicator the version indicator to be appended to the
- * resource path
- */
- public void setVersionIndicator(String versionIndicator) {
- this.versionIndicator = versionIndicator;
- }
-
- /**
- * Return true if the Resource should be unique, false otherwise. The default
- * value is <tt>true</tt> if the {@link #getId() ID} attribute is defined,
- * false otherwise.
- *
- * @return true if the Resource should be unique, false otherwise.
- */
- public boolean isUnique() {
- String id = getId();
-
- // If id is defined, import will be any duplicate import found will be
- // filtered out
- if (StringUtils.isNotBlank(id)) {
- return true;
- }
- return unique;
- }
-
- /**
- * Return Internal Explorer's <tt>conditional comment</tt> to wrap the
- * Resource with.
- *
- * @return Internal Explorer's conditional comment to wrap the Resource with.
- */
- public String getConditionalComment() {
- return conditionalComment;
- }
-
- /**
- * Set Internet Explorer's conditional comment to wrap the Resource with.
- *
- * @param conditionalComment Internet Explorer's conditional comment to wrap
- * the Resource with
- */
- public void setConditionalComment(String conditionalComment) {
- this.conditionalComment = conditionalComment;
- }
-
- // --------------------------------------------------------- Public methods
-
- /**
- * Render the HTML representation of the Resource 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 Resource element output
- * to
- */
- public void render(HtmlStringBuffer buffer) {
- renderConditionalCommentPrefix(buffer);
-
- if (getTag() == null) {
- return;
- }
- renderTagBegin(getTag(), buffer);
- renderTagEnd(getTag(), buffer);
-
- renderConditionalCommentSuffix(buffer);
- }
-
- // ------------------------------------------------ Package Private Methods
-
- /**
- * Add the {@link #getApplicationResourceVersionIndicator(org.apache.click.Context)}
- * to the specified resourcePath. If the version indicator is not defined
- * this method will return the resourcePath unchanged.
- *
- * @param resourcePath the resource path to add the version indicator to
- */
- String addVersionIndicator(String resourcePath) {
- String versionIndicator = getVersionIndicator();
-
- // If no resourcePath or version indicator is defined, exit early
- if (resourcePath == null || StringUtils.isBlank(versionIndicator)) {
- return resourcePath;
- }
-
- int start = resourcePath.lastIndexOf(".");
- // Exit early if extension is not found
- if (start < 0) {
- return resourcePath;
- }
-
- HtmlStringBuffer buffer = new HtmlStringBuffer();
- buffer.append(resourcePath.substring(0, start));
- buffer.append(versionIndicator);
- buffer.append(resourcePath.substring(start));
- return buffer.toString();
- }
-
- /**
- * Render the {@link #getConditionalComment() conditional comment} prefix
- * to the specified buffer. If the conditional comment is not defined this
- * method won't append to the buffer.
- *
- * @param buffer buffer to append the conditional comment prefix
- */
- void renderConditionalCommentPrefix(HtmlStringBuffer buffer) {
- String conditional = getConditionalComment();
-
- // Render IE conditional comment
- if (StringUtils.isNotBlank(conditional)) {
- buffer.append("<!--").append(conditional).append(">\n");
- }
- }
-
- /**
- * Render the {@link #getConditionalComment() conditional comment} suffix
- * to the specified buffer. If the conditional comment is not defined this
- * method won't append to the buffer.
- *
- * @param buffer buffer to append the conditional comment suffix
- */
- void renderConditionalCommentSuffix(HtmlStringBuffer buffer) {
- String conditional = getConditionalComment();
-
- // Close IE conditional comment
- if (StringUtils.isNotBlank(conditional)) {
- buffer.append("\n<![endif]-->");
- }
- }
-}
+/*
+ * 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.util.HtmlStringBuffer;
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * Provides a base class for rendering HEAD resources of an HTML page, for
+ * example JavaScript (<script>) and Cascading Stylesheets
+ * (<link>/<style>).
+ * <p/>
+ * Subclasses should override {@link #getTag()} to return a specific HTML tag.
+ * <p/>
+ * Below are some example Resource elements:
+ * <ul>
+ * <li>{@link JsImport}, for importing <tt>external</tt> JavaScript using the
+ * <script> element.</li>
+ * <li>{@link JsScript}, for including <tt>inline</tt> JavaScript using the
+ * <script> element.</li>
+ * <li>{@link CssImport}, for importing <tt>external</tt> Cascading Stylesheets
+ * using the <link> element.</li>
+ * <li>{@link CssStyle}, for including <tt>inline</tt> Cascading Stylesheets
+ * using the <style> element.</li>
+ * </ul>
+ *
+ * <a name="remove-duplicates"></a>
+ * <h3>Remove duplicates</h3>
+ * Click will ensure that duplicate Resource elements are removed by checking
+ * the {@link #isUnique()} property. Thus if the same Resource is imported
+ * mutliple times by the Page or different Controls, only one Resource will be
+ * rendered, if {@link #isUnique()} returns <tt>true</tt>.
+ * <p/>
+ * The rules for defining a unique Resource is as follows:
+ * <ul>
+ * <li>{@link JsImport} and {@link CssImport} is unique based on the
+ * attributes {@link JsImport#getSrc()} and {@link CssImport#getHref()}
+ * respectively.</li>
+ * <li>{@link JsScript} and {@link CssStyle} is unique if their HTML
+ * {@link #setId(java.lang.String) ID} attribute is set. The HTML
+ * spec defines that an element's HTML ID must be unique per page.</li>
+ * </ul>
+ * For example:
+ * <pre class="prettyprint">
+ * public class MyPage extends Page {
+ *
+ * public List getHeadElements() {
+ * // We use lazy loading to ensure the JavaScript and 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();
+ *
+ * JsImport jsImport = new JsImport("/js/mylib.js");
+ * // Click will ensure the library "/js/mylib.js" is only included
+ * // once in the Page
+ * headElements.add(jsImport);
+ *
+ * JsScript jsScript = new JsScript("alert('Hello!');");
+ * // Click won't ensure the script is unique because its ID
+ * // attribute is not defined
+ * headElements.add(jsScript);
+ *
+ * jsScript = new JsScript("alert('Hello!');");
+ * jsScript.setId("my-unique-script-id");
+ * // Click will ensure the script is unique because its ID attribute
+ * // is defined. Click will remove other scripts with the same ID
+ * headElements.add(jsScript);
+ *
+ * CssImport cssImport = new CssImport("/css/style.css");
+ * // Click will ensure the library "/css/style.css" is only
+ * // included once in the Page
+ * headElements.add(cssImport);
+ *
+ * CssScript cssScript = new CssScript("body { font-weight: bold; }");
+ * cssScript.setId("my-unique-style-id");
+ * // Click will ensure the css is unique because its ID attribute
+ * // is defined. Click will remove other css styles with the same ID
+ * headElements.add(cssScript);
+ * }
+ * return headElements;
+ * }
+ * } </pre>
+ *
+ * <a name="versioning"></a>
+ * <h3>Automatic Resource versioning</h3>
+ *
+ * ResourceElement provides the ability to automatically version elements
+ * according to Yahoo Performance Rule: <a target="_blank" href="http://developer.yahoo.com/performance/rules.html#expires">Add an Expires or a Cache-Control Header</a>.
+ * This rule recommends adding an expiry header to JavaScript, Css
+ * and image resources, which forces the browser to cache the resources. It also
+ * suggests <em>versioning</em> the resources so that each new release of the
+ * web application renders resources with different paths, forcing the browser
+ * to download the new resources.
+ * <p/>
+ * For detailed information on versioning JavaScript, Css and image resources
+ * see the <a href="../../../../extras-api/org/apache/click/extras/filter/PerformanceFilter.html">PerformanceFilter</a>.
+ * <p/>
+ * To enable versioning of JavaScript, Css and image resources the following
+ * conditions must be met:
+ * <ul>
+ * <li>the {@link org.apache.click.util.ClickUtils#ENABLE_RESOURCE_VERSION}
+ * request attribute must be set to <tt>true</tt></li>
+ * <li>the application mode must be either "production" or "profile"</li>
+ * <li>the {@link org.apache.click.util.ClickUtils#setApplicationVersion(java.lang.String)
+ * application version} must be set</li>
+ * </ul>
+ * <b>Please note:</b> <a href="../../../../extras-api/org/apache/click/extras/filter/PerformanceFilter.html">PerformanceFilter</a>
+ * handles the above steps for you.
+ *
+ * <a name="conditional-comment"></a>
+ * <h3>Conditional comment support for Internet Explorer</h3>
+ *
+ * Sometimes it is necessary to provide additional JavaScript and Css for
+ * Internet Explorer because it deviates quite often from the standards.
+ * <p/>
+ * Conditional comments allows you to wrap the resource in a special comment
+ * which only IE understands, meaning other browsers won't process the resource.
+ * <p/>
+ * You can read more about conditional comments
+ * <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx#syntax">here</a>
+ * and <a target="_blank" href="http://www.quirksmode.org/css/condcom.html">here</a>
+ * <p/>
+ * It has to be said that IE7 and up has much better support for Css, thus
+ * conditional comments are mostly used for IE6 and below.
+ * <pre class="prettyprint">
+ * public class MyPage extends Page {
+ *
+ * public List getHeadElements() {
+ * // We use lazy loading to ensure the JavaScript and 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();
+ *
+ * CssImport cssImport = new CssImport("/css/ie-style.css");
+ * // Use one of the predefined conditional comments to target IE6
+ * // and below
+ * cssImport.setConditionalComment(IE_LESS_THAN_IE7);
+ * headElements.add(cssImport);
+ *
+ * cssImport = new CssImport("/css/ie-style2.css");
+ * // Use a custom predefined conditional comments to target only IE6
+ * cssImport.setConditionalComment("[if IE 6]");
+ * headElements.add(cssImport);
+ * }
+ * return headElements;
+ * }
+ * } </pre>
+ *
+ * ResourceElement contains some predefined Conditional Comments namely
+ * {@link #IF_IE}, {@link #IF_LESS_THAN_IE7} and {@link #IF_IE7}.
+ *
+ * @author Bob Schellink
+ */
+public class ResourceElement extends Element {
+
+ // -------------------------------------------------------------- Constants
+
+ /**
+ * A predefined conditional comment to test if browser is IE. Value:
+ * <tt>[if IE]</tt>.
+ */
+ public static final String IF_IE = "[if IE]";
+
+ /**
+ * A predefined conditional comment to test if browser is less than IE7.
+ * Value: <tt>[if lt IE 7]</tt>.
+ */
+ public static final String IF_LESS_THAN_IE7 = "[if lt IE 7]";
+
+ /**
+ * A predefined conditional comment to test if browser is IE7. Value:
+ * <tt>[if IE 7]</tt>.
+ */
+ public static final String IF_IE7 = "[if IE 7]";
+
+ // -------------------------------------------------------------- Variables
+
+ /**
+ * The Internet Explorer conditional comment to wrap the Resource with.
+ */
+ private String conditionalComment;
+
+ /**
+ * Indicates if Click should ensure the import is unique, default value is
+ * <tt>false</tt>.
+ */
+ private boolean unique = false;
+
+ /**
+ * The <tt>version indicator</tt> to append to the Resource element.
+ */
+ private String versionIndicator;
+
+ // ------------------------------------------------------ Public properties
+
+ /**
+ * Return the <tt>version indicator</tt> to be appended to the resource
+ * path.
+ *
+ * @return the <tt>version indicator</tt> to be appended to the resource
+ * path.
+ */
+ public String getVersionIndicator() {
+ return versionIndicator;
+ }
+
+ /**
+ * Set the <tt>version indicator</tt> to be appended to the resource path.
+ *
+ * @param versionIndicator the version indicator to be appended to the
+ * resource path
+ */
+ public void setVersionIndicator(String versionIndicator) {
+ this.versionIndicator = versionIndicator;
+ }
+
+ /**
+ * Return true if the Resource should be unique, false otherwise. The default
+ * value is <tt>true</tt> if the {@link #getId() ID} attribute is defined,
+ * false otherwise.
+ *
+ * @return true if the Resource should be unique, false otherwise.
+ */
+ public boolean isUnique() {
+ String id = getId();
+
+ // If id is defined, import will be any duplicate import found will be
+ // filtered out
+ if (StringUtils.isNotBlank(id)) {
+ return true;
+ }
+ return unique;
+ }
+
+ /**
+ * Return Internal Explorer's <tt>conditional comment</tt> to wrap the
+ * Resource with.
+ *
+ * @return Internal Explorer's conditional comment to wrap the Resource with.
+ */
+ public String getConditionalComment() {
+ return conditionalComment;
+ }
+
+ /**
+ * Set Internet Explorer's conditional comment to wrap the Resource with.
+ *
+ * @param conditionalComment Internet Explorer's conditional comment to wrap
+ * the Resource with
+ */
+ public void setConditionalComment(String conditionalComment) {
+ this.conditionalComment = conditionalComment;
+ }
+
+ // --------------------------------------------------------- Public methods
+
+ /**
+ * Render the HTML representation of the Resource 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 Resource element output
+ * to
+ */
+ public void render(HtmlStringBuffer buffer) {
+ renderConditionalCommentPrefix(buffer);
+
+ if (getTag() == null) {
+ return;
+ }
+ renderTagBegin(getTag(), buffer);
+ renderTagEnd(getTag(), buffer);
+
+ renderConditionalCommentSuffix(buffer);
+ }
+
+ // ------------------------------------------------ Package Private Methods
+
+ /**
+ * Add the {@link #getApplicationResourceVersionIndicator(org.apache.click.Context)}
+ * to the specified resourcePath. If the version indicator is not defined
+ * this method will return the resourcePath unchanged.
+ *
+ * @param resourcePath the resource path to add the version indicator to
+ */
+ String addVersionIndicator(String resourcePath) {
+ String versionIndicator = getVersionIndicator();
+
+ // If no resourcePath or version indicator is defined, exit early
+ if (resourcePath == null || StringUtils.isBlank(versionIndicator)) {
+ return resourcePath;
+ }
+
+ int start = resourcePath.lastIndexOf(".");
+ // Exit early if extension is not found
+ if (start < 0) {
+ return resourcePath;
+ }
+
+ HtmlStringBuffer buffer = new HtmlStringBuffer();
+ buffer.append(resourcePath.substring(0, start));
+ buffer.append(versionIndicator);
+ buffer.append(resourcePath.substring(start));
+ return buffer.toString();
+ }
+
+ /**
+ * Render the {@link #getConditionalComment() conditional comment} prefix
+ * to the specified buffer. If the conditional comment is not defined this
+ * method won't append to the buffer.
+ *
+ * @param buffer buffer to append the conditional comment prefix
+ */
+ void renderConditionalCommentPrefix(HtmlStringBuffer buffer) {
+ String conditional = getConditionalComment();
+
+ // Render IE conditional comment
+ if (StringUtils.isNotBlank(conditional)) {
+ buffer.append("<!--").append(conditional).append(">\n");
+ }
+ }
+
+ /**
+ * Render the {@link #getConditionalComment() conditional comment} suffix
+ * to the specified buffer. If the conditional comment is not defined this
+ * method won't append to the buffer.
+ *
+ * @param buffer buffer to append the conditional comment suffix
+ */
+ void renderConditionalCommentSuffix(HtmlStringBuffer buffer) {
+ String conditional = getConditionalComment();
+
+ // Close IE conditional comment
+ if (StringUtils.isNotBlank(conditional)) {
+ buffer.append("\n<![endif]-->");
+ }
+ }
+}
|