KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > notification > Notifier


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.notification;
17
18 import org.apache.cocoon.Constants;
19 import org.apache.cocoon.xml.XMLUtils;
20
21 import org.apache.commons.lang.StringEscapeUtils;
22 import org.xml.sax.ContentHandler JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.helpers.AttributesImpl JavaDoc;
25
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * Generates a representations of the specified Notifying Object.
33  *
34  * @author <a HREF="mailto:nicolaken@supereva.it">Nicola Ken Barozzi</a>
35  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
36  * @version $Id: Notifier.java 164808 2005-04-26 16:07:03Z vgritsenko $
37  */

38 public class Notifier {
39
40     /**
41      * Generate notification information as a response.
42      * The notification is directly written to the OutputStream.
43      * @param n The <code>Notifying</code> object
44      * @param outputStream The output stream the notification is written to
45      * This could be <code>null</code>.
46      */

47     public static void notify(Notifying n, OutputStream JavaDoc outputStream, String JavaDoc mimetype) throws IOException JavaDoc {
48         //(NKB) FIXME should use error page templates, one per mime-type
49
// currently uses hardcoded html, should be used only as last resort.
50
notifyHTML(n, outputStream);
51     }
52
53     /**
54      * Generate notification information as html.
55      * The notification is directly written to the OutputStream.
56      * @param n The <code>Notifying</code> object
57      * @param outputStream The output stream the notification is written to
58      * This could be <code>null</code>.
59      */

60     private static void notifyHTML(Notifying n, OutputStream JavaDoc outputStream) throws IOException JavaDoc {
61         if (outputStream == null) {
62             return;
63         }
64
65         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
66
67         sb.append("<html><head><title>").append(n.getTitle()).append("</title>");
68         sb.append("<style><!--");
69         sb.append("body { background-color: white; color: black; font-family: verdana, helvetica, sanf serif;}");
70         sb.append("h1 {color: #336699; margin: 0px 0px 20px 0px; border-width: 0px 0px 1px 0px; border-style: solid; border-color: #336699;}");
71         sb.append("p.footer { color: #336699; border-width: 1px 0px 0px 0px; border-style: solid; border-color: #336699; }");
72         sb.append("span {color: #336699;}");
73         sb.append("pre {padding-left: 20px;}");
74         sb.append("a:link {font-weight: bold; color: #336699;}");
75         sb.append("a:visited {color: #336699; }");
76         sb.append("a:hover {color: #800000; background-color: #ffff80;}");
77         sb.append("a:active {color: #006666;}");
78         sb.append("--></style>");
79         sb.append("</head><body>");
80         sb.append("<h1>")
81           .append(StringEscapeUtils.escapeXml(n.getTitle())).append("</h1>");
82         sb.append("<p><span>Message:</span> ")
83           .append(StringEscapeUtils.escapeXml(n.getMessage())).append("</p>");
84         sb.append("<p><span>Description:</span> ")
85           .append(StringEscapeUtils.escapeXml(n.getDescription())).append("</p>");
86         sb.append("<p><span>Sender:</span> ")
87           .append(StringEscapeUtils.escapeXml(n.getSender())).append("</p>");
88         sb.append("<p><span>Source:</span> ")
89           .append(StringEscapeUtils.escapeXml(n.getSource())).append("</p>");
90
91         Map JavaDoc extras = n.getExtraDescriptions();
92         for (Iterator JavaDoc i = extras.entrySet().iterator(); i.hasNext(); ) {
93             Map.Entry JavaDoc me = (Map.Entry JavaDoc)i.next();
94             final String JavaDoc key = (String JavaDoc)me.getKey();
95             sb.append("<p><span>")
96             .append(key).append("</span><pre>")
97             .append(StringEscapeUtils.escapeXml(String.valueOf(me.getValue())))
98             .append("</pre></p>");
99         }
100         sb.append("<p class='footer'><a HREF='http://cocoon.apache.org/'>").append(Constants.COMPLETE_NAME).append("</p>");
101         sb.append("</body></html>");
102
103         outputStream.write(sb.toString().getBytes());
104     }
105
106     /**
107      * Generate notification information in XML format.
108      */

109     public static void notify(Notifying n, ContentHandler JavaDoc ch, String JavaDoc mimetype) throws SAXException JavaDoc {
110         final String JavaDoc PREFIX = Constants.ERROR_NAMESPACE_PREFIX;
111         final String JavaDoc URI = Constants.ERROR_NAMESPACE_URI;
112
113         // Start the document
114
ch.startDocument();
115         ch.startPrefixMapping(PREFIX, URI);
116
117         // Root element.
118
AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
119
120         atts.addAttribute(URI, "type", PREFIX + ":type", "CDATA", n.getType());
121         atts.addAttribute(URI, "sender", PREFIX + ":sender", "CDATA", n.getSender());
122         ch.startElement(URI, "notify", PREFIX + ":notify", atts);
123         ch.startElement(URI, "title", PREFIX + ":title", new AttributesImpl JavaDoc());
124         ch.characters(n.getTitle().toCharArray(), 0, n.getTitle().length());
125         ch.endElement(URI, "title", PREFIX + ":title");
126         ch.startElement(URI, "source", PREFIX + ":source", new AttributesImpl JavaDoc());
127         ch.characters(n.getSource().toCharArray(), 0, n.getSource().length());
128         ch.endElement(URI, "source", PREFIX + ":source");
129         ch.startElement(URI, "message", PREFIX + ":message", new AttributesImpl JavaDoc());
130
131         if (n.getMessage() != null) {
132             ch.characters(n.getMessage().toCharArray(), 0, n.getMessage().length());
133         }
134
135         ch.endElement(URI, "message", PREFIX + ":message");
136         ch.startElement(URI, "description", PREFIX + ":description", XMLUtils.EMPTY_ATTRIBUTES);
137         ch.characters(n.getDescription().toCharArray(), 0, n.getDescription().length());
138         ch.endElement(URI, "description", PREFIX + ":description");
139
140         Map JavaDoc extraDescriptions = n.getExtraDescriptions();
141         for (Iterator JavaDoc i = extraDescriptions.entrySet().iterator(); i.hasNext(); ) {
142             final Map.Entry JavaDoc me = (Map.Entry JavaDoc) i.next();
143             String JavaDoc key = (String JavaDoc) me.getKey();
144             String JavaDoc value = String.valueOf(me.getValue());
145             atts = new AttributesImpl JavaDoc();
146             atts.addAttribute(URI, "description", PREFIX + ":description", "CDATA", key);
147             ch.startElement(URI, "extra", PREFIX + ":extra", atts);
148             ch.characters(value.toCharArray(), 0, value.length());
149             ch.endElement(URI, "extra", PREFIX + ":extra");
150         }
151
152         // End root element.
153
ch.endElement(URI, "notify", PREFIX + ":notify");
154
155         // End the document.
156
ch.endPrefixMapping(PREFIX);
157         ch.endDocument();
158     }
159 }
160
Popular Tags