source: main/trunk/gli/src/org/greenstone/gatherer/feedback/SaveToXML.java@ 24915

Last change on this file since 24915 was 7315, checked in by kjdon, 20 years ago

Veronika's feedback code - still needs some work, but its disabled by default

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 KB
Line 
1package org.greenstone.gatherer.feedback;
2
3import java.awt.image.*;
4import java.io.*;// SAX classes.
5import javax.imageio.*;
6import org.xml.sax.*;
7import org.xml.sax.helpers.*;//JAXP 1.1
8import javax.xml.parsers.*;
9import javax.xml.transform.*;
10import javax.xml.transform.stream.*;
11import javax.xml.transform.sax.*;
12import java.util.Locale;
13import java.util.ResourceBundle;
14import java.text.MessageFormat;
15import java.util.*;
16
17/**
18 * This class will make xml files from the information given.
19 * @author Veronica Liesaputra
20 */
21public class SaveToXML
22{
23 /**
24 * This is the file where the xml file will be saved.
25 */
26 private FileOutputStream fos;
27 /**
28 * This is the transformer handler for the xml file.
29 */
30 private TransformerHandler hd;
31 /**
32 * This is the attribute implementation for the xml file.
33 */
34 private AttributesImpl atts;
35 /**
36 * This variable will hold the resource of the words that is stored in Messages.properties file.
37 * The calling using messages.getString(someString) will caused someString to be translated
38 * into some other string that is hold in that file.usually it will caused it to be translated
39 * to the language that the user use or choose to have as stated in Locale.
40 */
41 private static ResourceBundle messages;
42
43 /**
44 * This constructor will setup what languange should be use.
45 * @param msg will hold the resource of the words that is stored in Messages.properties file.
46 */
47 public SaveToXML(ResourceBundle msg)
48 {
49 messages = msg;
50 }
51
52 /**
53 * This will save the image, image filename,width and height to the xml file.
54 * @param image image
55 * @param filename image's filename
56 * @param width image's width
57 * @param height image's height
58 */
59 public void saveImage (String image,String filename,String width,String height)
60 {
61 try
62 {
63 hd.startElement("","","Title",atts);
64 hd.characters(filename.toCharArray(),0,filename.length());
65 hd.endElement("","","Title");
66 hd.startElement("","","Size",atts);
67 hd.startElement("","","Width",atts);
68 hd.characters(width.toCharArray(),0,width.length());
69 hd.endElement("","","Width");
70 hd.startElement("","","Height",atts);
71 hd.characters(height.toCharArray(),0,height.length());
72 hd.endElement("","","Height");
73 hd.endElement("","","Size");
74 hd.startElement("","","View",atts);
75 hd.characters(image.toCharArray(),0,image.length());
76 hd.endElement("","","View");
77 }
78 catch(SAXException sax) {}
79 }
80
81 /**
82 * This method will create and setup the xml file.
83 * @param xmlname this is the file name for the xml file.
84 * @param type this is the first tag that open the xml file.
85 */
86 public void open (String xmlname,String type)
87 {
88 try
89 {
90 File f;
91 f = new File(xmlname);
92 fos = new FileOutputStream(f);
93 PrintWriter out;
94 out = new PrintWriter(fos);
95 StreamResult streamResult;
96 streamResult = new StreamResult(out);
97 SAXTransformerFactory tf;
98 tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
99 hd = tf.newTransformerHandler();
100 Transformer serializer;
101 serializer = hd.getTransformer();
102
103 serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
104 //serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,dtdname);
105 serializer.setOutputProperty(OutputKeys.INDENT,"yes");
106
107 hd.setResult(streamResult);
108 atts = new AttributesImpl();
109
110 hd.startDocument();
111
112 hd.startElement("","",type,atts);
113 }
114 catch (IOException e){
115 System.err.println ("Unable to write to file");
116 System.exit(-1);
117 }catch (TransformerConfigurationException tce){
118 System.err.println("Error in: TransformerConfigurationException");
119 }catch (SAXException spe) {
120 // Error generated by the parser
121 System.err.println("Error in: (SAXException");
122 // Use the contained exception, if any
123 Exception x = spe;
124 if (spe.getException() != null)
125 x = spe.getException();
126 x.printStackTrace();
127 }
128 }
129
130 /**
131 * This method will setup to close the xml file.
132 * @param type this is the last tag that close the xml file.
133 */
134 public void close(String type)
135 {
136 try
137 {
138 hd.endElement("","",type);
139 hd.endDocument();
140 fos.close();
141 }
142 catch (IOException e){
143 System.err.println ("Unable to write to file");
144 System.exit(-1);
145 }catch (SAXException spe) {
146 // Error generated by the parser
147 System.err.println("Error in: (SAXException");
148 // Use the contained exception, if any
149 Exception x = spe;
150 if (spe.getException() != null)
151 x = spe.getException();
152 x.printStackTrace();
153 }
154 }
155
156 /**
157 * This method will add the open tag according to the number.
158 * @param num the number to say which type of tag it is.
159 */
160 public void startContent (int num)
161 {
162 try
163 {
164 switch(num)
165 {
166 case 0: hd.startElement("","","COMPONENT",atts);
167 break;
168 case 1: hd.startElement("","","Type",atts);
169 break;
170 case 2: hd.startElement("","","Title",atts);
171 break;
172 case 3: hd.startElement("","","Content",atts);
173 break;
174 case 4: hd.startElement("","","Selected",atts);
175 break;
176 case 5: hd.startElement("","","Visible",atts);
177 break;
178 case 6: hd.startElement("","","Image",atts);
179 break;
180 case 7: hd.startElement("","","Date",atts);
181 break;
182 case 8: hd.startElement("","","Command",atts);
183 break;
184 case 9: hd.startElement("","","COMPONENTS",atts);
185 break;
186 case 10:hd.startElement("","","LOG",atts);
187 break;
188 case 11:hd.startElement("","","Icon",atts);
189 break;
190 case 12:hd.startElement("","","ToolTipText",atts);
191 break;
192 case 13:hd.startElement("","","Comment",atts);
193 break;
194 }
195 }
196 catch (SAXException spe) {
197 // Error generated by the parser
198 System.err.println("Error in: (SAXException");
199 // Use the contained exception, if any
200 Exception x;
201 x = spe;
202 if (spe.getException() != null)
203 x = spe.getException();
204 x.printStackTrace();
205 }
206 }
207
208 /**
209 * This method will add the text to the tag.
210 * @param text is the text to be saved in xml files.
211 */
212 public void saveContent (String text)
213 {
214 try
215 {
216 if (text == null)
217 text = "\t";
218 hd.characters(text.toCharArray(),0,text.length());
219 }
220 catch (SAXException spe) {
221 // Error generated by the parser
222 System.err.println("Error in: (SAXException");
223 // Use the contained exception, if any
224 Exception x;
225 x = spe;
226 if (spe.getException() != null)
227 x = spe.getException();
228 x.printStackTrace();
229 }
230 }
231
232 /**
233 * This method will add the close tag according to the number.
234 * @param num the number to say which type of tag it is.
235 */
236 public void closeContent (int num)
237 {
238 try
239 {
240 switch(num)
241 {
242 case 0: hd.endElement("","","COMPONENT");
243 break;
244 case 1: hd.endElement("","","Type");
245 break;
246 case 2: hd.endElement("","","Title");
247 break;
248 case 3: hd.endElement("","","Content");
249 break;
250 case 4: hd.endElement("","","Selected");
251 break;
252 case 5: hd.endElement("","","Visible");
253 break;
254 case 6: hd.endElement("","","Image");
255 break;
256 case 7: hd.endElement("","","Date");
257 break;
258 case 8: hd.endElement("","","Command");
259 break;
260 case 9: hd.endElement("","","COMPONENTS");
261 break;
262 case 10:hd.endElement("","","LOG");
263 break;
264 case 11:hd.endElement("","","Icon");
265 break;
266 case 12:hd.endElement("","","ToolTipText");
267 break;
268 case 13:hd.endElement("","","Comment");
269 break;
270 }
271 }
272 catch (SAXException spe) {
273 // Error generated by the parser
274 System.err.println("Error in: (SAXException");
275 // Use the contained exception, if any
276 Exception x;
277 x = spe;
278 if (spe.getException() != null)
279 x = spe.getException();
280 x.printStackTrace();
281 }
282 }
283
284 /**
285 * This method save to xml all the information taken when user did the Feedback form.
286 * @param err_details is the information taken when user did the Feedback form.
287 * @param img is the images taken when user did the Feedback form
288 */
289 public void saveFeedback (String[] err_details,String[] img)
290 {
291 try
292 {
293 String dirname;
294 dirname = "xmlfeedback/";
295 FileOutputStream fos;
296 fos = new FileOutputStream(dirname + "feedback" + err_details[0] +
297 ".xml");
298 PrintWriter out;
299 out = new PrintWriter(fos);
300 StreamResult streamResult;
301 streamResult = new StreamResult(out);
302 SAXTransformerFactory tf;
303 tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
304 hd = tf.newTransformerHandler();
305 Transformer serializer;
306 serializer = hd.getTransformer();
307 serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
308 serializer.setOutputProperty(OutputKeys.INDENT,"yes");
309 hd.setResult(streamResult);
310
311
312 hd.startDocument();
313 atts = new AttributesImpl();
314 hd.startElement("","","BUGS",atts);
315
316 hd.startElement("","","IDCode",atts);
317 hd.characters(err_details[0].toCharArray(),0,err_details[0].length());
318 hd.endElement("","","IDCode");
319
320 hd.startElement("","","LastViewedWindow",atts);
321 hd.characters(err_details[1].toCharArray(),0,err_details[1].length());
322 hd.endElement("","","LastViewedWindow");
323
324 hd.startElement("","","Optionals",atts);
325
326 hd.startElement("","","Type",atts);
327 hd.characters(err_details[3].toCharArray(),0,err_details[3].length());
328 hd.endElement("","","Type");
329
330 hd.startElement("","","Urgency",atts);
331 hd.characters(err_details[4].toCharArray(),0,err_details[4].length());
332 hd.endElement("","","Urgency");
333 hd.endElement("","","Optionals");
334
335 hd.startElement("","","User",atts);
336 hd.startElement("","","Name",atts);
337 hd.characters(err_details[5].toCharArray(),0,err_details[5].length());
338 hd.endElement("","","Name");
339
340 hd.startElement("","","HomeDirectory",atts);
341 hd.characters(err_details[6].toCharArray(),0,err_details[6].length());
342 hd.endElement("","","HomeDirectory");
343
344 hd.startElement("","","WorkingDirectory",atts);
345 hd.characters(err_details[7].toCharArray(),0,err_details[7].length());
346 hd.endElement("","","WorkingDirectory");
347
348 hd.startElement("","","SMTP",atts);
349 hd.characters(err_details[19].toCharArray(),0,err_details[19].length());
350 hd.endElement("","","SMTP");
351
352 hd.startElement("","","Email",atts);
353 hd.characters(err_details[20].toCharArray(),0,err_details[20].length());
354 hd.endElement("","","Email");
355
356 hd.endElement("","","User");
357
358 hd.startElement("","","OpenFormTime",atts);
359 hd.characters(err_details[8].toCharArray(),0,err_details[8].length());
360 hd.endElement("","","OpenFormTime");
361
362 hd.startElement("","","SendFormTime",atts);
363 hd.characters(err_details[9].toCharArray(),0,err_details[9].length());
364 hd.endElement("","","SendFormTime");
365
366 hd.startElement("","","OperatingSystem",atts);
367 hd.characters(err_details[10].toCharArray(),0,err_details[10].length());
368 hd.endElement("","","OperatingSystem");
369
370 hd.startElement("","","Java",atts);
371
372 hd.startElement("","","JavaInformation",atts);
373 hd.characters(err_details[11].toCharArray(),0,err_details[11].length());
374 hd.endElement("","","JavaInformation");
375
376 hd.startElement("","","TotalMemory",atts);
377 hd.characters(err_details[21].toCharArray(),0,err_details[21].length());
378 hd.endElement("","","TotalMemory");
379
380 hd.startElement("","","MaxMemory",atts);
381 hd.characters(err_details[22].toCharArray(),0,err_details[22].length());
382 hd.endElement("","","MaxMemory");
383
384 hd.startElement("","","FreeMemory",atts);
385 hd.characters(err_details[23].toCharArray(),0,err_details[23].length());
386 hd.endElement("","","FreeMemory");
387
388 hd.startElement("","","DefaultLocale",atts);
389 hd.characters(err_details[12].toCharArray(),0,err_details[12].length());
390 hd.endElement("","","DefaultLocale");
391
392 hd.endElement("","","Java");
393
394 hd.startElement("","","Browser",atts);
395 hd.characters(err_details[13].toCharArray(),0,err_details[13].length());
396 hd.endElement("","","Browser");
397
398 hd.startElement("","","LocalHostName",atts);
399 hd.characters(err_details[14].toCharArray(),0,err_details[14].length());
400 hd.endElement("","","LocalHostName");
401
402 hd.startElement("","","LocalHostAddress",atts);
403 hd.characters(err_details[15].toCharArray(),0,err_details[15].length());
404 hd.endElement("","","LocalHostAddress");
405
406 hd.startElement("","","ScreenResolution",atts);
407 hd.characters(err_details[16].toCharArray(),0,err_details[16].length());
408 hd.endElement("","","ScreenResolution");
409
410 if (img!= null)
411 {
412 hd.startElement("","","Sequences",atts);
413 int i;
414 for ( i = 0 ; i < img.length ; i= i + 10)
415 {
416 hd.startElement("","","Sequence",atts);
417
418 hd.startElement("","","Title",atts);
419 hd.characters(img[i].toCharArray(),0,img[i].length());
420 hd.endElement("","","Title");
421
422 hd.startElement("","","Details",atts);
423 hd.characters(img[i+9].toCharArray(),0,img[i+9].length());
424 hd.endElement("","","Details");
425
426 hd.startElement("","","ImageDescription",atts);
427
428 hd.startElement("","","ScreenShot",atts);
429 hd.startElement("","","Image",atts);
430 saveImage(img[i+1],img[i+2],img[i+7],img[i+8]);
431 hd.endElement("","","Image");
432 hd.endElement("","","ScreenShot");
433
434 hd.startElement("","","ErrorLineAndScreenShot",atts);
435 hd.startElement("","","Image",atts);
436 saveImage(img[i+3],img[i+4],img[i+7],img[i+8]);
437 hd.endElement("","","Image");
438 hd.endElement("","","ErrorLineAndScreenShot");
439
440 hd.startElement("","","ErrorLineForScreenShot",atts);
441 hd.startElement("","","Image",atts);
442 saveImage(img[i+5],img[i+6],img[i+7],img[i+8]);
443 hd.endElement("","","Image");
444 hd.endElement("","","ErrorLineForScreenShot");
445
446 hd.endElement("","","ImageDescription");
447
448 hd.endElement("","","Sequence");
449 }
450 hd.endElement("","","Sequences");
451 }
452
453 hd.endElement("","","BUGS");
454 hd.endDocument();
455 fos.close();
456 }
457 catch (IOException e){
458 System.err.println ("Unable to write to file");
459 System.exit(-1);
460 }catch (TransformerConfigurationException tce){
461 System.err.println("Error in: TransformerConfigurationException");
462 }catch (SAXException spe) {
463 // Error generated by the parser
464 System.err.println("Error in: (SAXException");
465 // Use the contained exception, if any
466 Exception x;
467 x = spe;
468 if (spe.getException() != null)
469 x = spe.getException();
470 x.printStackTrace();
471 }
472 }
473}
474
475
476
477
478
479
480
481
482
Note: See TracBrowser for help on using the repository browser.