001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: EjbqlOrderByVisitor.java 5445 2004-09-17 08:25:02Z joaninh $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.lib;
025:
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Stack;
030:
031: import org.objectweb.jonas_ejb.deployment.ejbql.ASTEJBQL;
032: import org.objectweb.jonas_ejb.deployment.ejbql.ASTCmpPathExpression;
033: import org.objectweb.jonas_ejb.deployment.ejbql.ASTOrderByClause;
034: import org.objectweb.jonas_ejb.deployment.ejbql.ASTOrderByItem;
035: import org.objectweb.jonas_ejb.deployment.ejbql.ASTPath;
036: import org.objectweb.jonas_ejb.deployment.ejbql.SimpleNode;
037:
038: import org.objectweb.medor.api.Field;
039: import org.objectweb.medor.query.api.OrderField;
040: import org.objectweb.medor.query.api.QueryTreeField;
041: import org.objectweb.medor.query.lib.BasicOrderField;
042:
043: /**
044: * Implementation of a visitor that creates a list of org.objectweb.medor.query.api.OrderField
045: * to a given ORDER BY clause.
046: * Created on Aug 27, 2003
047: * @author Helene Joanin
048: */
049: public class EjbqlOrderByVisitor extends EjbqlAbstractVisitor {
050:
051: Map fields;
052: ArrayList orderFields;
053:
054: /**
055: * Constructor
056: * @param ejbql root of the lexical tree of the query
057: * @param fields Map with (name,QueryTreeField) pairs of all the variables appear in the query
058: */
059: public EjbqlOrderByVisitor(ASTEJBQL ejbql, Map _fields)
060: throws Exception {
061: orderFields = new ArrayList();
062: fields = _fields;
063: visit(ejbql);
064: }
065:
066: /**
067: * get the that was built from visiting the lexical tree
068: */
069: public OrderField[] getOrderFields() {
070: OrderField[] ofs = new OrderField[orderFields.size()];
071: Iterator itr = orderFields.iterator();
072: for (int i = 0; itr.hasNext(); i++) {
073: ofs[i] = (OrderField) itr.next();
074: }
075: return ofs;
076: }
077:
078: /**
079: * Visit child node.
080: * ORDER BY OrderByItem() (, OrderByItem() )*
081: */
082: public Object visit(ASTOrderByClause node, Object data) {
083: visit((SimpleNode) node, data);
084: return null;
085: }
086:
087: /**
088: * Visit child node.
089: */
090: public Object visit(ASTOrderByItem node, Object data) {
091: visit((SimpleNode) node, data);
092: QueryTreeField qtf = (QueryTreeField) ((Stack) data).pop();
093: orderFields.add(new BasicOrderField(qtf, !node.asc));
094: return null;
095: }
096:
097: /**
098: * Push corresponding MedorField to the stack.<br>
099: * cmp_path_expression ::= path
100: * was in initial BNF
101: * cmp_path_expression ::= {identification_variable | single_valued_cmr_path_expression}.cmp_field
102: */
103: public Object visit(ASTCmpPathExpression node, Object data) {
104: visit((SimpleNode) node, data);
105: try {
106: String path = (String) ((ASTPath) ((Stack) data).pop()).value;
107: // FIXME check type for cmp field
108: ((Stack) data).push((Field) fields.get(path));
109: } catch (Exception e) {
110: throw new VisitorException(e);
111: }
112: return null;
113: }
114:
115: /**
116: * Push the Node to the stack
117: */
118: public Object visit(ASTPath node, Object data) {
119: ((Stack) data).push(node);
120: return null;
121: }
122:
123: }
|