001: /*
002: * $Id: AbstractStringList.java 3947 2006-01-25 16:18:39Z joco01 $ $Revision:
003: * 3947 $ $Date: 2006-01-25 17:18:39 +0100 (Mi, 25 Jan 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.util.string;
019:
020: import wicket.Response;
021:
022: /**
023: * Provide some helpers to write javascript related tags to the response object.
024: *
025: * @author Juergen Donnerstag
026: */
027: public class JavascriptUtils {
028:
029: /** Script open tag */
030: public final static String SCRIPT_OPEN_TAG = "<script type=\"text/javascript\"><!--/*--><![CDATA[/*><!--*/\n";
031:
032: /** Script close tag */
033: public final static String SCRIPT_CLOSE_TAG = "\n/*-->]]>*/</script>\n";
034:
035: /** Script open tag */
036: public final static String SCRIPT_CONTENT_PREFIX = "<!--/*--><![CDATA[/*><!--*/\n";
037:
038: /** Script close tag */
039: public final static String SCRIPT_CONTENT_SUFFIX = "\n/*-->]]>*/";
040:
041: /** The response object */
042: private Response response;
043:
044: /**
045: * Construct.
046: *
047: * @param response
048: * The response object
049: * @param id
050: */
051: public JavascriptUtils(final Response response, String id) {
052: this .response = response;
053: writeOpenTag(response, id);
054: }
055:
056: /**
057: * Constructor without id for backward compatibility
058: *
059: * @param response
060: * The response object
061: * @param id
062: */
063: public JavascriptUtils(final Response response) {
064: this .response = response;
065: writeOpenTag(response);
066: }
067:
068: /**
069: * Escape quotes and double quotes so that they can be part of e.g. an alert
070: * call.
071: *
072: * @param input
073: * input
074: * @return Escaped version of the input
075: */
076: public static CharSequence escapeQuotes(final CharSequence input) {
077: CharSequence s = input;
078: if (s != null) {
079: s = Strings.replaceAll(s, "'", "\\'");
080: }
081: return s;
082: }
083:
084: /**
085: * Write a reference to a javascript file to the response object
086: *
087: * @param response
088: * The HTTP response
089: * @param url
090: * The javascript file URL
091: * @param id
092: * Unique identifier of element
093: */
094: public static void writeJavascriptUrl(final Response response,
095: final CharSequence url, final String id) {
096: response.write("<script type=\"text/javascript\" ");
097: if (id != null) {
098: response.write("id=\"" + id + "\" ");
099: }
100: response.write("src=\"");
101: response.write(url);
102: response.println("\"></script>");
103: }
104:
105: /**
106: * Write a reference to a javascript file to the response object
107: *
108: * @param response
109: * The HTTP response
110: * @param url
111: * The javascript file URL
112: */
113: public static void writeJavascriptUrl(final Response response,
114: final CharSequence url) {
115: writeJavascriptUrl(response, url, null);
116: }
117:
118: /**
119: * Write the simple text to the response object surrounded by a script tag.
120: *
121: * @param response
122: * The HTTP: response
123: * @param text
124: * The text to added in between the script tags
125: * @param id
126: * Unique identifier of element
127: */
128: public static void writeJavascript(final Response response,
129: final CharSequence text, String id) {
130: writeOpenTag(response, id);
131: response.write(text);
132: writeCloseTag(response);
133: }
134:
135: /**
136: * Write the simple text to the response object surrounded by a script tag.
137: *
138: * @param response
139: * The HTTP: response
140: * @param text
141: * The text to added in between the script tags
142: */
143: public static void writeJavascript(final Response response,
144: final CharSequence text) {
145: writeJavascript(response, text, null);
146: }
147:
148: /**
149: *
150: * @param response
151: * @param id
152: */
153: public static void writeOpenTag(final Response response, String id) {
154: response.write("<script type=\"text/javascript\" ");
155: if (id != null) {
156: response.write("id=\"" + id + "\"");
157: }
158: response.write(">");
159: response.write(SCRIPT_CONTENT_PREFIX);
160: }
161:
162: /**
163: *
164: * @param response
165: */
166: public static void writeOpenTag(final Response response) {
167: writeOpenTag(response, null);
168: }
169:
170: /**
171: *
172: * @param response
173: */
174: public static void writeCloseTag(final Response response) {
175: response.write(SCRIPT_CONTENT_SUFFIX);
176: response.println("</script>\n");
177:
178: }
179:
180: /**
181: * @see Response#write(java.lang.CharSequence)
182: * @param script
183: */
184: public void write(final CharSequence script) {
185: response.write(script);
186: }
187:
188: /**
189: * @see Response#println(java.lang.CharSequence)
190: * @param script
191: */
192: public void println(final CharSequence script) {
193: response.println(script);
194: }
195:
196: /**
197: * Write the script close tag to the response. The response output stream
198: * remains open.
199: */
200: public void close() {
201: writeCloseTag(response);
202: }
203: }
|