KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > xml2mi > lib > BasicMappingParser


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
24 package org.objectweb.jorm.xml2mi.lib;
25
26 import org.objectweb.jorm.metainfo.api.*;
27 import org.objectweb.jorm.metainfo.api.Class;
28 import org.objectweb.jorm.xml2mi.api.MappingParser;
29 import org.objectweb.jorm.util.io.api.PathExplorer;
30 import org.objectweb.jorm.util.io.lib.DirJavaExplorer;
31 import org.objectweb.jorm.api.PException;
32 import org.objectweb.util.monolog.api.Logger;
33 import org.objectweb.util.monolog.api.LoggerFactory;
34 import org.objectweb.util.monolog.api.BasicLevel;
35 import org.w3c.dom.Element JavaDoc;
36
37 import java.util.Map JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 /**
42  * ProxyParser is an implementation of the Parser interface. This object defines
43  * a parser which is able to dispatch operation to specific mapping parser,
44  * and able to build the corresponding meta information.
45  * @author X. Spengler
46  */

47 public abstract class BasicMappingParser
48     extends ParserHelper
49     implements MappingParser {
50
51     /**
52      * The meta information manager for managing meta-objects within the
53      * context of a mapper.
54      */

55     protected Manager metaInfoManager;
56
57     /**
58      * The current Class object.
59      */

60     protected Class JavaDoc currentClass;
61
62     /**
63      * pathExplorer is able to return input or output stream from a given file.
64      */

65     protected PathExplorer pathExplorer;
66
67     /**
68      * A Map of String objects.
69      * key = a String object that represents an id value.
70      * value = the associated GenClassRef object.
71      */

72     protected Map JavaDoc idvalue2genclassref;
73
74     static String JavaDoc fileSeparator = System.getProperty("file.separator");
75
76     ///////////////////////////////////////////////////////////////////
77
// from ProxyParserInt interface
78
///////////////////////////////////////////////////////////////////
79

80     /**
81      * Assigns a meta-information manager to a parser in order from it to
82      * create the meta-information related to the XML files it parses.
83      *
84      * @param mim the meta-information Manager to be associated to this
85      * parser
86      */

87     public void setMetaInfoManager(Manager mim) {
88         metaInfoManager = mim;
89     }
90
91     /**
92      * Assigns a PathExplorer object for locating files that have to be parsed.
93      *
94      * @param pathExpl The PathExplorer to be used for file location
95      */

96     public void setPathExplorer(PathExplorer pathExpl) {
97         pathExplorer = pathExpl;
98         if (pathExplorer == null)
99             pathExplorer = new DirJavaExplorer();
100     }
101
102     /**
103      * Assigns the current Class object to a parser in order from it to
104      * retrieve the meta-objects (for example, PrimitiveElement objects)
105      * that are referenced from XML mapping elements.
106      *
107      * @param currentClass the Jorm meta-object associated to the parsed class.
108      */

109     public void setCurrentClass(Class JavaDoc currentClass) {
110         this.currentClass = currentClass;
111     }
112
113
114     /**
115      * Returns a NameDef object given a NameDef name (name), a field or a scalarfield name (fn)
116      * and possibly the full qualified class name where it is defined. This NameDef object is
117      * referenced from a ReferenceMapping object.
118      * First seeks for a TypedElement object given a field or a scalarfield name and possibly the full
119      * qualified class name (fqcn) where it is defined.
120      * @param linkend a String object whose structure is defined as follows: /fqcn/fn/name or fn/name,
121      * tagName a String object whose value matches "rdb-class-ref-mapping" or
122      * "rdb-gen-class-ref-mapping".
123      * @return a NameDef object.
124      */

125     protected NameDef getRefNameDef(String JavaDoc linkend, String JavaDoc tagName) {
126         TypedElement typedElement = null;
127         NameDef nameDef = null;
128         String JavaDoc fieldName = null;
129         String JavaDoc namedefName = null;
130         int idx1 = linkend.indexOf("/");
131         if (idx1 == 0) {
132             // link-end value: /fqcn/fn/name
133
int idx2 = linkend.indexOf("/", idx1 + 1);
134             String JavaDoc fqcn = linkend.substring(1, idx2);
135             int idx3 = linkend.indexOf("/", idx2 + 1);
136             fieldName = linkend.substring(idx2 + 1, idx3);
137             namedefName = linkend.substring(idx3 + 1);
138             if (logger.isLoggable(BasicLevel.DEBUG)) {
139                 logger.log(BasicLevel.DEBUG,
140                             " fqcn " + fqcn +
141                            " fieldname " + fieldName +
142                            " namdefname " + namedefName);
143             }
144             fqcn = fqcn.replace('.', fileSeparator.charAt(0)) + ".pd";
145             if (logger.isLoggable(BasicLevel.DEBUG)) {
146                 if (motable.contains(fqcn)) {
147                     logger.log(BasicLevel.DEBUG, "fqcn is there " + fqcn);
148                 }
149                 else {
150                     logger.log(BasicLevel.DEBUG,"fqcn is not there " + fqcn);
151                 }
152             }
153             Class JavaDoc clazz = (Class JavaDoc) motable.get(fqcn);
154             typedElement = clazz.getTypedElement(fieldName);
155
156             if (logger.isLoggable(BasicLevel.DEBUG)) {
157                 logger.log(BasicLevel.DEBUG,
158                            "Get the field " + fieldName +
159                            "defined in <" + clazz.getFQName() +
160                            ">: <" + typedElement + ">");
161             }
162         } else {
163             // link-end value: fn/name
164
fieldName = linkend.substring(0, idx1);
165             namedefName = linkend.substring(idx1 + 1);
166             typedElement = currentClass.getTypedElement(fieldName);
167             if (logger.isLoggable(BasicLevel.DEBUG)) {
168                 logger.log(BasicLevel.DEBUG,
169                            "Get the field [" + fieldName +
170                            "] in current class <" + currentClass.getFQName() +
171                            ">: <" + typedElement + ">");
172             }
173         }
174         if (typedElement instanceof ClassRef) {
175             ClassRef classRef = (ClassRef) typedElement;
176             if (logger.isLoggable(BasicLevel.DEBUG)) {
177                 logger.log(BasicLevel.DEBUG,
178                            "Get the ref name def of the ClassRef " + fieldName);
179             }
180             nameDef = classRef.getRefNameDef(namedefName);
181         } else if (typedElement instanceof GenClassRef) {
182             GenClassRef genClassRef = (GenClassRef) typedElement;
183             if (logger.isLoggable(BasicLevel.DEBUG)) {
184                 logger.log(BasicLevel.DEBUG,
185                            "Get the ref name def of the GenClassRef " + fieldName);
186             }
187             nameDef = genClassRef.getRefNameDef(namedefName);
188         }
189         if (logger.isLoggable(BasicLevel.DEBUG)) {
190             logger.log(BasicLevel.DEBUG, "Returned name def: " + nameDef);
191         }
192         return nameDef;
193     }
194
195     /**
196      * Returns a NameDef object given the full qualified class name of an inherited class
197      * and possibly a NameDef name.
198      * @param linkend a String object whose structure is defined as follows:
199      * /fqcn or /fqcn/name,
200      * @return a NameDef object.
201      */

202     protected NameDef getNameDefFromSuper(String JavaDoc linkend) {
203         int idx1 = linkend.indexOf("/");
204         if (idx1 == 0) {
205             // link-end value: /fqcn...
206
int idx2 = linkend.indexOf("/", idx1 + 1);
207             String JavaDoc namedefName = null;
208             String JavaDoc fqcn = null;
209             if (idx2 != -1) {
210                 // link-end value: /fqcn/name
211
fqcn = linkend.substring(1, idx2);
212                 namedefName = linkend.substring(idx2 + 1);
213             } else {
214                 // link-end value: /fqcn
215
fqcn = linkend.substring(1);
216                 namedefName = "";
217             }
218             Class JavaDoc superClass = currentClass.getSuperClass(fqcn);
219             Iterator JavaDoc ndIt = superClass.getNameDefs().iterator();
220             while(ndIt.hasNext()) {
221                 NameDef nd = (NameDef) ndIt.next();
222                 if (namedefName.equals(nd.getName())) {
223                     if (logger.isLoggable(BasicLevel.DEBUG)) {
224                         logger.log(BasicLevel.DEBUG, "Returned name def: " + nd);
225                     }
226                     return nd;
227                 }
228
229             }
230         }
231         if (logger.isLoggable(BasicLevel.DEBUG)) {
232             logger.log(BasicLevel.DEBUG, "No nameDef found for the linkend " + linkend);
233         }
234         return null;
235     }
236
237     protected PrimitiveElement getPrimitiveElement(String JavaDoc linkend) {
238         PrimitiveElement pe = null;
239         int idx1 = linkend.indexOf("/");
240         if (idx1 == 0) {
241             // link-end value: /fqcn/fn
242
int idx2 = linkend.indexOf("/", idx1 + 1);
243             if (idx2 != -1) {
244                 // link-end value: /fqcn/name
245
String JavaDoc fqcn = linkend.substring(1, idx2);
246                 String JavaDoc fieldname = linkend.substring(idx2 + 1);
247                 Class JavaDoc superClass = currentClass.getSuperClass(fqcn);
248                 pe = (PrimitiveElement) superClass.getTypedElement(fieldname);
249             }
250         } else {
251             // link-end value: fn
252
pe = (PrimitiveElement) currentClass.getTypedElement(linkend);
253         }
254         return pe;
255     }
256
257     /**
258      * Seeks for a father node of type field from a name-def node and returns
259      * the corresponding field name.
260      * @param nameDef the name-def element.
261      * @return a field name.
262      */

263     protected String JavaDoc getFieldName(Element JavaDoc nameDef) {
264         logger.log(BasicLevel.DEBUG, "Fetching field name of the name def element " + nameDef.getNodeName());
265         Element JavaDoc currentElem = nameDef;
266         while (currentElem != null &&
267                 !currentElem.getNodeName().equals("field")) {
268             logger.log(BasicLevel.DEBUG, "current: " + currentElem.getNodeName());
269             currentElem = (Element JavaDoc) currentElem.getParentNode();
270         }
271         if (currentElem != null) {
272             if (logger.isLoggable(BasicLevel.DEBUG)) {
273                 logger.log(BasicLevel.DEBUG, "last: " + currentElem.getNodeName()
274                                              + " => name=" + currentElem.getAttribute("name"));
275             }
276             return currentElem.getAttribute("name");
277         } else {
278             return null;
279         }
280     }
281
282
283     /**
284      * Seeks for a ClassRef object given a class, a field name.
285      * This method returns the NameDef object of the ClassRef object.
286      *
287      * @return the NameDef object of the ClassRef object.
288      */

289     protected NameDef getClassRefNameDef(Class JavaDoc clazz,
290                                          String JavaDoc fieldName, String JavaDoc name) {
291         NameDef res = null;
292         TypedElement typedElement = clazz.getTypedElement(fieldName);
293         logger.log(BasicLevel.DEBUG, "Fetching field <" + clazz.getFQName() + "." + fieldName + ">: <" + typedElement + ">");
294         if (typedElement instanceof ClassRef) {
295             ClassRef classRef = (ClassRef) typedElement;
296             logger.log(BasicLevel.DEBUG, "Fetching name def of the ClassRef " + fieldName);
297             res = classRef.getRefNameDef(name);
298         } else if (typedElement instanceof GenClassRef) {
299             GenClassRef genClassRef = (GenClassRef) typedElement;
300             logger.log(BasicLevel.DEBUG, "Fetching name def of the ClassRef inside GenClassRef*: " + fieldName);
301             while ((genClassRef != null) && (!genClassRef.isClassRef())) {
302                 genClassRef = genClassRef.getGenClassRef();
303             }
304             if (genClassRef != null) {
305                 res = genClassRef.getRefNameDef(name);
306             }
307         }
308         logger.log(BasicLevel.DEBUG, "Returned name def: " + res);
309         return res;
310     }
311
312     /**
313      * Seeks for a father node of type field from a gen-class element or a
314      * name-def element. The name-def element represents the pname of a reference
315      * to a generic class.
316      *
317      * This method returns the nesting level of the generic class as well as the
318      * corresponding field name.
319      *
320      * @return the nesting level of a gen-class element.
321      */

322     protected int getGenClassNestingLevel(Element JavaDoc fromElem, String JavaDoc fieldName) {
323         int nestingLevel = 0;
324         Element JavaDoc currentElem = (Element JavaDoc) fromElem.getParentNode();
325         while ((currentElem != null) &&
326                 (!currentElem.getNodeName().equals("field"))) {
327             currentElem = (Element JavaDoc) currentElem.getParentNode();
328             if ((currentElem.getNodeName().equals("class-ref")) ||
329                     (currentElem.getNodeName().equals("gen-class-ref"))) {
330                 nestingLevel++;
331             }
332         }
333         if (currentElem != null) {
334             fieldName = currentElem.getAttribute("field-name");
335         }
336         return nestingLevel;
337     }
338
339     /**
340      * Seeks for a GenClassRef object given a class, a field name and a nesting
341      * level. This method returns the NameDef object of the GenClassRef object.
342      *
343      * @return the NameDef object of the GenClassRef object.
344      */

345     protected NameDef getGenClassRefNameDef(Class JavaDoc clazz, String JavaDoc fieldName,
346                                             String JavaDoc name, int nestingLevel) {
347         NameDef res = null;
348         GenClassRef genClassRef =
349                 getGenClassRef(clazz, fieldName, nestingLevel);
350         if (genClassRef != null) {
351             res = genClassRef.getRefNameDef(name);
352         }
353         return res;
354     }
355
356
357     /**
358      * Seeks for a GenClassRef object given a class, a field name and a nesting
359      * level. This method returns the NameDef object of the GenClassRef object.
360      *
361      * @return the NameDef object of the GenClassRef object.
362      */

363     protected GenClassRef getGenClassRef(Class JavaDoc clazz, String JavaDoc fieldName,
364                                          int nestingLevel) {
365         GenClassRef res = null;
366         TypedElement typedElement = clazz.getTypedElement(fieldName);
367         if (typedElement instanceof GenClassRef) {
368             res = (GenClassRef) typedElement;
369             GenClassRef genClassRef = (GenClassRef) typedElement;
370             while ((genClassRef != null) && (nestingLevel != 0)) {
371                 res = genClassRef.getGenClassRef();
372                 nestingLevel--;
373             }
374         }
375         return res;
376     }
377
378     /**
379      * Parses the mapping information related to a particular mapper and builds
380      * the corresponding Jorm meta-information. It can be class-related or
381      * generic class-related information.
382      * Manager, PathExplorer and currentClass must be set before calling this
383      * method.
384      *
385      * @param mappingElem the current XML node.
386      * @param mapping a Mapping object.
387      */

388     public abstract void parseMapping(Element JavaDoc mappingElem, Mapping mapping) throws PException;
389
390     /**
391      * Assigns an HashTable object to the MappingParser object.
392      *
393      * @param idvalue2genclassref an HashTable that associates an id value with a GenClassRef object.
394      */

395     public void setidvalue2genclassref(Map JavaDoc idvalue2genclassref) {
396         this.idvalue2genclassref = idvalue2genclassref;
397     }
398
399     /**
400      * Assigns an HashTable object to the MappingParser object.
401      *
402      * @param motable an HashTable that associates a pd file to its corresponding MetaObject.
403      */

404     public void setmotable(Hashtable JavaDoc motable) {
405         this.motable = motable;
406     }
407
408     ///////////////////////////////////////////////////////////////////
409
// from Loggable interface
410
///////////////////////////////////////////////////////////////////
411

412     /**
413      * Defines a logger object.
414      *
415      * @param logger the logger object
416      */

417     public void setLogger(Logger logger) {
418         this.logger = logger;
419     }
420
421     /**
422      * Defines the logger factory to obtain new loggers.
423      *
424      * @param loggerfactory The LoggerFactory object to obtain a logger object
425      */

426     public void setLoggerFactory(LoggerFactory loggerfactory) {
427         this.loggerFactory = loggerfactory;
428         if (logger == null && loggerFactory != null) {
429             logger = loggerFactory.getLogger("org.objectweb.jorm.xml2mi.rdb");
430         }
431     }
432
433     public Logger getLogger() {
434         return logger;
435     }
436
437     public LoggerFactory getLoggerFactory() {
438         return loggerFactory;
439     }
440 }
441
Popular Tags