001: /**
002: * $Id: TagSwapperHelper.java,v 1.6 2005/03/18 06:15:27 sc135395 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.desktop.template;
014:
015: import java.util.ArrayList;
016: import java.util.HashMap;
017: import java.io.File;
018:
019: import com.sun.portal.desktop.DesktopError;
020:
021: import com.sun.portal.desktop.context.DesktopAppContextThreadLocalizer;
022: import com.sun.portal.desktop.context.DesktopAppContext;
023:
024: public class TagSwapperHelper {
025: private static final String TAG = "tag:";
026: private static final int TAG_LENGTH = TAG.length();
027: private static final String URL = "url:";
028: private static final int URL_LENGTH = URL.length();
029: private static final String SURL = "surl:";
030: private static final int SURL_LENGTH = SURL.length();
031: private static final String DTURL = "dturl";
032: private static final int DTURL_LENGTH = DTURL.length();
033: private static final String DTAG = "dtag:";
034: private static final int DTAG_LENGTH = DTAG.length();
035: private static final String MSG = "msg:";
036: private static final int MSG_LENGTH = MSG.length();
037:
038: private static boolean tagEquals(StringBuffer b, String tag,
039: int start, int length) {
040: for (int i = 0; i < length; i++) {
041: if (tag.charAt(i) != b.charAt(start + i)) {
042: return false;
043: }
044: }
045:
046: return true;
047: }
048:
049: public static void parseTemplate(StringBuffer in,
050: ParsedTagArray result) {
051: if (in != null) {
052: int len = in.length();
053: if (len == 0) {
054: return;
055: } else {
056: char ch;
057: int tokenType = ParsedTagElement.STRING;
058: StringBuffer token = new StringBuffer(len);
059: ParsedTagElement tagElement = null;
060:
061: for (int i = 0; i < len; i++) {
062:
063: ch = in.charAt(i);
064: switch (ch) {
065:
066: case '[':
067: if ((i + DTAG_LENGTH < len)
068: && tagEquals(in, DTAG, i + 1,
069: DTAG_LENGTH)) {
070: if ((token.length() > 0)
071: && (tokenType == ParsedTagElement.STRING)) {
072: result.appendParsedElement(
073: ParsedTagElement.STRING, token
074: .toString());
075: }
076: tokenType = ParsedTagElement.DTAG;
077: token = new StringBuffer(len);
078: i = i + DTAG_LENGTH;
079:
080: } else if ((i + TAG_LENGTH < len)
081: && tagEquals(in, TAG, i + 1, TAG_LENGTH)) {
082: if ((token.length() > 0)
083: && (tokenType == ParsedTagElement.STRING)) {
084: result.appendParsedElement(
085: ParsedTagElement.STRING, token
086: .toString());
087: } else if ((token.length() > 0)
088: && (tokenType == ParsedTagElement.DTAG)) {
089: tagElement = new ParsedTagElement(
090: ParsedTagElement.TAG, token
091: .toString());
092: }
093: tokenType = ParsedTagElement.TAG;
094: token = new StringBuffer(len);
095: i = i + TAG_LENGTH;
096: } else if ((i + URL_LENGTH < len)
097: && tagEquals(in, URL, i + 1, URL_LENGTH)) {
098: if ((token.length() > 0)
099: && (tokenType == ParsedTagElement.STRING)) {
100: result.appendParsedElement(
101: ParsedTagElement.STRING, token
102: .toString());
103: } else if ((token.length() > 0)
104: && (tokenType == ParsedTagElement.DTAG)) {
105: tagElement = new ParsedTagElement(
106: ParsedTagElement.TAG, token
107: .toString());
108: }
109: tokenType = ParsedTagElement.URL;
110: token = new StringBuffer(len);
111: i = i + URL_LENGTH;
112: } else if ((i + SURL_LENGTH < len)
113: && tagEquals(in, SURL, i + 1,
114: SURL_LENGTH)) {
115: if ((token.length() > 0)
116: && (tokenType == ParsedTagElement.STRING)) {
117: result.appendParsedElement(
118: ParsedTagElement.STRING, token
119: .toString());
120: } else if ((token.length() > 0)
121: && (tokenType == ParsedTagElement.DTAG)) {
122: tagElement = new ParsedTagElement(
123: ParsedTagElement.TAG, token
124: .toString());
125: }
126: tokenType = ParsedTagElement.SURL;
127: token = new StringBuffer(len);
128: i = i + SURL_LENGTH;
129: } else if ((i + DTURL_LENGTH < len)
130: && tagEquals(in, DTURL, i + 1,
131: DTURL_LENGTH)) {
132: if ((token.length() > 0)
133: && (tokenType == ParsedTagElement.STRING)) {
134: result.appendParsedElement(
135: ParsedTagElement.STRING, token
136: .toString());
137: } else if ((token.length() > 0)
138: && (tokenType == ParsedTagElement.DTAG)) {
139: tagElement = new ParsedTagElement(
140: ParsedTagElement.TAG, token
141: .toString());
142: }
143: tokenType = ParsedTagElement.DTURL;
144: token = new StringBuffer(len);
145: i = i + DTURL_LENGTH;
146: } else if ((i + MSG_LENGTH < len)
147: && tagEquals(in, MSG, i + 1, MSG_LENGTH)) {
148: if ((token.length() > 0)
149: && (tokenType == ParsedTagElement.STRING)) {
150: result.appendParsedElement(
151: ParsedTagElement.STRING, token
152: .toString());
153: } else if ((token.length() > 0)
154: && (tokenType == ParsedTagElement.DTAG)) {
155: tagElement = new ParsedTagElement(
156: ParsedTagElement.TAG, token
157: .toString());
158: }
159: tokenType = ParsedTagElement.MSG;
160: token = new StringBuffer(len);
161: i = i + MSG_LENGTH;
162: } else {
163: token.append(ch);
164: }
165: break;
166:
167: case ']':
168: if (tokenType == ParsedTagElement.TAG) {
169: if ((token.length() > 0)
170: && (tokenType == ParsedTagElement.TAG)) {
171: if (tagElement != null) {
172: ParsedTagElement pElement = new ParsedTagElement(
173: ParsedTagElement.TAG, token
174: .toString());
175: tagElement.setDefault(pElement);
176: result
177: .appendParsedElement(tagElement);
178: tagElement = null;
179: } else {
180: result.appendParsedElement(
181: ParsedTagElement.TAG, token
182: .toString());
183: }
184: }
185: tokenType = ParsedTagElement.STRING;
186: token = new StringBuffer(len);
187: } else if (tokenType == ParsedTagElement.URL) {
188: if ((token.length() > 0)
189: && (tokenType == ParsedTagElement.URL)) {
190: if (tagElement != null) {
191: ParsedTagElement pElement = new ParsedTagElement(
192: ParsedTagElement.URL, token
193: .toString());
194: tagElement.setDefault(pElement);
195: result
196: .appendParsedElement(tagElement);
197: tagElement = null;
198: } else {
199: result.appendParsedElement(
200: ParsedTagElement.URL, token
201: .toString());
202: }
203: }
204: tokenType = ParsedTagElement.STRING;
205: token = new StringBuffer(len);
206: } else if (tokenType == ParsedTagElement.SURL) {
207: if ((token.length() > 0)
208: && (tokenType == ParsedTagElement.SURL)) {
209: if (tagElement != null) {
210: ParsedTagElement pElement = new ParsedTagElement(
211: ParsedTagElement.SURL,
212: token.toString());
213: tagElement.setDefault(pElement);
214: result
215: .appendParsedElement(tagElement);
216: tagElement = null;
217: } else {
218: result.appendParsedElement(
219: ParsedTagElement.SURL,
220: token.toString());
221: }
222:
223: }
224: tokenType = ParsedTagElement.STRING;
225: token = new StringBuffer(len);
226: } else if (tokenType == ParsedTagElement.DTURL) {
227: if (tagElement != null) {
228: ParsedTagElement pElement = new ParsedTagElement(
229: ParsedTagElement.DTURL, token
230: .toString());
231: tagElement.setDefault(pElement);
232: result.appendParsedElement(tagElement);
233: tagElement = null;
234: } else {
235: result.appendParsedElement(
236: ParsedTagElement.DTURL, null);
237: }
238: tokenType = ParsedTagElement.STRING;
239: token = new StringBuffer(len);
240: } else if (tokenType == ParsedTagElement.MSG) {
241: if ((token.length() > 0)
242: && (tokenType == ParsedTagElement.MSG)) {
243: if (tagElement != null) {
244: ParsedTagElement pElement = new ParsedTagElement(
245: ParsedTagElement.MSG, token
246: .toString());
247: tagElement.setDefault(pElement);
248: result
249: .appendParsedElement(tagElement);
250: tagElement = null;
251: } else {
252: result.appendParsedElement(
253: ParsedTagElement.MSG, token
254: .toString());
255: }
256: }
257: tokenType = ParsedTagElement.STRING;
258: token = new StringBuffer(len);
259:
260: } else {
261: token.append(ch);
262: }
263: break;
264:
265: default:
266: token.append(ch);
267: }
268: }
269: // Everything not finish up by now will be treated as STRING
270: if (token.length() > 0) {
271: result.appendParsedElement(ParsedTagElement.STRING,
272: token.toString());
273: }
274: }
275: }
276: }
277: }
|