BugMail.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  * The basic abstract mail representation of a BugProfile. A BugMail is the 
050  * formatted representation of a BugProfile; every BugProfile contains it.
051  
052  @author <A HREF="mailto:codeshaker@gmx.net">
053  *            Philipp Bartsch (codeshaker@gmx.net)</A>
054  *            <A HREF="../../../../gpl.txt">GPL License</A>
055  */
056 public abstract class BugMail {
057     
058     /** Stacktrace key*/
059     protected static  final String STACK_TRACE     = BugProfile.STACK_TRACE;
060     /** Error class key*/
061     protected static  final String ERROR_CLASS     = BugProfile.ERROR_CLASS;
062     /** User comment key*/
063     protected  static final String USER_COMMENT    = BugProfile.USER_COMMENT;
064     /** Recipient key*/
065     protected static  final String MAIL_TO         = BugProfile.MAIL_TO;
066     /** Message key*/
067     protected static  final String MESSAGE         = BugProfile.MESSAGE;
068     /** Date key*/
069     protected static  final String DATE            = BugProfile.DATE;
070     /** (Product) Version key*/
071     protected static  final String P_VERSION        = BugProfile.PRODUCT_VERSION;
072     /** Product name key*/
073     protected static  final String P_NAME           = BugProfile.PRODUCT_NAME;   
074     /** The mail format*/ 
075     protected static  final String SEND_FORMAT     = BugProfile.SEND_FORMAT;
076     /**OS depended line break*/
077     protected static  final String NEW_LINE        = System.getProperty("line.separator");
078     
079     /**
080      * Returns true, if the underlying format has enough informations for a valid transmission.
081      * Special formats like Bugzilla need at least some informations to generate a valid bug submission
082      * and this methods checks, if all "dependencies" are satisfied. If not, the special format gets 
083      * abandoned and the default mail format is generated and send to the address specified by 
084      * alt_recipients in common.settings.<BR>
085      * Attention: if the custom format equals the default format and this format can`t be satisfied, 
086      * text format will be used! Otherwise, you would create a infinite loop.
087      
088      @return true, if this format has all needed informations 
089      */
090     protected abstract boolean isSatisfied ()
091     
092     /**
093      * Returns the MIME type of the mail body delivered by generateContent(). Is used by the preview
094      * and the Message object.
095      *  
096      @return the mime type
097      @see #getMailBody the content generation method
098      */
099     protected abstract String getMimeType ();
100     
101     /**
102      * Returns a format dependend subject line for the mail message.
103      
104      @return the subject line
105      */
106     protected abstract String getSubjectLine ();
107         
108     /**
109      * Exports the related BugProfile properties into a format dependend mail body.
110      
111      @return the mail body
112      */
113     protected abstract String getMailBody ();
114 
115     /**
116      * Returns true, if the given key is one of the known basic properties.
117      
118      @param key       a property key
119      @return boolean  flag
120      */
121     protected static final boolean isStandardKey (final String key) {
122         return   key.equals(USER_COMMENT)
123               || key.equals(P_VERSION)
124               || key.equals(P_NAME)
125               || key.equals(STACK_TRACE)
126               || key.equals(DATE)
127               || key.equals(MESSAGE)
128               || key.equals(MAIL_TO)
129               || key.equals(ERROR_CLASS)
130               || key.equals(SEND_FORMAT);                
131     }    
132 
133     /**
134      * Replaces all occurences of subString with newString in string.
135      
136      @param string        the current string
137      @param subString        the replaced string
138      @param newString        the new token
139      @return                the custom string
140      */
141     protected static String replace_impl (final String string, 
142                                           final String subString, 
143                                           final String newString) {
144         
145         final StringBuffer result = new StringBuffer(string.length());
146         if (subString != null && newString != null) {    
147             int fi = 0//from index
148             int si; //index of subString found
149             while ((si = string.indexOf(subString, fi)) >= 0) {
150                 result.append(string.substring(fi, si));
151                 result.append(newString);
152                 fi = si + subString.length();
153             }//next occurence of subString
154             if (fi < string.length())
155                 result.append(string.substring(fi));
156             
157         else
158             result.append(string);
159                 
160         return result.toString();
161     }
162         
163 }