01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, available at the root
03: * application directory.
04: */
05: package org.geoserver.ows.kvp;
06:
07: import java.lang.reflect.Method;
08: import java.util.Iterator;
09: import java.util.Map;
10:
11: import org.eclipse.emf.ecore.EFactory;
12: import org.eclipse.emf.ecore.EObject;
13: import org.geoserver.ows.KvpRequestReader;
14: import org.geoserver.ows.util.OwsUtils;
15: import org.geotools.xml.EMFUtils;
16:
17: /**
18: * Web Feature Service Key Value Pair Request reader.
19: * <p>
20: * This request reader makes use of the Eclipse Modelling Framework
21: * reflection api.
22: * </p>
23: * @author Justin Deoliveira, The Open Planning Project
24: * @author Andrea Aime, TOPP
25: *
26: */
27: public class EMFKvpRequestReader extends KvpRequestReader {
28: /**
29: * Factory used to create model objects / requests.
30: */
31: protected EFactory factory;
32:
33: /**
34: * Creates the Wfs Kvp Request reader.
35: *
36: * @param requestBean The request class, which must be an emf class.
37: */
38: public EMFKvpRequestReader(Class requestBean, EFactory factory) {
39: super (requestBean);
40:
41: //make sure an eobject is passed in
42: if (!EObject.class.isAssignableFrom(requestBean)) {
43: String msg = "Request bean must be an EObject";
44: throw new IllegalArgumentException(msg);
45: }
46:
47: this .factory = factory;
48: }
49:
50: /**
51: * Reflectivley creates the request bean instance.
52: */
53: public Object createRequest() {
54: String className = getRequestBean().getName();
55:
56: //strip off package
57: int index = className.lastIndexOf('.');
58:
59: if (index != -1) {
60: className = className.substring(index + 1);
61: }
62:
63: Method create = OwsUtils.method(factory.getClass(), "create"
64: + className);
65:
66: try {
67: return create.invoke(factory, null);
68: } catch (Exception e) {
69: throw new RuntimeException(e);
70: }
71: }
72:
73: public Object read(Object request, Map kvp, Map rawKvp)
74: throws Exception {
75: //use emf reflection
76: EObject eObject = (EObject) request;
77:
78: for (Iterator e = kvp.entrySet().iterator(); e.hasNext();) {
79: Map.Entry entry = (Map.Entry) e.next();
80: String property = (String) entry.getKey();
81: Object value = entry.getValue();
82:
83: if (EMFUtils.has(eObject, property)) {
84: EMFUtils.set(eObject, property, value);
85: }
86: }
87:
88: return request;
89: }
90: }
|