PlainTextMail.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 low-sized mail body (about 1KB). 
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 PlainTextMail extends BugMail {
056     
057     /**The corresponding BugProfile*/    
058     private final BugProfile bug;
059         
060     /**
061      * Creates the MailBody.
062      
063      @param bugProfile the calling BugProfile that gets exported.
064      */
065     protected PlainTextMail (final BugProfile bugProfile) {
066         bug = bugProfile;
067     }
068 
069     /**
070      * Returns true.
071      *
072      @return always true; this format has no requirements
073      */
074     protected boolean isSatisfied () {
075         return true;
076     }
077 
078     /**
079      * Returns text/plain.
080      *
081      @return MIME type text/plain
082      */
083     protected final String getMimeType() {        
084         return "text/plain";
085     }
086     
087     /**
088      * Returns "DWU $message $bugprofile-hashsig"
089      
090      @return the subject line
091      */
092     protected final String getSubjectLine () {
093         return "DWU caught an error in \""
094                 + bug.getProperty(BugMail.ERROR_CLASS
095                 "\""
096                 (bug.containsKey(MESSAGE)
097                    ?": " + bug.getProperty(MESSAGE)
098                    "");
099     }
100     
101     /**
102      * Exports the given PropertySet (a BugProfile) into a plain text (without
103      * eye candy), that gets embedded into a mail body.
104      *
105      @return the plain text mail body
106      */
107     protected final String getMailBody () {
108         
109         final StringBuffer body = new StringBuffer();
110         
111         if (bug.containsKey(P_NAME))
112             body.append(getTextRow(P_NAME,  
113                                    bug.getProperty(P_NAME)));
114         if (bug.containsKey(P_VERSION))
115             body.append(getTextRow(P_VERSION ,
116                                    bug.getProperty(P_VERSION)))
117         body.append(getTextRow(ERROR_CLASS,
118                                bug.getProperty(ERROR_CLASS)))
119         if (bug.containsKey(MESSAGE))
120             body.append(getTextRow(MESSAGE,
121                                    bug.getProperty(MESSAGE)))
122         if (bug.containsKey(STACK_TRACE))
123             body.append(getTextRow(STACK_TRACE,
124                                    bug.getProperty(STACK_TRACE)))
125         if (bug.containsKey(USER_COMMENT))
126             body.append(getTextRow(USER_COMMENT,
127                                    bug.getProperty(USER_COMMENT)))
128         
129         body.append(NEW_LINE);
130         
131         // bundles the rest of the properties (custom ones) 
132         String[] keys = (String[]) bug.keySet().toArray(new String[0]);
133         for (int i = keys.length-1; i >= 0; i--) {                      
134             if (!isStandardKey(keys[i]))               
135                 body.append(getTextRow(keys[i],
136                                        wellform(bug.getProperty(keys[i]))));     
137         }
138         
139         body.append(NEW_LINE);
140                         
141         keys = ToolBox.getSortetSysPropArray();
142         for (int i = 0; i < keys.length; i++) {  
143             if (BugProfile.propertyFilter.contains(keys[i]))
144                 body.append(getTextRow(keys[i],
145                                        wellform(System.getProperty(keys[i]))));
146         }
147         
148         return body.toString();
149     }    
150     
151     /**
152      * Returns a formatted textrow for a key-value pair
153      
154      @param key     the label
155      @param value     the content
156      @return         the formatted textrow
157      */
158     private static final String getTextRow (final String key,
159                                             final String value) {
160         return    NEW_LINE
161                 "<" + key + ">" + NEW_LINE             
162                 + value    + NEW_LINE;
163     }
164     
165     /**
166      * Returns the given text with replaced linebreaks.
167      
168      @param     text the text to be wellformed
169      @return         the wellformed text    
170      */
171     private static String wellform (String  text) {        
172         return replace_impl(text,
173                             ";",
174                             NEW_LINE);
175     }        
176 }