svn-gvsig-desktop / trunk / libraries / libTopology / src / org / gvsig / topology / TopologyPersister.java @ 16396
History | View | Annotate | Download (4.07 KB)
1 |
/*
|
---|---|
2 |
* Created on 07-sep-2007
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
/* CVS MESSAGES:
|
45 |
*
|
46 |
* $Id:
|
47 |
* $Log:
|
48 |
*/
|
49 |
package org.gvsig.topology; |
50 |
|
51 |
import java.io.BufferedReader; |
52 |
import java.io.File; |
53 |
import java.io.FileInputStream; |
54 |
import java.io.FileOutputStream; |
55 |
import java.io.InputStreamReader; |
56 |
import java.io.OutputStreamWriter; |
57 |
import java.util.Map; |
58 |
|
59 |
import org.apache.log4j.Logger; |
60 |
import org.exolab.castor.xml.Marshaller; |
61 |
|
62 |
import com.iver.cit.gvsig.fmap.MapContext; |
63 |
import com.iver.utiles.XMLEntity; |
64 |
import com.iver.utiles.xml.XMLEncodingUtils; |
65 |
import com.iver.utiles.xmlEntity.generate.XmlTag; |
66 |
|
67 |
/**
|
68 |
* Class with the responsability of persist topologies and their elements.
|
69 |
*/
|
70 |
public class TopologyPersister { |
71 |
|
72 |
public static final String FILE_PARAM_NAME = "file"; |
73 |
public static final String DEFAULT_ENCODING = "UTF-8"; |
74 |
|
75 |
private static Logger logger = Logger.getLogger(TopologyPersister.class |
76 |
.getName()); |
77 |
|
78 |
/**
|
79 |
* Persist a topology to the specified file.
|
80 |
*
|
81 |
* @param topology
|
82 |
* topology to persist
|
83 |
* @param storageParams
|
84 |
* map with params to storage
|
85 |
*/
|
86 |
public static void persist(Topology topology, Map<String, ?> storageParams) { |
87 |
try {
|
88 |
String fileName = (String) storageParams.get(FILE_PARAM_NAME); |
89 |
File file = new File(fileName); |
90 |
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()); |
91 |
OutputStreamWriter writer = new OutputStreamWriter(fos, |
92 |
DEFAULT_ENCODING); |
93 |
Marshaller m = new Marshaller(writer);
|
94 |
m.setEncoding(DEFAULT_ENCODING); |
95 |
|
96 |
XMLEntity xml = topology.getXMLEntity(); |
97 |
xml.putProperty("followHeaderEncoding", true, false); |
98 |
m.marshal(xml.getXmlTag()); |
99 |
|
100 |
} catch (Exception e) { |
101 |
logger.error("error guardando la topologia " + topology.getName(),
|
102 |
e); |
103 |
} |
104 |
} |
105 |
|
106 |
public static Topology load(MapContext mapContext, Map <String, ?> storageParams) { |
107 |
String fileName = (String) storageParams.get(FILE_PARAM_NAME); |
108 |
try {
|
109 |
File file = new File(fileName); |
110 |
BufferedReader reader =null; |
111 |
String encoding = XMLEncodingUtils.getEncoding(file);
|
112 |
FileInputStream stream = new FileInputStream(file); |
113 |
reader = new BufferedReader(new InputStreamReader(stream, encoding)); |
114 |
XmlTag tag = (XmlTag) XmlTag.unmarshal(reader); |
115 |
XMLEntity xml=new XMLEntity(tag);
|
116 |
return Topology.createFromXML(mapContext, xml);
|
117 |
|
118 |
} catch(Exception e) { |
119 |
logger.error("Error cargando la topologia del recurso "+fileName, e);
|
120 |
} |
121 |
return null; |
122 |
} |
123 |
|
124 |
public static ITopologyErrorContainer createErrorContainerFromXML( |
125 |
XMLEntity xml) { |
126 |
ITopologyErrorContainer solution = null;
|
127 |
String className = null; |
128 |
if (xml.contains("className")) { |
129 |
className = xml.getStringProperty("className");
|
130 |
Class clazz = null; |
131 |
ITopologyErrorContainer obj = null;
|
132 |
try {
|
133 |
clazz = Class.forName(className);
|
134 |
obj = (ITopologyErrorContainer) clazz.newInstance(); |
135 |
obj.setXMLEntity(xml); |
136 |
} catch (Exception e) { |
137 |
logger.error(e); |
138 |
} |
139 |
} else {
|
140 |
return null; |
141 |
} |
142 |
return solution;
|
143 |
} |
144 |
|
145 |
} |