/* Class Sender By Martin Carlsson Internetprogrammering 4 Course */ import java.net.*; import java.io.*; import org.jdom.output.*; import org.jdom.*; /** *Class Sender creates and sends an XML document. *@author Martin Carlsson */ public class Sender { private PrintWriter out; private Chat gui; private Socket socket; /** *Constructs a Sender object/instance *@param socket Socket for the connection *@param gui GUI reference */ public Sender(Socket socket, Chat gui) { this.gui = gui; this.socket = socket; try { out = new PrintWriter(socket.getOutputStream(), true); } catch(IOException iOe) { String problem = "Problem with connection to host: " + gui.getHost() + "on port: " + gui.getPort() +". Might be a problem with your Internet connection! \n" + iOe.getMessage(); System.err.println(problem +"\n" + iOe.getMessage()); iOe.printStackTrace(); gui.setErrorMessage(problem); } } /** *SCloses the output stream */ public void closeOutStream() { if(out != null) out.close(); } /** **Creates an XML document object and sends it to the Chat server *@param n innehållet i element name *@param e innehållet i element email *@param h innehållet i element homepage *@param msg meddelandet som läggs till i body elementet */ public void sendAway(String n, String e, String h, String msg) { try { //Doctype objectet and the global elements root, header and protocol are created DocType dT = new DocType("message", "http://www.webpelican.com/internet-programming-4/xml-based-java-chat-client/message.dtd"); Element root = new Element("message"); Element header = new Element("header"); Element protocol = new Element("protocol"); //The Protocol element gets it's content and is put to the header element. Element type = new Element("type").addContent("CTTP"); Element version = new Element("version").addContent("1.0"); Element command = new Element("command").addContent("MESS"); protocol.addContent(type); protocol.addContent(version); protocol.addContent(command); header.addContent(protocol); //The Id elementet is created and put in the header element Element id = new Element("id"); Element name = new Element("name").addContent(n); Element email = new Element("email").addContent(e); Element homepage = new Element("homepage").addContent(h); Element host = new Element("host").addContent(socket.getLocalAddress().getHostName()); id.addContent(name).addContent(email).addContent(homepage).addContent(host); header.addContent(id); //Body element is created, and the message is put as it's content. //Header and body is added and the XML document created. Element body = new Element("body").addContent(msg); root.addContent(header); root.addContent(body); Document doc = new Document(root, dT); Format format = Format.getCompactFormat(); format.setLineSeparator(""); format.setExpandEmptyElements(true); XMLOutputter xmlOutputter = new XMLOutputter(format); //More pretty for the command prompt Format prettyFormat = Format.getPrettyFormat(); prettyFormat.setExpandEmptyElements(true); XMLOutputter prettyXmlOut = new XMLOutputter(prettyFormat); String docString = xmlOutputter.outputString(doc); //The document is sent as a string. //and the pretty printout is done in the command prompt if(out != null) out.println(docString); System.out.println("*****Outgoing message:"); prettyXmlOut.output(doc, System.out); } catch(IOException iOe) { String problem = "Problem sending the message! Check your connection! \n" + iOe.getMessage(); System.err.println(problem +"\n" + iOe.getMessage()); iOe.printStackTrace(); gui.setErrorMessage(problem); } } } // End class Sender