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