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 de.tu_cottbus.r2ml.ObjectVariable;
13: import nz.org.take.Variable;
14:
15: class ObjectVariableHandler implements XmlTypeHandler {
16:
17: public Object importObject(Object obj) throws R2MLException {
18: ObjectVariable oVar = (ObjectVariable) obj;
19: R2MLDriver driver = R2MLDriver.get();
20: MappingContext context = MappingContext.get();
21: Variable var = context.getVariable(oVar.getName());
22: if (oVar.getClassID() == null && var == null)
23: throw new R2MLException("untyped object variable "
24: + oVar.getName());
25: if (var != null) {
26: if (var.getType() == null) {
27: Class type = driver.getDatatypeMapper().getType(
28: oVar.getClassID());
29: var.setType(type);
30: }
31: return var;
32: }
33: try {
34: var = new Variable();
35: var.setName(oVar.getName());
36: Class type;
37: try {
38: type = driver.getDatatypeMapper().getType(
39: oVar.getClassID());
40: } catch (R2MLException e) {
41: throw new R2MLException(
42: "unknown classid for variable "
43: + oVar.getName() + ":"
44: + oVar.getClassID() == null ? "nothing"
45: : oVar.getClassID().toString(), e);
46: }
47:
48: var.setType(type);
49: if (driver.logger.isInfoEnabled()) {
50: driver.logger.info("Create new Variable ("
51: + var.getName() + ":" + var.getType() + ").");
52: }
53: } catch (NullPointerException e) {
54: String msg = "No Class specified for "
55: + oVar.getClassID().toString() + ".";
56: throw new R2MLException(msg, e);
57: }
58: context.addVariable(oVar.getName(), var);
59: return var;
60: }
61:
62: }
|