01: /***
02: * jwma Java WebMail
03: * Copyright (c) 2000-2003 jwma team
04: *
05: * jwma is free software; you can distribute and use this source
06: * under the terms of the BSD-style license received along with
07: * the distribution.
08: ***/package dtw.webmail.util;
09:
10: import org.apache.oro.text.regex.*;
11:
12: /**
13: * Class that implements a Substitution, replacing special
14: * HTML unsafe characters with their entity representations.
15: *
16: * @author Dieter Wimberger
17: * @version 0.9.7 07/02/2003
18: */
19: public class CharacterSubstitution implements Substitution {
20:
21: /**
22: * Appends the substitution to a buffer containing the original input
23: * with substitutions applied for the pattern matches found so far.
24: * For maximum flexibility, the original input as well as the
25: * PatternMatcher and Pattern used to find the match are included as
26: * arguments. However, you will almost never find a need to use those
27: * arguments when creating your own Substitution implementations.
28: * <p>
29: * For performance reasons, rather than provide a getSubstitution method
30: * that returns a String used by Util.substitute, we have opted to pass
31: * a StringBuffer argument from Util.substitute to which the Substitution
32: * must append data. The contract that an appendSubstitution
33: * implementation must abide by is that the appendBuffer may only be
34: * appended to. appendSubstitution() may not alter the appendBuffer in
35: * any way other than appending to it.
36: * <p>
37: * This method is invoked by Util.substitute every time it finds a match.
38: * After finding a match, Util.substitute appends to the appendBuffer
39: * all of the original input occuring between the end of the last match
40: * and the beginning of the current match. Then it invokes
41: * appendSubstitution(), passing the appendBuffer, current match, and
42: * other information as arguments. The substitutionCount keeps track
43: * of how many substitutions have been performed so far by an invocation
44: * of Util.substitute. Its value starts at 1 when the first substitution
45: * is found and appendSubstitution is invoked for the first time. It
46: * will NEVER be zero or a negative value.
47: * <p>
48: * @param appendBuffer The buffer containing the new string resulting
49: * from performing substitutions on the original input.
50: * @param match The current match causing a substitution to be made.
51: * @param substitutionCount The number of substitutions that have been
52: * performed so far by Util.substitute.
53: * @param originalInput The original input upon which the substitutions are
54: * being performed.
55: * @param matcher The PatternMatcher used to find the current match.
56: * @param pattern The Pattern used to find the current match.
57: */
58: public void appendSubstitution(StringBuffer appendBuffer,
59: MatchResult match, int substitutionCount,
60: PatternMatcherInput originalInput, PatternMatcher matcher,
61: Pattern pattern) {
62:
63: char c = match.toString().charAt(0);
64: appendBuffer.append(char2Entity(c));
65:
66: }//appendSubstitution
67:
68: /**
69: * Translates (a few) chars to their corresponding entity.
70: */
71: private String char2Entity(char c) {
72: switch (c) {
73: case AMPERSAND_CHAR:
74: return AMPERSAND_ENTITY;
75: case GT_CHAR:
76: return GT_ENTITY;
77: case LT_CHAR:
78: return LT_ENTITY;
79: case QUOT_CHAR:
80: return QUOT_ENTITY;
81: default:
82: return "" + c;
83: }
84:
85: }//char2Entity
86:
87: private static final char AMPERSAND_CHAR = '&';
88: private static final String AMPERSAND_ENTITY = "&";
89: private static final char GT_CHAR = '>';
90: private static final String GT_ENTITY = ">";
91: private static final char LT_CHAR = '<';
92: private static final String LT_ENTITY = "<";
93: private static final char QUOT_CHAR = '"';
94: private static final String QUOT_ENTITY = """;
95:
96: }//CharacterSubstitution
|