1 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 ; 23 import org.xml.sax.SAXException ; 24 import org.xml.sax.helpers.AttributesImpl ; 25 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 38 public class Notifier { 39 40 47 public static void notify(Notifying n, OutputStream outputStream, String mimetype) throws IOException { 48 notifyHTML(n, outputStream); 51 } 52 53 60 private static void notifyHTML(Notifying n, OutputStream outputStream) throws IOException { 61 if (outputStream == null) { 62 return; 63 } 64 65 StringBuffer sb = new StringBuffer (); 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 extras = n.getExtraDescriptions(); 92 for (Iterator i = extras.entrySet().iterator(); i.hasNext(); ) { 93 Map.Entry me = (Map.Entry )i.next(); 94 final String key = (String )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 109 public static void notify(Notifying n, ContentHandler ch, String mimetype) throws SAXException { 110 final String PREFIX = Constants.ERROR_NAMESPACE_PREFIX; 111 final String URI = Constants.ERROR_NAMESPACE_URI; 112 113 ch.startDocument(); 115 ch.startPrefixMapping(PREFIX, URI); 116 117 AttributesImpl atts = new AttributesImpl (); 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 ()); 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 ()); 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 ()); 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 extraDescriptions = n.getExtraDescriptions(); 141 for (Iterator i = extraDescriptions.entrySet().iterator(); i.hasNext(); ) { 142 final Map.Entry me = (Map.Entry ) i.next(); 143 String key = (String ) me.getKey(); 144 String value = String.valueOf(me.getValue()); 145 atts = new AttributesImpl (); 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 ch.endElement(URI, "notify", PREFIX + ":notify"); 154 155 ch.endPrefixMapping(PREFIX); 157 ch.endDocument(); 158 } 159 } 160 | Popular Tags |