KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > lib > JormPathHelper


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23 package org.objectweb.jorm.lib;
24
25 import org.objectweb.jorm.metainfo.api.GenClassRef;
26 import org.objectweb.jorm.metainfo.api.Class;
27 import org.objectweb.jorm.metainfo.api.ClassRef;
28 import org.objectweb.jorm.metainfo.api.MetaObject;
29 import org.objectweb.jorm.naming.api.PNameCoder;
30 import org.objectweb.jorm.api.PMapper;
31 import org.objectweb.jorm.api.PClassMapping;
32 import org.objectweb.jorm.genclass.lib.GenClassMapping;
33
34 /**
35  * This class is a Helper for the Jorm user (Speedo, JOnAS, Medor, ...) which
36  * uses pathes. A Path is a string which designates a persistent object class.
37  * A path always begins with the name of a jorm persistent class.
38  *
39  * Some path examples:
40  * - '/org.toto.titi' represents the persistent class 'org.toto.titi'
41  * - '/org.toto.titi/f1' represents the persistent class referenced by the
42  * field 'f1' of the persistent class 'org.toto.titi'.
43  * @author S.Chassande-Barrioz
44  */

45 public class JormPathHelper {
46
47     public final static char SEP = '/';
48     public final static String JavaDoc ELEMENT = SEP + "element";
49
50     /**
51      * Retrieves the path of a GenClass.
52      * @param gcr is the Jorm meta object repesenting the generic class.
53      * @param element indicates if the returned path must represent the element
54      * of the generic class or the generic class itself.
55      */

56     public static String JavaDoc getPath(GenClassRef gcr, boolean element) {
57         MetaObject parent = gcr.getParent();
58         if (parent instanceof Class JavaDoc) {
59             return getPath((Class JavaDoc) parent) + SEP + gcr.getName()
60                     + (element ? ELEMENT : "");
61         } else if (parent instanceof GenClassRef) {
62             GenClassRef p = (GenClassRef) parent;
63             return getPath(p, false) + SEP + p.getGenClassName()
64                     + (element ? ELEMENT : "");
65         } else {
66             return null;
67         }
68     }
69
70     /**
71      * Retrieves the path of the reference to a class. This reference can be
72      * hosted by a class or generic class.
73      * @param cr is the Jorm meta object representing the reference.
74      */

75     public static String JavaDoc getPath(ClassRef cr) {
76         MetaObject parent = cr.getParent();
77         if (parent instanceof Class JavaDoc) {
78             return getPath((Class JavaDoc) parent) + SEP + cr.getName();
79         } else if (parent instanceof GenClassRef) {
80             GenClassRef p = (GenClassRef) parent;
81             return getPath(p, false) + SEP + p.getGenClassName() + ELEMENT;
82         } else {
83             return null;
84         }
85     }
86
87     public static String JavaDoc replaceFieldOwner(String JavaDoc path, String JavaDoc newOwner) {
88         if (path == null) {
89             return null;
90         }
91         int idx = path.indexOf(SEP);
92         if (idx == -1) {
93             return path;
94         }
95         if (idx == 0) {
96             idx = path.indexOf(SEP, 1);
97         }
98         if (idx == -1) {
99             return path;
100         }
101         return getPath(newOwner) + path.substring(idx);
102     }
103
104     /**
105      * Retrieves the path of the class. This is the fully qualied class name of
106      * the class.
107      * @param clazz is the Jorm meta object representing the persistent class.
108      */

109     public static String JavaDoc getPath(Class JavaDoc clazz) {
110         return getPath(clazz.getFQName());
111     }
112
113     public static String JavaDoc getPath(String JavaDoc fqclassname) {
114         return SEP + fqclassname;
115     }
116
117     public static String JavaDoc getPath(String JavaDoc sourceclassName,
118                                  String JavaDoc refFieldName,
119                                  String JavaDoc[] genClassNames) {
120         StringBuffer JavaDoc cn = new StringBuffer JavaDoc();
121         cn.append(getPath(sourceclassName));
122         cn.append(SEP);
123         cn.append(refFieldName);
124         for (int i = 1; i < genClassNames.length; i++) {
125             cn.append(SEP);
126             cn.append(genClassNames[i - 1]);
127         }
128         return cn.toString();
129     }
130
131     /**
132      * Retrieves the PNameCoder instance corresponding to a path
133      * @param path is th path to reach the PNameCoder.
134      * @param mapper is the PMapper to find the first class name.
135      */

136     public static PNameCoder getPNameCoder(String JavaDoc path, PMapper mapper) {
137         if (path == null || path.length() == 0 || mapper == null) {
138             return null;
139         }
140         int idx = path.indexOf(SEP);
141         if (idx == -1) {
142             // PName of a class
143
PClassMapping pcm = mapper.lookup(path);
144             if (pcm == null) {
145                 return null;
146             } else {
147                 PNameCoder pnc = pcm.getClassPNameCoder();
148                 if (pnc == null) {
149                     pnc = pcm.getPBinder();
150                 }
151                 return pnc;
152             }
153         } else if (idx == 0) {
154             return getPNameCoder(path.substring(1), mapper);
155         } else {
156             //the first part of the pnamingcontext id is a class name
157
return getPNameCoder(path.substring(idx + 1),
158                                  mapper.lookup(path.substring(0, idx)));
159         }
160     }
161
162     /**
163      * Retrieves the PNameCoder instance corresponding to a path
164      * @param path is th path to reach the PNameCoder.
165      * @param pcm is the which is the start of the path
166      */

167     private static PNameCoder getPNameCoder(String JavaDoc path, PClassMapping pcm) {
168         if (pcm == null) {
169             return null;
170         }
171         int idx = path.indexOf(SEP);
172         if (pcm instanceof GenClassMapping) {
173             if (idx == -1) {
174                 return pcm.getPBinder();
175             } else {
176                 String JavaDoc subpath = path.substring(idx + 1);
177                 if ((SEP + subpath).startsWith(ELEMENT)) {
178                     return pcm.getPNameCoder();
179                 }
180                 return getPNameCoder(
181                         subpath,
182                         pcm.getGenClassMapping());
183             }
184         } else {
185             if (idx == -1) {
186                 return pcm.getPNameCoder(path);
187             } else {
188                 return getPNameCoder(
189                         path.substring(idx + 1),
190                         pcm.getGenClassMapping(path.substring(0, idx)));
191             }
192         }
193     }
194
195
196     /**
197      * Retrieves the GenClassMapping instance corresponding to a path
198      * @param path is th path to reach the GenClassMapping
199      * (PClassMapping implementation).
200      * @param mapper is the PMapper to find the first class name.
201      */

202     public static PClassMapping getPClassMapping(String JavaDoc path,
203                                                  PMapper mapper) {
204         if (path == null || path.length() == 0 || mapper == null) {
205             return null;
206         }
207         int idx = path.indexOf(SEP);
208         if (idx == -1) {
209             //The path represents a persistent class and not a generic
210
//persistent class
211
return mapper.lookup(path);
212         } else if (idx == 0) {
213             return getPClassMapping(path.substring(1), mapper);
214         }
215         return getPClassMapping(
216                 path.substring(idx + 1),
217                 mapper.lookup(path.substring(0, idx)));
218     }
219
220     private static PClassMapping getPClassMapping(String JavaDoc path,
221                                                   PClassMapping pcm) {
222         if (pcm == null) {
223             return null;
224         }
225         int idx = path.indexOf(SEP);
226         if (pcm instanceof GenClassMapping) {
227             if (idx == -1) {
228                 return pcm.getGenClassMapping();
229             } else {
230                 String JavaDoc subpath = path.substring(idx + 1);
231                 if ((SEP + subpath).startsWith(ELEMENT)) {
232                     return null;
233                 }
234                 return getPClassMapping(
235                         subpath,
236                         pcm.getGenClassMapping());
237             }
238         } else {
239             if (idx == -1) {
240                 return pcm.getGenClassMapping(path);
241             } else {
242                 return getPClassMapping(
243                         path.substring(idx + 1),
244                         pcm.getGenClassMapping(path.substring(0, idx)));
245             }
246         }
247     }
248
249     public static String JavaDoc getOriginClass(String JavaDoc path) {
250         if (path == null || path.length() == 0) {
251             return null;
252         }
253         int idx = path.indexOf(SEP);
254         if (idx == 0) {
255             idx = path.indexOf(SEP, idx);
256         }
257         if (idx == -1) {
258             return path;
259         } else {
260             int idx2 = path.indexOf(SEP, idx + 1);
261             if (idx2 == -1) {
262                 return path.substring(idx + 1);
263             } else {
264                 return path.substring(idx + 1, idx2);
265             }
266         }
267     }
268 }
269
Popular Tags