NS6Filter.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 import java.io.File;
049 import java.util.StringTokenizer;
050 
051 /**
052  * This class filters NetScape 6.x (and higher) and Mozilla profiles and imports
053  * necessary data.
054  
055  @author <A HREF="mailto:codeshaker@gmx.net">
056  *            Philipp Bartsch (codeshaker@gmx.net)</A>
057  *            <A HREF="../../../../gpl.txt">GPL License</A>
058  */
059 public final class   NS6Filter
060              extends ExtendedFilter {
061     
062     /**The requested preferences file. Value is "prefs.js".*/
063     private static final String fileName     = "prefs.js";
064     /**A filechooser description. Value is "Netscape 6/Mozilla >> prefs.js"*/
065     private static final String description  = "Netscape 6/Mozilla >> prefs.js";  
066     
067     /**
068      * Creates a NS6.x filter.
069      */
070     NS6Filter () {
071 
072     }
073 
074     /**
075      * Extracts the requested content out of a given token
076      
077      @param  token    the document token
078      @return String    the requested content
079      */
080     private static String extractEntry (final String token) {
081         return token.substring(token.indexOf(", \""3,
082                                token.length()-2);
083     }
084 
085     /**
086      * Returns true, if the given preferences file contains at least a SMTP-
087      * Server and user`s mail address.
088      * Furthermore extracts necessary adn available data of a NS4 preferences
089      * file.
090      @param  file    preferences file
091      @return boolean returns true, if smtp & mail address section available
092      
093      @see org.shaker.dwu.ExtendedFilter#resolvePropFile(File)
094      */
095     protected final boolean resolvePropFile (final File file) {
096         final String content = readFileToString(file);
097         final boolean hasPOP = content.indexOf("mail.server" +
098                                                ".server1.realhostname"> -1
099                                || content.indexOf("mail.server" +
100                                                      ".server1.hostname"> -1;
101         final boolean hasSMTP = content.indexOf("mail.smtpserver" +
102                                                 ".smtp1.hostname"> -1;
103         final boolean hasUSER = content.indexOf("mail.server" +
104                                                 ".server1.userName"> -1;
105         final boolean hasMAIL = content.indexOf("mail.identity" +
106                                                 ".id1.useremail"> -1;
107         
108         // we need at least a working smtp server and a email
109         if (hasSMTP && hasMAIL) {            
110             StringTokenizer tokenizer = new StringTokenizer(content,
111                                                             ";");
112             String token;
113             while (tokenizer.hasMoreTokens()) {
114                 token = tokenizer.nextToken();
115                 if (hasPOP &&
116                     token.indexOf("mail.server.server1.realhostname"> -||
117                     token.indexOf("mail.server.server1.hostname"> -1) {
118                     DocWhatsUp.SETTINGS.setProperty("mail.pop",
119                                                     extractEntry(token));                                   
120                 else if (token.indexOf("mail.smtpserver.smtp1.hostname")>-1) {
121                     DocWhatsUp.SETTINGS.setProperty("mail.host",
122                                                       extractEntry(token));    
123                 else if (hasUSER &&
124                            token.indexOf("mail.server.server1.userName"> -1) {
125                     DocWhatsUp.SETTINGS.setProperty("mail.user",
126                                                     extractEntry(token));                        
127                 else if (token.indexOf("mail.identity.id1.useremail"> -1) {
128                     DocWhatsUp.SETTINGS.setProperty("mail.from",
129                                                     extractEntry(token));    
130                 }
131             }
132             return true;
133         else {            
134             return false;
135         }
136     }
137 
138     
139     /**
140      * Returns true, if the given file is "prefs.js" (and for directories to
141      * support filesystem browsing).
142      
143      @param     file    the file
144      @return  boolean flag
145      
146      @see javax.swing.filechooser.FileFilter#accept(java.io.File)
147      */
148     public final boolean accept(final File file) {
149         return file.isDirectory() || file.getName().equals(fileName);
150     }
151     
152     /**
153      * Returns a filechooser description. @see #description for descriptions
154      * content
155      
156      @return String the description
157      @see javax.swing.filechooser.FileFilter#getDescription()
158      */
159     public final String getDescription() {
160         return description;
161     }
162 }