Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libIverUtiles / src / com / iver / utiles / xmlentity / XMLEntityState.java @ 24019

History | View | Annotate | Download (4.54 KB)

1
package com.iver.utiles.xmlentity;
2

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

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

    
11
public class XMLEntityState implements PersistentState {
12

    
13
        final private static String KEY_CLASSNAME = "_classname_";
14
        final private static String KEY_CHILD = "_child_";
15

    
16
        protected XMLEntity xmlEntity;
17
        protected XMLEntityManager manager;
18

    
19
        public XMLEntityState(XMLEntityManager manager) {
20
                this.manager = manager;
21
                this.xmlEntity = new XMLEntity();
22
        }
23

    
24
        public XMLEntityState(XMLEntityManager manager, XMLEntity xmlEntity) {
25
                this.manager = manager;
26
                this.xmlEntity = xmlEntity;
27
        }
28

    
29
        public XMLEntity getXMLEntity() {
30
                return this.xmlEntity;
31
        }
32

    
33
        private PersistentState createState(XMLEntity xmlEntity)
34
                        throws PersistenceException {
35
                return manager.createState(xmlEntity);
36
        }
37

    
38
        public PersistentState createState() throws PersistenceException {
39
                return manager.createState();
40
        }
41

    
42
        public void setTheClass(Object obj) {
43
                this.xmlEntity.putProperty(KEY_CLASSNAME, obj.getClass().getName());
44
        }
45

    
46
        public void setTheClass(Class theClass) {
47
                this.xmlEntity.putProperty(KEY_CLASSNAME, theClass.getName());
48
        }
49

    
50
        public void setTheClass(String name) {
51
                this.xmlEntity.putProperty(KEY_CLASSNAME, name);
52
        }
53

    
54
        public String getTheClassName() {
55
                try {
56
                        return this.xmlEntity.getStringProperty(KEY_CLASSNAME);
57
                } catch (NotExistInXMLEntity e) {
58
                        return null;
59
                }
60
        }
61

    
62
        public Object get(String name) throws PersistenceException {
63
                XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
64
                if( value == null ) {
65
                        try {
66
                                return this.xmlEntity.getStringProperty(name);
67
                        } catch (NotExistInXMLEntity e) {
68
                                throw new PersistenceValueNotFoundException(name, e);
69
                        }
70
                }
71
                return manager.create(createState(value));
72
        }
73

    
74
        public PersistentState getState(String name)
75
                        throws PersistenceException {
76
                XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
77
                if (value == null) {
78
                        throw new PersistenceValueNotFoundException(name);
79
                }
80
                return createState(value);
81
        }
82

    
83
        public boolean getBoolean(String name)
84
                        throws PersistenceValueNotFoundException {
85
                try {
86
                        return this.xmlEntity.getBooleanProperty(name);
87
                } catch (NotExistInXMLEntity e) {
88
                        throw new PersistenceValueNotFoundException(name, e);
89
                }
90
        }
91

    
92
        public double getDouble(String name)
93
                        throws PersistenceValueNotFoundException {
94
                try {
95
                        return this.xmlEntity.getDoubleProperty(name);
96
                } catch (NotExistInXMLEntity e) {
97
                        throw new PersistenceValueNotFoundException(name, e);
98
                }
99
        }
100

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

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

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

    
125
        public PersistentState set(String name, String value) {
126
                this.xmlEntity.putProperty(name, value);
127
                return this;
128
        }
129

    
130
        public PersistentState set(String name, int value) {
131
                this.xmlEntity.putProperty(name, value);
132
                return this;
133
        }
134

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

    
140
        public PersistentState set(String name, double value) {
141
                this.xmlEntity.putProperty(name, value);
142
                return this;
143
        }
144

    
145
        public PersistentState set(String name, float value) {
146
                this.xmlEntity.putProperty(name, value);
147
                return this;
148
        }
149

    
150
        public PersistentState set(String name, boolean value) {
151
                this.xmlEntity.putProperty(name, value);
152
                return this;
153
        }
154

    
155
        public PersistentState set(String name, PersistentState state) {
156
                XMLEntityState lstate = (XMLEntityState) state;
157
                lstate.xmlEntity.putProperty("_child_", name);
158
                this.xmlEntity.addChild(lstate.xmlEntity);
159
                return this;
160
        }
161

    
162
        public PersistentState set(String name, Persistent obj)
163
                        throws PersistenceException {
164
                XMLEntityState state = (XMLEntityState) createState();
165
                obj.getState(state);
166
                state.xmlEntity.putProperty("_child_", name);
167
                this.xmlEntity.addChild(state.xmlEntity);
168
                return this;
169
        }
170

    
171
}