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