1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.gridsystems.nextgrid.api.pom;
19
20 import java.beans.PropertyDescriptor;
21 import java.net.URI;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import javax.xml.namespace.QName;
26
27 import nextgrid.api.pom.AbstractProcess;
28 import nextgrid.api.pom.ControlProcess;
29 import nextgrid.api.pom.Grounding;
30 import nextgrid.api.pom.Process;
31 import nextgrid.api.pom.Service;
32
33 import org.apache.commons.jxpath.DynamicPropertyHandler;
34 import org.apache.commons.jxpath.JXPathBeanInfo;
35
36
37
38
39
40
41 public class ProcessImplXBeanInfo implements JXPathBeanInfo,
42 DynamicPropertyHandler {
43
44
45
46
47 private static final String[] PROPERTY_NAMES = {
48 "attributes",
49 "children",
50 "description",
51 "grounding",
52 "id",
53 "inputs",
54 "name",
55 "outputs",
56 "parent",
57 "root",
58 "selected",
59 };
60
61
62
63
64 private static final Object[] EMPTY_ARRAY = new Object[0];
65
66
67
68
69 public Class<?> getDynamicPropertyHandlerClass() {
70 return ProcessImplXBeanInfo.class;
71 }
72
73
74
75
76 public PropertyDescriptor getPropertyDescriptor(String arg0) {
77 return null;
78 }
79
80
81
82
83 public PropertyDescriptor[] getPropertyDescriptors() {
84 return new PropertyDescriptor[0];
85 }
86
87
88
89
90 public boolean isAtomic() {
91 return false;
92 }
93
94
95
96
97 public boolean isDynamic() {
98 return true;
99 }
100
101
102
103
104 public Object getProperty(Object obj, String propertyName) {
105 if (obj instanceof Process) {
106 Process p = (Process)obj;
107 if ("attributes".equals(propertyName)) {
108 Map<QName, String> attribs = p.getAttributes();
109 Map<String, String> attributes = new HashMap<String, String>();
110 for (Map.Entry<QName, String> entry : attribs.entrySet()) {
111 QName key = entry.getKey();
112 attributes.put(toString(key), entry.getValue());
113 }
114 return attributes;
115 } else if ("children".equals(propertyName)) {
116 return getChildrenOf(p);
117 } else if ("description".equals(propertyName)) {
118 return p.getDescription();
119 } else if ("id".equals(propertyName)) {
120 URI id = p.getId();
121 return (id == null) ? null : id.toString();
122 } else if ("inputs".equals(propertyName)) {
123 return p.getInputs();
124 } else if ("name".equals(propertyName)) {
125 return p.getName();
126 } else if ("outputs".equals(propertyName)) {
127 return p.getOutputs();
128 } else if ("parent".equals(propertyName)) {
129 return p.getParent();
130 } else if ("root".equals(propertyName)) {
131 return p.findRoot();
132 }
133
134 if (p instanceof AbstractProcess) {
135 AbstractProcess ap = (AbstractProcess)p;
136
137 if ("candidates".equals(propertyName)) {
138 return ap.getCandidates();
139 } else if ("selected".equals(propertyName)) {
140 return ap.getSelected();
141 }
142 }
143
144 if (p instanceof Service) {
145 Service s = (Service)p;
146
147 if ("grounding".equals(propertyName)) {
148 return s.getGrounding();
149 }
150 }
151 }
152
153 return null;
154 }
155
156
157
158
159 public String[] getPropertyNames(Object obj) {
160 if (obj instanceof Process) {
161 String[] copy = new String[PROPERTY_NAMES.length];
162 System.arraycopy(PROPERTY_NAMES, 0, copy, 0, PROPERTY_NAMES.length);
163 return copy;
164 } else {
165 return new String[0];
166 }
167 }
168
169
170
171
172 public void setProperty(Object arg0, String arg1, Object arg2) {
173
174
175 }
176
177
178
179
180
181
182
183 private Object[] getChildrenOf(Process p) {
184 if (p instanceof AbstractProcess) {
185 Process selected = ((AbstractProcess)p).getSelected();
186 return (selected == null) ? EMPTY_ARRAY : new Object[] { selected };
187 } else if (p instanceof ControlProcess) {
188 return ((ControlProcess)p).getChildren();
189 } else if (p instanceof Service) {
190 Grounding g = ((Service)p).getGrounding();
191 return (g == null) ? EMPTY_ARRAY : new Object[] { g };
192 } else {
193 return EMPTY_ARRAY;
194 }
195 }
196
197
198
199
200
201
202
203 private String toString(QName qname) {
204 String ns = qname.getNamespaceURI();
205 String local = qname.getLocalPart();
206 String s = ((ns == null) ? "" : ns) + "_" + local;
207 return s.replaceAll("[\\:\\/\\%\\$\\&\\#\\@\\;]", "_");
208 }
209
210 }