KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > fos > generator > FosBindingMOP


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.generator;
25
26 import org.apache.velocity.context.Context;
27
28 import org.objectweb.jorm.generator.api.MOP;
29 import org.objectweb.jorm.generator.lib.CommonGenerator;
30 import org.objectweb.jorm.type.api.PType;
31 import org.objectweb.jorm.util.api.Loggable;
32 import org.objectweb.jorm.metainfo.api.Class;
33 import org.objectweb.jorm.api.PException;
34 import org.objectweb.util.monolog.api.Logger;
35 import org.objectweb.util.monolog.api.LoggerFactory;
36
37 /**
38  * @author P. Dechamboux
39  */

40 public class FosBindingMOP implements MOP, Loggable {
41     protected Logger logger = null;
42     protected LoggerFactory loggerFactory = null;
43
44     public void initContext(Context ctx, Class JavaDoc clazz,
45                             String JavaDoc projectName,
46                             String JavaDoc mapperName) throws PException {
47         ctx.put("bindingMOPTools", this);
48     }
49
50     public String JavaDoc[] getTemplateLibraries() {
51         String JavaDoc[] libs = {CommonGenerator.MAPPER_TEMPLATE_DIR + "fos/generator/FosBinding.vm"};
52         return libs;
53     }
54
55     public String JavaDoc getSerRead(PType type)
56             throws Exception JavaDoc {
57         return "read" + getSer(type);
58     }
59
60     public String JavaDoc getSerWrite(PType type)
61             throws Exception JavaDoc {
62         return "write" + getSer(type);
63     }
64
65     private String JavaDoc getSer(PType type) {
66         switch (type.getTypeCode()) {
67         case PType.TYPECODE_BOOLEAN:
68             return "Boolean";
69         case PType.TYPECODE_BYTE:
70             return "Byte";
71         case PType.TYPECODE_CHAR:
72             return "Char";
73         case PType.TYPECODE_DOUBLE:
74             return "Double";
75         case PType.TYPECODE_FLOAT:
76             return "Float";
77         case PType.TYPECODE_INT:
78             return "Int";
79         case PType.TYPECODE_LONG:
80             return "Long";
81         case PType.TYPECODE_SHORT:
82             return "Short";
83         default:
84             return "Object";
85         }
86     }
87
88     public String JavaDoc getSerObjectCast(PType type) {
89         switch (type.getTypeCode()) {
90         case PType.TYPECODE_OBJBOOLEAN:
91             return "(Boolean) ";
92         case PType.TYPECODE_OBJBYTE:
93             return "(Byte) ";
94         case PType.TYPECODE_OBJCHAR:
95             return "(Character) ";
96         case PType.TYPECODE_OBJDOUBLE:
97             return "(Double) ";
98         case PType.TYPECODE_OBJFLOAT:
99             return "(Float) ";
100         case PType.TYPECODE_OBJINT:
101             return "(Integer) ";
102         case PType.TYPECODE_OBJLONG:
103             return "(Long) ";
104         case PType.TYPECODE_OBJSHORT:
105             return "(Short) ";
106         case PType.TYPECODE_STRING:
107             return "(String) ";
108         case PType.TYPECODE_DATE:
109             return "(Date) ";
110         case PType.TYPECODE_SERIALIZED:
111             return "(java.io.Serializable) ";
112         case PType.TYPECODE_BIGDECIMAL:
113             return "(java.math.BigDecimal) ";
114         case PType.TYPECODE_BYTEARRAY:
115             return "(byte[]) ";
116         case PType.TYPECODE_CHARARRAY:
117             return "(char[]) ";
118         default:
119             return "";
120         }
121     }
122
123     // IMPLEMENTATION OF METHDOS FROM THE Loggable INTERFACE
124

125     public Logger getLogger() {
126         return logger;
127     }
128
129     public LoggerFactory getLoggerFactory() {
130         return loggerFactory;
131     }
132
133     public void setLogger(Logger l) {
134         logger = l;
135     }
136
137     public void setLoggerFactory(LoggerFactory lf) {
138         loggerFactory = lf;
139         if (loggerFactory != null && logger == null)
140             loggerFactory.getLogger(getClass().getName());
141     }
142 }
143
Popular Tags