KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.jorm.xml2mi.lib;
19
20 import org.objectweb.jorm.metainfo.api.NameDef;
21 import org.objectweb.jorm.metainfo.api.ClassMapping;
22 import org.objectweb.jorm.metainfo.api.Class;
23 import org.objectweb.jorm.util.api.Loggable;
24 import org.objectweb.util.monolog.api.BasicLevel;
25 import org.objectweb.util.monolog.api.Logger;
26 import org.objectweb.util.monolog.api.LoggerFactory;
27
28 import java.util.Hashtable JavaDoc;
29
30 /**
31  * This class is a common helper between generic and specific parsers.
32  *
33  * @author S.Chassande-Barrioz
34  */

35 public class ParserHelper implements Loggable {
36
37     static String JavaDoc fileSeparator = System.getProperty("file.separator");
38     static String JavaDoc pathSeparator = System.getProperty("path.separator");
39
40     /**
41      * This structure contains Class and CompositeName objects.
42      * key = a JORM persistent definition file
43      * value = the associated MetaObject object.
44      */

45     protected Hashtable JavaDoc motable = null;
46
47     /**
48      * a simple logger to log
49      */

50     protected Logger logger;
51
52     /**
53      * a logger factory to create other loggers if needed
54      */

55     protected LoggerFactory loggerFactory;
56
57
58     /**
59      * Returns a NameDef object given a NameDef name (name) and possibly the full qualified
60      * class name (fqcn) where it is defined. This NameDef object is referenced from an
61      * IdentifierMapping object.
62      * @param linkend a String object whose structure is defined as follows: /fqcn/name or /fqcn or name
63      * @return a NameDef object.
64      */

65     protected NameDef getIdNameDef(Class JavaDoc currentClass, String JavaDoc linkend) {
66         NameDef nameDef = null;
67         int idx1 = linkend.indexOf("/");
68         if (idx1 == 0) { // link-end value: /fqcn/name or /fqcn
69
String JavaDoc fqcn = null;
70             String JavaDoc namedefName = null;
71             int idx2 = linkend.indexOf("/", idx1 + 1);
72             if (idx2 == -1) { // link-end value: /fqcn
73
fqcn = linkend.substring(1);
74                 namedefName = "";
75             } else { // link-end value: /fqcn/name
76
fqcn = linkend.substring(1, idx2);
77                 namedefName = linkend.substring(idx2 + 1);
78
79             }
80             if (logger.isLoggable(BasicLevel.DEBUG)) {
81                 logger.log(BasicLevel.DEBUG,
82                            " fqcn " + fqcn +
83                            " namdefname " + namedefName);
84             }
85             fqcn = fqcn.replace('.', fileSeparator.charAt(0)) + ".pd";
86             Class JavaDoc clazz = (Class JavaDoc) motable.get(fqcn);
87             if (clazz != null) {
88                 if (logger.isLoggable(BasicLevel.DEBUG)) {
89                     logger.log(BasicLevel.DEBUG,
90                                "Fetching the id name def of the Class " + clazz.getFQName());
91                 }
92                 nameDef = clazz.getNameDef(namedefName);
93             }
94
95         } else { // link-end value: name
96
if (logger.isLoggable(BasicLevel.DEBUG)) {
97                 logger.log(BasicLevel.DEBUG,
98                            "Fetching the id name def of the current Class " + currentClass.getFQName());
99             }
100             nameDef = currentClass.getNameDef(linkend);
101         }
102
103         if ((nameDef != null) && (logger.isLoggable(BasicLevel.DEBUG))) {
104             if (nameDef.isFieldName())
105                 logger.log(BasicLevel.DEBUG, "fieldName: " + nameDef.getFieldName());
106             else if (nameDef.isNameRef())
107                 logger.log(BasicLevel.DEBUG, "NameRef: " + nameDef.getNameRef());
108         }
109         return nameDef;
110     }
111
112     protected NameDef getIdNameDef(ClassMapping classMapping, String JavaDoc linkend) {
113         return getIdNameDef((Class JavaDoc) classMapping.getLinkedMO(), linkend);
114     }
115
116     // IMPLEMENTATION OF METHODS FROM THE Loggable INTERFACE
117

118     /**
119      * Defines a logger object.
120      *
121      * @param logger the logger object
122      */

123     public void setLogger(Logger logger) {
124         this.logger = logger;
125     }
126
127     /**
128      * Defines the logger factory to obtain new loggers.
129      *
130      * @param loggerfactory The LoggerFactory object to obtain a logger object
131      */

132     public void setLoggerFactory(LoggerFactory loggerfactory) {
133         this.loggerFactory = loggerfactory;
134     }
135
136     public Logger getLogger() {
137         return logger;
138     }
139
140     public LoggerFactory getLoggerFactory() {
141         return loggerFactory;
142     }
143 }
144
Popular Tags