001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: Expression.java 3513 2006-12-08 05:54:11Z yling $
023: */
024: package com.bostechcorp.cbesb.common.trn.compiler;
025:
026: import java.util.Vector;
027:
028: public class Expression {
029:
030: public class ExpVar {
031: private String name;
032: private short index;
033: private int startPos = -1;
034:
035: public ExpVar(String name, int startPos) {
036: super ();
037: this .name = name;
038: this .startPos = startPos;
039: String sIndex = name.substring(1, name.length());
040: index = (short) Integer.parseInt(sIndex);
041:
042: }
043:
044: public short getIndex() {
045: return index;
046: }
047:
048: public void setIndex(short index) {
049: this .index = index;
050: }
051:
052: public String getName() {
053: return name;
054: }
055:
056: public void setName(String name) {
057: this .name = name;
058: }
059:
060: public int getStartPos() {
061: return startPos;
062: }
063:
064: public void setStartPos(int startPos) {
065: this .startPos = startPos;
066: }
067:
068: }
069:
070: private String strExp;
071: private Vector<ExpVar> varList = new Vector<ExpVar>();
072: private short maxIndex = 0;
073:
074: public String getStrExp() {
075: return strExp;
076: }
077:
078: public void setStrExp(String strExp) {
079: this .strExp = strExp;
080: }
081:
082: public Vector getVarList() {
083: return varList;
084: }
085:
086: public short getMaxIndex() {
087: return maxIndex;
088: }
089:
090: public void setMaxIndex(short maxIndex) {
091: this .maxIndex = maxIndex;
092: }
093:
094: public Expression(String strExp) {
095: super ();
096: this .strExp = strExp;
097: }
098:
099: /**
100: * build the exp and construct Vector<ExpVar> varList
101: *
102: */
103: public void build() {
104: int len = strExp.length();
105: int pos = strExp.indexOf("?");
106: int bPos = pos;
107: while (pos >= 0 && pos < len) {
108: if (pos == len - 1) {
109: String var = strExp.substring(bPos, pos + 1);
110: ExpVar expVar = new ExpVar(var, bPos);
111: if (maxIndex < expVar.index)
112: maxIndex = expVar.index;
113: varList.add(expVar);
114: break;
115: }
116: char ch = strExp.charAt(++pos);
117: if (ch >= '0' && ch <= '9') {
118: continue;
119: } else {
120: String var = strExp.substring(bPos, pos);
121: ExpVar expVar = new ExpVar(var, bPos);
122: if (maxIndex < expVar.index)
123: maxIndex = expVar.index;
124:
125: varList.add(expVar);
126: pos = strExp.indexOf("?", pos);
127: bPos = pos;
128: }
129:
130: }
131:
132: }
133:
134: public String getJavaExp(String[] varArray) {
135: String javaExp = "";
136: int lastPos = 0;
137: for (int i = 0; i < varList.size(); i++) {
138: ExpVar var = (ExpVar) varList.get(i);
139: javaExp = javaExp
140: + this .strExp.substring(lastPos, var.startPos)
141: + varArray[var.index - 1];
142: lastPos = var.startPos + var.name.length();
143: }
144: javaExp = javaExp
145: + this.strExp.substring(lastPos, this.strExp.length());
146:
147: return javaExp;
148:
149: }
150:
151: }
|