Revision 28076

View differences:

branches/v2_0_0_prep/libraries/libFMap_mapcontext/src/org/gvsig/fmap/mapcontext/layers/vectorial/FLyrVect.java
127 127
import org.gvsig.tools.observer.Observable;
128 128
import org.gvsig.tools.observer.Observer;
129 129
import org.gvsig.tools.persistence.PersistenceException;
130
import org.gvsig.tools.persistence.PersistenceManager;
130 131
import org.gvsig.tools.persistence.PersistentState;
131 132
import org.gvsig.tools.persistence.xmlentity.XMLEntityManager;
132 133
import org.gvsig.tools.persistence.xmlentity.XMLEntityState;
......
1074 1075
			xml.addChild(xmlLegend);
1075 1076
		}
1076 1077
		try {
1077
			PersistentState stateFeatureStore=getFeatureStore().getState();
1078
			PersistenceManager manager = ToolsLocator.getPersistenceManager();
1079
			PersistentState stateFeatureStore=manager.getState(getFeatureStore());
1078 1080
			stateFeatureStore.set("tagName","featureStore");
1079 1081
			xml.addChild(((XMLEntityState)stateFeatureStore).getXMLEntity());
1080 1082
		} catch (ReadException e) {
branches/v2_0_0_prep/libraries/libFMap_mapcontext/src/org/gvsig/fmap/mapcontext/layers/vectorial/ReprojectDefaultGeometry.java
161 161
	/* (non-Javadoc)
162 162
	 * @see org.gvsig.tools.persistence.Persistent#loadState(org.gvsig.tools.persistence.PersistentState)
163 163
	 */
164
	public void loadState(PersistentState state) throws PersistenceException {
164
	public void saveToState(PersistentState state) throws PersistenceException {
165 165
		// TODO Auto-generated method stub
166 166

  
167 167
	}
branches/v2_0_0_prep/libraries/libFMap_mapcontext/src-test/org/gvsig/fmap/mapcontext/rendering/TestAbstractIntervalLegend.java
465 465
			return null;
466 466
		}
467 467

  
468
		public void loadState(PersistentState state)
468
		public void saveToState(PersistentState state)
469 469
				throws PersistenceException {
470 470
			// TODO Auto-generated method stub
471 471

  
branches/v2_0_0_prep/libraries/libFMap_mapcontext/src-test/org/gvsig/fmap/mapcontext/rendering/TestIClassifiedLegend.java
467 467
			return null;
468 468
		}
469 469

  
470
		public void loadState(PersistentState state)
470
		public void saveToState(PersistentState state)
471 471
				throws PersistenceException {
472 472
			// TODO Auto-generated method stub
473 473

  
branches/v2_0_0_prep/libraries/libIverUtiles/src-test/com/iver/utiles/TestXMLEntity.java
74 74
import java.io.File;
75 75
import java.io.FileReader;
76 76

  
77
import org.gvsig.tools.IverUtilesLibrary;
78
import org.gvsig.tools.ToolsLibrary;
79
import org.gvsig.tools.ToolsLocator;
80
import org.gvsig.tools.locator.Library;
81

  
77 82
import junit.framework.TestCase;
78 83

  
79 84
import com.iver.utiles.xmlEntity.generate.XmlTag;
......
84 89
	private XMLEntity gvp2;
85 90

  
86 91
	public void setUp() {
92
		Library toolsLib = new ToolsLibrary();
93
		toolsLib.initialize();
94
		
95
		Library utilsLib = new IverUtilesLibrary();
96
		utilsLib.initialize();
97
		
98
		toolsLib.postInitialize();
99
		utilsLib.postInitialize();
100
		
101

  
87 102
		x1 = new XMLEntity();
88 103
		x2 = new XMLEntity();
89 104
		x3 = new XMLEntity();
branches/v2_0_0_prep/libraries/libIverUtiles/src-test/com/iver/utiles/TestXMLEntityPersistenceManager.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

  
42
/* CVS MESSAGES:
43
*
44
* $Id: TestXMLEntity.java 10602 2007-03-05 11:15:50Z jaume $
45
* $Log$
46
* Revision 1.7  2007-03-05 11:15:43  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.6  2007/03/05 10:03:12  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.5  2007/03/05 09:00:11  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.4  2007/03/02 13:35:56  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.3  2007/03/02 13:27:32  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.2  2007/03/02 13:24:53  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.1  2007/03/02 13:23:50  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1  2006/08/29 06:18:17  jaume
68
* *** empty log message ***
69
*
70
*
71
*/
72
package com.iver.utiles;
73

  
74
import java.io.BufferedInputStream;
75
import java.io.BufferedOutputStream;
76
import java.io.FileInputStream;
77
import java.io.FileNotFoundException;
78
import java.io.FileOutputStream;
79
import java.io.IOException;
80
import java.io.InputStreamReader;
81
import java.io.OutputStreamWriter;
82
import java.io.Reader;
83
import java.io.UnsupportedEncodingException;
84
import java.io.Writer;
85
import java.util.ArrayList;
86
import java.util.Iterator;
87

  
88
import junit.framework.TestCase;
89

  
90
import org.gvsig.tools.IverUtilesLibrary;
91
import org.gvsig.tools.ToolsLibrary;
92
import org.gvsig.tools.ToolsLocator;
93
import org.gvsig.tools.locator.Library;
94
import org.gvsig.tools.locator.LocatorException;
95
import org.gvsig.tools.persistence.PersistenceException;
96
import org.gvsig.tools.persistence.PersistenceManager;
97
import org.gvsig.tools.persistence.Persistent;
98
import org.gvsig.tools.persistence.PersistentState;
99

  
100
public class TestXMLEntityPersistenceManager extends TestCase {
101
	private XMLEntity x1, x2, x3, x4;
102
	private XMLEntity gvp1;
103
	private XMLEntity gvp2;
104
	private ArrayList list;
105
	private String string1;
106
	private String string2;
107
	private String string3;
108
	private String string4;
109
	private String string5;
110
	private int integer1;
111
	private int integer2;
112
	private int integer3;
113
	private long long1;
114
	private long long2;
115
	private double double1;
116
	private double double2;
117
	private double double3;
118
	private double double4;
119
	private double double5;
120
	private boolean bool1;
121
	private boolean bool2;
122
	private MyPersistentObject myPersObj;
123
	private final String ENCODING = "UTF-8";
124
	private MyPersistentContainer container1, container2;
125
	
126

  
127
	public void setUp() throws Exception {
128
		super.setUp();
129
		Library toolsLib = new ToolsLibrary();
130
		toolsLib.initialize();
131
		
132
		Library utilsLib = new IverUtilesLibrary();
133
		utilsLib.initialize();
134
		
135
		toolsLib.postInitialize();
136
		utilsLib.postInitialize();
137

  
138
		string1 = "This is my first$$ String to check";
139
		string2 = "An very long long long asdasfsafffffffffasfasasdffffffsf"
140
			+ "long long long long long asdfaaaddddddddddddddddddddddddasdsd"
141
			+ "fbkdfjger gsdf glkwerqwsdvsi?omodsgt string with special"
142
			+ "characters and etc etc asdfasdfwqfwefwfasdfasfasfasfasfasfasfas"
143
			+ "asfasfasfasfasfasfasfasfdasdsdfsafsafasfasfsafasfasfasfasfasfas"
144
			+ "safasfasfasdfkb sdfd  ertge ertwgfwrt43 fbgdsf gw wfwf sdgas  "
145
			+ "asfwr324rf635r sdgsa swe3er 6546               sfasdfsafas asfa"
146
			+ "asdfasfasf435hdhge sfsd g dfsafasfsa as";
147
		string3 = "pero perico pero";
148
		string4 = "piripi pi";
149
		string5 = "lepe lepero";
150

  
151
		integer1 = 9323823;
152
		integer2 = -234234234;
153
		integer3 = 0;
154
		
155
		double1 = 23234.234;
156
		double2 = -674633.2423423;
157
		double3 = 0.0;
158
		double4 = 0.23432445634;
159
		double5 = -0.2342323;
160
		
161
		bool1 = true;
162
		bool2 = false;
163
		
164
		myPersObj = new MyPersistentObject();
165
	}
166

  
167
	protected void tearDown() throws Exception {
168
		super.tearDown();
169
		list = null;
170
		container1 = null;
171
		container2 = null;
172
	}
173

  
174
	public void testMarshall1() {
175
		marshall1();
176
		
177
		try {
178
			Reader test = getReader("/tmp/persistence-test.xml");
179
			Reader sample = getReader("testdata/persistence-test.xml");
180
			char[] array1 = new char[1024];
181
			char[] array2 = new char[1024];
182
			while (sample.read(array1)!=-1) {
183
				test.read(array2);
184
				for (int i=0; i<array1.length; i++) {
185
					assertEquals(array1[i], array2[i]);
186
				}
187
			}
188
			
189
		} catch (UnsupportedEncodingException e) {
190
			e.printStackTrace();
191
			fail(e.getMessage());
192
		} catch (FileNotFoundException e) {
193
			e.printStackTrace();
194
			fail(e.getMessage());
195
		} catch (IOException e) {
196
			e.printStackTrace();
197
			fail(e.getMessage());
198
		}
199
	}
200

  
201
	public Reader getReader(String fileName) throws UnsupportedEncodingException, FileNotFoundException {
202
		return new InputStreamReader(
203
				new BufferedInputStream(
204
						new FileInputStream(fileName)), ENCODING) {
205
		};
206
	}
207

  
208
	public Writer getWriter(String fileName) throws UnsupportedEncodingException, FileNotFoundException{
209
		return new OutputStreamWriter(
210
				new BufferedOutputStream(
211
						new FileOutputStream(fileName)), ENCODING) {
212
		};
213
	}
214
	
215
	public void testMarshall2() {
216
		marshall2();
217
		
218
		try {
219
			Reader test = getReader("/tmp/persistence-test2.xml");
220
			Reader sample = getReader("testdata/persistence-test2.xml");
221
			char[] array1 = new char[1024];
222
			char[] array2 = new char[1024];
223
			while (sample.read(array1)!=-1) {
224
				test.read(array2);
225
				for (int i=0; i<array1.length; i++) {
226
					assertEquals(array1[i], array2[i]);
227
				}
228
			}
229
		} catch (UnsupportedEncodingException e) {
230
			e.printStackTrace();
231
			fail(e.getMessage());
232
		} catch (FileNotFoundException e) {
233
			e.printStackTrace();
234
			fail(e.getMessage());
235
		} catch (IOException e) {
236
			e.printStackTrace();
237
			fail(e.getMessage());
238
		}
239
	}
240

  
241
	private void initList() {
242
		if (list==null) {
243
			list = new ArrayList();
244
			list.add(string1);
245
			list.add(string2);
246
			list.add(new Integer(integer1));
247
			list.add(new Integer(integer2));
248
			list.add(new Integer(integer3));
249
			list.add(new Boolean(bool1));
250
			list.add(new Boolean(bool2));
251
			list.add(myPersObj);
252
			initContainer2();
253
			list.add(container2);
254
		}
255
	}
256

  
257
	private void marshall1() {
258
		initList();
259
		
260
		MyPersistentList originalList = new MyPersistentList(list);
261
		
262
		try {
263
			PersistentState state = ToolsLocator.getPersistenceManager().getState(originalList);
264
			Writer writer = getWriter("/tmp/persistence-test.xml");
265
			state.save(writer);
266
		} catch (FileNotFoundException e) {
267
			e.printStackTrace();
268
			fail(e.getMessage());
269
		} catch (LocatorException e) {
270
			e.printStackTrace();
271
			fail(e.getMessage());
272
		} catch (PersistenceException e) {
273
			e.printStackTrace();
274
			fail(e.getMessage());
275
		} catch (UnsupportedEncodingException e) {
276
			e.printStackTrace();
277
			fail(e.getMessage());
278
		}
279
	}
280

  
281
	public void testUnmarshall1() {
282
		try {
283
			initList();
284
			Reader reader = getReader("testdata/persistence-test.xml");
285
			PersistentState state = ToolsLocator.getPersistenceManager().loadState(reader);
286
			Iterator it = state.getIterator("myList");
287
			int i = 0;
288
			while (it.hasNext()) {
289
				Object obj = it.next();
290
				if (i<list.size()) {
291
					assertEquals(obj, list.get(i++));
292
				}
293
				else {
294
					fail("Iterator size is shorter than original list");
295
				}
296
			}
297
			if (i!=list.size()) {
298
				fail("Iterator size is longer than original list");
299
			}
300
			
301
		} catch (UnsupportedEncodingException e) {
302
			e.printStackTrace();
303
			fail(e.getMessage());
304
		} catch (FileNotFoundException e) {
305
			e.printStackTrace();
306
			fail(e.getMessage());
307
		} catch (LocatorException e) {
308
			e.printStackTrace();
309
			fail(e.getMessage());
310
		} catch (PersistenceException e) {
311
			e.printStackTrace();
312
			fail(e.getMessage());
313
		}
314
	}
315

  
316
	public void testUnmarshall2() {
317
		try {
318
			initContainer1();
319
			Reader reader = getReader("testdata/persistence-test2.xml");
320
			PersistenceManager manager = ToolsLocator.getPersistenceManager();
321
			PersistentState state = manager.loadState(reader);
322
			Object obj = manager.create(state);
323
			assertEquals(container1, obj);
324
			
325
		} catch (UnsupportedEncodingException e) {
326
			e.printStackTrace();
327
			fail(e.getMessage());
328
		} catch (FileNotFoundException e) {
329
			e.printStackTrace();
330
			fail(e.getMessage());
331
		} catch (LocatorException e) {
332
			e.printStackTrace();
333
			fail(e.getMessage());
334
		} catch (PersistenceException e) {
335
			e.printStackTrace();
336
			fail(e.getMessage());
337
		}
338
	}
339

  
340
	private void initContainer2() {
341
		if (container2==null) {
342
			container2 = new MyPersistentContainer();
343
			container2.setValue1(myPersObj);
344
			container2.setValue2(string4);
345
			container2.setValue3(string5);
346
		}
347
	}
348

  
349
	private void initContainer1() {
350
		if (container1==null) {
351
			initList();
352
			container1 = new MyPersistentContainer();
353
			container1.setValue1(string3);
354
			container1.setValue2(new MyPersistentList(list));
355
			container1.setValue3(myPersObj);
356
		}
357
	}
358

  
359
	private void marshall2() {
360
		initContainer1();	
361
			try {
362
				PersistentState state = ToolsLocator.getPersistenceManager().getState(container1);
363
				Writer writer = getWriter("/tmp/persistence-test2.xml");
364
				state.save(writer);
365
			} catch (FileNotFoundException e) {
366
				e.printStackTrace();
367
				fail(e.getMessage());
368
			} catch (LocatorException e) {
369
				e.printStackTrace();
370
				fail(e.getMessage());
371
			} catch (PersistenceException e) {
372
				e.printStackTrace();
373
				fail(e.getMessage());
374
			} catch (UnsupportedEncodingException e) {
375
				e.printStackTrace();
376
				fail(e.getMessage());
377
			}
378
	}
379

  
380
	public void testIterator() {
381
		list = new ArrayList();
382
		list.add(string1);
383
		list.add(string2);
384
		list.add(new Integer(integer1));
385
		list.add(new Integer(integer2));
386
		list.add(new Integer(integer3));
387
		list.add(new Boolean(bool1));
388
		list.add(new Boolean(bool2));
389
		list.add(myPersObj);
390
		
391
		MyPersistentList originalList = new MyPersistentList(list);
392
		try {
393
			PersistentState state = ToolsLocator.getPersistenceManager().getState(originalList);
394
			MyPersistentList resultList = new MyPersistentList();
395
			resultList.setState(state);
396
			checkEquals(originalList, resultList);
397
 			
398
		} catch (LocatorException e) {
399
			// TODO Auto-generated catch block
400
			e.printStackTrace();
401
			fail(e.getMessage());
402
		} catch (PersistenceException e) {
403
			// TODO Auto-generated catch block
404
			e.printStackTrace();
405
			fail(e.getMessage());
406
		}
407
	}
408

  
409
    public void checkEquals(MyPersistentList originalList, MyPersistentList resultList) {
410
    	if (originalList == null) {
411
    		if (resultList == null) {
412
    			return;
413
    		}
414
    		else {
415
    			fail("List1 is null");
416
    		}
417
    	}
418
    	else if (resultList == null) {
419
    		fail("List2 is null");
420
    	}
421

  
422
    	Iterator iterator1 = originalList.iterator();
423
    	Iterator iterator2 = resultList.iterator();
424
    	while(iterator1.hasNext() && iterator2.hasNext()) {
425
    	    Object o1 = iterator1.next();
426
    	    Object o2 = iterator2.next();
427
    	    if (!(o1==null ? o2==null : o1.equals(o2))) {
428
    	    	failNotEquals("Items are not equal", o1, o2);
429
    	    }
430
    	}
431
    	if (iterator1.hasNext() || iterator2.hasNext()) {
432
    		fail("Lists have different length");
433
    	}
434
    }
435

  
436
	public static class MyPersistentList implements Persistent {
437
		public ArrayList list;
438
		public MyPersistentList() {
439
			
440
		}
441
		public MyPersistentList(ArrayList list) {
442
			this.list = list;
443
		}
444

  
445
		public void saveToState(PersistentState state)
446
				throws PersistenceException {
447
			state.set("myList", list.iterator());
448
			
449
		}
450

  
451
		public void setState(PersistentState state) throws PersistenceException {
452
			Iterator it = state.getIterator("myList");
453
			list = new ArrayList();
454
			while (it.hasNext()) {
455
				list.add(it.next());
456
			}
457
		}
458
		@Override
459
		public boolean equals(Object obj) {
460
			if (obj instanceof MyPersistentList) {
461
				return list.equals(((MyPersistentList)obj).list);
462
			}
463
			return false;
464
		}
465
		
466
		public Iterator iterator() {
467
			return list.iterator();
468
		}
469
	}
470
	
471
	public static class MyPersistentObject implements Persistent {
472
		private String value = "Sopa de cabrita";
473
		private int intValue = 20090625;
474

  
475
		public void saveToState(PersistentState state)
476
				throws PersistenceException {
477
			state.set("miValor", value);
478
			state.set("numero", intValue);
479
			
480
		}
481

  
482
		public String getValue() {
483
			return value;
484
		}
485

  
486
		public void setValue(String val) {
487
			value = val;
488
		}
489

  
490
		public void setState(PersistentState state) throws PersistenceException {
491
			value = state.getString("miValor");
492
			intValue = state.getInt("numero");
493
		}
494

  
495
		public boolean equals(Object obj) {
496
			if (obj instanceof MyPersistentObject) {
497
				MyPersistentObject myPers = (MyPersistentObject) obj;
498
				return value.equals(myPers.getValue());
499
			}
500
			return false;
501
		}
502
	}
503

  
504
	public static class MyPersistentContainer implements Persistent {
505
		private Object value1;
506
		private Object value2;
507
		private Object value3;
508

  
509
		public void saveToState(PersistentState state)
510
				throws PersistenceException {
511
			state.set("miObjetito1", value1);
512
			state.set("miObjetito2", value2);
513
			state.set("miObjetito3", value3);
514
			
515
		}
516
		public Object getValue1() {
517
			return value1;
518
		}
519
		public void setValue1(Object val) {
520
			value1 = val;
521
		}
522
		public Object getValue2() {
523
			return value2;
524
		}
525
		public void setValue2(Object val) {
526
			value2 = val;
527
		}
528
		public Object getValue3() {
529
			return value3;
530
		}
531
		public void setValue3(Object val) {
532
			value3 = val;
533
		}
534

  
535
		public void setState(PersistentState state) throws PersistenceException {
536
			value1 = state.get("miObjetito1");
537
			value2 = state.get("miObjetito2");
538
			value3 = state.get("miObjetito3");
539
		}
540

  
541
		public boolean equals(Object obj) {
542
			if (obj instanceof MyPersistentContainer) {
543
				MyPersistentContainer myPers = (MyPersistentContainer) obj;
544
				if (!value1.equals(myPers.getValue1())) {
545
					return false;
546
				}
547
				if (!value2.equals(myPers.getValue2())) {
548
					return false;
549
				}
550
				if (!value3.equals(myPers.getValue3())) {
551
					return false;
552
				}
553
				else {
554
					return true;
555
				}
556
			}
557
			return false;
558
		}
559
		
560
	}
561
}
0 562

  
branches/v2_0_0_prep/libraries/libIverUtiles/src-test/com/iver/utiles/AllTests.java
20 20
		suite.addTestSuite(TestStringUtilities.class);
21 21
		suite.addTestSuite(TestVectorUtilities.class);
22 22
		suite.addTestSuite(TestXMLEntity.class);
23
		suite.addTestSuite(TestXMLEntityPersistenceManager.class);
23 24
		//$JUnit-END$
24 25
		return suite;
25 26
	}
branches/v2_0_0_prep/libraries/libIverUtiles/src/org/gvsig/tools/IverUtilesLibrary.java
1
package org.gvsig.tools;
2

  
3
import org.gvsig.tools.locator.BaseLibrary;
4
import org.gvsig.tools.locator.ReferenceNotRegisteredException;
5
import org.gvsig.tools.persistence.xmlentity.XMLEntityManager;
6

  
7

  
8
public class IverUtilesLibrary extends BaseLibrary {
9

  
10
	public void initialize() throws ReferenceNotRegisteredException {
11
		super.initialize();
12
		ToolsLocator.registerDefaultPersistenceManager(XMLEntityManager.class);
13
	}
14
	
15
	public void postInitialize() throws ReferenceNotRegisteredException {
16
		super.postInitialize();
17
	}
18
}
0 19

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

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

  
8 8
import com.iver.utiles.XMLEntity;
9 9

  
10 10
public class XMLEntityManager extends AbstractPersistenceManager {
11

  
12

  
11
	public PersistentState getState(Persistent obj) throws PersistenceException {
12
		XMLEntityState state = (XMLEntityState) super.getState(obj);
13
		state.setTheClass(obj.getClass().getName());
14
		return state;
15
	}
16
	
17
	public PersistentState getState(Object obj) throws PersistenceException {
18
		XMLEntityState state;
19
		if (obj instanceof Persistent) {
20
			state = (XMLEntityState) getState((Persistent)obj);
21
		}
22
		else {
23
			// perform things for Strings, PersistentStates, Iterators, arrays, etc
24
			state = createStateInstance();
25
			state.setTheClass(obj.getClass().getName());
26
		}
27
		return state;
28
	}
29
	
13 30
	public PersistentState createState(XMLEntity xmlEntity)
14 31
			throws PersistenceException {
15 32
		return new XMLEntityState(this, xmlEntity);
16 33
	}
17 34

  
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;
35
	public XMLEntityState createStateInstance() {
36
		return new XMLEntityState(this);
26 37
	}
27 38

  
28 39
}
branches/v2_0_0_prep/libraries/libIverUtiles/src/org/gvsig/tools/persistence/xmlentity/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
}
branches/v2_0_0_prep/libraries/libIverUtiles/src/com/iver/utiles/XMLEntity.java
1291 1291
	}
1292 1292

  
1293 1293
	/**
1294
	 * Devuelve el primer hijo cuyo nombre es igual a 'value'.
1295
	 * El nombre de un XMLEntity viene determinado por el valor del
1296
	 * atributo 'name' del xml-tag.
1297
	 *
1298
	 * @param value valor a comparar
1299
	 * @return XMLEntity hijo o null si no se encuentra
1300
	 */
1301
	public XMLEntity firstChild(String value) {
1302
		int num = this.getChildrenCount();
1303
		XMLEntity child;
1304
		for (int i=0;i < num; i++) {
1305
			child = this.getChild(i);
1306
			try {
1307
				if (child.getXmlTag().getName().equals(value)) {
1308
					return child;
1309
				}
1310
			} catch (NotExistInXMLEntity e) {
1311
				// Nothing to do
1312
			}
1313
		}
1314
		return null;
1315
	}
1316

  
1317
	/**
1294 1318
	 * Devuelve el indice del primer hijo que el valor de su propieda 'key'
1295 1319
	 * es igual a 'value'
1296 1320
	 *
......
1315 1339
	}
1316 1340

  
1317 1341
	/**
1342
	 * Devuelve el indice del primer hijo cuyo nombre es igual a 'value'.
1343
	 * El nombre de un XMLEntity viene determinado por el valor del
1344
	 * atributo 'name' del xml-tag.
1345
	 *
1346
	 * @param value valor a comparar
1347
	 * @return int indice del hijo o -1 si no se encuentra
1348
	 */
1349
	public int firstIndexOfChild(String value) {
1350
		int num = this.getChildrenCount();
1351
		XMLEntity child;
1352
		for (int i=0;i < num; i++) {
1353
			try {
1354
				child = this.getChild(i);
1355
				if (child.getXmlTag().getName().equals(value)) {
1356
					return i;
1357
				}
1358
			} catch (NotExistInXMLEntity e) {
1359
				// Nothing to do
1360
			}
1361
		}
1362
		return -1;
1363
	}
1364

  
1365
	/**
1318 1366
	 * Devuelve un iterador sobre los hijos que cumplen la condicion
1319 1367
	 * que el valor de su propiedad 'key' es igual a 'value'
1320 1368
	 *
branches/v2_0_0_prep/libraries/libIverUtiles/testdata/persistence-test.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<xml-tag xmlns="http://www.gvsig.gva.es"><property key="_classname_" value="com.iver.utiles.TestXMLEntityPersistenceManager$MyPersistentList"/><xml-tag name="_item_data_"><xml-tag name="myList"><property key="_classname_" value="org.gvsig.tools.persistence.xmlentity.XMLEntityState$PersistentIterator"/><xml-tag name="_item_data_"><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.String"/><property key="_value_" value="This is my first$$ String to check"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.String"/><property key="_value_" value="An very long long long asdasfsafffffffffasfasasdffffffsflong long long long long asdfaaaddddddddddddddddddddddddasdsdfbkdfjger gsdf glkwerqwsdvsiñomodsgt string with specialcharacters and etc etc asdfasdfwqfwefwfasdfasfasfasfasfasfasfasasfasfasfasfasfasfasfasfdasdsdfsafsafasfasfsafasfasfasfasfasfassafasfasfasdfkb sdfd  ertge ertwgfwrt43 fbgdsf gw wfwf sdgas  asfwr324rf635r sdgsa swe3er 6546               sfasdfsafas asfaasdfasfasf435hdhge sfsd g dfsafasfsa as"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.Integer"/><property key="_value_" value="9323823"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.Integer"/><property key="_value_" value="-234234234"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.Integer"/><property key="_value_" value="0"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.Boolean"/><property key="_value_" value="true"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.Boolean"/><property key="_value_" value="false"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="com.iver.utiles.TestXMLEntityPersistenceManager$MyPersistentObject"/><xml-tag name="_item_data_"><property key="miValor" value="Sopa de cabrita"/><property key="numero" value="20090625"/></xml-tag></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="com.iver.utiles.TestXMLEntityPersistenceManager$MyPersistentContainer"/><xml-tag name="_item_data_"><property key="miObjetito2" value="piripi pi"/><property key="miObjetito3" value="lepe lepero"/><xml-tag name="miObjetito1"><property key="_classname_" value="com.iver.utiles.TestXMLEntityPersistenceManager$MyPersistentObject"/><xml-tag name="_item_data_"><property key="miValor" value="Sopa de cabrita"/><property key="numero" value="20090625"/></xml-tag></xml-tag></xml-tag></xml-tag></xml-tag></xml-tag></xml-tag></xml-tag>
0 3

  
branches/v2_0_0_prep/libraries/libIverUtiles/testdata/persistence-test2.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<xml-tag xmlns="http://www.gvsig.gva.es"><property key="_classname_" value="com.iver.utiles.TestXMLEntityPersistenceManager$MyPersistentContainer"/><xml-tag name="_item_data_"><property key="miObjetito1" value="pero perico pero"/><xml-tag name="miObjetito2"><property key="_classname_" value="com.iver.utiles.TestXMLEntityPersistenceManager$MyPersistentList"/><xml-tag name="_item_data_"><xml-tag name="myList"><property key="_classname_" value="org.gvsig.tools.persistence.xmlentity.XMLEntityState$PersistentIterator"/><xml-tag name="_item_data_"><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.String"/><property key="_value_" value="This is my first$$ String to check"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.String"/><property key="_value_" value="An very long long long asdasfsafffffffffasfasasdffffffsflong long long long long asdfaaaddddddddddddddddddddddddasdsdfbkdfjger gsdf glkwerqwsdvsiñomodsgt string with specialcharacters and etc etc asdfasdfwqfwefwfasdfasfasfasfasfasfasfasasfasfasfasfasfasfasfasfdasdsdfsafsafasfasfsafasfasfasfasfasfassafasfasfasdfkb sdfd  ertge ertwgfwrt43 fbgdsf gw wfwf sdgas  asfwr324rf635r sdgsa swe3er 6546               sfasdfsafas asfaasdfasfasf435hdhge sfsd g dfsafasfsa as"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.Integer"/><property key="_value_" value="9323823"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.Integer"/><property key="_value_" value="-234234234"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.Integer"/><property key="_value_" value="0"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.Boolean"/><property key="_value_" value="true"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="java.lang.Boolean"/><property key="_value_" value="false"/></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="com.iver.utiles.TestXMLEntityPersistenceManager$MyPersistentObject"/><xml-tag name="_item_data_"><property key="miValor" value="Sopa de cabrita"/><property key="numero" value="20090625"/></xml-tag></xml-tag><xml-tag name="_iterator_item_"><property key="_classname_" value="com.iver.utiles.TestXMLEntityPersistenceManager$MyPersistentContainer"/><xml-tag name="_item_data_"><property key="miObjetito2" value="piripi pi"/><property key="miObjetito3" value="lepe lepero"/><xml-tag name="miObjetito1"><property key="_classname_" value="com.iver.utiles.TestXMLEntityPersistenceManager$MyPersistentObject"/><xml-tag name="_item_data_"><property key="miValor" value="Sopa de cabrita"/><property key="numero" value="20090625"/></xml-tag></xml-tag></xml-tag></xml-tag></xml-tag></xml-tag></xml-tag></xml-tag><xml-tag name="miObjetito3"><property key="_classname_" value="com.iver.utiles.TestXMLEntityPersistenceManager$MyPersistentObject"/><xml-tag name="_item_data_"><property key="miValor" value="Sopa de cabrita"/><property key="numero" value="20090625"/></xml-tag></xml-tag></xml-tag></xml-tag>
0 3

  
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/AbstractPersistenceManager.java
1
package org.gvsig.tools.persistence;
2

  
3
import java.util.HashMap;
4
import java.util.Map;
5

  
6
import org.gvsig.tools.ToolsLocator;
7

  
8
public abstract class AbstractPersistenceManager implements PersistenceManager {
9

  
10
	protected Map alias;
11

  
12
	public static PersistentState getState(Persistent obj)
13
			throws PersistenceException {
14
		PersistenceManager manager = ToolsLocator.getPersistenceManager();
15
		PersistentState state = manager.createState(obj, true);
16
		return state;
17
	}
18

  
19
	protected AbstractPersistenceManager() {
20
		alias = new HashMap();
21
	}
22

  
23
	public void addAlias(String name, Class aClass) {
24
		alias.put(name, aClass);
25
	}
26

  
27
	public void addAlias(String name, String className) {
28
		alias.put(name, className);
29
	}
30

  
31
	public PersistentState createState(Object obj) throws PersistenceException {
32
		return createState(obj, true);
33
	}
34

  
35
	public Object create(PersistentState state) throws PersistenceException {
36
		String className = state.getTheClassName();
37
		if (className == null) {
38
			throw new PersistenceException(null); // FIXME
39
		}
40
		try {
41
			Class theClass;
42

  
43
			Object x = alias.get(className);
44
			if (x instanceof Class) {
45
				theClass = (Class) x;
46
			} else if (x instanceof String) {
47
				theClass = Class.forName((String) x);
48
			} else { // x is null
49
				theClass = Class.forName(className);
50
			}
51
			Persistent obj = (Persistent) theClass.newInstance();
52
			obj.setState(state);
53
			return obj;
54
		} catch (ClassNotFoundException e) {
55
			throw new PersistenceException(e);
56
		} catch (InstantiationException e) {
57
			throw new PersistenceException(e);
58
		} catch (IllegalAccessException e) {
59
			throw new PersistenceException(e);
60
		}
61
	}
62

  
63
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/AbstractPersistent.java
1
package org.gvsig.tools.persistence;
2

  
3
import org.gvsig.tools.ToolsLocator;
4

  
5
public abstract class AbstractPersistent implements Persistent {
6

  
7
	public PersistentState getState() throws PersistenceException {
8
		PersistenceManager manager = ToolsLocator.getPersistenceManager();
9
		PersistentState state = manager.createState(this, true);
10
		return state;
11
	}
12

  
13
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/validation/ValidationError.java
1
package org.gvsig.tools.persistence.validation;
2

  
3
/**
4
 * <p>Encapsulates a validation error and allows to query
5
 * it.</p>.
6
 *
7
 * @see ValidationResult
8
 * @see org.gvsig.tools.dynobject.DynStruct
9
 * @see org.gvsig.tools.persistence.PersistenceManager
10
 *
11
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
12
 * @author IVER T.I. <http://www.iver.es>
13
 */
14
public interface ValidationError {
15

  
16
	/**
17
	 * <p>Validation error: The specified attribute name does not exist in the
18
	 * DynStruct definition.</p>
19
	 */
20
	public final int INVALID_NAME = 0;
21
	/**
22
	 * <p>Validation error: The name of the attribute is correct, but the type
23
	 * of the assigned value does not match in the DynStruct definition.
24
	 */
25
	public final int WRONG_VALUE = 1;
26

  
27
	/**
28
	 * <p>Validation error: The definition of the persisted object was not registered
29
	 * so it was not possible to validate it.</p>
30
	 */
31
	public final int MISSING_DEFINITION = 2;
32

  
33
	/**
34
	 * <p>Gets the kind of error. Possible values include: ValidationError.INVALID_NAME,
35
	 * ValidationError.WRONG_VALUE and ValidationError.MISSING_DEFINITION.</p>
36
	 *
37
	 * @return ValidationError.INVALID_NAME, ValidationError.WRONG_VALUE or MISSING_DEFINITION
38
	 * @see ValidationError.INVALID_NAME
39
	 * @see ValidationError.WRONG_VALUE
40
	 * @see ValidationError.MISSING_DEFINITION
41
	 */
42
	public int getType();
43

  
44
	/**
45
	 * <p>Gets the name of the attribute which failed the validation.</p>.
46
	 *
47
	 * @return The name of the field which failed the validation.
48
	 */
49
	public String getFieldName();
50

  
51
	/**
52
	 * <p>Gets the value of the attribute which failed the validation.</p>
53
	 *
54
	 * @return
55
	 */
56
	public Object getValue();
57

  
58
	/**
59
	 * <p>Gets a textual report of the error. Normally, this should include
60
	 * information about the type of error, the name of the involved attribute
61
	 * and its assigned value.</p>
62
	 *
63
	 * @return A plain explanation of the error.
64
	 */
65
	public String getInfo();
66
}
0 67

  
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/validation/ValidationResult.java
1
package org.gvsig.tools.persistence.validation;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff