Revision 24378

View differences:

branches/v2_0_0_prep/libraries/libIverUtiles/src/org/gvsig/tools/persistence/xmlentity/XMLEntityState.java
1
package org.gvsig.tools.persistence.xmlentity;
2

  
3
import java.util.Iterator;
4

  
5
import org.gvsig.tools.persistence.PersistenceException;
6
import org.gvsig.tools.persistence.PersistenceValueNotFoundException;
7
import org.gvsig.tools.persistence.Persistent;
8
import org.gvsig.tools.persistence.PersistentState;
9

  
10
import com.iver.utiles.NotExistInXMLEntity;
11
import com.iver.utiles.XMLEntity;
12

  
13
public class XMLEntityState implements PersistentState {
14

  
15
	final private static String KEY_CLASSNAME = "_classname_";
16
	final private static String KEY_CHILD = "_child_";
17
	final private static String ITEM = "_item_";
18

  
19
	protected XMLEntity xmlEntity;
20
	protected XMLEntityManager manager;
21

  
22
	public XMLEntityState(XMLEntityManager manager) {
23
		this.manager = manager;
24
		this.xmlEntity = new XMLEntity();
25
	}
26

  
27
	public XMLEntityState(XMLEntityManager manager, XMLEntity xmlEntity) {
28
		this.manager = manager;
29
		this.xmlEntity = xmlEntity;
30
	}
31

  
32
	public XMLEntity getXMLEntity() {
33
		return this.xmlEntity;
34
	}
35

  
36
	private PersistentState createState(XMLEntity xmlEntity)
37
			throws PersistenceException {
38
		return manager.createState(xmlEntity, false);
39
	}
40

  
41
	public PersistentState createState(Object obj) throws PersistenceException {
42
		return manager.createState(obj);
43
	}
44

  
45
	public PersistentState createState(Object obj, boolean initialize)
46
			throws PersistenceException {
47
		return manager.createState(obj, initialize);
48
	}
49

  
50
	public void setTheClass(Object obj) {
51
		this.xmlEntity.putProperty(KEY_CLASSNAME, obj.getClass().getName());
52
	}
53

  
54
	public void setTheClass(Class theClass) {
55
		this.xmlEntity.putProperty(KEY_CLASSNAME, theClass.getName());
56
	}
57

  
58
	public void setTheClass(String name) {
59
		this.xmlEntity.putProperty(KEY_CLASSNAME, name);
60
	}
61

  
62
	public String getTheClassName() {
63
		try {
64
			return this.xmlEntity.getStringProperty(KEY_CLASSNAME);
65
		} catch (NotExistInXMLEntity e) {
66
			return null;
67
		}
68
	}
69

  
70
	public Object get(String name) throws PersistenceException {
71
		XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
72
		if( value == null ) {
73
			try {
74
				return this.xmlEntity.getStringProperty(name);
75
			} catch (NotExistInXMLEntity e) {
76
				throw new PersistenceValueNotFoundException(name, e);
77
			}
78
		}
79
		return manager.create(createState(value));
80
	}
81

  
82
	public PersistentState getState(String name)
83
			throws PersistenceException {
84
		XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
85
		if (value == null) {
86
			throw new PersistenceValueNotFoundException(name);
87
		}
88
		return createState(value);
89
	}
90

  
91
	public boolean getBoolean(String name)
92
			throws PersistenceValueNotFoundException {
93
		try {
94
			return this.xmlEntity.getBooleanProperty(name);
95
		} catch (NotExistInXMLEntity e) {
96
			throw new PersistenceValueNotFoundException(name, e);
97
		}
98
	}
99

  
100
	public double getDouble(String name)
101
			throws PersistenceValueNotFoundException {
102
		try {
103
			return this.xmlEntity.getDoubleProperty(name);
104
		} catch (NotExistInXMLEntity e) {
105
			throw new PersistenceValueNotFoundException(name, e);
106
		}
107
	}
108

  
109
	public float getFloat(String name) throws PersistenceValueNotFoundException {
110
		try {
111
			return this.xmlEntity.getFloatProperty(name);
112
		} catch (NotExistInXMLEntity e) {
113
			throw new PersistenceValueNotFoundException(name, e);
114
		}
115
	}
116

  
117
	public int getInt(String name) throws PersistenceValueNotFoundException {
118
		try {
119
			return this.xmlEntity.getIntProperty(name);
120
		} catch (NotExistInXMLEntity e) {
121
			throw new PersistenceValueNotFoundException(name, e);
122
		}
123
	}
124

  
125
	public long getLong(String name) throws PersistenceValueNotFoundException {
126
		try {
127
			return this.xmlEntity.getLongProperty(name);
128
		} catch (NotExistInXMLEntity e) {
129
			throw new PersistenceValueNotFoundException(name, e);
130
		}
131
	}
132

  
133
	public PersistentState set(String name, String value) {
134
		this.xmlEntity.putProperty(name, value);
135
		return this;
136
	}
137

  
138
	public PersistentState set(String name, int value) {
139
		this.xmlEntity.putProperty(name, value);
140
		return this;
141
	}
142

  
143
	public PersistentState set(String name, long value) {
144
		this.xmlEntity.putProperty(name, value);
145
		return this;
146
	}
147

  
148
	public PersistentState set(String name, double value) {
149
		this.xmlEntity.putProperty(name, value);
150
		return this;
151
	}
152

  
153
	public PersistentState set(String name, float value) {
154
		this.xmlEntity.putProperty(name, value);
155
		return this;
156
	}
157

  
158
	public PersistentState set(String name, boolean value) {
159
		this.xmlEntity.putProperty(name, value);
160
		return this;
161
	}
162

  
163
	public PersistentState set(String name, PersistentState state) {
164
		XMLEntityState lstate = (XMLEntityState) state;
165
		lstate.xmlEntity.putProperty(KEY_CHILD, name);
166
		this.xmlEntity.addChild(lstate.xmlEntity);
167
		return this;
168
	}
169

  
170
	public PersistentState set(String name, Persistent obj)
171
			throws PersistenceException {
172
		XMLEntityState state = (XMLEntityState) createState(obj);
173
		state.xmlEntity.putProperty(KEY_CHILD, name);
174
		this.xmlEntity.addChild(state.xmlEntity);
175
		return this;
176
	}
177

  
178
	public class PersistentIterator implements Iterator, Persistent {
179
		private XMLEntity xmlEntity = null;
180
		private Iterator it;
181
		public boolean hasNext() {
182
			return it.hasNext();
183
		}
184
		public Object next() {
185
			Object value = null;
186
			XMLEntity xmlEntity = (XMLEntity) it.next();
187
			String className = xmlEntity.getStringProperty(KEY_CLASSNAME);
188
			if (className.equals(String.class.getName())) {
189
				value = xmlEntity.getObjectProperty("value");
190
			} else if (className.equals(Integer.class.getName())) {
191
				value = xmlEntity.getObjectProperty("value");
192
			} else if (className.equals(Long.class.getName())) {
193
				value = xmlEntity.getObjectProperty("value");
194
			} else if (className.equals(Double.class.getName())) {
195
				value = xmlEntity.getObjectProperty("value");
196
			} else if (className.equals(Float.class.getName())) {
197
				value = xmlEntity.getObjectProperty("value");
198
			} else if (className.equals(Boolean.class.getName())) {
199
				value = xmlEntity.getObjectProperty("value");
200
			} else {
201
				// FIXME: ???? Persistent
202
			}
203
			return value;
204
		}
205
		public void remove() {
206
			throw new UnsupportedOperationException();
207
		}
208
		public void setState(PersistentState state) throws PersistenceException {
209
			this.xmlEntity = ((XMLEntityState) state).getXMLEntity();
210
			this.it = this.xmlEntity.findChildren(KEY_CHILD, ITEM);
211
		}
212
		public PersistentState getState() throws PersistenceException {
213
			throw new UnsupportedOperationException();
214
		}
215
		public void loadState(PersistentState state)
216
				throws PersistenceException {
217
			throw new UnsupportedOperationException();
218
		}
219
	}
220

  
221
	public Iterator getIterator(String name) throws PersistenceException {
222
		XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
223
		if (value == null) {
224
			throw new PersistenceValueNotFoundException(name);
225
		}
226
		return (Iterator) manager.create(createState(value));
227
	}
228

  
229
	public PersistentState set(String name, Iterator it)
230
			throws PersistenceException {
231
		XMLEntityState stateList = (XMLEntityState) createState(null);
232
		stateList.setTheClass(PersistentIterator.class);
233
		while (it.hasNext()) {
234
			Object value = it.next();
235
			XMLEntityState stateItem = (XMLEntityState) createState(value);
236
			if (value instanceof Persistent || value instanceof Integer
237
					|| value instanceof Long || value instanceof Double
238
					|| value instanceof Float || value instanceof String
239
					|| value instanceof Boolean) {
240
				stateItem.xmlEntity.putProperty("value", value);
241
				stateItem.xmlEntity.putProperty(KEY_CHILD, ITEM);
242
				stateList.xmlEntity.addChild(stateItem.xmlEntity);
243

  
244
			} else {
245
				// FIXME: que pete
246
			}
247
		}
248
		stateList.xmlEntity.putProperty(KEY_CHILD, name);
249
		this.xmlEntity.addChild(stateList.xmlEntity);
250
		return this;
251
	}
252

  
253
	public Iterator getNames() {
254
		// TODO Auto-generated method stub
255
		return null;
256
	}
257

  
258
	public PersistentState set(String name, Object value)
259
			throws PersistenceException {
260
		if (value instanceof Persistent) {
261
			XMLEntityState state = (XMLEntityState) createState(value);
262
			state.xmlEntity.putProperty(KEY_CHILD, name);
263
			this.xmlEntity.addChild(state.xmlEntity);
264

  
265
		} else if (value instanceof Integer || value instanceof Long
266
				|| value instanceof Double || value instanceof Float
267
				|| value instanceof String || value instanceof Boolean) {
268
			this.xmlEntity.putProperty(name, value);
269
		} else {
270
			throw new IllegalArgumentException(name);
271
		}
272
		return this;
273
	}
274

  
275
}
0 276

  
branches/v2_0_0_prep/libraries/libIverUtiles/src/org/gvsig/tools/persistence/xmlentity/XMLEntityManager.java
1
package org.gvsig.tools.persistence.xmlentity;
2

  
3
import org.gvsig.tools.persistence.AbstractPersistenceManager;
4
import org.gvsig.tools.persistence.PersistenceException;
5
import org.gvsig.tools.persistence.Persistent;
6
import org.gvsig.tools.persistence.PersistentState;
7

  
8
import com.iver.utiles.XMLEntity;
9

  
10
public class XMLEntityManager extends AbstractPersistenceManager {
11

  
12

  
13
	public PersistentState createState(XMLEntity xmlEntity)
14
			throws PersistenceException {
15
		return new XMLEntityState(this, xmlEntity);
16
	}
17

  
18
	public PersistentState createState(Object obj, boolean initialize)
19
			throws PersistenceException {
20
		PersistentState state = new XMLEntityState(this);
21
		state.setTheClass(obj);
22
		if (initialize && obj instanceof Persistent) {
23
			((Persistent) obj).loadState(state);
24
		}
25
		return state;
26
	}
27

  
28
}
0 29

  
branches/v2_0_0_prep/libraries/libIverUtiles/src/com/iver/utiles/xmlentity/XMLEntityManager.java
1
package com.iver.utiles.xmlentity;
2

  
3
import org.gvsig.tools.persistence.AbstractPersistenceManager;
4
import org.gvsig.tools.persistence.PersistenceException;
5
import org.gvsig.tools.persistence.Persistent;
6
import org.gvsig.tools.persistence.PersistentState;
7

  
8
import com.iver.utiles.XMLEntity;
9

  
10
public class XMLEntityManager extends AbstractPersistenceManager {
11

  
12

  
13
	public PersistentState createState(XMLEntity xmlEntity)
14
			throws PersistenceException {
15
		return new XMLEntityState(this, xmlEntity);
16
	}
17

  
18
	public PersistentState createState(Object obj, boolean initialize)
19
			throws PersistenceException {
20
		PersistentState state = new XMLEntityState(this);
21
		state.setTheClass(obj);
22
		if (initialize && obj instanceof Persistent) {
23
			((Persistent) obj).loadState(state);
24
		}
25
		return state;
26
	}
27

  
28
}
branches/v2_0_0_prep/libraries/libIverUtiles/src/com/iver/utiles/xmlentity/XMLEntityState.java
1
package com.iver.utiles.xmlentity;
2

  
3
import java.util.Iterator;
4

  
5
import org.gvsig.tools.persistence.PersistenceException;
6
import org.gvsig.tools.persistence.PersistenceValueNotFoundException;
7
import org.gvsig.tools.persistence.Persistent;
8
import org.gvsig.tools.persistence.PersistentState;
9

  
10
import com.iver.utiles.NotExistInXMLEntity;
11
import com.iver.utiles.XMLEntity;
12

  
13
public class XMLEntityState implements PersistentState {
14

  
15
	final private static String KEY_CLASSNAME = "_classname_";
16
	final private static String KEY_CHILD = "_child_";
17
	final private static String ITEM = "_item_";
18

  
19
	protected XMLEntity xmlEntity;
20
	protected XMLEntityManager manager;
21

  
22
	public XMLEntityState(XMLEntityManager manager) {
23
		this.manager = manager;
24
		this.xmlEntity = new XMLEntity();
25
	}
26

  
27
	public XMLEntityState(XMLEntityManager manager, XMLEntity xmlEntity) {
28
		this.manager = manager;
29
		this.xmlEntity = xmlEntity;
30
	}
31

  
32
	public XMLEntity getXMLEntity() {
33
		return this.xmlEntity;
34
	}
35

  
36
	private PersistentState createState(XMLEntity xmlEntity)
37
			throws PersistenceException {
38
		return manager.createState(xmlEntity, false);
39
	}
40

  
41
	public PersistentState createState(Object obj) throws PersistenceException {
42
		return manager.createState(obj);
43
	}
44

  
45
	public PersistentState createState(Object obj, boolean initialize)
46
			throws PersistenceException {
47
		return manager.createState(obj, initialize);
48
	}
49

  
50
	public void setTheClass(Object obj) {
51
		this.xmlEntity.putProperty(KEY_CLASSNAME, obj.getClass().getName());
52
	}
53

  
54
	public void setTheClass(Class theClass) {
55
		this.xmlEntity.putProperty(KEY_CLASSNAME, theClass.getName());
56
	}
57

  
58
	public void setTheClass(String name) {
59
		this.xmlEntity.putProperty(KEY_CLASSNAME, name);
60
	}
61

  
62
	public String getTheClassName() {
63
		try {
64
			return this.xmlEntity.getStringProperty(KEY_CLASSNAME);
65
		} catch (NotExistInXMLEntity e) {
66
			return null;
67
		}
68
	}
69

  
70
	public Object get(String name) throws PersistenceException {
71
		XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
72
		if( value == null ) {
73
			try {
74
				return this.xmlEntity.getStringProperty(name);
75
			} catch (NotExistInXMLEntity e) {
76
				throw new PersistenceValueNotFoundException(name, e);
77
			}
78
		}
79
		return manager.create(createState(value));
80
	}
81

  
82
	public PersistentState getState(String name)
83
			throws PersistenceException {
84
		XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
85
		if (value == null) {
86
			throw new PersistenceValueNotFoundException(name);
87
		}
88
		return createState(value);
89
	}
90

  
91
	public boolean getBoolean(String name)
92
			throws PersistenceValueNotFoundException {
93
		try {
94
			return this.xmlEntity.getBooleanProperty(name);
95
		} catch (NotExistInXMLEntity e) {
96
			throw new PersistenceValueNotFoundException(name, e);
97
		}
98
	}
99

  
100
	public double getDouble(String name)
101
			throws PersistenceValueNotFoundException {
102
		try {
103
			return this.xmlEntity.getDoubleProperty(name);
104
		} catch (NotExistInXMLEntity e) {
105
			throw new PersistenceValueNotFoundException(name, e);
106
		}
107
	}
108

  
109
	public float getFloat(String name) throws PersistenceValueNotFoundException {
110
		try {
111
			return this.xmlEntity.getFloatProperty(name);
112
		} catch (NotExistInXMLEntity e) {
113
			throw new PersistenceValueNotFoundException(name, e);
114
		}
115
	}
116

  
117
	public int getInt(String name) throws PersistenceValueNotFoundException {
118
		try {
119
			return this.xmlEntity.getIntProperty(name);
120
		} catch (NotExistInXMLEntity e) {
121
			throw new PersistenceValueNotFoundException(name, e);
122
		}
123
	}
124

  
125
	public long getLong(String name) throws PersistenceValueNotFoundException {
126
		try {
127
			return this.xmlEntity.getLongProperty(name);
128
		} catch (NotExistInXMLEntity e) {
129
			throw new PersistenceValueNotFoundException(name, e);
130
		}
131
	}
132

  
133
	public PersistentState set(String name, String value) {
134
		this.xmlEntity.putProperty(name, value);
135
		return this;
136
	}
137

  
138
	public PersistentState set(String name, int value) {
139
		this.xmlEntity.putProperty(name, value);
140
		return this;
141
	}
142

  
143
	public PersistentState set(String name, long value) {
144
		this.xmlEntity.putProperty(name, value);
145
		return this;
146
	}
147

  
148
	public PersistentState set(String name, double value) {
149
		this.xmlEntity.putProperty(name, value);
150
		return this;
151
	}
152

  
153
	public PersistentState set(String name, float value) {
154
		this.xmlEntity.putProperty(name, value);
155
		return this;
156
	}
157

  
158
	public PersistentState set(String name, boolean value) {
159
		this.xmlEntity.putProperty(name, value);
160
		return this;
161
	}
162

  
163
	public PersistentState set(String name, PersistentState state) {
164
		XMLEntityState lstate = (XMLEntityState) state;
165
		lstate.xmlEntity.putProperty(KEY_CHILD, name);
166
		this.xmlEntity.addChild(lstate.xmlEntity);
167
		return this;
168
	}
169

  
170
	public PersistentState set(String name, Persistent obj)
171
			throws PersistenceException {
172
		XMLEntityState state = (XMLEntityState) createState(obj);
173
		state.xmlEntity.putProperty(KEY_CHILD, name);
174
		this.xmlEntity.addChild(state.xmlEntity);
175
		return this;
176
	}
177

  
178
	public class PersistentIterator implements Iterator, Persistent {
179
		private XMLEntity xmlEntity = null;
180
		private Iterator it;
181
		public boolean hasNext() {
182
			return it.hasNext();
183
		}
184
		public Object next() {
185
			Object value = null;
186
			XMLEntity xmlEntity = (XMLEntity) it.next();
187
			String className = xmlEntity.getStringProperty(KEY_CLASSNAME);
188
			if (className.equals(String.class.getName())) {
189
				value = xmlEntity.getObjectProperty("value");
190
			} else if (className.equals(Integer.class.getName())) {
191
				value = xmlEntity.getObjectProperty("value");
192
			} else if (className.equals(Long.class.getName())) {
193
				value = xmlEntity.getObjectProperty("value");
194
			} else if (className.equals(Double.class.getName())) {
195
				value = xmlEntity.getObjectProperty("value");
196
			} else if (className.equals(Float.class.getName())) {
197
				value = xmlEntity.getObjectProperty("value");
198
			} else if (className.equals(Boolean.class.getName())) {
199
				value = xmlEntity.getObjectProperty("value");
200
			} else {
201
				// FIXME: ???? Persistent
202
			}
203
			return value;
204
		}
205
		public void remove() {
206
			throw new UnsupportedOperationException();
207
		}
208
		public void setState(PersistentState state) throws PersistenceException {
209
			this.xmlEntity = ((XMLEntityState) state).getXMLEntity();
210
			this.it = this.xmlEntity.findChildren(KEY_CHILD, ITEM);
211
		}
212
		public PersistentState getState() throws PersistenceException {
213
			throw new UnsupportedOperationException();
214
		}
215
		public void loadState(PersistentState state)
216
				throws PersistenceException {
217
			throw new UnsupportedOperationException();
218
		}
219
	}
220

  
221
	public Iterator getIterator(String name) throws PersistenceException {
222
		XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
223
		if (value == null) {
224
			throw new PersistenceValueNotFoundException(name);
225
		}
226
		return (Iterator) manager.create(createState(value));
227
	}
228

  
229
	public PersistentState set(String name, Iterator it)
230
			throws PersistenceException {
231
		XMLEntityState stateList = (XMLEntityState) createState(null);
232
		stateList.setTheClass(PersistentIterator.class);
233
		while (it.hasNext()) {
234
			Object value = it.next();
235
			XMLEntityState stateItem = (XMLEntityState) createState(value);
236
			if (value instanceof Persistent || value instanceof Integer
237
					|| value instanceof Long || value instanceof Double
238
					|| value instanceof Float || value instanceof String
239
					|| value instanceof Boolean) {
240
				stateItem.xmlEntity.putProperty("value", value);
241
				stateItem.xmlEntity.putProperty(KEY_CHILD, ITEM);
242
				stateList.xmlEntity.addChild(stateItem.xmlEntity);
243

  
244
			} else {
245
				// FIXME: que pete
246
			}
247
		}
248
		stateList.xmlEntity.putProperty(KEY_CHILD, name);
249
		this.xmlEntity.addChild(stateList.xmlEntity);
250
		return this;
251
	}
252

  
253
	public Iterator getNames() {
254
		// TODO Auto-generated method stub
255
		return null;
256
	}
257

  
258
	public PersistentState set(String name, Object value)
259
			throws PersistenceException {
260
		if (value instanceof Persistent) {
261
			XMLEntityState state = (XMLEntityState) createState(value);
262
			state.xmlEntity.putProperty(KEY_CHILD, name);
263
			this.xmlEntity.addChild(state.xmlEntity);
264

  
265
		} else if (value instanceof Integer || value instanceof Long
266
				|| value instanceof Double || value instanceof Float
267
				|| value instanceof String || value instanceof Boolean) {
268
			this.xmlEntity.putProperty(name, value);
269
		} else {
270
			throw new IllegalArgumentException(name);
271
		}
272
		return this;
273
	}
274

  
275
}

Also available in: Unified diff