01: /**
02: * $Id: ProducerRewriter.java,v 1.4 2005/02/08 06:41:17 sm140826 Exp $
03: * Copyright 2003 Sun Microsystems, Inc. All
04: * rights reserved. Use of this product is subject
05: * to license terms. Federal Acquisitions:
06: * Commercial Software -- Government Users
07: * Subject to Standard License Terms and
08: * Conditions.
09: *
10: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
11: * are trademarks or registered trademarks of Sun Microsystems,
12: * Inc. in the United States and other countries.
13: */package com.sun.portal.wsrp.producer.markup.impl;
14:
15: import com.sun.portal.wsrp.common.Boyer;
16: import com.iplanet.am.util.Debug;
17: import java.util.Map;
18:
19: public class ProducerRewriter {
20:
21: private static String TOKEN_START = "{wsrp-";
22: private static String TOKEN_END = "}";
23:
24: private Debug _debug = null;
25:
26: public ProducerRewriter(Debug debug) {
27: }
28:
29: public String rewrite(String url, Map paramMap) {
30:
31: int currentStart = 0;
32: int lastCurrentStart = 0;
33: int beginTokenPosition = 0;
34: StringBuffer buffer = new StringBuffer(url.length() + 100);
35:
36: while (currentStart < url.length()) {
37: beginTokenPosition = searchToken(url, TOKEN_START,
38: currentStart);
39:
40: //
41: // Break out if no more token is found.
42: //
43: if (beginTokenPosition == -1) {
44: // no more tokens
45: buffer.append(url.substring(currentStart));
46: break;
47: }
48:
49: String token = getFullToken(url, beginTokenPosition);
50: String type = getTokenType(token);
51: String value = (String) paramMap.get(type);
52: buffer.append(url.substring(currentStart,
53: beginTokenPosition));
54: if (value != null) {
55: buffer.append(value);
56: }
57: lastCurrentStart = currentStart;
58: currentStart = beginTokenPosition + token.length();
59: }
60: return buffer.toString();
61: }
62:
63: /**
64: * Get the full token given the start position of the token. If no well-
65: * formed token is found, a null will be returned.
66: **/
67: private static String getFullToken(String url, int beginPosition) {
68: int endPosition = url.indexOf(TOKEN_END, beginPosition);
69: if (endPosition == -1) {
70: return null;
71: } else {
72: return url.substring(beginPosition, endPosition + 1);
73: }
74: }
75:
76: /**
77: * Get the token type given a full token.
78: **/
79: private static String getTokenType(String fullToken) {
80: return fullToken.substring(1, fullToken.length() - 1);
81: }
82:
83: /**
84: * Calls Boyer algorithm to look for a string token.
85: *
86: * @param content
87: * @param pattern
88: * @param start
89: */
90: private int searchToken(String content, String pattern, int start) {
91: return Boyer.indexOf(content, pattern, start);
92: }
93:
94: }
|