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 | 24378 | cordinyana | package org.gvsig.tools.persistence.xmlentity; |
---|---|---|---|
2 | 24019 | jjdelcerro | |
3 | 28076 | cmartinez | import java.io.IOException; |
4 | import java.io.Reader; |
||
5 | import java.io.Writer; |
||
6 | 24042 | jjdelcerro | import java.util.Iterator; |
7 | 24946 | jmvivo | import java.util.NoSuchElementException; |
8 | 24042 | jjdelcerro | |
9 | 28076 | cmartinez | import org.exolab.castor.xml.MarshalException; |
10 | import org.exolab.castor.xml.Marshaller; |
||
11 | import org.exolab.castor.xml.ValidationException; |
||
12 | 24019 | jjdelcerro | 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 | 28076 | cmartinez | import org.gvsig.tools.persistence.impl.AbstractPersistentState; |
17 | 24019 | jjdelcerro | |
18 | import com.iver.utiles.NotExistInXMLEntity; |
||
19 | import com.iver.utiles.XMLEntity; |
||
20 | 28076 | cmartinez | import com.iver.utiles.xmlEntity.generate.XmlTag; |
21 | 24019 | jjdelcerro | |
22 | 28076 | cmartinez | public class XMLEntityState extends AbstractPersistentState { |
23 | final private static int DATA_POS = 0; |
||
24 | |||
25 | 24019 | jjdelcerro | final private static String KEY_CLASSNAME = "_classname_"; |
26 | 28076 | cmartinez | 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 | 24019 | jjdelcerro | |
34 | protected XMLEntity xmlEntity;
|
||
35 | protected XMLEntityManager manager;
|
||
36 | 28076 | cmartinez | protected XMLEntity data;
|
37 | 24019 | jjdelcerro | |
38 | public XMLEntityState(XMLEntityManager manager) {
|
||
39 | 28076 | cmartinez | this(manager, new XMLEntity()); |
40 | 24019 | jjdelcerro | } |
41 | |||
42 | public XMLEntityState(XMLEntityManager manager, XMLEntity xmlEntity) {
|
||
43 | this.manager = manager;
|
||
44 | this.xmlEntity = xmlEntity;
|
||
45 | 28076 | cmartinez | this.data = xmlEntity.firstChild(KEY_DATA);
|
46 | 24019 | jjdelcerro | } |
47 | |||
48 | 28076 | cmartinez | protected void init() { |
49 | this.data = new XMLEntity(); |
||
50 | this.data.getXmlTag().setName(KEY_DATA);
|
||
51 | this.xmlEntity.addChild(this.data); |
||
52 | } |
||
53 | |||
54 | 24019 | jjdelcerro | 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 | 28076 | cmartinez | return this.xmlEntity.getStringProperty(KEY_CLASSNAME); |
72 | } |
||
73 | |||
74 | protected XMLEntity getData() {
|
||
75 | if (this.data==null) { |
||
76 | init(); |
||
77 | 24019 | jjdelcerro | } |
78 | 28076 | cmartinez | return this.data; |
79 | 24019 | jjdelcerro | } |
80 | |||
81 | 28076 | cmartinez | 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 | 24019 | jjdelcerro | public Object get(String name) throws PersistenceException { |
90 | 28076 | cmartinez | XMLEntity value = this.getData().firstChild(name);
|
91 | 24019 | jjdelcerro | if( value == null ) { |
92 | try {
|
||
93 | 28076 | cmartinez | return this.getData().getStringProperty(name); |
94 | 24019 | jjdelcerro | } catch (NotExistInXMLEntity e) {
|
95 | throw new PersistenceValueNotFoundException(name, e); |
||
96 | } |
||
97 | } |
||
98 | 28076 | cmartinez | return manager.create(manager.createState(value));
|
99 | 24019 | jjdelcerro | } |
100 | |||
101 | public boolean getBoolean(String name) |
||
102 | throws PersistenceValueNotFoundException {
|
||
103 | try {
|
||
104 | 28076 | cmartinez | return this.getData().getBooleanProperty(name); |
105 | 24019 | jjdelcerro | } catch (NotExistInXMLEntity e) {
|
106 | throw new PersistenceValueNotFoundException(name, e); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | public double getDouble(String name) |
||
111 | throws PersistenceValueNotFoundException {
|
||
112 | try {
|
||
113 | 28076 | cmartinez | return this.getData().getDoubleProperty(name); |
114 | 24019 | jjdelcerro | } catch (NotExistInXMLEntity e) {
|
115 | throw new PersistenceValueNotFoundException(name, e); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | public float getFloat(String name) throws PersistenceValueNotFoundException { |
||
120 | try {
|
||
121 | 28076 | cmartinez | return this.getData().getFloatProperty(name); |
122 | 24019 | jjdelcerro | } catch (NotExistInXMLEntity e) {
|
123 | throw new PersistenceValueNotFoundException(name, e); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | public int getInt(String name) throws PersistenceValueNotFoundException { |
||
128 | try {
|
||
129 | 28076 | cmartinez | return this.getData().getIntProperty(name); |
130 | 24019 | jjdelcerro | } catch (NotExistInXMLEntity e) {
|
131 | throw new PersistenceValueNotFoundException(name, e); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | public long getLong(String name) throws PersistenceValueNotFoundException { |
||
136 | try {
|
||
137 | 28076 | cmartinez | return this.getData().getLongProperty(name); |
138 | 24019 | jjdelcerro | } catch (NotExistInXMLEntity e) {
|
139 | throw new PersistenceValueNotFoundException(name, e); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | 28076 | cmartinez | public String getString(String name) |
144 | throws PersistenceValueNotFoundException {
|
||
145 | return this.getData().getStringProperty(name); |
||
146 | 24019 | jjdelcerro | } |
147 | |||
148 | 28076 | cmartinez | public void set(String name, String value) { |
149 | this.getData().putProperty(name, value);
|
||
150 | 24019 | jjdelcerro | } |
151 | |||
152 | 28076 | cmartinez | public void set(String name, int value) { |
153 | this.getData().putProperty(name, value);
|
||
154 | 24019 | jjdelcerro | } |
155 | |||
156 | 28076 | cmartinez | public void set(String name, long value) { |
157 | this.getData().putProperty(name, value);
|
||
158 | 24019 | jjdelcerro | } |
159 | |||
160 | 28076 | cmartinez | public void set(String name, double value) { |
161 | this.getData().putProperty(name, value);
|
||
162 | 24019 | jjdelcerro | } |
163 | |||
164 | 28076 | cmartinez | public void set(String name, float value) { |
165 | this.getData().putProperty(name, value);
|
||
166 | 24019 | jjdelcerro | } |
167 | |||
168 | 28076 | cmartinez | public void set(String name, boolean value) { |
169 | this.getData().putProperty(name, value);
|
||
170 | 24019 | jjdelcerro | } |
171 | |||
172 | 28076 | cmartinez | public void set(String name, Persistent obj) |
173 | 24019 | jjdelcerro | throws PersistenceException {
|
174 | 28076 | cmartinez | XMLEntityState state = (XMLEntityState) manager.getState(obj); |
175 | state.setName(name); |
||
176 | this.getData().addChild(state.xmlEntity);
|
||
177 | 24019 | jjdelcerro | } |
178 | |||
179 | 28076 | cmartinez | public static class PersistentIterator implements Iterator, Persistent { |
180 | private XMLEntityState state = null; |
||
181 | int current = 0; |
||
182 | |||
183 | public PersistentIterator() {
|
||
184 | |||
185 | } |
||
186 | |||
187 | 24042 | jjdelcerro | public boolean hasNext() { |
188 | 28076 | cmartinez | return (state!=null && current< state.getData().getChildrenCount()); |
189 | 24042 | jjdelcerro | } |
190 | public Object next() { |
||
191 | Object value = null; |
||
192 | 28076 | cmartinez | XMLEntity xmlEntity = (XMLEntity) state.getData().getChild(current++); |
193 | 24042 | jjdelcerro | String className = xmlEntity.getStringProperty(KEY_CLASSNAME);
|
194 | if (className.equals(String.class.getName())) { |
||
195 | 28076 | cmartinez | value = xmlEntity.getObjectProperty(VALUE); |
196 | 24042 | jjdelcerro | } else if (className.equals(Integer.class.getName())) { |
197 | 28076 | cmartinez | value = xmlEntity.getIntProperty(VALUE); |
198 | 24042 | jjdelcerro | } else if (className.equals(Long.class.getName())) { |
199 | 28076 | cmartinez | value = xmlEntity.getLongProperty(VALUE); |
200 | 24042 | jjdelcerro | } else if (className.equals(Double.class.getName())) { |
201 | 28076 | cmartinez | value = xmlEntity.getDoubleProperty(VALUE); |
202 | 24042 | jjdelcerro | } else if (className.equals(Float.class.getName())) { |
203 | 28076 | cmartinez | value = xmlEntity.getFloatProperty(VALUE); |
204 | 24042 | jjdelcerro | } else if (className.equals(Boolean.class.getName())) { |
205 | 28076 | cmartinez | value = xmlEntity.getBooleanProperty(VALUE); |
206 | 24042 | jjdelcerro | } |
207 | 28076 | cmartinez | 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 | 24042 | jjdelcerro | return value;
|
226 | } |
||
227 | 28076 | cmartinez | |
228 | 24042 | jjdelcerro | public void remove() { |
229 | throw new UnsupportedOperationException(); |
||
230 | } |
||
231 | 28468 | cmartinez | public void loadFromState(PersistentState state) throws PersistenceException { |
232 | 28076 | cmartinez | if (state instanceof XMLEntityState) { |
233 | this.state = (XMLEntityState) state;
|
||
234 | current = 0;
|
||
235 | } |
||
236 | else {
|
||
237 | 28249 | cmartinez | throw new PersistenceException("setState(PersistentState): Not supported yet"); |
238 | 28076 | cmartinez | } |
239 | 24042 | jjdelcerro | } |
240 | 28076 | cmartinez | |
241 | public void saveToState(PersistentState state) |
||
242 | 24081 | jjdelcerro | throws PersistenceException {
|
243 | throw new UnsupportedOperationException(); |
||
244 | } |
||
245 | 24042 | jjdelcerro | } |
246 | 28076 | cmartinez | |
247 | public Iterator getIterator(String name) |
||
248 | throws PersistenceException, PersistenceValueNotFoundException
|
||
249 | { |
||
250 | XMLEntity value = this.getData().firstChild(name);
|
||
251 | 24042 | jjdelcerro | if (value == null) { |
252 | throw new PersistenceValueNotFoundException(name); |
||
253 | } |
||
254 | 28076 | cmartinez | return (Iterator) manager.create(manager.createState(value)); |
255 | 24042 | jjdelcerro | } |
256 | |||
257 | 28076 | cmartinez | public void set(String name, Iterator it) |
258 | 24042 | jjdelcerro | throws PersistenceException {
|
259 | 28076 | cmartinez | XMLEntityState stateList = manager.createStateInstance(); |
260 | stateList.setName(name); |
||
261 | 24042 | jjdelcerro | stateList.setTheClass(PersistentIterator.class); |
262 | while (it.hasNext()) {
|
||
263 | Object value = it.next();
|
||
264 | 28076 | cmartinez | 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 | 24042 | jjdelcerro | || value instanceof Long || value instanceof Double |
278 | || value instanceof Float || value instanceof String |
||
279 | || value instanceof Boolean) { |
||
280 | 28076 | cmartinez | 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 | 24042 | jjdelcerro | } else {
|
287 | 28076 | cmartinez | //TODO add a meaningful message and maybe use a more specific exception
|
288 | throw new PersistenceException(new RuntimeException()); |
||
289 | 24042 | jjdelcerro | } |
290 | } |
||
291 | 28076 | cmartinez | // stateList.xmlEntity.putProperty(KEY_CHILD, name);
|
292 | this.getData().addChild(stateList.xmlEntity);
|
||
293 | 24042 | jjdelcerro | } |
294 | 28076 | cmartinez | |
295 | |||
296 | 24042 | jjdelcerro | |
297 | 24807 | jmvivo | private class NamesIterator implements Iterator { |
298 | |||
299 | 24946 | jmvivo | private String name; |
300 | private XMLEntity xmlEntity;
|
||
301 | 24807 | jmvivo | private int index; |
302 | 24946 | jmvivo | private int propertiyCount; |
303 | 24807 | jmvivo | |
304 | public NamesIterator(XMLEntity xmlEntity) {
|
||
305 | this.xmlEntity = xmlEntity;
|
||
306 | 24946 | jmvivo | this.name = null; |
307 | 24807 | jmvivo | this.index = 0; |
308 | 24946 | jmvivo | this.propertiyCount = xmlEntity.getPropertyCount();
|
309 | 24807 | jmvivo | } |
310 | |||
311 | 24946 | jmvivo | 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 | 24807 | jmvivo | public boolean hasNext() { |
330 | 24946 | jmvivo | this.testNext();
|
331 | return this.index < this.propertiyCount; |
||
332 | 24807 | jmvivo | } |
333 | |||
334 | public Object next() { |
||
335 | 24946 | jmvivo | 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 | 24807 | jmvivo | } |
343 | |||
344 | public void remove() { |
||
345 | throw new UnsupportedOperationException(); |
||
346 | } |
||
347 | |||
348 | } |
||
349 | |||
350 | 24081 | jjdelcerro | public Iterator getNames() { |
351 | 24807 | jmvivo | return new NamesIterator(this.xmlEntity); |
352 | 24081 | jjdelcerro | } |
353 | 24042 | jjdelcerro | |
354 | 28076 | cmartinez | public void set(String name, Object value) |
355 | 24081 | jjdelcerro | throws PersistenceException {
|
356 | if (value instanceof Persistent) { |
||
357 | 28076 | cmartinez | set(name, (Persistent)value); |
358 | 24081 | jjdelcerro | } else if (value instanceof Integer || value instanceof Long |
359 | || value instanceof Double || value instanceof Float |
||
360 | || value instanceof String || value instanceof Boolean) { |
||
361 | 28076 | cmartinez | this.getData().putProperty(name, value);
|
362 | } else if (value instanceof Iterator) { |
||
363 | set(name, (Iterator)value);
|
||
364 | } |
||
365 | else {
|
||
366 | 24081 | jjdelcerro | throw new IllegalArgumentException(name); |
367 | } |
||
368 | } |
||
369 | |||
370 | 28076 | cmartinez | 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 | 24019 | jjdelcerro | } |