001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.transformers;
011:
012: import java.io.Reader;
013: import java.io.Writer;
014: import java.util.HashMap;
015: import java.util.Map;
016:
017: /**
018: * Encodings related Javascript It can escape single quotes, by replacing them by \\', as is needed in document.write actions.
019:
020: * @author Michiel Meeuwissen
021: * @since MMBase-1.7.4
022: */
023:
024: public class Javascript extends ConfigurableReaderTransformer implements
025: CharTransformer {
026: private final static String SINGLE_QUOTES = "JAVASCRIPT_ESCAPE_SINGLE_QUOTES";
027: private final static String DOUBLE_QUOTES = "JAVASCRIPT_ESCAPE_DOUBLE_QUOTES";
028: private final static String BOTH_QUOTES = "JAVASCRIPT_ESCAPE_BOTH_QUOTES";
029: public final static int ESCAPE_SINGLE_QUOTES = 1;
030: public final static int ESCAPE_DOUBLE_QUOTES = 2;
031: public final static int ESCAPE_BOTH_QUOTES = 3;
032:
033: public Javascript() {
034: super (ESCAPE_SINGLE_QUOTES);
035: }
036:
037: public Javascript(int conf) {
038: super (conf);
039: }
040:
041: /**
042: * Escapes a quote
043: * @param escapeChar The quote character to be escaped.
044: * @return the writer
045: */
046: public static Writer escapeChar(Reader r, Writer w, char escapeChar) {
047: try {
048: while (true) {
049: int c = r.read();
050: if (c == -1)
051: break;
052: if (c == escapeChar)
053: w.write('\\');
054: if (c == '\\')
055: w.write('\\');
056: if (c == '\n') {
057: w.write("\\n");
058: } else if (c == '\r') {
059: } else {
060: w.write(c);
061: }
062: }
063: } catch (java.io.IOException e) {
064: }
065: return w;
066: }
067:
068: public static Writer escapeChar(Reader r, Writer w) {
069: try {
070: while (true) {
071: int c = r.read();
072: if (c == -1)
073: break;
074: if (c == '\'' || c == '"')
075: w.write('\\');
076: if (c == '\\')
077: w.write('\\');
078: if (c == '\n') {
079: w.write("\\n");
080: } else if (c == '\r') {
081: } else {
082: w.write(c);
083: }
084: }
085: } catch (java.io.IOException e) {
086: }
087: return w;
088: }
089:
090: /**
091: * Unescapes \-escapes in a string.
092: */
093: public static Writer escapeCharBack(Reader r, Writer w) {
094: try {
095: boolean skipNext = false;
096: while (true) {
097: int c = r.read();
098: if (c == -1)
099: break;
100: if (c == '\'') {
101: if (skipNext) {
102: skipNext = false;
103: } else {
104: w.write(c);
105: skipNext = true;
106: }
107: } else {
108: w.write(c);
109: skipNext = false;
110: }
111: }
112: } catch (java.io.IOException e) {
113: }
114: return w;
115: }
116:
117: /**
118: * Used when registering this class as a possible Transformer
119: */
120:
121: public Map<String, Config> transformers() {
122: Map<String, Config> h = new HashMap<String, Config>();
123: h.put(SINGLE_QUOTES, new Config(Sql.class,
124: ESCAPE_SINGLE_QUOTES,
125: "Escape single quotes for Javascript statements"));
126: h.put(DOUBLE_QUOTES, new Config(Sql.class,
127: ESCAPE_DOUBLE_QUOTES,
128: "Escape single quotes for Javascript statements"));
129: h
130: .put(
131: BOTH_QUOTES,
132: new Config(Sql.class, ESCAPE_BOTH_QUOTES,
133: "Escape single and double quotes for Javascript statements"));
134: return h;
135: }
136:
137: public Writer transform(Reader r, Writer w) {
138: switch (to) {
139: case ESCAPE_SINGLE_QUOTES:
140: return escapeChar(r, w, '\'');
141: case ESCAPE_DOUBLE_QUOTES:
142: return escapeChar(r, w, '\"');
143: case ESCAPE_BOTH_QUOTES:
144: return escapeChar(r, w);
145:
146: default:
147: throw new UnsupportedOperationException("Cannot transform");
148: }
149: }
150:
151: public Writer transformBack(Reader r, Writer w) {
152: switch (to) {
153: case ESCAPE_SINGLE_QUOTES:
154: case ESCAPE_DOUBLE_QUOTES:
155: case ESCAPE_BOTH_QUOTES:
156: return escapeCharBack(r, w);
157: default:
158: throw new UnsupportedOperationException("Cannot transform");
159: }
160: }
161:
162: public String getEncoding() {
163: switch (to) {
164: case ESCAPE_SINGLE_QUOTES:
165: return SINGLE_QUOTES;
166: case ESCAPE_DOUBLE_QUOTES:
167: return DOUBLE_QUOTES;
168: case ESCAPE_BOTH_QUOTES:
169: return BOTH_QUOTES;
170: default:
171: return "UNKNOWN";
172: }
173: }
174: }
|