Line | Hits | Source |
---|---|---|
1 | /******************************************************************************* | |
2 | ||
3 | * Demetrix process modelling system | |
4 | ||
5 | * | |
6 | ||
7 | * Copyright (c) 2003, 2004 Dimitri A. Pissarenko | |
8 | ||
9 | * | |
10 | ||
11 | * This file is part of Demetrix. | |
12 | ||
13 | * | |
14 | ||
15 | * Demetrix is free software; you can redistribute it and/or modify | |
16 | ||
17 | * it under the terms of the GNU General Public License as published by | |
18 | ||
19 | * the Free Software Foundation; either version 2.1 of the License, or | |
20 | ||
21 | * (at your option) any later version. | |
22 | ||
23 | * | |
24 | ||
25 | * Demetrix is distributed in the hope that it will be useful, | |
26 | ||
27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
28 | ||
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
30 | ||
31 | * GNU General Public License for more details. | |
32 | ||
33 | * | |
34 | ||
35 | * You should have received a copy of the GNU General Public License | |
36 | ||
37 | * along with Demetrix; if not, write to the Free Software | |
38 | ||
39 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
40 | ||
41 | * | |
42 | ||
43 | * For further information you may | |
44 | ||
45 | * | |
46 | ||
47 | * - send an e-mail in Russian, German or English to dimitri.pissarenko@gmx.net | |
48 | ||
49 | * - look at http://sourceforge.net/projects/demetrix/ | |
50 | ||
51 | * - look at http://demetrix.sourceforge.net/ | |
52 | ||
53 | * - look at http://members.inode.at/d.pissarenko/ | |
54 | ||
55 | * | |
56 | ||
57 | *****************************************************************************/ | |
58 | ||
59 | package net.sourceforge.demetrix.util; | |
60 | ||
61 | ||
62 | ||
63 | import java.io.BufferedInputStream; | |
64 | ||
65 | import java.io.File; | |
66 | ||
67 | import java.io.FileInputStream; | |
68 | ||
69 | import java.io.FileNotFoundException; | |
70 | ||
71 | import java.io.FileOutputStream; | |
72 | ||
73 | import java.io.IOException; | |
74 | ||
75 | ||
76 | ||
77 | import javax.xml.transform.Transformer; | |
78 | ||
79 | import javax.xml.transform.TransformerConfigurationException; | |
80 | ||
81 | import javax.xml.transform.TransformerException; | |
82 | ||
83 | import javax.xml.transform.TransformerFactory; | |
84 | ||
85 | import javax.xml.transform.sax.SAXSource; | |
86 | ||
87 | import javax.xml.transform.stream.StreamResult; | |
88 | ||
89 | ||
90 | ||
91 | import org.apache.log4j.Logger; | |
92 | ||
93 | import org.jxv.config.JXVConfig; | |
94 | ||
95 | import org.jxv.document.reader.DocumentReader; | |
96 | ||
97 | import org.jxv.document.sax.DocumentSAXView; | |
98 | ||
99 | import org.jxv.reader.JXVReaderHelper; | |
100 | ||
101 | import org.xml.sax.InputSource; | |
102 | ||
103 | ||
104 | ||
105 | /** | |
106 | ||
107 | * @author Dimitri Pissarenko | |
108 | ||
109 | * | |
110 | ||
111 | */ | |
112 | ||
113 | public class XmlSerializerDeserializer { | |
114 | ||
115 | private static XmlSerializerDeserializer instance; | |
116 | ||
117 | private Transformer transformer; | |
118 | ||
119 | private Logger logger; | |
120 | ||
121 | public final static String JXV_CONFIG_FILE_NAME="config/jxv-config.xml"; | |
122 | ||
123 | ||
124 | ||
125 | ||
126 | ||
127 | protected XmlSerializerDeserializer() | |
128 | ||
129 | 0 | { |
130 | ||
131 | this.logger=Logger.getLogger(getClass()); | |
132 | ||
133 | 0 | JXVConfig.getSystemConfig().getBaseContext().importXMLConfig(XmlSerializerDeserializer.JXV_CONFIG_FILE_NAME); |
134 | ||
135 | try | |
136 | ||
137 | { | |
138 | ||
139 | 0 | this.transformer=TransformerFactory.newInstance().newTransformer(); |
140 | ||
141 | } | |
142 | ||
143 | 0 | catch (TransformerConfigurationException exception) |
144 | ||
145 | { | |
146 | ||
147 | this.logger.error("", exception); | |
148 | ||
149 | 0 | } |
150 | ||
151 | 0 | } |
152 | ||
153 | public static XmlSerializerDeserializer getInstance() | |
154 | ||
155 | { | |
156 | ||
157 | 0 | if (instance==null) |
158 | ||
159 | { | |
160 | ||
161 | 0 | instance=new XmlSerializerDeserializer(); |
162 | ||
163 | } | |
164 | ||
165 | 0 | return instance; |
166 | ||
167 | } | |
168 | ||
169 | public void writeObject(Object object2Export, File targetFile, String configName) | |
170 | ||
171 | { | |
172 | ||
173 | 0 | FileOutputStream fileOutputStream = null; |
174 | ||
175 | 0 | SAXSource src=null; |
176 | ||
177 | ||
178 | ||
179 | try | |
180 | ||
181 | { | |
182 | ||
183 | 0 | fileOutputStream = new FileOutputStream(targetFile); |
184 | ||
185 | ||
186 | ||
187 | ||
188 | ||
189 | 0 | src = DocumentSAXView.getInstance(object2Export.getClass(), |
190 | ||
191 | configName).getSAXSource(object2Export); | |
192 | ||
193 | 0 | transformer.transform(src, new StreamResult(fileOutputStream)); |
194 | ||
195 | ||
196 | ||
197 | 0 | fileOutputStream.close(); |
198 | ||
199 | } | |
200 | ||
201 | 0 | catch (FileNotFoundException exception) |
202 | ||
203 | { | |
204 | ||
205 | this.logger.error("", exception); | |
206 | ||
207 | } | |
208 | ||
209 | 0 | catch (TransformerException exception) |
210 | ||
211 | { | |
212 | ||
213 | this.logger.error("", exception); | |
214 | ||
215 | } | |
216 | ||
217 | 0 | catch (IOException exception) |
218 | ||
219 | { | |
220 | ||
221 | this.logger.error("", exception); | |
222 | ||
223 | 0 | } |
224 | ||
225 | 0 | } |
226 | ||
227 | public Object readObject(File file, Class classToRead, String configName) | |
228 | ||
229 | { | |
230 | ||
231 | 0 | Object returnValue=null; |
232 | ||
233 | 0 | DocumentReader reader=null; |
234 | ||
235 | 0 | BufferedInputStream bufferedInputStream=null; |
236 | ||
237 | ||
238 | ||
239 | try | |
240 | ||
241 | { | |
242 | ||
243 | 0 | bufferedInputStream=new BufferedInputStream(new FileInputStream(file)); |
244 | ||
245 | 0 | reader=new DocumentReader(classToRead, configName); |
246 | ||
247 | 0 | returnValue=JXVReaderHelper.read(new InputSource(bufferedInputStream), reader); |
248 | ||
249 | 0 | bufferedInputStream.close(); |
250 | ||
251 | } | |
252 | ||
253 | 0 | catch (Exception exception) |
254 | ||
255 | { | |
256 | ||
257 | this.logger.error("", exception); | |
258 | ||
259 | 0 | } |
260 | ||
261 | 0 | return returnValue; |
262 | ||
263 | } | |
264 | ||
265 | } | |
266 |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |