KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > stringreplacement > AbstractReplacementVariableReplacer


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.core.stringreplacement;
21
22 import java.util.regex.Matcher JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import com.sslexplorer.boot.Replacer;
29 import com.sslexplorer.boot.Util;
30
31 public abstract class AbstractReplacementVariableReplacer implements Replacer {
32     
33     public final static Log log = LogFactory.getLog(AbstractReplacementVariableReplacer.class);
34
35     public abstract String JavaDoc processReplacementVariable(Pattern JavaDoc pattern, Matcher JavaDoc matcher, String JavaDoc replacementPattern, String JavaDoc type, String JavaDoc key) throws Exception JavaDoc;
36
37     public String JavaDoc getReplacement(Pattern JavaDoc pattern, Matcher JavaDoc matcher, String JavaDoc replacementPattern) {
38         String JavaDoc match = matcher.group();
39         String JavaDoc key = match.substring(2, match.length() - 1);
40         try {
41             // Extract the type and key
42
int idx = key.indexOf(":");
43             if (idx == -1) {
44                 throw new Exception JavaDoc("String replacement pattern is in incorrect format for " + key
45                     + ". Must be <TYPE>:<key>");
46             }
47             String JavaDoc type = key.substring(0, idx);
48             key = key.substring(idx + 1);
49             if (log.isDebugEnabled())
50                 log.debug("Found replacement variable " + type + ":" + key);
51             
52             // Determine the encoding the use (if any). Default to 'p' (plain)
53
char enc = 'p';
54             if(type.startsWith("[")) {
55                 enc = type.charAt(1);
56                 type = type.substring(3);
57             }
58             String JavaDoc val = processReplacementVariable(pattern, matcher, replacementPattern, type, key);
59             if(val != null) {
60                 switch(enc) {
61                     case 'u':
62                         // URL encoded
63
val = Util.urlEncode(val);
64                         break;
65                     case 'p':
66                         // Plain
67
break;
68                     default:
69                         throw new Exception JavaDoc("Invalid encoding '" + enc + "'");
70                 }
71             }
72             return val;
73         } catch (Exception JavaDoc e) {
74             log.error("A replacement failed for " + key + ".", e);
75         }
76         return null;
77     }
78     
79 }
80
Popular Tags