| 1 | /** |
| 2 | * Copyright (C) 2006 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.parser; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import hu.netmind.beankeeper.common.StoreException; |
| 23 | import org.apache.log4j.Logger; |
| 24 | |
| 25 | /** |
| 26 | * An expression is basically a List, which contains the terms, operators |
| 27 | * and subexpression in order. It can contain these objects (filled by |
| 28 | * parser): |
| 29 | * <ul> |
| 30 | * <li><strong>Expression</strong>: This indicates a sub-expression.</li> |
| 31 | * <li><strong>String</strong>: This is an operator (unary or binary).</li> |
| 32 | * <li><strong>ConstantTerm</strong>: Indicates a constant with the given value.</li> |
| 33 | * <li><strong>ReferenceTerm</strong>: A reference to a table's attribute.</li> |
| 34 | * </ul> |
| 35 | * @author Brautigam Robert |
| 36 | * @version Revision: $Revision$ |
| 37 | */ |
| 38 | public class Expression extends ArrayList |
| 39 | { |
| 40 | private static Logger logger = Logger.getLogger(Expression.class); |
| 41 | |
| 42 | private boolean markedBlock; |
| 43 | |
| 44 | public Expression() |
| 45 | { |
| 46 | super(); |
| 47 | } |
| 48 | |
| 49 | public Expression(Expression expr) |
| 50 | { |
| 51 | super(expr); |
| 52 | setMarkedBlock(expr.isMarkedBlock()); |
| 53 | } |
| 54 | |
| 55 | public Expression deepCopy() |
| 56 | { |
| 57 | // Copy object |
| 58 | Expression result = new Expression(); |
| 59 | result.setMarkedBlock(isMarkedBlock()); |
| 60 | // Recursive copy of content |
| 61 | for ( int i=0; i<size(); i++ ) |
| 62 | { |
| 63 | Object value = get(i); |
| 64 | if ( value instanceof Expression ) |
| 65 | result.add( ((Expression) value).deepCopy() ); |
| 66 | else |
| 67 | result.add( value ); |
| 68 | } |
| 69 | // Return deep copy |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | public void replace(TableTerm oldTerm, TableTerm newTerm) |
| 74 | { |
| 75 | replace(oldTerm,newTerm,null); |
| 76 | } |
| 77 | |
| 78 | public void replace(TableTerm oldTerm, TableTerm newTerm,String newColumnName) |
| 79 | { |
| 80 | for ( int i=0; i<size(); i++ ) |
| 81 | { |
| 82 | Object value = get(i); |
| 83 | if ( value instanceof Expression ) |
| 84 | { |
| 85 | ((Expression) value).replace(oldTerm,newTerm,newColumnName); |
| 86 | } else if ( (value instanceof ReferenceTerm) && (value.equals(oldTerm)) ) { |
| 87 | ReferenceTerm newReferenceTerm = new ReferenceTerm( (ReferenceTerm) value); |
| 88 | newReferenceTerm.setTableName(newTerm.getTableName()); |
| 89 | newReferenceTerm.setAlias(newTerm.getAlias()); |
| 90 | if ( newColumnName != null ) |
| 91 | newReferenceTerm.setColumnName(newColumnName); |
| 92 | set(i,newReferenceTerm); |
| 93 | if ( logger.isDebugEnabled() ) |
| 94 | logger.debug("replaced: "+value+" with "+newTerm+"("+newColumnName+"): "+newReferenceTerm); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public TableTerm getTableTerm(String tableName) |
| 100 | { |
| 101 | TableTerm result = null; |
| 102 | for ( int o=0; (o<size()) && (result==null); o++ ) |
| 103 | { |
| 104 | Object term = get(o); |
| 105 | if ( (term instanceof TableTerm) && |
| 106 | (tableName.equals(((TableTerm)term).getTableName())) ) |
| 107 | result = (TableTerm) term; |
| 108 | if ( term instanceof Expression ) |
| 109 | result = ((Expression)term).getTableTerm(tableName); |
| 110 | } |
| 111 | return result; |
| 112 | } |
| 113 | |
| 114 | public boolean isMarkedBlock() |
| 115 | { |
| 116 | return markedBlock; |
| 117 | } |
| 118 | public void setMarkedBlock(boolean markedBlock) |
| 119 | { |
| 120 | this.markedBlock=markedBlock; |
| 121 | } |
| 122 | |
| 123 | public String toString() |
| 124 | { |
| 125 | StringBuffer result = new StringBuffer("[Expr:"); |
| 126 | for ( int i=0; i<size(); i++ ) |
| 127 | result.append(get(i).toString()); |
| 128 | result.append("]"); |
| 129 | return result.toString(); |
| 130 | } |
| 131 | |
| 132 | } |
| 133 | |
| 134 | |