1 5 package org.infoglue.cms.util; 6 7 import java.io.IOException ; 8 import java.io.Writer ; 9 import java.util.Map ; 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 22 public class XMLNotificationWriter implements NotificationListener 23 { 24 private String boundary = ""; 25 private Writer out = null; 26 private Thread workingThread = null; 27 private XMLWriter xmlWriter = null; 28 private XStream xStream = null; 29 30 public XMLNotificationWriter(Writer out, String encoding, String boundary, Thread 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 map) 45 { 46 } 47 48 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 e) 66 { 67 if(workingThread != null) workingThread.interrupt(); 68 } 69 } 70 } 71 | Popular Tags |