root / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / persistence / xml / ZipXMLPersistenceManager.java @ 30620
History | View | Annotate | Download (8.47 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 IVER T.I {{Task}}
|
26 |
*/
|
27 |
|
28 |
/**
|
29 |
*
|
30 |
*/
|
31 |
package org.gvsig.tools.persistence.xml; |
32 |
|
33 |
import java.io.FileInputStream; |
34 |
import java.io.IOException; |
35 |
import java.io.InputStream; |
36 |
import java.io.OutputStream; |
37 |
import java.io.PrintWriter; |
38 |
import java.text.MessageFormat; |
39 |
import java.util.Iterator; |
40 |
import java.util.Map; |
41 |
import java.util.Map.Entry; |
42 |
import java.util.zip.ZipEntry; |
43 |
import java.util.zip.ZipInputStream; |
44 |
import java.util.zip.ZipOutputStream; |
45 |
|
46 |
import org.gvsig.tools.dataTypes.DataTypes; |
47 |
import org.gvsig.tools.dynobject.DynField; |
48 |
import org.gvsig.tools.dynobject.DynStruct; |
49 |
import org.gvsig.tools.persistence.PersistenceException; |
50 |
import org.gvsig.tools.persistence.PersistenceRuntimeException; |
51 |
import org.gvsig.tools.persistence.PersistentState; |
52 |
import org.gvsig.tools.persistence.impl.ImplementationPersistentState; |
53 |
import org.gvsig.tools.persistence.impl.PersistentContext; |
54 |
|
55 |
/**
|
56 |
* @author jmvivo
|
57 |
*
|
58 |
*/
|
59 |
public class ZipXMLPersistenceManager extends XMLPersistenceManager { |
60 |
|
61 |
private static final String PERSISTENT_ZIP_ENTRY_NAME = "persitentState.xml"; |
62 |
private static final String BASE_SCHEMA_ENTRY_NAME = "persistentState_base.xsd"; |
63 |
|
64 |
/* (non-Javadoc)
|
65 |
* @see org.gvsig.tools.persistence.PersistenceManager#loadState(java.io.Reader)
|
66 |
*/
|
67 |
public PersistentState loadState(InputStream in) |
68 |
throws PersistenceException {
|
69 |
PersistentContext context = this.createPersistentContext();
|
70 |
|
71 |
ZipInputStream zIn = new ZipInputStream(in); |
72 |
|
73 |
ZipEntry zEntry;
|
74 |
try {
|
75 |
while ((zEntry = zIn.getNextEntry()) != null) { |
76 |
|
77 |
if (zEntry.getName().equals(PERSISTENT_ZIP_ENTRY_NAME)) {
|
78 |
return super.loadState(zIn); |
79 |
} |
80 |
} |
81 |
} catch (IOException e) { |
82 |
throw new PersistenceException(e); |
83 |
} |
84 |
throw new PersistenceException( |
85 |
"Invalid Input: Zip Entry of Persistent State not found: "
|
86 |
+ PERSISTENT_ZIP_ENTRY_NAME); |
87 |
} |
88 |
|
89 |
public void saveState(PersistentState state, OutputStream out) |
90 |
throws PersistenceException {
|
91 |
|
92 |
ZipOutputStream zOut = new ZipOutputStream(out); |
93 |
|
94 |
|
95 |
try{
|
96 |
// insert data
|
97 |
zOut.putNextEntry(new ZipEntry(PERSISTENT_ZIP_ENTRY_NAME)); |
98 |
XMLPersistentStateWriter xmlWriter = getWriterInstance(); |
99 |
|
100 |
xmlWriter.write((ImplementationPersistentState) state, zOut, this
|
101 |
.getDomains()); |
102 |
|
103 |
zOut.putNextEntry(new ZipEntry(BASE_SCHEMA_ENTRY_NAME)); |
104 |
|
105 |
|
106 |
// insert base schema
|
107 |
FileInputStream baseSchemaIs = new FileInputStream( |
108 |
ZipXMLPersistenceManager.class.getResource( |
109 |
"persistentState_base.xsd").getFile());
|
110 |
byte[] buff = new byte[1024*5]; |
111 |
int readed;
|
112 |
while ((readed = baseSchemaIs.read(buff)) > 0) { |
113 |
zOut.write(buff, 0, readed);
|
114 |
} |
115 |
baseSchemaIs.close(); |
116 |
|
117 |
// insert registered schemas
|
118 |
Map domains = this.getDomains(); |
119 |
Entry entry; |
120 |
Iterator iterDomainsEntry = domains.entrySet().iterator();
|
121 |
while (iterDomainsEntry.hasNext()) {
|
122 |
entry = (Entry) iterDomainsEntry.next(); |
123 |
zOut.putNextEntry(new ZipEntry(entry.getKey()+".xsd")); |
124 |
writeSchema(zOut, (String) entry.getKey(), (String) entry |
125 |
.getValue()); |
126 |
} |
127 |
|
128 |
zOut.finish(); |
129 |
} catch (IOException e) { |
130 |
throw new PersistenceException(e); |
131 |
} |
132 |
|
133 |
} |
134 |
|
135 |
/*
|
136 |
private void writeCurrentSchema(OutputStream out, Map domains) {
|
137 |
PrintWriter printer = new PrintWriter(out);
|
138 |
printer.println(MessageFormat.format(
|
139 |
ZipXMLSchemaConstants.XSD_HEAD_VALUE,
|
140 |
new Object[] { DEFAULT_DOMAIN_URL }));
|
141 |
|
142 |
Iterator iter = domains.keySet().iterator();
|
143 |
|
144 |
while (iter.hasNext()) {
|
145 |
printer.println(MessageFormat.format(
|
146 |
ZipXMLSchemaConstants.XSD_IMPORT_SCHEMA_VALUE,
|
147 |
new Object[] { iter.next() + ".xsd" }));
|
148 |
}
|
149 |
|
150 |
printer.println(ZipXMLSchemaConstants.XSD_END);
|
151 |
|
152 |
printer.flush();
|
153 |
|
154 |
}
|
155 |
*/
|
156 |
|
157 |
private void writeSchema(OutputStream out, String domainName, |
158 |
String domainURL) {
|
159 |
PrintWriter printer = new PrintWriter(out); |
160 |
printer.println(MessageFormat.format(
|
161 |
ZipXMLSchemaConstants.XSD_HEAD_VALUE, |
162 |
new Object[] { |
163 |
domainName, domainURL })); |
164 |
|
165 |
|
166 |
Map domainDefinitions = getDomainDefinitions(domainName);
|
167 |
Entry entry; |
168 |
Iterator iter = domainDefinitions.entrySet().iterator();
|
169 |
while (iter.hasNext()) {
|
170 |
entry = (Entry) iter.next(); |
171 |
writeClassDefinitionSchema(printer, (String) entry.getKey(),
|
172 |
(DynStruct) entry.getValue()); |
173 |
} |
174 |
|
175 |
|
176 |
printer.println(ZipXMLSchemaConstants.XSD_END); |
177 |
|
178 |
printer.flush(); |
179 |
|
180 |
} |
181 |
|
182 |
private void writeClassDefinitionSchema(PrintWriter printer, |
183 |
String className, DynStruct definition) {
|
184 |
|
185 |
printer.print(MessageFormat.format(
|
186 |
ZipXMLSchemaConstants.XSD_START_CLASS_VALUE, |
187 |
new Object[] { className })); |
188 |
|
189 |
DynField[] fields = definition.getDynFields();
|
190 |
DynField field; |
191 |
String typeId = ""; |
192 |
|
193 |
boolean allowNull;
|
194 |
|
195 |
for (int i = 0; i < fields.length; i++) { |
196 |
field = fields[i]; |
197 |
if (!field.isPersistent()) {
|
198 |
continue;
|
199 |
} |
200 |
allowNull = field.isMandatory() && field.getDefaultValue() == null;
|
201 |
|
202 |
switch (field.getType()) {
|
203 |
case DataTypes.STRING:
|
204 |
if (allowNull) {
|
205 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_STRING; |
206 |
} else {
|
207 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_STRING_NOTNULL; |
208 |
} |
209 |
break;
|
210 |
case DataTypes.BOOLEAN:
|
211 |
if (allowNull) {
|
212 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_BOOLEAN; |
213 |
} else {
|
214 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_BOOLEAN_NOTNULL; |
215 |
} |
216 |
break;
|
217 |
case DataTypes.INT:
|
218 |
if (allowNull) {
|
219 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_INTEGER; |
220 |
} else {
|
221 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_INTEGER_NOTNULL; |
222 |
} |
223 |
break;
|
224 |
|
225 |
case DataTypes.LONG:
|
226 |
if (allowNull) {
|
227 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_LONG; |
228 |
} else {
|
229 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_LONG_NOTNULL; |
230 |
} |
231 |
break;
|
232 |
|
233 |
case DataTypes.FLOAT:
|
234 |
if (allowNull) {
|
235 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_FLOAT; |
236 |
} else {
|
237 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_FLOAT_NOTNULL; |
238 |
} |
239 |
break;
|
240 |
|
241 |
case DataTypes.DOUBLE:
|
242 |
if (allowNull) {
|
243 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_FLOAT; |
244 |
} else {
|
245 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_FLOAT_NOTNULL; |
246 |
} |
247 |
break;
|
248 |
|
249 |
case DataTypes.SET:
|
250 |
if (allowNull) {
|
251 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_SET; |
252 |
} else {
|
253 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_SET_NOTNULL; |
254 |
} |
255 |
break;
|
256 |
|
257 |
case DataTypes.LIST:
|
258 |
if (allowNull) {
|
259 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_LIST; |
260 |
} else {
|
261 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_LIST_NOTNULL; |
262 |
} |
263 |
break;
|
264 |
|
265 |
case DataTypes.MAP:
|
266 |
if (allowNull) {
|
267 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_MAP; |
268 |
} else {
|
269 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_MAP_NOTNULL; |
270 |
} |
271 |
break;
|
272 |
|
273 |
case DataTypes.DATE:
|
274 |
if (allowNull) {
|
275 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_DATE; |
276 |
} else {
|
277 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_DATE_NOTNULL; |
278 |
} |
279 |
break;
|
280 |
|
281 |
case DataTypes.OBJECT:
|
282 |
if (allowNull) {
|
283 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_OBJECT; |
284 |
} else {
|
285 |
typeId = ZipXMLSchemaConstants.XSD_ATTRUBUTE_TYPE_OBJECT_NOTNULL; |
286 |
} |
287 |
break;
|
288 |
|
289 |
default:
|
290 |
throw new PersistenceRuntimeException( |
291 |
"Unssuported type in class definition: " + className
|
292 |
+ "#" + field.getName() + ": " |
293 |
+ DataTypes.TYPE_NAMES[field.getType()]); |
294 |
} |
295 |
|
296 |
if (field.isMandatory()) {
|
297 |
printer.print(MessageFormat.format(
|
298 |
ZipXMLSchemaConstants.XSD_ATTRUBUTE_MANDATORY_VALUE, |
299 |
new String[] { field.getName(), typeId })); |
300 |
} else {
|
301 |
printer.print(MessageFormat.format(
|
302 |
ZipXMLSchemaConstants.XSD_ATTRUBUTE_VALUE, |
303 |
new String[] { field.getName(), typeId })); |
304 |
} |
305 |
} |
306 |
|
307 |
printer.println(ZipXMLSchemaConstants.XSD_END_CLASS); |
308 |
|
309 |
} |
310 |
|
311 |
|
312 |
} |