KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > XMLNotificationWriter


1 /*
2  * Created on 2005-apr-27
3  *
4  */

5 package org.infoglue.cms.util;
6
7 import java.io.IOException JavaDoc;
8 import java.io.Writer JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.dom4j.DocumentException;
12 import org.dom4j.DocumentHelper;
13 import org.dom4j.io.OutputFormat;
14 import org.dom4j.io.XMLWriter;
15
16 import com.thoughtworks.xstream.XStream;
17
18 /**
19  * @author Stefan Sik
20  *
21  */

22 public class XMLNotificationWriter implements NotificationListener
23 {
24     private String JavaDoc boundary = "";
25     private Writer JavaDoc out = null;
26     private Thread JavaDoc workingThread = null;
27     private XMLWriter xmlWriter = null;
28     private XStream xStream = null;
29
30     public XMLNotificationWriter(Writer JavaDoc out, String JavaDoc encoding, String JavaDoc boundary, Thread JavaDoc workingThread, boolean compact, boolean supressDecl)
31     {
32         this.out = out;
33         this.boundary = boundary;
34         this.workingThread = workingThread;
35         
36         OutputFormat format = compact ? OutputFormat.createCompactFormat() : OutputFormat.createPrettyPrint();
37         format.setSuppressDeclaration(supressDecl);
38         format.setEncoding(encoding);
39         format.setExpandEmptyElements(!compact);
40         xmlWriter = new XMLWriter(out, format);
41         xStream = new XStream();
42     }
43     
44     public void setContextParameters(Map JavaDoc map)
45     {
46     }
47
48     /*
49      * @see org.infoglue.cms.util.NotificationListener#notify(org.infoglue.cms.util.NotificationMessage)
50      */

51     public void notify(NotificationMessage message)
52     {
53         try
54         {
55             xmlWriter.write(DocumentHelper.parseText(xStream.toXML(message)));
56             xmlWriter.flush();
57             out.write(("\r\n" + boundary + "\r\n"));
58             out.flush();
59
60         }
61         catch (DocumentException e)
62         {
63             e.printStackTrace();
64         }
65         catch (IOException JavaDoc e)
66         {
67             if(workingThread != null) workingThread.interrupt();
68         }
69     }
70 }
71
Popular Tags