001: // @@
002: // @@
003: /*
004: * Wi.Ser Framework
005: *
006: * Version: 1.8.1, 20-September-2007
007: * Copyright (C) 2005 Dirk von der Weiden <dvdw@imail.de>
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library located in LGPL.txt in the
021: * license directory; if not, write to the
022: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
023: * Boston, MA 02111-1307, USA.
024: *
025: * If this agreement does not cover your requirements, please contact us
026: * via email to get detailed information about the commercial license
027: * or our service offerings!
028: *
029: */
030: // @@
031: package de.ug2t.kernel;
032:
033: import java.util.*;
034:
035: public final class KeStringTemplate {
036: private static final int INVAR = 0;
037: private static final int OUTVAR = 1;
038:
039: // @@
040:
041: private boolean pem_free = false;
042: private char pem_varSign = '$';
043: private boolean pem_inTr = false;
044: private int pem_size = 10;
045:
046: public int pcmf_getLength() {
047: return (this .pem_size);
048: };
049:
050: protected KeLinkedList pem_segments = new KeLinkedList();
051:
052: private int pem_state = OUTVAR;
053:
054: /**
055: *
056: */
057: public KeStringTemplate(KeStringTemplate xTpl) {
058: if (xTpl == null)
059: xTpl = new KeStringTemplate("");
060:
061: KeLinkedList.KeListIterator l_it = xTpl.pem_segments
062: .listIterator();
063: while (l_it.hasNext()) {
064: if (l_it.isNextVar())
065: this .pem_segments.pcmf_addVar(l_it.next());
066: else
067: this .pem_segments.pcmf_add(l_it.next());
068: }
069:
070: this .pem_size = xTpl.pcmf_getLength();
071:
072: return;
073: }
074:
075: public KeStringTemplate(KeStringTemplate xTpl, ArrayList xVars) {
076: if (xTpl == null)
077: xTpl = new KeStringTemplate("");
078:
079: KeLinkedList.KeListIterator l_it = xTpl.pem_segments
080: .listIterator();
081: while (l_it.hasNext()) {
082: if (l_it.isNextVar()) {
083: Object l_next = l_it.next();
084: this .pem_segments.pcmf_addVar(l_next);
085: xVars.add(l_next);
086: } else
087: this .pem_segments.pcmf_add(l_it.next());
088: }
089:
090: this .pem_size = xTpl.pcmf_getLength();
091:
092: return;
093: }
094:
095: public KeStringTemplate(String xString) {
096: if (xString == null)
097: xString = "";
098:
099: StringTokenizer l_tok = new StringTokenizer(xString, "\\{}",
100: true);
101: String l_string = null;
102: char l_sep = ' ';
103: while (l_tok.hasMoreTokens()) {
104: l_string = l_tok.nextToken();
105: if (l_string.length() == 1) {
106: l_sep = l_string.charAt(0);
107: if (l_sep == '\\' && this .pem_free == false) {
108: this .pem_free = true;
109: continue;
110: }
111: ;
112: if (!this .pem_free && l_sep == '{'
113: && this .pem_state == OUTVAR) {
114: this .pem_state = INVAR;
115: continue;
116: } else if (!this .pem_free && l_sep == '}'
117: && this .pem_state == INVAR) {
118: this .pem_state = OUTVAR;
119: continue;
120: }
121: } else if (this .pem_free) {
122: l_string = "\\" + l_string;
123: }
124:
125: if (l_string.charAt(0) == this .pem_varSign) {
126: this .pem_segments.pcmf_addVar(l_string);
127: this .pem_size += l_string.length();
128: } else {
129: this .pem_segments.pcmf_add(l_string);
130: this .pem_size += l_string.length();
131: }
132:
133: this .pem_free = false;
134: }
135: }
136:
137: public KeStringTemplate(String xString, ArrayList xVars) {
138: if (xString == null)
139: xString = "";
140:
141: StringTokenizer l_tok = new StringTokenizer(xString, "\\{}",
142: true);
143: String l_string = null;
144: char l_sep = ' ';
145: while (l_tok.hasMoreTokens()) {
146: l_string = l_tok.nextToken();
147: if (l_string.length() == 1) {
148: l_sep = l_string.charAt(0);
149: if (l_sep == '\\' && this .pem_free == false) {
150: this .pem_free = true;
151: continue;
152: }
153: ;
154: if (!this .pem_free && l_sep == '{'
155: && this .pem_state == OUTVAR) {
156: this .pem_state = INVAR;
157: continue;
158: } else if (!this .pem_free && l_sep == '}'
159: && this .pem_state == INVAR) {
160: this .pem_state = OUTVAR;
161: continue;
162: }
163: } else if (this .pem_free) {
164: l_string = "\\" + l_string;
165: }
166:
167: if (l_string.charAt(0) == this .pem_varSign) {
168: this .pem_segments.pcmf_addVar(l_string);
169: this .pem_size += l_string.length();
170: xVars.add(l_string);
171: } else {
172: this .pem_segments.pcmf_add(l_string);
173: this .pem_size += l_string.length();
174: }
175:
176: this .pem_free = false;
177: }
178: }
179:
180: public String toString() {
181: Iterator l_it = this .pem_segments.listIterator();
182: StringBuffer l_res = new StringBuffer(this .pem_size + 10);
183: while (l_it.hasNext()) {
184: l_res.append(l_it.next().toString());
185: }
186: return (l_res.toString());
187: }
188:
189: public char[] toArray() {
190: Iterator l_it = this .pem_segments.listIterator();
191: char[] l_res = new char[this .pem_size];
192: String l_str = null;
193: int l_idx = 0;
194: int l_len = 0;
195: while (l_it.hasNext()) {
196: l_str = l_it.next().toString();
197: l_len = l_str.length();
198: l_str.getChars(0, l_len, l_res, l_idx);
199: l_idx += l_len;
200: }
201: return (l_res);
202: }
203:
204: public void pcmf_beginTR() {
205: // @@
206: }
207:
208: public void pcmf_endTR(boolean xCommit) {
209: // @@
210: }
211:
212: public boolean pcmf_setRegVals() {
213: boolean l_ret = false;
214: KeLinkedList.KeListIterator l_it = this .pem_segments
215: .listIterator();
216: String l_org = null;
217: String l_new = null;
218: while (l_it.hasNext()) {
219: if (l_it.isNextVar() == false) {
220: l_it.next();
221: continue;
222: }
223: l_org = l_it.next().toString();
224: l_new = KeTools.pcmf_deRef(l_org.substring(1), null);
225: if (l_new != null) {
226: l_ret = true;
227: this .pem_size = this .pem_size - l_org.length()
228: + l_new.length();
229: l_it.set(l_new);
230: }
231: }
232: return (l_ret);
233: }
234:
235: public KeStringTemplate pcmf_stringSubst(String xOrg, String xNew) {
236: // @@
237:
238: KeLinkedList.KeListIterator l_it = this .pem_segments
239: .listIterator();
240: while (l_it.hasNext()) {
241: if (l_it.isNextVar()) {
242: if (xOrg.equals(l_it.next().toString())) {
243: if (xNew != null) {
244: this .pem_size = this .pem_size - xOrg.length()
245: + xNew.length();
246: l_it.set(xNew);
247: }
248: }
249: } else
250: l_it.next();
251: }
252:
253: return (this );
254: }
255:
256: public KeStringTemplate pcmf_stringdirectSubst(String xOrg,
257: String xNew) {
258: KeLinkedList.KeListIterator l_it = this .pem_segments
259: .listIterator();
260: while (l_it.hasNext()) {
261: if (l_it.isNextVar()) {
262: if (xOrg.equals(l_it.next().toString())) {
263: if (xNew != null) {
264: this .pem_size = this .pem_size - xOrg.length()
265: + xNew.length();
266: l_it.set(xNew);
267: }
268: ;
269: }
270: } else
271: l_it.next();
272: }
273: return (this );
274: }
275:
276: public KeStringTemplate pcmf_stringdirectSingleSubst(String xOrg,
277: String xNew) {
278: KeLinkedList.KeListIterator l_it = this .pem_segments
279: .listIterator();
280: while (l_it.hasNext()) {
281: if (l_it.isNextVar()) {
282: if (xOrg.equals(l_it.next().toString())) {
283: if (xNew != null) {
284: this .pem_size = this .pem_size - xOrg.length()
285: + xNew.length();
286: l_it.set(xNew);
287: }
288: ;
289: break;
290: }
291: } else
292: l_it.next();
293: }
294: return (this );
295: }
296:
297: public KeStringTemplate pcmf_stringdirectSubst(String xOrg,
298: String xNew, String xNewVar) {
299: KeLinkedList.KeListIterator l_it = this .pem_segments
300: .listIterator();
301: while (l_it.hasNext()) {
302:
303: if (l_it.isNextVar()) {
304: if (xOrg.equals(l_it.next().toString())) {
305: l_it.set(xNew);
306: l_it.addVar(xNewVar);
307: this .pem_size = this .pem_size + xNew.length();
308: }
309: } else
310: l_it.next();
311: }
312:
313: return (this );
314: }
315:
316: public KeStringTemplate pcmf_stringdirectSubst(String xOrg,
317: KeStringTemplate xNew) {
318: KeLinkedList.KeListIterator l_it = this .pem_segments
319: .listIterator();
320: KeLinkedList.KeListIterator l_it2 = null;
321: Object l_obj = null;
322:
323: while (l_it.hasNext()) {
324: if (l_it.isNextVar()) {
325: if (xOrg.equals(l_it.next().toString())) {
326: l_it2 = xNew.pem_segments.listIterator();
327: if (l_it2.hasNext()) {
328: l_obj = l_it2.next();
329: l_it.set(l_obj);
330: this .pem_size = this .pem_size - xOrg.length()
331: + l_obj.toString().length();
332: }
333:
334: while (l_it2.hasNext()) {
335: if (l_it2.isNextVar()) {
336: l_obj = l_it2.next();
337: l_it.addVar(l_obj);
338: this .pem_size = this .pem_size
339: + l_obj.toString().length();
340: } else {
341: l_obj = l_it2.next();
342: l_it.add(l_obj);
343: this .pem_size = this .pem_size
344: + l_obj.toString().length();
345: }
346: }
347: }
348: } else
349: l_it.next();
350: }
351: return (this );
352: }
353:
354: public KeStringTemplate pcmf_stringdirectSingleSubst(String xOrg,
355: KeStringTemplate xNew) {
356: KeLinkedList.KeListIterator l_it = this .pem_segments
357: .listIterator();
358: KeLinkedList.KeListIterator l_it2 = null;
359: Object l_obj = null;
360:
361: while (l_it.hasNext()) {
362: if (l_it.isNextVar()) {
363: if (xOrg.equals(l_it.next().toString())) {
364: l_it2 = xNew.pem_segments.listIterator();
365: if (l_it2.hasNext()) {
366: l_obj = l_it2.next();
367: l_it.set(l_obj);
368: this .pem_size = this .pem_size - xOrg.length()
369: + l_obj.toString().length();
370: }
371:
372: while (l_it2.hasNext()) {
373: if (l_it2.isNextVar()) {
374: l_obj = l_it2.next();
375: l_it.addVar(l_obj);
376: this .pem_size = this .pem_size
377: + l_obj.toString().length();
378: } else {
379: l_obj = l_it2.next();
380: l_it.add(l_obj);
381: this .pem_size = this .pem_size
382: + l_obj.toString().length();
383: }
384: }
385: break;
386: }
387: } else
388: l_it.next();
389: }
390: return (this );
391: }
392:
393: public KeStringTemplate pcmf_stringAddEnd(KeStringTemplate xNew) {
394: KeLinkedList.KeListIterator l_it = xNew.pem_segments
395: .listIterator();
396: while (l_it.hasNext()) {
397: if (l_it.isNextVar())
398: this .pem_segments.pcmf_addVar(l_it.next());
399: else
400: this .pem_segments.pcmf_add(l_it.next());
401: }
402:
403: this .pem_size += xNew.pcmf_getLength();
404:
405: return (this );
406: }
407:
408: public KeStringTemplate pcmf_stringAddFront(KeStringTemplate xNew) {
409: KeLinkedList.KeListIterator l_it = xNew.pem_segments
410: .listIterator();
411: KeLinkedList.KeListIterator l_it2 = this .pem_segments
412: .listIterator();
413:
414: while (l_it.hasNext()) {
415: if (l_it.isNextVar())
416: l_it2.addVar(l_it.next());
417: else
418: l_it2.add(l_it.next());
419: }
420:
421: this .pem_size += xNew.pcmf_getLength();
422:
423: return (this );
424: }
425:
426: public KeStringTemplate pcmf_stringAddEnd(String xNew) {
427: this .pem_segments.pcmf_add(xNew);
428: this .pem_size += xNew.length();
429: return (this );
430: }
431:
432: public KeStringTemplate pcmf_stringAddFront(String xNew) {
433: ListIterator l_it2 = this .pem_segments.listIterator();
434: l_it2.add(xNew);
435: this .pem_size += xNew.length();
436:
437: return (this );
438: }
439:
440: public KeStringTemplate pcmf_stringAddVarEnd(String xNew) {
441: this .pem_segments.pcmf_addVar(xNew);
442: this .pem_size += xNew.length();
443: return (this );
444: }
445:
446: public KeStringTemplate pcmf_stringAddVarFront(String xNew) {
447: KeLinkedList.KeListIterator l_it2 = this .pem_segments
448: .listIterator();
449: l_it2.addVar(xNew);
450: this .pem_size += xNew.length();
451:
452: return (this );
453: }
454:
455: public void pcmf_setVarSign(char xSign) {
456: this .pem_varSign = xSign;
457: };
458:
459: public String toRawString() {
460: KeLinkedList.KeListIterator l_it = this .pem_segments
461: .listIterator();
462: StringBuffer l_res = new StringBuffer(1024);
463: while (l_it.hasNext()) {
464: if (l_it.isNextVar()) {
465: l_res.append("{");
466: l_res.append(l_it.next().toString());
467: l_res.append("}");
468: } else
469: l_res.append(l_it.next().toString());
470: }
471: return (l_res.toString());
472: }
473:
474: public boolean pcmf_isInTr() {
475: return (this.pem_inTr);
476: }
477: }
|