Revision 16396 trunk/libraries/libTopology/src/org/gvsig/topology/TopologyPersister.java

View differences:

TopologyPersister.java
48 48
 */
49 49
package org.gvsig.topology;
50 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;
51 57
import java.util.Map;
52 58

  
53 59
import org.apache.log4j.Logger;
60
import org.exolab.castor.xml.Marshaller;
54 61

  
62
import com.iver.cit.gvsig.fmap.MapContext;
55 63
import com.iver.utiles.XMLEntity;
64
import com.iver.utiles.xml.XMLEncodingUtils;
65
import com.iver.utiles.xmlEntity.generate.XmlTag;
56 66

  
57

  
58 67
/**
59
 * Class with the responsability of persist topologies
60
 * and their elements.
68
 * Class with the responsability of persist topologies and their elements.
61 69
 */
62 70
public class TopologyPersister {
63
	
64
	private static Logger logger = Logger.getLogger(TopologyPersister.class.getName());
65
 
66
	public static void persist(Topology topology) {
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
		}
67 104
	}
68
	 
69
	public static Topology load(Map storeParams) {
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
		} 
70 121
		return null;
71 122
	}
72
	
73
	public static ITopologyErrorContainer createErrorContainerFromXML(XMLEntity xml){
123

  
124
	public static ITopologyErrorContainer createErrorContainerFromXML(
125
			XMLEntity xml) {
74 126
		ITopologyErrorContainer solution = null;
75 127
		String className = null;
76
		if(xml.contains("className")){
128
		if (xml.contains("className")) {
77 129
			className = xml.getStringProperty("className");
78 130
			Class clazz = null;
79 131
			ITopologyErrorContainer obj = null;
......
83 135
				obj.setXMLEntity(xml);
84 136
			} catch (Exception e) {
85 137
				logger.error(e);
86
			} 
87
		}else{
138
			}
139
		} else {
88 140
			return null;
89 141
		}
90 142
		return solution;
91 143
	}
92
	 
144

  
93 145
}
94
 

Also available in: Unified diff