KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > oql > core > util > ObjectPath


1 package com.genimen.djeneric.repository.oql.core.util;
2
3 import java.util.StringTokenizer JavaDoc;
4
5 import com.genimen.djeneric.repository.DjAssociation;
6 import com.genimen.djeneric.repository.DjValueObject;
7 import com.genimen.djeneric.repository.exceptions.DjenericException;
8 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
9 import com.genimen.djeneric.repository.oql.core.MatchException;
10 import com.genimen.djeneric.repository.oql.core.SimpleNode;
11 import com.genimen.djeneric.repository.oql.core.nodes.MatchingContext;
12
13 public class ObjectPath
14 {
15   public final static Object JavaDoc evaluateProperty(String JavaDoc path, MatchingContext context, SimpleNode contextNode)
16       throws MatchException
17   {
18     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, ".");
19
20     String JavaDoc curId = st.nextToken();
21     Object JavaDoc obj;
22     try
23     {
24       obj = context.getObject().get(curId);
25     }
26     catch (DjenericException e)
27     {
28       throw new MatchException(e.getMessage(), contextNode.beginLine, contextNode.beginColumn);
29     }
30
31     return evaluatePath(contextNode, st, curId, obj);
32   }
33
34   private static Object JavaDoc evaluatePath(SimpleNode contextNode, StringTokenizer JavaDoc st, String JavaDoc curId, Object JavaDoc obj)
35       throws MatchException
36   {
37     try
38     {
39       while (st.hasMoreElements())
40       {
41         if (!(obj instanceof DjValueObject)) throw new MatchException("Identifier " + curId + " is not a model object",
42             contextNode.beginLine, contextNode.beginColumn);
43         DjValueObject djo = (DjValueObject) obj;
44         curId = st.nextToken();
45         try
46         {
47           obj = djo.get(curId);
48         }
49         catch (ObjectNotDefinedException onde)
50         {
51           // Is this the last property of the chain? If not rethrow; it's definitely an error
52
if (st.hasMoreElements()) throw onde;
53           // It is the last; we might have found a relation name then:
54
try
55           {
56             DjAssociation assoc = djo.getDetailAssociationByName(curId);
57             // Bingo; return the result now:
58
return assoc.getObjects();
59           }
60           catch (ObjectNotDefinedException discard)
61           {
62             throw onde;
63           }
64         }
65         if (obj == null) return null;
66       }
67       return obj;
68     }
69     catch (DjenericException onde)
70     {
71       throw new MatchException(onde.getMessage(), contextNode.beginLine, contextNode.beginColumn);
72     }
73   }
74 }
Popular Tags