com.globalmentor.javascript.JavaScript Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of globalmentor-javascript Show documentation
Show all versions of globalmentor-javascript Show documentation
GlobalMentor Java JavaScript library.
/*
* Copyright © 1996-2008 GlobalMentor, Inc.
*
* Licensed 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 com.globalmentor.javascript;
import java.net.URI;
import com.globalmentor.net.ContentType;
import static com.globalmentor.java.StringBuilders.*;
/**
* Constants for working with JavaScript.
* @author Garret Wilson
* @see RFC 4329: Scripting Media Types
*/
public class JavaScript {
/** The JavaScript MIME subtype. */
public static final String JAVASCRIPT_SUBTYPE = "javascript";
/** The content type for JavaScript: application/javascript
. */
public static final ContentType JAVASCRIPT_CONTENT_TYPE = ContentType.create(ContentType.APPLICATION_PRIMARY_TYPE, JAVASCRIPT_SUBTYPE);
/** The obsolete content type for JavaScript: text/javascript
. */
public static final ContentType JAVASCRIPT_OBSOLETE_CONTENT_TYPE = ContentType.create(ContentType.TEXT_PRIMARY_TYPE, JAVASCRIPT_SUBTYPE);
/** The name extension for JavaScript files. */
public static final String JS_NAME_EXTENSION = "js";
/** The delimiter '.' for object properties. */
public static final char PROPERTY_DELIMITER = '.';
/** The character '[' for starting an array index. */
public static final char ARRAY_INDEX_BEGIN_CHAR = '[';
/** The character ']' for ending an array index. */
public static final char ARRAY_INDEX_END_CHAR = ']';
/** The character '[' for starting an array. */
public static final char ARRAY_BEGIN_CHAR = '[';
/** The character ']' for ending an array. */
public static final char ARRAY_END_CHAR = ']';
/** The character ',' for separating array elements. */
public static final char ARRAY_DELIMITER = ',';
/** The character '{' for starting an associative array. */
public static final char ASSOCIATIVE_ARRAY_BEGIN_CHAR = '{';
/** The character '}' for ending an associative array. */
public static final char ASSOCIATIVE_ARRAY_END_CHAR = '}';
/** The character ',' for separating associative array elements. */
public static final char ASSOCIATIVE_ARRAY_DELIMITER = ',';
/** The character ';' for separating associative array key/value pairs. */
public static final char ASSOCIATIVE_ARRAY_KEY_VALUE_DELIMITER = ':';
/** The character '{' for starting a block. */
public static final char BLOCK_BEGIN_CHAR = '{';
/** The character '}' for ending a block. */
public static final char BLOCK_END_CHAR = '}';
/** The character used to escape characters in an encoded string. */
public static final char ESCAPE_CHAR = '\\';
/** The character '(' for starting a parameter list. */
public static final char PARAMETER_BEGIN_CHAR = '(';
/** The character ')' for ending a parameter list. */
public static final char PARAMETER_END_CHAR = ')';
/** The delimiter ',' for method parameter separation. */
public static final char PARAMETER_DELIMITER = ',';
/** The character '=' for variable assignment. */
public static final char ASSIGNMENT_CHAR = '=';
/** The character ';' indicating the ending of a statement. */
public static final char STATEMENT_END_CHAR = ';';
/** The characters that must be encoded in a string value. */
public static final char[] STRING_ENCODE_CHARS = new char[] { '"', '\\', '/', '\b', '\f', '\n', '\r', '\t' };
/** The strings with which encoded characters will be replaced in a string value. */
public static final String[] STRING_ENCODE_REPLACEMENT_STRINGS = new String[] { "\\\"", "\\\\", "\\/", "\\b", "\\f", "\\n", "\\r", "\\t" };
/** The if keyword. */
public static final String IF_KEYWORD = "if";
/** The else keyword. */
public static final String ELSE_KEYWORD = "else";
/** The return keyword. */
public static final String RETURN_KEYWORD = "return";
/** The negation operator character. */
public static final char NOT_OPERATOR = '!';
/** The document identifier. */
public static final String DOCUMENT = "document";
/** The forms identifier. */
public static final String FORMS = "forms";
/** The alert method. */
public static final String ALERT_METHOD = "alert";
/** The confirm method. */
public static final String CONFIRM_METHOD = "confirm";
/** The form submission method. */
public static final String SUBMIT_METHOD = "submit";
/** The null identifier. */
public static final String NULL = "null";
/** The boolean true identifier. */
public static final String TRUE = "true";
/** The boolean false identifier. */
public static final String FALSE = "false";
/**
* Escapes a string for JavaScript.
*
* - '\\' is changed to "\\\\"
* - '\'' is changed to "\\\'"
* - '"' is changed to "\\\""
* - '\n' is changed to "\\n"
* - '\t' is changed to "\\t"
*
* @param string The string to escape.
* @return A JavasScript-safe string.
*/
public static String escape(final String string) {
final StringBuilder stringBuilder = new StringBuilder(string); //create a new string builder based upon the string
replace(stringBuilder, '\\', "\\\\"); //TODO use constants
replace(stringBuilder, '\'', "\\\'"); //TODO use constants
replace(stringBuilder, '"', "\\\""); //TODO use constants
replace(stringBuilder, '\n', "\\n"); //TODO use constants
replace(stringBuilder, '\t', "\\t"); //TODO use constants
return stringBuilder.toString(); //return the escaped string
}
/**
* Returns the name of a method: method()
* @param method The name of the method.
* @param parameters The method parameters.
* @return The name of the method.
*/
public static String getMethod(final String method, final String... parameters) {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(method); //method
stringBuilder.append(PARAMETER_BEGIN_CHAR); //(
for(final String parameter : parameters) { //look at each parameter
stringBuilder.append(parameter).append(PARAMETER_DELIMITER); //parameter,
}
if(parameters.length > 0) { //if there was at least one parameter
stringBuilder.deleteCharAt(stringBuilder.length() - 1); //remove the last parameter delimiter
}
stringBuilder.append(PARAMETER_END_CHAR); //)
return stringBuilder.toString(); //return the property method we constructed
}
/**
* Returns the name of an object property: object.property
* @param object The name of the object.
* @param property The name of the property.
* @return The name of the object property.
*/
public static String getObjectProperty(final String object, final String property) {
return object + PROPERTY_DELIMITER + property;
}
/**
* Returns the name of an object method: object.method()
* @param object The name of the object.
* @param method The name of the method.
* @param parameters The method parameters.
* @return The name of the object method.
*/
public static String getObjectMethod(final String object, final String method, final String... parameters) {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(object).append(PROPERTY_DELIMITER); //object.
stringBuilder.append(getMethod(method, parameters)); //method(parameters)
return stringBuilder.toString(); //return the object method we constructed
}
/**
* Returns the name of an object indexed property: object['property']
* @param object The name of the object.
* @param property The name of the property.
* @return The name of the object indexed property.
*/
public static String getObjectIndexedProperty(final String object, final String property) {
return object + ARRAY_INDEX_BEGIN_CHAR + '\'' + property + '\'' + ARRAY_INDEX_END_CHAR;
}
/**
* Returns the name of the form variable with the given ID: document.forms[formID]
* @param formID The ID of the form to return.
* @return The name of the form variable.
*/
public static String getFormVariable(final String formID) {
return getObjectIndexedProperty(getObjectProperty(DOCUMENT, FORMS), formID);
}
/**
* Returns the name of a form component variable: document.forms[formID][componentID]
* @param formID The ID of the form containing the component.
* @param componentID The ID of the component variable to return.
* @return The name of the form component variable.
*/
public static String getFormComponentVariable(final String formID, final String componentID) {
return getObjectIndexedProperty(getFormVariable(formID), componentID);
}
/**
* Returns the name of a variable assignment: variable=value
* @param variable The variable to which the value should be assigned.
* @param value The value to assign to the variable, or null
.
* @return The name of the variable assignment.
*/
public static String setValue(final String variable, final String value) {
return variable + ASSIGNMENT_CHAR + (value != null ? value : NULL);
}
/**
* Returns the name of a variable assignment with a literal variable: variable='value'
The value is escaped.
* @param variable The variable to which the value should be assigned.
* @param value The literal value to assign to the variable, or null
.
* @return The name of the variable assignment with the literal variable.
*/
public static String setLiteralValue(final String variable, final String value) {
return setValue(variable, value != null ? '\'' + escape(value) + '\'' : null); //TODO use a constant
}
/**
* Returns a string representing a form component value assignment:
* document.forms[formID][componentID].property=value
* @param formID The ID of the form containing the component.
* @param componentID The ID of the component variable.
* @param property The name of the component property.
* @param value The value to assign to the form component property.
* @return The string representing the form component value assignment.
*/
public static String setFormComponentPropertyValue(final String formID, final String componentID, final String property, final String value) {
return setValue(getObjectProperty(getFormComponentVariable(formID, componentID), property), value); //set the form component property value
}
/**
* Returns a string representing a form component literal value assignment:
* document.forms[formID][componentID].property='value'
* @param formID The ID of the form containing the component.
* @param componentID The ID of the component variable.
* @param property The name of the component property.
* @param value The literal value to assign to the form component property.
* @return The string representing the form component literal value assignment.
*/
public static String setFormComponentPropertyLiteralValue(final String formID, final String componentID, final String property, final String value) {
return setLiteralValue(getObjectProperty(getFormComponentVariable(formID, componentID), property), value); //set the form component property value
}
/**
* Returns a string representing an alert: alert(text)
* @param text The text to present to the user.
* @return The string representing the alert.
*/
public static String alert(final String text) {
return getMethod(ALERT_METHOD, text); //get the method for alerting
}
/**
* Returns a string representing an alert of literal text: alert('textID')
The text is correctly escaped.
* @param text The text to present to the user.
* @return The string representing the alert of literal text.
*/
public static String alertLiteral(final String text) {
return alert('\'' + escape(text) + '\''); //place the text in quotes
}
/**
* Returns a string representing a confirmation: confirm(text)
* @param text The text to present to the user.
* @return The string representing the confirmation.
*/
public static String confirm(final String text) {
return getMethod(CONFIRM_METHOD, text); //get the method for confirming by presenting a message
}
/**
* Returns a string representing a confirmation of literal text: confirm('text')
The text is correctly escaped.
* @param text The text to present to the user.
* @return The string representing the confirmation of literal text.
*/
public static String confirmLiteral(final String text) {
return confirm('\'' + escape(text) + '\''); //place the text in quotes
}
/**
* Returns a string representing a form submission: document.forms[formID].submit()
* @param formID The ID of the form containing the component.
* @return The string representing the form submission.
*/
public static String submitForm(final String formID) {
return getObjectMethod(getFormVariable(formID), SUBMIT_METHOD); //get the method for submitting a form
}
/**
* Creates a statement: statement
;
* @param statement The statement to create.
* @return The statement created.
*/
public static String createStatement(final String statement) {
return statement + STATEMENT_END_CHAR;
}
/**
* Returns a negation: !expression
* @param expression The expression to negate.
* @return A string representing the negation of the expression.
*/
public static String createNot(final String expression) {
return new StringBuilder().append(NOT_OPERATOR).append(expression).toString();
}
/**
* Returns a string representing an if()
block: if(testExpression){thenBlock}
* @param testExpression The expression to test.
* @param thenBlock The block to execute if the test evaluates to true
.
* @return A string representing an if statement.
*/
public static String createIf(final String testExpression, final String thenBlock) {
return createIf(testExpression, thenBlock, null); //create an if with no else block
}
/**
* Returns a string representing an if()
block: if(testExpression){thenBlock}else{elseBlock}
* @param testExpression The expression to test.
* @param thenBlock The block to execute if the test evaluates to true
.
* @param elseBlock The block to execute if the test evaluates to false
, or null
if there should be no else block.
* @return A string representing an if statement.
*/
public static String createIf(final String testExpression, final String thenBlock, final String elseBlock) {
final StringBuilder stringBuilder = new StringBuilder(IF_KEYWORD);
stringBuilder.append(PARAMETER_BEGIN_CHAR); //(
stringBuilder.append(testExpression); //testExpression
stringBuilder.append(PARAMETER_END_CHAR); //)
stringBuilder.append(BLOCK_BEGIN_CHAR); //{
stringBuilder.append(thenBlock); //thenBlock
stringBuilder.append(BLOCK_END_CHAR); //}
if(elseBlock != null) { //if we have an else block
stringBuilder.append(ELSE_KEYWORD); //else
stringBuilder.append(BLOCK_BEGIN_CHAR); //{
stringBuilder.append(elseBlock); //thenBlock
stringBuilder.append(BLOCK_END_CHAR); //}
}
return stringBuilder.toString(); //return the string we created
}
/**
* Creates JavaScript code for popping up a window.
* @param uri The URI to show in the new window.
* @param name The name of the window; must not have spaces to work on IE.
* @return The JavaScript code.
*/
public static String popupWindow(final URI uri, final String name) {
return //TODO testing
"var win=window.open('" + uri + "','" + name + "','width=640,height=480,scrollbars=yes');" + "if(window.focus){win.focus()};";
}
/**
* Creates a return statement: return value;
* @param value The value to return.
* @return The return statement.
*/
public static String returnValue(final Object value) {
return createStatement(RETURN_KEYWORD + ' ' + value);
}
}