svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libIverUtiles / src / org / gvsig / tools / persistence / xmlentity / XMLEntityState.java @ 28076
History | View | Annotate | Download (11.9 KB)
1 |
package org.gvsig.tools.persistence.xmlentity; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.Reader; |
5 |
import java.io.Writer; |
6 |
import java.util.Iterator; |
7 |
import java.util.NoSuchElementException; |
8 |
|
9 |
import org.exolab.castor.xml.MarshalException; |
10 |
import org.exolab.castor.xml.Marshaller; |
11 |
import org.exolab.castor.xml.ValidationException; |
12 |
import org.gvsig.tools.persistence.PersistenceException; |
13 |
import org.gvsig.tools.persistence.PersistenceValueNotFoundException; |
14 |
import org.gvsig.tools.persistence.Persistent; |
15 |
import org.gvsig.tools.persistence.PersistentState; |
16 |
import org.gvsig.tools.persistence.impl.AbstractPersistentState; |
17 |
|
18 |
import com.iver.utiles.NotExistInXMLEntity; |
19 |
import com.iver.utiles.XMLEntity; |
20 |
import com.iver.utiles.xmlEntity.generate.XmlTag; |
21 |
|
22 |
public class XMLEntityState extends AbstractPersistentState { |
23 |
final private static int DATA_POS = 0; |
24 |
|
25 |
final private static String KEY_CLASSNAME = "_classname_"; |
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"; |
33 |
|
34 |
protected XMLEntity xmlEntity;
|
35 |
protected XMLEntityManager manager;
|
36 |
protected XMLEntity data;
|
37 |
|
38 |
public XMLEntityState(XMLEntityManager manager) {
|
39 |
this(manager, new XMLEntity()); |
40 |
} |
41 |
|
42 |
public XMLEntityState(XMLEntityManager manager, XMLEntity xmlEntity) {
|
43 |
this.manager = manager;
|
44 |
this.xmlEntity = xmlEntity;
|
45 |
this.data = xmlEntity.firstChild(KEY_DATA);
|
46 |
} |
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 |
|
54 |
public XMLEntity getXMLEntity() {
|
55 |
return this.xmlEntity; |
56 |
} |
57 |
|
58 |
public void setTheClass(Object obj) { |
59 |
this.xmlEntity.putProperty(KEY_CLASSNAME, obj.getClass().getName());
|
60 |
} |
61 |
|
62 |
public void setTheClass(Class theClass) { |
63 |
this.xmlEntity.putProperty(KEY_CLASSNAME, theClass.getName());
|
64 |
} |
65 |
|
66 |
public void setTheClass(String name) { |
67 |
this.xmlEntity.putProperty(KEY_CLASSNAME, name);
|
68 |
} |
69 |
|
70 |
public String getTheClassName() { |
71 |
return this.xmlEntity.getStringProperty(KEY_CLASSNAME); |
72 |
} |
73 |
|
74 |
protected XMLEntity getData() {
|
75 |
if (this.data==null) { |
76 |
init(); |
77 |
} |
78 |
return this.data; |
79 |
} |
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 |
|
89 |
public Object get(String name) throws PersistenceException { |
90 |
XMLEntity value = this.getData().firstChild(name);
|
91 |
if( value == null ) { |
92 |
try {
|
93 |
return this.getData().getStringProperty(name); |
94 |
} catch (NotExistInXMLEntity e) {
|
95 |
throw new PersistenceValueNotFoundException(name, e); |
96 |
} |
97 |
} |
98 |
return manager.create(manager.createState(value));
|
99 |
} |
100 |
|
101 |
public PersistentState getState(String name) |
102 |
throws PersistenceException {
|
103 |
XMLEntity value = this.getData().firstChild(name);
|
104 |
if (value == null) { |
105 |
throw new PersistenceValueNotFoundException(name); |
106 |
} |
107 |
return manager.createState(value);
|
108 |
} |
109 |
|
110 |
public boolean getBoolean(String name) |
111 |
throws PersistenceValueNotFoundException {
|
112 |
try {
|
113 |
return this.getData().getBooleanProperty(name); |
114 |
} catch (NotExistInXMLEntity e) {
|
115 |
throw new PersistenceValueNotFoundException(name, e); |
116 |
} |
117 |
} |
118 |
|
119 |
public double getDouble(String name) |
120 |
throws PersistenceValueNotFoundException {
|
121 |
try {
|
122 |
return this.getData().getDoubleProperty(name); |
123 |
} catch (NotExistInXMLEntity e) {
|
124 |
throw new PersistenceValueNotFoundException(name, e); |
125 |
} |
126 |
} |
127 |
|
128 |
public float getFloat(String name) throws PersistenceValueNotFoundException { |
129 |
try {
|
130 |
return this.getData().getFloatProperty(name); |
131 |
} catch (NotExistInXMLEntity e) {
|
132 |
throw new PersistenceValueNotFoundException(name, e); |
133 |
} |
134 |
} |
135 |
|
136 |
public int getInt(String name) throws PersistenceValueNotFoundException { |
137 |
try {
|
138 |
return this.getData().getIntProperty(name); |
139 |
} catch (NotExistInXMLEntity e) {
|
140 |
throw new PersistenceValueNotFoundException(name, e); |
141 |
} |
142 |
} |
143 |
|
144 |
public long getLong(String name) throws PersistenceValueNotFoundException { |
145 |
try {
|
146 |
return this.getData().getLongProperty(name); |
147 |
} catch (NotExistInXMLEntity e) {
|
148 |
throw new PersistenceValueNotFoundException(name, e); |
149 |
} |
150 |
} |
151 |
|
152 |
public String getString(String name) |
153 |
throws PersistenceValueNotFoundException {
|
154 |
return this.getData().getStringProperty(name); |
155 |
} |
156 |
|
157 |
public void set(String name, String value) { |
158 |
this.getData().putProperty(name, value);
|
159 |
} |
160 |
|
161 |
public void set(String name, int value) { |
162 |
this.getData().putProperty(name, value);
|
163 |
} |
164 |
|
165 |
public void set(String name, long value) { |
166 |
this.getData().putProperty(name, value);
|
167 |
} |
168 |
|
169 |
public void set(String name, double value) { |
170 |
this.getData().putProperty(name, value);
|
171 |
} |
172 |
|
173 |
public void set(String name, float value) { |
174 |
this.getData().putProperty(name, value);
|
175 |
} |
176 |
|
177 |
public void set(String name, boolean value) { |
178 |
this.getData().putProperty(name, value);
|
179 |
} |
180 |
|
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) |
200 |
throws PersistenceException {
|
201 |
XMLEntityState state = (XMLEntityState) manager.getState(obj); |
202 |
state.setName(name); |
203 |
this.getData().addChild(state.xmlEntity);
|
204 |
} |
205 |
|
206 |
public static class PersistentIterator implements Iterator, Persistent { |
207 |
private XMLEntityState state = null; |
208 |
int current = 0; |
209 |
|
210 |
public PersistentIterator() {
|
211 |
|
212 |
} |
213 |
|
214 |
public boolean hasNext() { |
215 |
return (state!=null && current< state.getData().getChildrenCount()); |
216 |
} |
217 |
public Object next() { |
218 |
Object value = null; |
219 |
XMLEntity xmlEntity = (XMLEntity) state.getData().getChild(current++); |
220 |
String className = xmlEntity.getStringProperty(KEY_CLASSNAME);
|
221 |
if (className.equals(String.class.getName())) { |
222 |
value = xmlEntity.getObjectProperty(VALUE); |
223 |
} else if (className.equals(Integer.class.getName())) { |
224 |
value = xmlEntity.getIntProperty(VALUE); |
225 |
} else if (className.equals(Long.class.getName())) { |
226 |
value = xmlEntity.getLongProperty(VALUE); |
227 |
} else if (className.equals(Double.class.getName())) { |
228 |
value = xmlEntity.getDoubleProperty(VALUE); |
229 |
} else if (className.equals(Float.class.getName())) { |
230 |
value = xmlEntity.getFloatProperty(VALUE); |
231 |
} else if (className.equals(Boolean.class.getName())) { |
232 |
value = xmlEntity.getBooleanProperty(VALUE); |
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 |
} |
252 |
return value;
|
253 |
} |
254 |
|
255 |
public void remove() { |
256 |
throw new UnsupportedOperationException(); |
257 |
} |
258 |
public void setState(PersistentState state) throws PersistenceException { |
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 |
} |
267 |
} |
268 |
|
269 |
public void saveToState(PersistentState state) |
270 |
throws PersistenceException {
|
271 |
throw new UnsupportedOperationException(); |
272 |
} |
273 |
} |
274 |
|
275 |
public Iterator getIterator(String name) |
276 |
throws PersistenceException, PersistenceValueNotFoundException
|
277 |
{ |
278 |
XMLEntity value = this.getData().firstChild(name);
|
279 |
if (value == null) { |
280 |
throw new PersistenceValueNotFoundException(name); |
281 |
} |
282 |
return (Iterator) manager.create(manager.createState(value)); |
283 |
} |
284 |
|
285 |
public void set(String name, Iterator it) |
286 |
throws PersistenceException {
|
287 |
XMLEntityState stateList = manager.createStateInstance(); |
288 |
stateList.setName(name); |
289 |
stateList.setTheClass(PersistentIterator.class); |
290 |
while (it.hasNext()) {
|
291 |
Object value = it.next();
|
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 |
305 |
|| value instanceof Long || value instanceof Double |
306 |
|| value instanceof Float || value instanceof String |
307 |
|| value instanceof Boolean) { |
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 |
|
314 |
} else {
|
315 |
//TODO add a meaningful message and maybe use a more specific exception
|
316 |
throw new PersistenceException(new RuntimeException()); |
317 |
} |
318 |
} |
319 |
// stateList.xmlEntity.putProperty(KEY_CHILD, name);
|
320 |
this.getData().addChild(stateList.xmlEntity);
|
321 |
} |
322 |
|
323 |
|
324 |
|
325 |
private class NamesIterator implements Iterator { |
326 |
|
327 |
private String name; |
328 |
private XMLEntity xmlEntity;
|
329 |
private int index; |
330 |
private int propertiyCount; |
331 |
|
332 |
public NamesIterator(XMLEntity xmlEntity) {
|
333 |
this.xmlEntity = xmlEntity;
|
334 |
this.name = null; |
335 |
this.index = 0; |
336 |
this.propertiyCount = xmlEntity.getPropertyCount();
|
337 |
} |
338 |
|
339 |
private void testNext() { |
340 |
if (this.name != null) { |
341 |
return;
|
342 |
} |
343 |
String tmpName;
|
344 |
while (this.index < this.propertiyCount) { |
345 |
tmpName = this.xmlEntity.getPropertyName(index);
|
346 |
index++; |
347 |
if (!tmpName.equals(KEY_CHILD) || tmpName.equals(KEY_CLASSNAME)) {
|
348 |
continue;
|
349 |
} else {
|
350 |
this.name = tmpName;
|
351 |
break;
|
352 |
} |
353 |
} |
354 |
|
355 |
} |
356 |
|
357 |
public boolean hasNext() { |
358 |
this.testNext();
|
359 |
return this.index < this.propertiyCount; |
360 |
} |
361 |
|
362 |
public Object next() { |
363 |
this.testNext();
|
364 |
if (this.name == null) { |
365 |
throw new NoSuchElementException(); |
366 |
} |
367 |
String tmpName = this.name; |
368 |
this.name = null; |
369 |
return tmpName;
|
370 |
} |
371 |
|
372 |
public void remove() { |
373 |
throw new UnsupportedOperationException(); |
374 |
} |
375 |
|
376 |
} |
377 |
|
378 |
public Iterator getNames() { |
379 |
return new NamesIterator(this.xmlEntity); |
380 |
} |
381 |
|
382 |
public void set(String name, Object value) |
383 |
throws PersistenceException {
|
384 |
if (value instanceof Persistent) { |
385 |
set(name, (Persistent)value); |
386 |
} else if (value instanceof Integer || value instanceof Long |
387 |
|| value instanceof Double || value instanceof Float |
388 |
|| value instanceof String || value instanceof Boolean) { |
389 |
this.getData().putProperty(name, value);
|
390 |
} else if (value instanceof Iterator) { |
391 |
set(name, (Iterator)value);
|
392 |
} |
393 |
else {
|
394 |
throw new IllegalArgumentException(name); |
395 |
} |
396 |
} |
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 |
} |
424 |
} |