Revision 28076 branches/v2_0_0_prep/libraries/libIverUtiles/src/org/gvsig/tools/persistence/xmlentity/XMLEntityState.java
XMLEntityState.java | ||
---|---|---|
1 | 1 |
package org.gvsig.tools.persistence.xmlentity; |
2 | 2 |
|
3 |
import java.io.IOException; |
|
4 |
import java.io.Reader; |
|
5 |
import java.io.Writer; |
|
3 | 6 |
import java.util.Iterator; |
4 | 7 |
import java.util.NoSuchElementException; |
5 | 8 |
|
9 |
import org.exolab.castor.xml.MarshalException; |
|
10 |
import org.exolab.castor.xml.Marshaller; |
|
11 |
import org.exolab.castor.xml.ValidationException; |
|
6 | 12 |
import org.gvsig.tools.persistence.PersistenceException; |
7 | 13 |
import org.gvsig.tools.persistence.PersistenceValueNotFoundException; |
8 | 14 |
import org.gvsig.tools.persistence.Persistent; |
9 | 15 |
import org.gvsig.tools.persistence.PersistentState; |
16 |
import org.gvsig.tools.persistence.impl.AbstractPersistentState; |
|
10 | 17 |
|
11 | 18 |
import com.iver.utiles.NotExistInXMLEntity; |
12 | 19 |
import com.iver.utiles.XMLEntity; |
20 |
import com.iver.utiles.xmlEntity.generate.XmlTag; |
|
13 | 21 |
|
14 |
public class XMLEntityState implements PersistentState { |
|
15 |
|
|
22 |
public class XMLEntityState extends AbstractPersistentState { |
|
23 |
final private static int DATA_POS = 0; |
|
24 |
|
|
16 | 25 |
final private static String KEY_CLASSNAME = "_classname_"; |
17 |
final private static String KEY_CHILD = "_child_"; |
|
18 |
final private static String ITEM = "_item_"; |
|
26 |
final private static String KEY_DATA = "_item_data_"; |
|
27 |
final private static String VALUE = "_value_"; |
|
28 |
|
|
29 |
final private static String KEY_CHILD = "_childName_"; |
|
30 |
final private static String ITEM = "_iterator_item_"; |
|
31 |
|
|
32 |
public static String PROJECTENCODING = "UTF-8"; |
|
19 | 33 |
|
20 | 34 |
protected XMLEntity xmlEntity; |
21 | 35 |
protected XMLEntityManager manager; |
36 |
protected XMLEntity data; |
|
22 | 37 |
|
23 | 38 |
public XMLEntityState(XMLEntityManager manager) { |
24 |
this.manager = manager; |
|
25 |
this.xmlEntity = new XMLEntity(); |
|
39 |
this(manager, new XMLEntity()); |
|
26 | 40 |
} |
27 | 41 |
|
28 | 42 |
public XMLEntityState(XMLEntityManager manager, XMLEntity xmlEntity) { |
29 | 43 |
this.manager = manager; |
30 | 44 |
this.xmlEntity = xmlEntity; |
45 |
this.data = xmlEntity.firstChild(KEY_DATA); |
|
31 | 46 |
} |
32 | 47 |
|
48 |
protected void init() { |
|
49 |
this.data = new XMLEntity(); |
|
50 |
this.data.getXmlTag().setName(KEY_DATA); |
|
51 |
this.xmlEntity.addChild(this.data); |
|
52 |
} |
|
53 |
|
|
33 | 54 |
public XMLEntity getXMLEntity() { |
34 | 55 |
return this.xmlEntity; |
35 | 56 |
} |
36 | 57 |
|
37 |
private PersistentState createState(XMLEntity xmlEntity) |
|
38 |
throws PersistenceException { |
|
39 |
return manager.createState(xmlEntity); |
|
40 |
} |
|
41 |
|
|
42 |
public PersistentState createState(Object obj) throws PersistenceException { |
|
43 |
return manager.createState(obj); |
|
44 |
} |
|
45 |
|
|
46 |
public PersistentState createState(Object obj, boolean initialize) |
|
47 |
throws PersistenceException { |
|
48 |
return manager.createState(obj, initialize); |
|
49 |
} |
|
50 |
|
|
51 | 58 |
public void setTheClass(Object obj) { |
52 | 59 |
this.xmlEntity.putProperty(KEY_CLASSNAME, obj.getClass().getName()); |
53 | 60 |
} |
... | ... | |
61 | 68 |
} |
62 | 69 |
|
63 | 70 |
public String getTheClassName() { |
64 |
try { |
|
65 |
return this.xmlEntity.getStringProperty(KEY_CLASSNAME); |
|
66 |
} catch (NotExistInXMLEntity e) { |
|
67 |
return null; |
|
71 |
return this.xmlEntity.getStringProperty(KEY_CLASSNAME); |
|
72 |
} |
|
73 |
|
|
74 |
protected XMLEntity getData() { |
|
75 |
if (this.data==null) { |
|
76 |
init(); |
|
68 | 77 |
} |
78 |
return this.data; |
|
69 | 79 |
} |
70 | 80 |
|
81 |
protected void setName(String name) { |
|
82 |
this.xmlEntity.getXmlTag().setName(name); |
|
83 |
} |
|
84 |
|
|
85 |
protected String getName() { |
|
86 |
return this.xmlEntity.getXmlTag().getName(); |
|
87 |
} |
|
88 |
|
|
71 | 89 |
public Object get(String name) throws PersistenceException { |
72 |
XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
|
|
90 |
XMLEntity value = this.getData().firstChild(name);
|
|
73 | 91 |
if( value == null ) { |
74 | 92 |
try { |
75 |
return this.xmlEntity.getStringProperty(name);
|
|
93 |
return this.getData().getStringProperty(name);
|
|
76 | 94 |
} catch (NotExistInXMLEntity e) { |
77 | 95 |
throw new PersistenceValueNotFoundException(name, e); |
78 | 96 |
} |
79 | 97 |
} |
80 |
return manager.create(createState(value)); |
|
98 |
return manager.create(manager.createState(value));
|
|
81 | 99 |
} |
82 | 100 |
|
83 | 101 |
public PersistentState getState(String name) |
84 | 102 |
throws PersistenceException { |
85 |
XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
|
|
103 |
XMLEntity value = this.getData().firstChild(name);
|
|
86 | 104 |
if (value == null) { |
87 | 105 |
throw new PersistenceValueNotFoundException(name); |
88 | 106 |
} |
89 |
return createState(value); |
|
107 |
return manager.createState(value);
|
|
90 | 108 |
} |
91 | 109 |
|
92 | 110 |
public boolean getBoolean(String name) |
93 | 111 |
throws PersistenceValueNotFoundException { |
94 | 112 |
try { |
95 |
return this.xmlEntity.getBooleanProperty(name);
|
|
113 |
return this.getData().getBooleanProperty(name);
|
|
96 | 114 |
} catch (NotExistInXMLEntity e) { |
97 | 115 |
throw new PersistenceValueNotFoundException(name, e); |
98 | 116 |
} |
... | ... | |
101 | 119 |
public double getDouble(String name) |
102 | 120 |
throws PersistenceValueNotFoundException { |
103 | 121 |
try { |
104 |
return this.xmlEntity.getDoubleProperty(name);
|
|
122 |
return this.getData().getDoubleProperty(name);
|
|
105 | 123 |
} catch (NotExistInXMLEntity e) { |
106 | 124 |
throw new PersistenceValueNotFoundException(name, e); |
107 | 125 |
} |
... | ... | |
109 | 127 |
|
110 | 128 |
public float getFloat(String name) throws PersistenceValueNotFoundException { |
111 | 129 |
try { |
112 |
return this.xmlEntity.getFloatProperty(name);
|
|
130 |
return this.getData().getFloatProperty(name);
|
|
113 | 131 |
} catch (NotExistInXMLEntity e) { |
114 | 132 |
throw new PersistenceValueNotFoundException(name, e); |
115 | 133 |
} |
... | ... | |
117 | 135 |
|
118 | 136 |
public int getInt(String name) throws PersistenceValueNotFoundException { |
119 | 137 |
try { |
120 |
return this.xmlEntity.getIntProperty(name);
|
|
138 |
return this.getData().getIntProperty(name);
|
|
121 | 139 |
} catch (NotExistInXMLEntity e) { |
122 | 140 |
throw new PersistenceValueNotFoundException(name, e); |
123 | 141 |
} |
... | ... | |
125 | 143 |
|
126 | 144 |
public long getLong(String name) throws PersistenceValueNotFoundException { |
127 | 145 |
try { |
128 |
return this.xmlEntity.getLongProperty(name);
|
|
146 |
return this.getData().getLongProperty(name);
|
|
129 | 147 |
} catch (NotExistInXMLEntity e) { |
130 | 148 |
throw new PersistenceValueNotFoundException(name, e); |
131 | 149 |
} |
132 | 150 |
} |
133 | 151 |
|
134 |
public PersistentState set(String name, String value) {
|
|
135 |
this.xmlEntity.putProperty(name, value);
|
|
136 |
return this; |
|
152 |
public String getString(String name)
|
|
153 |
throws PersistenceValueNotFoundException {
|
|
154 |
return this.getData().getStringProperty(name);
|
|
137 | 155 |
} |
138 | 156 |
|
139 |
public PersistentState set(String name, int value) { |
|
140 |
this.xmlEntity.putProperty(name, value); |
|
141 |
return this; |
|
157 |
public void set(String name, String value) { |
|
158 |
this.getData().putProperty(name, value); |
|
142 | 159 |
} |
143 | 160 |
|
144 |
public PersistentState set(String name, long value) { |
|
145 |
this.xmlEntity.putProperty(name, value); |
|
146 |
return this; |
|
161 |
public void set(String name, int value) { |
|
162 |
this.getData().putProperty(name, value); |
|
147 | 163 |
} |
148 | 164 |
|
149 |
public PersistentState set(String name, double value) { |
|
150 |
this.xmlEntity.putProperty(name, value); |
|
151 |
return this; |
|
165 |
public void set(String name, long value) { |
|
166 |
this.getData().putProperty(name, value); |
|
152 | 167 |
} |
153 | 168 |
|
154 |
public PersistentState set(String name, float value) { |
|
155 |
this.xmlEntity.putProperty(name, value); |
|
156 |
return this; |
|
169 |
public void set(String name, double value) { |
|
170 |
this.getData().putProperty(name, value); |
|
157 | 171 |
} |
158 | 172 |
|
159 |
public PersistentState set(String name, boolean value) { |
|
160 |
this.xmlEntity.putProperty(name, value); |
|
161 |
return this; |
|
173 |
public void set(String name, float value) { |
|
174 |
this.getData().putProperty(name, value); |
|
162 | 175 |
} |
163 | 176 |
|
164 |
public PersistentState set(String name, PersistentState state) { |
|
165 |
XMLEntityState lstate = (XMLEntityState) state; |
|
166 |
lstate.xmlEntity.putProperty(KEY_CHILD, name); |
|
167 |
this.xmlEntity.addChild(lstate.xmlEntity); |
|
168 |
return this; |
|
177 |
public void set(String name, boolean value) { |
|
178 |
this.getData().putProperty(name, value); |
|
169 | 179 |
} |
170 | 180 |
|
171 |
public PersistentState set(String name, Persistent obj) |
|
181 |
public void set(String name, PersistentState state) throws PersistenceException { |
|
182 |
if (state instanceof XMLEntityState) { |
|
183 |
XMLEntityState lstate = (XMLEntityState) state; |
|
184 |
|
|
185 |
XMLEntityState wrappingState = manager.createStateInstance(); |
|
186 |
wrappingState.setTheClass(PersistentState.class); |
|
187 |
wrappingState.setName(name); |
|
188 |
wrappingState.getData().addChild(lstate.getXMLEntity()); |
|
189 |
this.getData().addChild(wrappingState.xmlEntity); |
|
190 |
} |
|
191 |
else { |
|
192 |
// TODO if the state comes from a different implementation, use |
|
193 |
// TODO the names iterator to get all the values and persist them |
|
194 |
throw new PersistenceException("", "Not yet implemented", -1); |
|
195 |
// TODO use a proper exception format |
|
196 |
} |
|
197 |
} |
|
198 |
|
|
199 |
public void set(String name, Persistent obj) |
|
172 | 200 |
throws PersistenceException { |
173 |
XMLEntityState state = (XMLEntityState) createState(obj); |
|
174 |
state.xmlEntity.putProperty(KEY_CHILD, name); |
|
175 |
this.xmlEntity.addChild(state.xmlEntity); |
|
176 |
return this; |
|
201 |
XMLEntityState state = (XMLEntityState) manager.getState(obj); |
|
202 |
state.setName(name); |
|
203 |
this.getData().addChild(state.xmlEntity); |
|
177 | 204 |
} |
178 | 205 |
|
179 |
public class PersistentIterator implements Iterator, Persistent { |
|
180 |
private XMLEntity xmlEntity = null; |
|
181 |
private Iterator it; |
|
206 |
public static class PersistentIterator implements Iterator, Persistent { |
|
207 |
private XMLEntityState state = null; |
|
208 |
int current = 0; |
|
209 |
|
|
210 |
public PersistentIterator() { |
|
211 |
|
|
212 |
} |
|
213 |
|
|
182 | 214 |
public boolean hasNext() { |
183 |
return it.hasNext();
|
|
215 |
return (state!=null && current< state.getData().getChildrenCount());
|
|
184 | 216 |
} |
185 | 217 |
public Object next() { |
186 | 218 |
Object value = null; |
187 |
XMLEntity xmlEntity = (XMLEntity) it.next();
|
|
219 |
XMLEntity xmlEntity = (XMLEntity) state.getData().getChild(current++);
|
|
188 | 220 |
String className = xmlEntity.getStringProperty(KEY_CLASSNAME); |
189 | 221 |
if (className.equals(String.class.getName())) { |
190 |
value = xmlEntity.getObjectProperty("value");
|
|
222 |
value = xmlEntity.getObjectProperty(VALUE);
|
|
191 | 223 |
} else if (className.equals(Integer.class.getName())) { |
192 |
value = xmlEntity.getObjectProperty("value");
|
|
224 |
value = xmlEntity.getIntProperty(VALUE);
|
|
193 | 225 |
} else if (className.equals(Long.class.getName())) { |
194 |
value = xmlEntity.getObjectProperty("value");
|
|
226 |
value = xmlEntity.getLongProperty(VALUE);
|
|
195 | 227 |
} else if (className.equals(Double.class.getName())) { |
196 |
value = xmlEntity.getObjectProperty("value");
|
|
228 |
value = xmlEntity.getDoubleProperty(VALUE);
|
|
197 | 229 |
} else if (className.equals(Float.class.getName())) { |
198 |
value = xmlEntity.getObjectProperty("value");
|
|
230 |
value = xmlEntity.getFloatProperty(VALUE);
|
|
199 | 231 |
} else if (className.equals(Boolean.class.getName())) { |
200 |
value = xmlEntity.getObjectProperty("value"); |
|
201 |
} else { |
|
202 |
// FIXME: ???? Persistent |
|
232 |
value = xmlEntity.getBooleanProperty(VALUE); |
|
203 | 233 |
} |
234 |
else if (className.equals(PersistentState.class)) { |
|
235 |
if (xmlEntity.getChildrenCount()>0) { |
|
236 |
try { |
|
237 |
value = state.manager.create(state.manager.createState(xmlEntity)); |
|
238 |
} catch (PersistenceException e) { |
|
239 |
// FIXME |
|
240 |
throw new RuntimeException(e); |
|
241 |
} |
|
242 |
} |
|
243 |
} |
|
244 |
else { // suppose it is a Persistent object |
|
245 |
try { |
|
246 |
value = state.manager.create(state.manager.createState(xmlEntity)); |
|
247 |
} catch (PersistenceException e) { |
|
248 |
// FIXME |
|
249 |
throw new RuntimeException(e); |
|
250 |
} |
|
251 |
} |
|
204 | 252 |
return value; |
205 | 253 |
} |
254 |
|
|
206 | 255 |
public void remove() { |
207 | 256 |
throw new UnsupportedOperationException(); |
208 | 257 |
} |
209 | 258 |
public void setState(PersistentState state) throws PersistenceException { |
210 |
this.xmlEntity = ((XMLEntityState) state).getXMLEntity(); |
|
211 |
this.it = this.xmlEntity.findChildren(KEY_CHILD, ITEM); |
|
259 |
if (state instanceof XMLEntityState) { |
|
260 |
this.state = (XMLEntityState) state; |
|
261 |
current = 0; |
|
262 |
} |
|
263 |
else { |
|
264 |
// FIXME |
|
265 |
throw new PersistenceException("", "Not supported yet", -1); |
|
266 |
} |
|
212 | 267 |
} |
213 |
public PersistentState getState() throws PersistenceException { |
|
214 |
throw new UnsupportedOperationException(); |
|
215 |
} |
|
216 |
public void loadState(PersistentState state) |
|
268 |
|
|
269 |
public void saveToState(PersistentState state) |
|
217 | 270 |
throws PersistenceException { |
218 | 271 |
throw new UnsupportedOperationException(); |
219 | 272 |
} |
220 | 273 |
} |
221 |
|
|
222 |
public Iterator getIterator(String name) throws PersistenceException { |
|
223 |
XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name); |
|
274 |
|
|
275 |
public Iterator getIterator(String name) |
|
276 |
throws PersistenceException, PersistenceValueNotFoundException |
|
277 |
{ |
|
278 |
XMLEntity value = this.getData().firstChild(name); |
|
224 | 279 |
if (value == null) { |
225 | 280 |
throw new PersistenceValueNotFoundException(name); |
226 | 281 |
} |
227 |
return (Iterator) manager.create(createState(value)); |
|
282 |
return (Iterator) manager.create(manager.createState(value));
|
|
228 | 283 |
} |
229 | 284 |
|
230 |
public PersistentState set(String name, Iterator it)
|
|
285 |
public void set(String name, Iterator it)
|
|
231 | 286 |
throws PersistenceException { |
232 |
XMLEntityState stateList = (XMLEntityState) createState(new XMLEntity()); |
|
287 |
XMLEntityState stateList = manager.createStateInstance(); |
|
288 |
stateList.setName(name); |
|
233 | 289 |
stateList.setTheClass(PersistentIterator.class); |
234 | 290 |
while (it.hasNext()) { |
235 | 291 |
Object value = it.next(); |
236 |
XMLEntityState stateItem = (XMLEntityState) createState(value); |
|
237 |
if (value instanceof Persistent || value instanceof Integer |
|
292 |
if (value instanceof Persistent) { |
|
293 |
XMLEntityState persistentState = |
|
294 |
(XMLEntityState) manager.getState((Persistent)value); |
|
295 |
persistentState.setName(ITEM); |
|
296 |
stateList.getData().addChild(persistentState.xmlEntity); |
|
297 |
} |
|
298 |
else if (value instanceof XMLEntityState) { |
|
299 |
XMLEntityState persistentState = |
|
300 |
(XMLEntityState) value; |
|
301 |
persistentState.setName(ITEM); |
|
302 |
stateList.getData().addChild(persistentState.xmlEntity); |
|
303 |
} |
|
304 |
else if (value instanceof Integer |
|
238 | 305 |
|| value instanceof Long || value instanceof Double |
239 | 306 |
|| value instanceof Float || value instanceof String |
240 | 307 |
|| value instanceof Boolean) { |
241 |
stateItem.xmlEntity.putProperty("value", value); |
|
242 |
stateItem.xmlEntity.putProperty(KEY_CHILD, ITEM); |
|
243 |
stateList.xmlEntity.addChild(stateItem.xmlEntity); |
|
244 |
|
|
308 |
XMLEntityState stateItem = (XMLEntityState) manager.createStateInstance(); |
|
309 |
stateItem.setName(ITEM); |
|
310 |
stateItem.xmlEntity.putProperty(KEY_CLASSNAME, value.getClass().getName()); |
|
311 |
stateItem.xmlEntity.putProperty(VALUE, value); |
|
312 |
stateList.getData().addChild(stateItem.xmlEntity); |
|
313 |
|
|
245 | 314 |
} else { |
246 |
// FIXME: que pete |
|
315 |
//TODO add a meaningful message and maybe use a more specific exception |
|
316 |
throw new PersistenceException(new RuntimeException()); |
|
247 | 317 |
} |
248 | 318 |
} |
249 |
stateList.xmlEntity.putProperty(KEY_CHILD, name); |
|
250 |
this.xmlEntity.addChild(stateList.xmlEntity); |
|
251 |
return this; |
|
319 |
// stateList.xmlEntity.putProperty(KEY_CHILD, name); |
|
320 |
this.getData().addChild(stateList.xmlEntity); |
|
252 | 321 |
} |
322 |
|
|
323 |
|
|
253 | 324 |
|
254 | 325 |
private class NamesIterator implements Iterator { |
255 | 326 |
|
... | ... | |
308 | 379 |
return new NamesIterator(this.xmlEntity); |
309 | 380 |
} |
310 | 381 |
|
311 |
public PersistentState set(String name, Object value)
|
|
382 |
public void set(String name, Object value)
|
|
312 | 383 |
throws PersistenceException { |
313 | 384 |
if (value instanceof Persistent) { |
314 |
XMLEntityState state = (XMLEntityState) createState(value); |
|
315 |
state.xmlEntity.putProperty(KEY_CHILD, name); |
|
316 |
this.xmlEntity.addChild(state.xmlEntity); |
|
317 |
|
|
385 |
set(name, (Persistent)value); |
|
318 | 386 |
} else if (value instanceof Integer || value instanceof Long |
319 | 387 |
|| value instanceof Double || value instanceof Float |
320 | 388 |
|| value instanceof String || value instanceof Boolean) { |
321 |
this.xmlEntity.putProperty(name, value); |
|
322 |
} else { |
|
389 |
this.getData().putProperty(name, value); |
|
390 |
} else if (value instanceof Iterator) { |
|
391 |
set(name, (Iterator)value); |
|
392 |
} |
|
393 |
else { |
|
323 | 394 |
throw new IllegalArgumentException(name); |
324 | 395 |
} |
325 |
return this; |
|
326 | 396 |
} |
327 | 397 |
|
398 |
public void load(Reader reader) throws PersistenceException { |
|
399 |
try { |
|
400 |
Object obj = XmlTag.unmarshal(reader); |
|
401 |
xmlEntity = new XMLEntity((XmlTag)obj); |
|
402 |
data = xmlEntity.firstChild(KEY_DATA); |
|
403 |
} catch (MarshalException e) { |
|
404 |
throw new PersistenceException(e); |
|
405 |
} catch (ValidationException e) { |
|
406 |
throw new PersistenceException(e); |
|
407 |
} |
|
408 |
} |
|
409 |
|
|
410 |
public void save(Writer writer) throws PersistenceException { |
|
411 |
try { |
|
412 |
Marshaller m = new Marshaller(writer); |
|
413 |
m.setEncoding(PROJECTENCODING); |
|
414 |
m.marshal(xmlEntity.getXmlTag()); |
|
415 |
} catch (IOException e) { |
|
416 |
throw new PersistenceException(e); |
|
417 |
} catch (MarshalException e) { |
|
418 |
throw new PersistenceException(e); |
|
419 |
} catch (ValidationException e) { |
|
420 |
throw new PersistenceException(e); |
|
421 |
} |
|
422 |
|
|
423 |
} |
|
328 | 424 |
} |
Also available in: Unified diff