KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > fos > xml2mi > FosParser


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.mapper.fos.xml2mi;
25
26 import org.objectweb.jorm.xml2mi.lib.BasicMappingParser;
27 import org.objectweb.jorm.metainfo.api.Class;
28 import org.objectweb.jorm.metainfo.api.Mapping;
29 import org.objectweb.jorm.metainfo.api.NameDef;
30 import org.objectweb.jorm.metainfo.api.GenClassRef;
31 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
32 import org.objectweb.jorm.metainfo.api.TypedElement;
33 import org.objectweb.jorm.api.PException;
34 import org.objectweb.jorm.mapper.fos.metainfo.FosMapping;
35 import org.objectweb.jorm.mapper.fos.metainfo.FosClassMapping;
36 import org.objectweb.jorm.mapper.fos.metainfo.FosGenClassMapping;
37 import org.objectweb.util.monolog.api.BasicLevel;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41
42 import java.util.Iterator JavaDoc;
43
44 /**
45  * @author P. Dechamboux
46  */

47 public class FosParser extends BasicMappingParser {
48     /**
49      * Parses the mapping information related to a particular mapper and builds
50      * the corresponding Jorm meta-information. It can be class-related or
51      * generic class-related information.
52      * Manager, PathExplorer and currentClass must be set before calling this
53      * method.
54      * @param classMappingElem the current XML node.
55      * @param mapping a Mapping object.
56      */

57     private void parseClassMapping(Element classMappingElem, Mapping mapping) {
58         FosClassMapping fosClassMapping = (FosClassMapping) mapping.createClassMapping("");
59         fosClassMapping.setLogger(logger);
60         Class JavaDoc c = (Class JavaDoc) fosClassMapping.getLinkedMO();
61         Iterator JavaDoc it = c.getFields().iterator();
62         while (it.hasNext()) {
63             Object JavaDoc fd = it.next();
64             if (fd instanceof PrimitiveElement) {
65                 fosClassMapping.addFieldMapping(((PrimitiveElement) fd).getName());
66             }
67         }
68         NodeList JavaDoc children = classMappingElem.getChildNodes();
69         String JavaDoc dirname = null;
70         for (int i = 0; i < children.getLength(); i++) {
71             Node JavaDoc child = children.item(i);
72             String JavaDoc childName = child.getNodeName();
73             if (logger.isLoggable(BasicLevel.DEBUG)) {
74                 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">");
75             }
76             if (childName.equals("fos-dir-name")) {
77                 dirname = child.getFirstChild().getNodeValue();
78                 if (logger.isLoggable(BasicLevel.DEBUG)) {
79                     logger.log(BasicLevel.DEBUG, "read dirname: " + dirname);
80                 }
81             } else if (childName.equals("id-mapping")) {
82                 String JavaDoc linkend = ((Element) child).getAttribute("link-end");
83                 // Gets the NameDef object. It may be inherited from a super-class.
84
NameDef nd = getIdNameDef(fosClassMapping, linkend);
85                 if (nd.isNameRef()) {
86                     Iterator JavaDoc it2 = nd.iterateField();
87                     while (it2.hasNext()) {
88                         fosClassMapping.addFieldMapping((String JavaDoc) it2.next());
89                     }
90                 } else {
91                     fosClassMapping.addFieldMapping(nd.getFieldName());
92                 }
93                 fosClassMapping.createIdentifierMapping(nd);
94             } else if (childName.equals("fos-ref-mapping")) {
95                 String JavaDoc linkend = ((Element) child).getAttribute("link-end");
96                 // Gets the NameDef object, possibly beyond the scope of the current class if
97
// this NameDef is inherited from a super-class.
98
NameDef nd = getRefNameDef(linkend, childName);
99                 if (nd.isNameRef()) {
100                     Iterator JavaDoc it2 = nd.iterateField();
101                     while (it2.hasNext()) {
102                         fosClassMapping.addFieldMapping((String JavaDoc) it2.next());
103                     }
104                 } else {
105                     fosClassMapping.addFieldMapping(nd.getFieldName());
106                 }
107                 fosClassMapping.createReferenceMapping(null, nd);
108             } else if (childName.equals("#text")) {
109             } else {
110                 logger.log(BasicLevel.WARN, "element <" + childName + "> unknown !");
111             }
112             if (logger.isLoggable(BasicLevel.DEBUG))
113                 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">");
114         }
115         fosClassMapping.setDirName(dirname);
116     }
117
118     /**
119      * Parses a relational generic class mapping element and builds the
120      * corresponding Jorm meta-information.
121      * Manager, PathExplorer and currentClass must be set before calling this
122      * method.
123      *
124      * @param genClassMappingElem the current XML node,
125      * @param mapping a Mapping object.
126      */

127     private void parseGenClassMapping(Element genClassMappingElem,
128                                       Mapping mapping) {
129
130         String JavaDoc linkend = genClassMappingElem.getAttribute("link-end");
131         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
132             getLogger().log(BasicLevel.DEBUG,
133                             "linkend of fos-gen-class-mapping <" + linkend + ">");
134         }
135         GenClassRef genClassRef = (GenClassRef) idvalue2genclassref.get(linkend);
136         if ((genClassRef != null) &&
137                 (getLogger().isLoggable(BasicLevel.DEBUG))) {
138             getLogger().log(BasicLevel.DEBUG,
139                             "GenClassRef name " + genClassRef.getName());
140         }
141         // Creates a GenClassMapping object.
142
if (getLogger().isLoggable(BasicLevel.DEBUG)) {
143             getLogger().log(BasicLevel.DEBUG,
144                             "create a new BasicFosGenClassMapping for the current " +
145                             "Class (" + currentClass.getName() + ")");
146         }
147         FosGenClassMapping fosGenClassMapping = ((FosMapping) mapping)
148                 .createGenClassMapping("", genClassRef);
149         fosGenClassMapping.setLogger(logger);
150         Iterator JavaDoc it = genClassRef.getIndexFields().iterator();
151         while (it.hasNext()) {
152             fosGenClassMapping.addFieldMapping(((TypedElement) it.next()).getName());
153         }
154         NodeList JavaDoc children = genClassMappingElem.getChildNodes();
155         String JavaDoc dirname = null;
156         for (int i = 0; i < children.getLength(); i++) {
157             Node JavaDoc child = children.item(i);
158             String JavaDoc childName = child.getNodeName();
159             if (logger.isLoggable(BasicLevel.DEBUG)) {
160                 logger.log(BasicLevel.DEBUG, "begin =<" + childName + ">");
161             }
162             if (childName.equals("fos-dir-name")) {
163                 dirname = child.getFirstChild().getNodeValue();
164                 if (logger.isLoggable(BasicLevel.DEBUG)) {
165                     logger.log(BasicLevel.DEBUG, "read dirname: " + dirname);
166                 }
167             } else if (childName.equals("id-mapping")) {
168                 // The link-end attribute contains the name of the associated name-def.
169
String JavaDoc namedefName = ((Element) child).getAttribute("link-end");
170                 // Gets the corresponding NameDef object.
171
NameDef nd = genClassRef.getIdNameDef(namedefName);
172                 if (nd.isNameRef()) {
173                     Iterator JavaDoc it2 = nd.iterateField();
174                     while (it2.hasNext()) {
175                         fosGenClassMapping.addFieldMapping((String JavaDoc) it2.next());
176                     }
177                 } else {
178                     fosGenClassMapping.addFieldMapping(nd.getFieldName());
179                 }
180                 if (logger.isLoggable(BasicLevel.DEBUG)) {
181                     logger.log(BasicLevel.DEBUG,
182                                "Fetching the ID name def of the GenClassRef " + namedefName);
183                     if (nd.isFieldName())
184                         logger.log(BasicLevel.DEBUG, "fieldName: " + nd.getFieldName());
185                     else if (nd.isNameRef())
186                         logger.log(BasicLevel.DEBUG, "NameRef: " + nd.getNameRef());
187                 }
188                 // Creates an IdentifierMapping object
189
fosGenClassMapping.createIdentifierMapping(nd);
190             } else if (childName.equals("fos-ref-mapping")) {
191                 String JavaDoc namedefName = ((Element) child).getAttribute("link-end");
192                 NameDef nd = null;
193                 //TODO: Verifier
194
if (genClassRef.isPrimitive()) {
195                     logger.log(BasicLevel.WARN, "You define a generic class of " +
196                                                 "primitive and a useless name-def for the elements: " +
197                                                 "generic class id=" + genClassRef.getGenClassId());
198                     continue;
199                 } else if (genClassRef.isClassRef()) {
200                     if (logger.isLoggable(BasicLevel.DEBUG)) {
201                         logger.log(BasicLevel.DEBUG,
202                                    "Get the ref name def of the GenClassRef " + namedefName);
203                     }
204                     nd = genClassRef.getClassRef().getRefNameDef(namedefName);
205                     if (nd.isNameRef()) {
206                         Iterator JavaDoc it2 = nd.iterateField();
207                         while (it2.hasNext()) {
208                             fosGenClassMapping.addFieldMapping((String JavaDoc) it2.next());
209                         }
210                     } else {
211                         fosGenClassMapping.addFieldMapping(nd.getFieldName());
212                     }
213                 } else if (genClassRef.isGenClassRef()) {
214                     if (logger.isLoggable(BasicLevel.DEBUG)) {
215                         logger.log(BasicLevel.DEBUG,
216                                    "Get the ref name def of the ClassRef " + namedefName);
217                     }
218                     nd = genClassRef.getGenClassRef().getRefNameDef(namedefName);
219                     if (nd.isNameRef()) {
220                         Iterator JavaDoc it2 = nd.iterateField();
221                         while (it2.hasNext()) {
222                             fosGenClassMapping.addFieldMapping((String JavaDoc) it2.next());
223                         }
224                     } else {
225                         fosGenClassMapping.addFieldMapping(nd.getFieldName());
226                     }
227                 }
228                 String JavaDoc ruleName = ((Element) child).getAttribute("rule-name");
229                 // Creates a ReferenceMapping.
230
fosGenClassMapping.createReferenceMapping(ruleName, nd);
231             } else if (childName.equals("#text")) {
232             } else {
233                 logger.log(BasicLevel.WARN, "element <" + childName + "> unknown !");
234             }
235             if (logger.isLoggable(BasicLevel.DEBUG)) {
236                 logger.log(BasicLevel.DEBUG, "end =<" + childName + ">");
237             }
238         }
239         fosGenClassMapping.setDirName(dirname);
240     }
241
242     /**
243      * Parses the mapping information related to a particular mapper and builds
244      * the corresponding Jorm meta-information. It can be class-related or
245      * generic class-related information.
246      * Manager, PathExplorer and currentClass must be set before calling this
247      * method.
248      *
249      * @param mappingElem the current XML node.
250      * @param mapping a Mapping object.
251      */

252     public void parseMapping(Element mappingElem, Mapping mapping) throws PException {
253         if (mappingElem.getNodeName().equals("fos-class-mapping")) {
254             parseClassMapping(mappingElem, mapping);
255         } else if (mappingElem.getNodeName().equals("fos-gen-class-mapping")) {
256             parseGenClassMapping(mappingElem, mapping);
257         } else {
258             throw new PException("Mapping element not supported by FOS: " + mappingElem.getNodeName());
259         }
260     }
261 }
262
Popular Tags