KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > ejb > query > PathNode


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.ejb.query;
13
14 import com.versant.core.metadata.FieldMetaData;
15 import com.versant.core.metadata.ClassMetaData;
16
17 /**
18  * Dot separated list of identifiers.
19  */

20 public class PathNode extends Node {
21
22     public static final int WHERE = 1;
23     public static final int SELECT = 2;
24     public static final int GROUP_BY = 3;
25     public static final int ORDER_BY = 4;
26     public static final int AGGREGATE = 5;
27     public static final int CONSTRUCTOR = 6;
28     public static final int JOIN = 7;
29     public static final int COLLECTION_MEMBER = 8;
30
31     private String JavaDoc[] list = new String JavaDoc[4];
32     private int size;
33     private int parentType;
34
35     private NavBase navBase;
36     private FieldMetaData fmd;
37
38     /**
39      * Create with given parent type (SELECT, GROUP_BY etc).
40      */

41     public PathNode(int parentType) {
42         this.parentType = parentType;
43     }
44
45     /**
46      * Is this node under a SELECT, GROUP_BY etc.
47      */

48     public int getParentType() {
49         return parentType;
50     }
51
52     public void add(String JavaDoc identifier) {
53         if (size == list.length) {
54             String JavaDoc[] a = new String JavaDoc[list.length + 4];
55             System.arraycopy(list, 0, a, 0, list.length);
56             list = a;
57         }
58         list[size++] = identifier;
59     }
60
61     public String JavaDoc get(int i) {
62         if (i >= size) {
63             throw new ArrayIndexOutOfBoundsException JavaDoc(i + " >= " + size);
64         }
65         return list[i];
66     }
67
68     public int size() {
69         return size;
70     }
71
72     public Object JavaDoc arrive(NodeVisitor v, Object JavaDoc msg) {
73         return v.arrivePathNode(this, msg);
74     }
75
76     public String JavaDoc getParentTypeString() {
77         switch (parentType) {
78             case WHERE: return "WHERE";
79             case SELECT: return "SELECT";
80             case GROUP_BY: return "GROUP_BY";
81             case ORDER_BY: return "ORDER_BY";
82             case AGGREGATE: return "AGGREGATE";
83             case CONSTRUCTOR: return "CONSTRUCTOR";
84             case JOIN: return "JOIN";
85             case COLLECTION_MEMBER: return "COLLECTION_MEMBER";
86         }
87         return "<? parentType " + parentType + " ?>";
88     }
89
90     public String JavaDoc toStringImp() {
91         if (size == 0) {
92             return "<? no identifiers in PathExpression?>";
93         }
94         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
95         s.append(list[0]);
96         for (int i = 1; i < size; i++) {
97             s.append('.');
98             s.append(list[i]);
99         }
100         if (fmd != null) {
101             s.append('%');
102             s.append(fmd.getQName());
103         } else if (navBase != null) {
104             s.append('%');
105             s.append(navBase.getNavClassMetaData().qname);
106         }
107         return s.toString();
108     }
109
110     /**
111      * Get the identification variable or last field navigated to get to our
112      * field. If getFmd() is null then we have no simple field and this path is
113      * returning the identification variable or object field on its own.
114      */

115     public NavBase getNavBase() {
116         return navBase;
117     }
118
119     /**
120      * If this path ends in a field then this is it. It will be a field
121      * of getNavBase().getNavClassMetaData().
122      */

123     public FieldMetaData getFmd() {
124         return fmd;
125     }
126
127     public void resolve(ResolveContext rc) {
128         if (size == 1) { // identification var on its own
129
String JavaDoc name = get(0);
130             navBase = rc.checkIdVarExists(name, this);
131         } else { // field value
132
navBase = rc.resolveJoinPath(this, false, false, size - 1);
133             ClassMetaData cmd = navBase.getNavClassMetaData();
134             String JavaDoc name = get(size - 1);
135             fmd = cmd.getFieldMetaData(name);
136             if (fmd == null) {
137                 throw rc.createUserException("No such field '" + name +
138                         "' on " + cmd.qname, this);
139             }
140         }
141     }
142
143 }
144
145
Popular Tags