1 package org.codehaus.xfire.transport.http;
2
3 import java.io.OutputStream;
4 import java.util.Collection;
5 import java.util.Iterator;
6
7 import javax.xml.stream.XMLOutputFactory;
8 import javax.xml.stream.XMLStreamException;
9 import javax.xml.stream.XMLStreamWriter;
10
11 import org.codehaus.xfire.service.Service;
12
13 /***
14 * Provides a basic HTML description of a {@link Service}.
15 *
16 * @author <a href="poutsma@mac.com">Arjen Poutsma</a>
17 */
18 public class HtmlServiceWriter
19 {
20 private static final String XHTML_STRICT_DTD = "<!DOCTYPE html " +
21 "PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " +
22 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";
23
24 /***
25 * Writes a HTML list of services to the given stream. Each service is described with its name.
26 *
27 * @param out the stream to write to
28 * @param services the services
29 * @throws XMLStreamException if an XML writing exception occurs
30 */
31 public void write(OutputStream out, Collection services)
32 throws XMLStreamException
33 {
34 XMLOutputFactory factory = XMLOutputFactory.newInstance();
35 XMLStreamWriter writer = factory.createXMLStreamWriter(out);
36
37 writer.writeStartDocument();
38 writePreamble(writer, "XFire Services");
39
40 writer.writeStartElement("body");
41 writer.writeStartElement("p");
42 writer.writeCharacters("No such service");
43 writer.writeEndElement();
44 if (!services.isEmpty())
45 {
46 writer.writeStartElement("p");
47 writer.writeCharacters("Services:");
48 writer.writeEndElement();
49 writer.writeStartElement("ul");
50 for (Iterator iterator = services.iterator(); iterator.hasNext();)
51 {
52 Service service = (Service) iterator.next();
53 writer.writeStartElement("li");
54 writer.writeCharacters(service.getName().toString());
55 writer.writeEndElement();
56 }
57 }
58
59 writer.writeEndDocument();
60 writer.flush();
61 }
62
63 /***
64 * Writes a HTML description of a service to the given stream.
65 *
66 * @param out the stream to write to
67 * @param service the service
68 * @throws XMLStreamException if an XML writing exception occurs
69 */
70 public void write(OutputStream out, Service service)
71 throws XMLStreamException
72 {
73 XMLOutputFactory factory = XMLOutputFactory.newInstance();
74 XMLStreamWriter writer = factory.createXMLStreamWriter(out);
75
76 writer.writeStartDocument();
77 String title = service.getName() + " Web Service";
78 writePreamble(writer, title);
79
80 writer.writeStartElement("body");
81 writer.writeStartElement("h1");
82 writer.writeCharacters(title);
83 writer.writeEndElement();
84
85 writer.writeEndDocument();
86 writer.flush();
87 }
88
89 private void writePreamble(XMLStreamWriter writer, String title)
90 throws XMLStreamException
91 {
92 writer.writeDTD(XHTML_STRICT_DTD);
93 writer.writeStartElement("html");
94 writer.writeStartElement("head");
95 writer.writeStartElement("title");
96 writer.writeCharacters(title);
97 writer.writeEndElement();
98 writer.writeEndElement();
99 }
100
101
102 }