svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libIverUtiles / src / org / gvsig / tools / persistence / xmlentity / XMLEntityState.java @ 28468
History | View | Annotate | Download (11 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 boolean getBoolean(String name) |
102 |
throws PersistenceValueNotFoundException {
|
103 |
try {
|
104 |
return this.getData().getBooleanProperty(name); |
105 |
} catch (NotExistInXMLEntity e) {
|
106 |
throw new PersistenceValueNotFoundException(name, e); |
107 |
} |
108 |
} |
109 |
|
110 |
public double getDouble(String name) |
111 |
throws PersistenceValueNotFoundException {
|
112 |
try {
|
113 |
return this.getData().getDoubleProperty(name); |
114 |
} catch (NotExistInXMLEntity e) {
|
115 |
throw new PersistenceValueNotFoundException(name, e); |
116 |
} |
117 |
} |
118 |
|
119 |
public float getFloat(String name) throws PersistenceValueNotFoundException { |
120 |
try {
|
121 |
return this.getData().getFloatProperty(name); |
122 |
} catch (NotExistInXMLEntity e) {
|
123 |
throw new PersistenceValueNotFoundException(name, e); |
124 |
} |
125 |
} |
126 |
|
127 |
public int getInt(String name) throws PersistenceValueNotFoundException { |
128 |
try {
|
129 |
return this.getData().getIntProperty(name); |
130 |
} catch (NotExistInXMLEntity e) {
|
131 |
throw new PersistenceValueNotFoundException(name, e); |
132 |
} |
133 |
} |
134 |
|
135 |
public long getLong(String name) throws PersistenceValueNotFoundException { |
136 |
try {
|
137 |
return this.getData().getLongProperty(name); |
138 |
} catch (NotExistInXMLEntity e) {
|
139 |
throw new PersistenceValueNotFoundException(name, e); |
140 |
} |
141 |
} |
142 |
|
143 |
public String getString(String name) |
144 |
throws PersistenceValueNotFoundException {
|
145 |
return this.getData().getStringProperty(name); |
146 |
} |
147 |
|
148 |
public void set(String name, String value) { |
149 |
this.getData().putProperty(name, value);
|
150 |
} |
151 |
|
152 |
public void set(String name, int value) { |
153 |
this.getData().putProperty(name, value);
|
154 |
} |
155 |
|
156 |
public void set(String name, long value) { |
157 |
this.getData().putProperty(name, value);
|
158 |
} |
159 |
|
160 |
public void set(String name, double value) { |
161 |
this.getData().putProperty(name, value);
|
162 |
} |
163 |
|
164 |
public void set(String name, float value) { |
165 |
this.getData().putProperty(name, value);
|
166 |
} |
167 |
|
168 |
public void set(String name, boolean value) { |
169 |
this.getData().putProperty(name, value);
|
170 |
} |
171 |
|
172 |
public void set(String name, Persistent obj) |
173 |
throws PersistenceException {
|
174 |
XMLEntityState state = (XMLEntityState) manager.getState(obj); |
175 |
state.setName(name); |
176 |
this.getData().addChild(state.xmlEntity);
|
177 |
} |
178 |
|
179 |
public static class PersistentIterator implements Iterator, Persistent { |
180 |
private XMLEntityState state = null; |
181 |
int current = 0; |
182 |
|
183 |
public PersistentIterator() {
|
184 |
|
185 |
} |
186 |
|
187 |
public boolean hasNext() { |
188 |
return (state!=null && current< state.getData().getChildrenCount()); |
189 |
} |
190 |
public Object next() { |
191 |
Object value = null; |
192 |
XMLEntity xmlEntity = (XMLEntity) state.getData().getChild(current++); |
193 |
String className = xmlEntity.getStringProperty(KEY_CLASSNAME);
|
194 |
if (className.equals(String.class.getName())) { |
195 |
value = xmlEntity.getObjectProperty(VALUE); |
196 |
} else if (className.equals(Integer.class.getName())) { |
197 |
value = xmlEntity.getIntProperty(VALUE); |
198 |
} else if (className.equals(Long.class.getName())) { |
199 |
value = xmlEntity.getLongProperty(VALUE); |
200 |
} else if (className.equals(Double.class.getName())) { |
201 |
value = xmlEntity.getDoubleProperty(VALUE); |
202 |
} else if (className.equals(Float.class.getName())) { |
203 |
value = xmlEntity.getFloatProperty(VALUE); |
204 |
} else if (className.equals(Boolean.class.getName())) { |
205 |
value = xmlEntity.getBooleanProperty(VALUE); |
206 |
} |
207 |
else if (className.equals(PersistentState.class)) { |
208 |
if (xmlEntity.getChildrenCount()>0) { |
209 |
try {
|
210 |
value = state.manager.create(state.manager.createState(xmlEntity)); |
211 |
} catch (PersistenceException e) {
|
212 |
// FIXME
|
213 |
throw new RuntimeException(e); |
214 |
} |
215 |
} |
216 |
} |
217 |
else { // suppose it is a Persistent object |
218 |
try {
|
219 |
value = state.manager.create(state.manager.createState(xmlEntity)); |
220 |
} catch (PersistenceException e) {
|
221 |
// FIXME
|
222 |
throw new RuntimeException(e); |
223 |
} |
224 |
} |
225 |
return value;
|
226 |
} |
227 |
|
228 |
public void remove() { |
229 |
throw new UnsupportedOperationException(); |
230 |
} |
231 |
public void loadFromState(PersistentState state) throws PersistenceException { |
232 |
if (state instanceof XMLEntityState) { |
233 |
this.state = (XMLEntityState) state;
|
234 |
current = 0;
|
235 |
} |
236 |
else {
|
237 |
throw new PersistenceException("setState(PersistentState): Not supported yet"); |
238 |
} |
239 |
} |
240 |
|
241 |
public void saveToState(PersistentState state) |
242 |
throws PersistenceException {
|
243 |
throw new UnsupportedOperationException(); |
244 |
} |
245 |
} |
246 |
|
247 |
public Iterator getIterator(String name) |
248 |
throws PersistenceException, PersistenceValueNotFoundException
|
249 |
{ |
250 |
XMLEntity value = this.getData().firstChild(name);
|
251 |
if (value == null) { |
252 |
throw new PersistenceValueNotFoundException(name); |
253 |
} |
254 |
return (Iterator) manager.create(manager.createState(value)); |
255 |
} |
256 |
|
257 |
public void set(String name, Iterator it) |
258 |
throws PersistenceException {
|
259 |
XMLEntityState stateList = manager.createStateInstance(); |
260 |
stateList.setName(name); |
261 |
stateList.setTheClass(PersistentIterator.class); |
262 |
while (it.hasNext()) {
|
263 |
Object value = it.next();
|
264 |
if (value instanceof Persistent) { |
265 |
XMLEntityState persistentState = |
266 |
(XMLEntityState) manager.getState((Persistent)value); |
267 |
persistentState.setName(ITEM); |
268 |
stateList.getData().addChild(persistentState.xmlEntity); |
269 |
} |
270 |
else if (value instanceof XMLEntityState) { |
271 |
XMLEntityState persistentState = |
272 |
(XMLEntityState) value; |
273 |
persistentState.setName(ITEM); |
274 |
stateList.getData().addChild(persistentState.xmlEntity); |
275 |
} |
276 |
else if (value instanceof Integer |
277 |
|| value instanceof Long || value instanceof Double |
278 |
|| value instanceof Float || value instanceof String |
279 |
|| value instanceof Boolean) { |
280 |
XMLEntityState stateItem = (XMLEntityState) manager.createStateInstance(); |
281 |
stateItem.setName(ITEM); |
282 |
stateItem.xmlEntity.putProperty(KEY_CLASSNAME, value.getClass().getName()); |
283 |
stateItem.xmlEntity.putProperty(VALUE, value); |
284 |
stateList.getData().addChild(stateItem.xmlEntity); |
285 |
|
286 |
} else {
|
287 |
//TODO add a meaningful message and maybe use a more specific exception
|
288 |
throw new PersistenceException(new RuntimeException()); |
289 |
} |
290 |
} |
291 |
// stateList.xmlEntity.putProperty(KEY_CHILD, name);
|
292 |
this.getData().addChild(stateList.xmlEntity);
|
293 |
} |
294 |
|
295 |
|
296 |
|
297 |
private class NamesIterator implements Iterator { |
298 |
|
299 |
private String name; |
300 |
private XMLEntity xmlEntity;
|
301 |
private int index; |
302 |
private int propertiyCount; |
303 |
|
304 |
public NamesIterator(XMLEntity xmlEntity) {
|
305 |
this.xmlEntity = xmlEntity;
|
306 |
this.name = null; |
307 |
this.index = 0; |
308 |
this.propertiyCount = xmlEntity.getPropertyCount();
|
309 |
} |
310 |
|
311 |
private void testNext() { |
312 |
if (this.name != null) { |
313 |
return;
|
314 |
} |
315 |
String tmpName;
|
316 |
while (this.index < this.propertiyCount) { |
317 |
tmpName = this.xmlEntity.getPropertyName(index);
|
318 |
index++; |
319 |
if (!tmpName.equals(KEY_CHILD) || tmpName.equals(KEY_CLASSNAME)) {
|
320 |
continue;
|
321 |
} else {
|
322 |
this.name = tmpName;
|
323 |
break;
|
324 |
} |
325 |
} |
326 |
|
327 |
} |
328 |
|
329 |
public boolean hasNext() { |
330 |
this.testNext();
|
331 |
return this.index < this.propertiyCount; |
332 |
} |
333 |
|
334 |
public Object next() { |
335 |
this.testNext();
|
336 |
if (this.name == null) { |
337 |
throw new NoSuchElementException(); |
338 |
} |
339 |
String tmpName = this.name; |
340 |
this.name = null; |
341 |
return tmpName;
|
342 |
} |
343 |
|
344 |
public void remove() { |
345 |
throw new UnsupportedOperationException(); |
346 |
} |
347 |
|
348 |
} |
349 |
|
350 |
public Iterator getNames() { |
351 |
return new NamesIterator(this.xmlEntity); |
352 |
} |
353 |
|
354 |
public void set(String name, Object value) |
355 |
throws PersistenceException {
|
356 |
if (value instanceof Persistent) { |
357 |
set(name, (Persistent)value); |
358 |
} else if (value instanceof Integer || value instanceof Long |
359 |
|| value instanceof Double || value instanceof Float |
360 |
|| value instanceof String || value instanceof Boolean) { |
361 |
this.getData().putProperty(name, value);
|
362 |
} else if (value instanceof Iterator) { |
363 |
set(name, (Iterator)value);
|
364 |
} |
365 |
else {
|
366 |
throw new IllegalArgumentException(name); |
367 |
} |
368 |
} |
369 |
|
370 |
public void load(Reader reader) throws PersistenceException { |
371 |
try {
|
372 |
Object obj = XmlTag.unmarshal(reader);
|
373 |
xmlEntity = new XMLEntity((XmlTag)obj);
|
374 |
data = xmlEntity.firstChild(KEY_DATA); |
375 |
} catch (MarshalException e) { |
376 |
throw new PersistenceException(e); |
377 |
} catch (ValidationException e) {
|
378 |
throw new PersistenceException(e); |
379 |
} |
380 |
} |
381 |
|
382 |
public void save(Writer writer) throws PersistenceException { |
383 |
try {
|
384 |
Marshaller m = new Marshaller(writer);
|
385 |
m.setEncoding(PROJECTENCODING); |
386 |
m.marshal(xmlEntity.getXmlTag()); |
387 |
} catch (IOException e) { |
388 |
throw new PersistenceException(e); |
389 |
} catch (MarshalException e) { |
390 |
throw new PersistenceException(e); |
391 |
} catch (ValidationException e) {
|
392 |
throw new PersistenceException(e); |
393 |
} |
394 |
|
395 |
} |
396 |
} |