001: /**
002: * Copyright (C) 2006 NetMind Consulting Bt.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 3 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package hu.netmind.persistence.parser;
018:
019: import java.util.LinkedList;
020: import hu.netmind.persistence.StoreException;
021:
022: /**
023: * An expression is basically a List, which contains the terms, operators
024: * and subexpression in order. It can contain these objects (filled by
025: * parser):
026: * <ul>
027: * <li><strong>Expression</strong>: This indicates a sub-expression.</li>
028: * <li><strong>String</strong>: This is an operator (unary or binary).</li>
029: * <li><strong>ConstantTerm</strong>: Indicates a constant with the given value.</li>
030: * <li><strong>ReferenceTerm</strong>: A reference to a table's attribute.</li>
031: * </ul>
032: * @author Brautigam Robert
033: * @version Revision: $Revision$
034: */
035: public class Expression extends LinkedList {
036: private boolean markedBlock;
037: private TableTerm leftTableTerm;
038:
039: public Expression() {
040: super ();
041: }
042:
043: public Expression(Expression expr) {
044: super (expr);
045: setMarkedBlock(expr.isMarkedBlock());
046: setLeftTableTerm(expr.getLeftTableTerm());
047: }
048:
049: public Expression deepCopy() {
050: // Copy object
051: Expression result = new Expression();
052: result.setMarkedBlock(isMarkedBlock());
053: result.setLeftTableTerm(getLeftTableTerm());
054: // Recursive copy of content
055: for (int i = 0; i < size(); i++) {
056: Object value = get(i);
057: if (value instanceof Expression)
058: result.add(((Expression) value).deepCopy());
059: else
060: result.add(value);
061: }
062: // Return deep copy
063: return result;
064: }
065:
066: public void removeLeftTableTerm(TableTerm leftTerm) {
067: for (int i = 0; i < size(); i++) {
068: Object value = get(i);
069: if (value instanceof Expression) {
070: Expression expr = (Expression) value;
071: if ((expr.getLeftTableTerm() != null)
072: && (expr.getLeftTableTerm().equals(leftTerm))) {
073: // It's a left table term, so remove
074: remove(i);
075: // Remove operator before
076: i--;
077: if ((i < 0) || (!"and".equals(get(i))))
078: throw new StoreException(
079: "tried to remove left term: "
080: + leftTerm
081: + ", but it was not as expected.");
082: remove(i);
083: i--;
084: } else {
085: // Other expression
086: expr.removeLeftTableTerm(leftTerm);
087: }
088: }
089: }
090: }
091:
092: public void replace(TableTerm oldTerm, TableTerm newTerm) {
093: for (int i = 0; i < size(); i++) {
094: Object value = get(i);
095: if (value instanceof Expression) {
096: ((Expression) value).replace(oldTerm, newTerm);
097: } else if ((value instanceof ReferenceTerm)
098: && (value.equals(oldTerm))) {
099: set(i, new ReferenceTerm(newTerm,
100: ((ReferenceTerm) value).getColumnName()));
101: }
102: }
103: }
104:
105: public TableTerm getTableTerm(String tableName) {
106: TableTerm result = null;
107: for (int o = 0; (o < size()) && (result == null); o++) {
108: Object term = get(o);
109: if ((term instanceof TableTerm)
110: && (tableName.equals(((TableTerm) term)
111: .getTableName())))
112: result = (TableTerm) term;
113: if (term instanceof Expression)
114: result = ((Expression) term).getTableTerm(tableName);
115: }
116: return result;
117: }
118:
119: public boolean isMarkedBlock() {
120: return markedBlock;
121: }
122:
123: public void setMarkedBlock(boolean markedBlock) {
124: this .markedBlock = markedBlock;
125: }
126:
127: public String toString() {
128: StringBuffer result = new StringBuffer("[Expr:");
129: for (int i = 0; i < size(); i++)
130: result.append(get(i).toString());
131: result.append("]");
132: return result.toString();
133: }
134:
135: public TableTerm getLeftTableTerm() {
136: return leftTableTerm;
137: }
138:
139: public void setLeftTableTerm(TableTerm leftTableTerm) {
140: this.leftTableTerm = leftTableTerm;
141: }
142:
143: }
|