01: /*
02: * Copyright 2007 Bastian Schenke Licensed under the Apache License, Version 2.0 (the "License");
03: * you may not use this file except in compliance with the License.
04: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
05: * Unless required by applicable law or agreed to in writing, software distributed under the
06: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
07: * either express or implied. See the License for the specific language governing permissions
08: * and limitations under the License.
09: */
10: package nz.org.take.r2ml;
11:
12: import java.lang.reflect.Constructor;
13:
14: import org.apache.log4j.Priority;
15:
16: import de.tu_cottbus.r2ml.TypedLiteral;
17: import nz.org.take.Constant;
18:
19: /**
20: *
21: * @author Bastian Schenke (bastian.schenke@googlemail.com)
22: *
23: */
24: class TypedLiteralHandler implements XmlTypeHandler {
25:
26: /**
27: * Maps a TypedLiteral to a Constant.
28: *
29: * @param obj
30: * a TypedLiteral
31: * @return Constant representing the literal
32: *
33: * @see nz.org.take.r2ml.XmlTypeHandler#importObject(java.lang.Object,
34: * nz.org.take.r2ml.R2MLDriver)
35: */
36: public Object importObject(Object obj) throws R2MLException {
37: Constant constant = new Constant();
38: TypedLiteral tl = (TypedLiteral) obj;
39: // System.out.println("TypedLiteral with type " + tl.getDatatypeID());
40: R2MLDriver driver = R2MLDriver.get();
41: try {
42: constant.setType(driver.getDatatypeMapper().getType(
43: tl.getDatatypeID()));
44: } catch (RuntimeException e1) {
45: // System.out.println("TypedLiteral with type " +
46: // tl.getDatatypeID());
47: e1.printStackTrace();
48: }
49: if (constant.getType() == String.class) {
50: constant.setObject(tl.getLexicalValue());
51: //return constant;
52: } else {
53: // constant.getType().isPrimitive() ||
54: // else if (Number.class.isAssignableFrom(constant.getType()) ||
55: // constant.getType() == Boolean.class) {
56: // constant.setObject(tl.getLexicalValue());
57: // return constant;
58: // }
59: // constant.setRef("_" + tl.getLexicalValue());
60: try {
61: Constructor constructor = constant.getType()
62: .getConstructor(new Class[] { String.class });
63: Object value = constructor
64: .newInstance(new Object[] { tl
65: .getLexicalValue() });
66: constant.setObject(value);
67: if (driver.logger.isInfoEnabled()) {
68: driver.logger.info("Create new Constant ("
69: + constant.getType() + ":<"
70: + tl.getLexicalValue() + ">)");
71: }
72: } catch (Exception e) {
73: System.out
74: .println("##################################");
75: constant.setRef("_" + tl.getLexicalValue());
76: System.out
77: .println("##################################");
78: // return constant;
79: }
80: }
81: return constant;
82: }
83:
84: }
|