KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > core > util > ObjectPath


1 package com.genimen.djeneric.tools.generator.core.util;
2
3 import java.util.StringTokenizer JavaDoc;
4
5 import com.genimen.djeneric.repository.DjAssociation;
6 import com.genimen.djeneric.repository.DjExtent;
7 import com.genimen.djeneric.repository.DjList;
8 import com.genimen.djeneric.repository.DjObject;
9 import com.genimen.djeneric.repository.exceptions.DjenericException;
10 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
11
12 public class ObjectPath
13 {
14   public final static Object JavaDoc evaluateProperty(String JavaDoc path, ParseContext context) throws EvaluationException
15   {
16     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, ".");
17
18     String JavaDoc curId = st.nextToken();
19     Object JavaDoc obj = context.getObjectStack().lookup(curId);
20     if (obj == null)
21     {
22       obj = context.getConstStack().lookup(curId);
23       if (obj == null)
24       {
25         try
26         {
27           DjExtent extent = context.getManager().getExtent(curId);
28           return context.getSession().getObjects(extent);
29         }
30         catch (ObjectNotDefinedException onde)
31         {
32           // ingore; throw a more descriptive exception below
33
}
34         catch (DjenericException de)
35         {
36           throw new EvaluationException(de.getMessage());
37         }
38
39         throw new EvaluationException("Identifier " + curId + " in path " + path + " not defined");
40       }
41       return obj;
42     }
43
44     try
45     {
46       while (st.hasMoreElements())
47       {
48         if (obj instanceof DjList) throw new EvaluationException("Identifier " + curId
49                                                                  + " is a set; can not be use here");
50         if (!(obj instanceof DjObject)) throw new EvaluationException("Identifier " + curId + " is not a model object");
51         DjObject djo = (DjObject) obj;
52         curId = st.nextToken();
53         try
54         {
55           obj = djo.get(curId);
56         }
57         catch (ObjectNotDefinedException onde)
58         {
59           // Is this the last property of the chain? If not rethrow; it's definitely an error
60
if (st.hasMoreElements()) throw onde;
61           // It is the last; we might have found a relation name then:
62
try
63           {
64             DjAssociation assoc = djo.getDetailAssociationByName(curId);
65             // Bingo; return the result now:
66
return assoc.getObjects();
67           }
68           catch (ObjectNotDefinedException discard)
69           {
70             throw onde;
71           }
72         }
73         if (obj == null) return null;
74       }
75       return obj;
76     }
77     catch (DjenericException onde)
78     {
79       throw new EvaluationException(onde.getMessage());
80     }
81   }
82 }
Popular Tags