/* * Output.java * * Created on den 31 augusti 2001, 09:25 */ package ip1.u5.d; import java.io.*; import org.jdom.*; import org.jdom.output.*; import java.net.InetAddress; /** * * @author h&m * @version */ public class Output implements Runnable{ private OutputStream output; private String message; private String name; private String email; private String homepage; /** Creates new Output. Sätter igång en tråd som skickar meddelandet och avslutar sig*/ public Output(OutputStream output, String message, String name, String email, String homepage) { this.output = output; this.message = message; this.name = name; this.email = email; this.homepage = homepage; (new Thread(this).start()); } public void run() { try { Element root = new Element("message"); DocType dtd = new DocType("message", "message.dtd"); Document doc = new Document(root, dtd); // Bygg ett nytt meddelande root.addContent( (new Element("header")).addContent( (new Element("protocol")).addContent( (new Element("type")).addContent("CTTP")).addContent( (new Element("version")).addContent("1.0")).addContent( (new Element("command")).addContent("MESS"))).addContent( (new Element("id")).addContent( (new Element("name")).addContent(name)).addContent( (new Element("email")).addContent(email)).addContent( (new Element("homepage")).addContent(homepage)).addContent( (new Element("host")).addContent(InetAddress.getLocalHost().getHostName())))).addContent( (new Element("body")).addContent(message)); // Undvik radbrytningar och skicka meddelandet XMLOutputter outputter = new XMLOutputter(); outputter.setLineSeparator(""); StringWriter stringWriter = new StringWriter(); outputter.output(doc, stringWriter); (new PrintWriter(output, true)).println(stringWriter.toString()); } catch(IllegalAddException e) { System.out.println(e.getMessage()); } catch(IOException e) { System.out.println(e.getMessage()); } } }