HTMLMail.java
001 /*
002  * This class is part of DocWhatsUp, a bug profiling and -reporting lib.
003  
004  * Copyright (C)
005  
006  * This library is free software; you can redistribute it and/or modify it
007  * under the terms of the GNU General Public License as published by the
008  * Free Software Foundation; either version 2 of the License, or (at your
009  * option) any later version.
010  
011  * This program is distributed in the hope that it will be useful, but
012  * WITHOUT ANY WARRANTY; without even the implied warranty of
013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
014  * Public License for more details.
015  *  
016  * You should have received a copy of the GNU General Public License along
017  * with this program; if not, write to the Free Software Foundation, Inc.,
018  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019  
020  * EXTENSION:
021  * Linking DocWhatsUp statically or dynamically with other modules is making a
022  * combined work based on DocWhatsUp.  Thus, the terms and conditions of the 
023  * GNU General Public License cover the whole combination.
024  *
025  * As a special exception, the copyright holder of DocWhatsUp give you
026  * permission to link DocWhatsUp with independent modules that communicate with
027  * DocWhatsUp solely through the DocWhatsUp.java interface, regardless of the 
028  * license terms of these independent modules, and to copy and distribute the
029  * resulting combined work under terms of your choice, provided that
030  * every copy of the combined work is accompanied by a complete copy of
031  * the source code of DocWhatsUp (the version of DocWhatsUp used to produce the
032  * combined work), being distributed under the terms of the GNU General
033  * Public License plus this exception.  An independent module is a module
034  * which is not derived from or based on DocWhatsUp.
035 
036  * Note that people who make modified versions of DocWhatsUp are not obligated
037  * to grant this special exception for their modified versions; it is
038  * their choice whether to do so.  The GNU General Public License gives
039  * permission to release a modified version without this exception; this
040  * exception also makes it possible to release a modified version which
041  * carries forward this exception.
042  
043  * Author: Philipp Bartsch; codeshaker@gmx.net
044  */
045 
046 package org.shaker.dwu;
047 
048 /**
049  * This subclass of MailBody delivers a well formatted and mail body (above 5KB). 
050  
051  @author <A HREF="mailto:codeshaker@gmx.net">
052  *            Philipp Bartsch (codeshaker@gmx.net)</A>
053  *            <A HREF="../../../../gpl.txt">GPL License</A>
054  */
055 final class HTMLMail extends BugMail {
056     
057     /**The corresponding BugProfile*/    
058     private final BugProfile bug;
059         
060         
061     /**
062      * Returns true.
063      *
064      @return always true, this format has no requirements
065      */
066     protected boolean isSatisfied () {
067         return true;
068     }
069     
070     /**
071      * Creates the MailBody.
072      
073      @param bugProfile the calling BugProfile that gets exported.
074      */
075     protected HTMLMail (final BugProfile bugProfile) {
076         bug = bugProfile;
077     }
078     
079     /**
080      * Returns text/html.
081      *
082      @return MIME type text/html
083      */
084     protected final String getMimeType() {
085         return "text/html";
086     }
087 
088     /**
089      * Returns "DWU $message $bugprofile-hashsig"
090      
091      @return the subject line
092      */
093     protected final String getSubjectLine () {
094         return "DWU caught an error in \""
095                 + bug.getProperty(BugMail.ERROR_CLASS
096                 "\""
097                 (bug.containsKey(MESSAGE)
098                    ?": " + bug.getProperty(MESSAGE)
099                    "");
100     }
101     
102     /**
103      * Returns a clean designed HTML mail body.
104      *
105      @return the html formatted mail body
106      */
107     protected final String getMailBody () {
108         final StringBuffer body = new StringBuffer();
109         
110         body.append(   "<html>" + NEW_LINE
111                      "<body>" + NEW_LINE
112                      "<font align=\"center\" " +
113                                      "face=\"Arial\" " +
114                                      "size=\"-1\" " +
115                                      "color=\"black\">" + NEW_LINE);
116         body.append(getTableHeader());
117     
118         if (bug.containsKey(P_NAME))
119             body.append(getTableRow(P_NAME,
120                                     "<b>" 
121                                     + bug.getProperty(P_NAME)
122                                     "</b>",
123                                     false,
124                                     true));                         
125         if (bug.containsKey(P_VERSION))
126             body.append(getTableRow(P_VERSION,
127                                     "<b>" 
128                                     + bug.getProperty(P_VERSION)
129                                     "</b>",
130                                     false,
131                                     true))
132         body.append(getTableRow(ERROR_CLASS,
133                                 bug.getProperty(ERROR_CLASS),
134                                 true,
135                                 true))
136         if (bug.containsKey(MESSAGE))
137             body.append(getTableRow(MESSAGE,
138                                     bug.getProperty(MESSAGE),
139                                     true,
140                                     true))
141         if (bug.containsKey(STACK_TRACE))
142             body.append(getTableRow(STACK_TRACE,
143                                     bug.getProperty(STACK_TRACE),
144                                     true,
145                                     true))
146         if (bug.containsKey(USER_COMMENT))
147             body.append(getTableRow(USER_COMMENT,
148                                     "<pre>" 
149                                     bug.getProperty(USER_COMMENT+
150                                     "</pre>",
151                                     true,
152                                     true))
153 
154         // bundles the rest of the properties (custom ones) 
155         String[] keys = (String[]) bug.keySet().toArray(new String[0]);
156         for (int i = keys.length-1; i >= 0; i--) {                      
157             if (!isStandardKey(keys[i]))               
158                 body.append(getTableRow(keys[i],
159                                         bug.getProperty(keys[i]),
160                                         false,
161                                         true));     
162         }
163         
164         keys = ToolBox.getSortetSysPropArray();        
165         for (int i = 0; i < keys.length; i++) {  
166             if (BugProfile.propertyFilter.contains(keys[i]))
167                 body.append(getTableRow(keys[i],
168                                         System.getProperty(keys[i]),
169                                         true,
170                                         false));
171         }        
172 
173         body.append(getTableTail());       
174         // finished             
175 
176         // close html document
177         body.append(  "</font>"
178                     "</body>"
179                     "</html>");
180         return body.toString();
181     }    
182     
183 
184     /**
185      * Returns a locally used, standardized html tableheader.
186      *
187      @return the table header code string
188      */
189     private static final String getTableHeader () {
190         return "<table border=\"0\" cellspacing=\"1\" cellpadding=\"0\">"
191                + NEW_LINE;
192     }
193 
194     /**
195      * Returns a locally used, standardized html table tail
196      
197      @param   key          the inserted key
198      @param   value      the inserted value
199      @param   wellform  specifies wheter to replace ";" and linebreaks with
200      *                    html line breaks (br)
201      @param   highlight specifies wheter to highlight the row label with a
202      *                    blue or the standard grey background
203      @return  the row (html code)
204      */ 
205     private static final String getTableRow (final String  key,
206                                              final String  value,
207                                              final boolean wellform,
208                                              final boolean highlight) {
209         return   "<tr>" + NEW_LINE +
210                      "<td valign=\"top\"" + NEW_LINE +
211                          "align=\"right\"" + NEW_LINE +
212                          "bgColor=\"" (highlight
213                                          "#6495ED\">"
214                                          "silver\">"+ NEW_LINE +
215                              "<font face=\"Arial\" size=\"2\">" + NEW_LINE +
216                                     "<nobr>" + key + "</nobr>" +  NEW_LINE +
217                              "</font>" + NEW_LINE +
218                      "</td>" + NEW_LINE +
219                      "<td width=\"5\">" + NEW_LINE +
220                          "&nbsp;" + NEW_LINE +         
221                      "</td>" + NEW_LINE +
222                      "<td valign=\"top\"" + NEW_LINE +
223                          "width=\"600\">" + NEW_LINE +
224                          "<font face=\"Courier\" size=\"2\">" + NEW_LINE +
225                              (value.indexOf(BugMail.NEW_LINE> -|| wellform
226                               ? wellform(value)
227                               : value+ NEW_LINE +
228                          "</font>" + NEW_LINE +
229                      "</td>" + NEW_LINE +
230                  "</tr>" + NEW_LINE;                                                                          
231     }
232     
233     /**
234      * Returns a locally used, standardized html table tail
235      
236      @return String the tail (html code)
237      */
238     private static final String getTableTail () {
239         return    "</table>"+ NEW_LINE +
240                   "<br clear=\"all\">" + NEW_LINE;                            
241     }
242     
243     /**
244      * Returns the given text with html-entity replaced linebreaks and tabs.
245      
246      @param     text the text to be wellformed
247      @return         the wellformed text    
248      */
249     private static String wellform (String text) {
250         
251         text = replace_impl(text,
252                             NEW_LINE,
253                             "<br>");
254         text = replace_impl(text,
255                             ":",
256                             "<br>");                    
257         return replace_impl(text,
258                             ";",
259                             "<br>");
260     }    
261 }