public class JavaScriptWriter
extends java.io.Writer
var t = "properly formatted javascript literal expression";But if you want that value to be variable, then given that you have the value as a string in memory, you need to properly encode the value. If there are any quote characters, they need to be escaped with a backslash. If there are any newline characters, they need to be replaced with the appropriate backslash expression. If there are Unicode characters >128, they need to be encoded into hex value expressions. That is what this class will do. There are two ways: a writer and an encoding method. Both methods assume you are constructing your java script expressions by writing them to a Writer object. This is more efficient than concatenating strings together. 1. A Writer Instance. If you want to use a method that is prepared to write a raw value to a Writer, and you want that raw value to be JavaScript encoded, then you can construct a JAvaScriptWriter on your existing writer, and pass JavaScriptWriter to the routine that will write the raw value. Every (raw) value written will be converted as it is passed on to the wrapped Writer. An example of this use is if you have a DOM tree (which can stream itself to a Writer) but you want the serialized DOM to be placed into a JS literal form on a web page. Passing the JAvaScriptWriter to the DOM for serialization will mean that all the XML output will be properly escaped as a JavaScript expression. The code that is writing the value does not know that the value is going into a JavaScript literal expression ... it just writes. 2. A Static Method. If the value you want to convert is a string (as is often the case) you don't need to construct a new object for this. Just call the static encode method, passing a Writer and the string you want converted. The string will be converted as it is written to the Writer. the Java to ccreate the above expression above might be:
Writer w; // given as the place the page is being written String val = "properly formatted javascript literal expression"; w.write("var t = \""); JavaScriptWriter.encode(w, val); w.write("\";\n");The val can be ANY legal String value, e.g. something a user typed in. The output stream of characters will accurately represent the literal expression for val. Note also that this does NOT do encoding for HTML in order to avoid problems with HTML when putting JS within a web page. Consider using an HTMLWriter inside this to take care of angle brackets and such. Author: Keith Swenson Copyright: Keith Swenson, all rights reserved License: This code is made available under the GNU Lesser GPL license.
Constructor and Description |
---|
JavaScriptWriter(java.io.Writer _wrapped) |
Modifier and Type | Method and Description |
---|---|
void |
close() |
static void |
decode(java.lang.StringBuffer res,
java.lang.String literalString)
Takes a single JavaScript literal and converts it back to a String value.
|
static void |
encode(java.io.Writer w,
java.lang.String val)
Encodes a single
String value to a JavaScript literal
expression. |
static java.lang.String |
encodeToString(java.lang.String val)
THIS IS NOT EFFICIENT.
|
void |
flush() |
void |
write(char[] chs,
int start,
int len) |
void |
write(int c) |
public void write(int c) throws java.io.IOException
write
in class java.io.Writer
java.io.IOException
public void write(char[] chs, int start, int len) throws java.io.IOException
write
in class java.io.Writer
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.Writer
java.io.IOException
public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.Writer
java.io.IOException
public static void encode(java.io.Writer w, java.lang.String val) throws java.io.IOException
String
value to a JavaScript literal
expression.
If you are constructing a JavaScript expression and you have a String value that you want to be expressed as a String literal in the JavaScript, you must use this method to scan the String and convert any embedded problematic characters into their escaped equivalents. The result of the conversion is written into the stream that you pass in.
Do NOT simply paste quotes before and after the string!
w
- The Writer object to which the encoded String value is added.val
- The String
value to encode.java.io.IOException
public static void decode(java.lang.StringBuffer res, java.lang.String literalString) throws java.lang.Exception
Note: Quotes in the middle of the string that are not allowed without being escaped by a backslash. Newline characters are not allowed either.
res
- The StringBuffer
object to which the converted
literalString is added.literalString
- The JavaScript literal to be converted. Must NOT have quotes
around it, only the stuff inside the quotes.java.lang.Exception
- Thrown if either parameter is null.public static java.lang.String encodeToString(java.lang.String val) throws java.lang.Exception
java.lang.Exception