Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/util/EmailFtpAddressUtil.java


1   /*
2    * EmailFtpAddressUtil.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:30 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.util;
12  
13  import java.util.StringTokenizer;
14  import java.util.Vector;
15  /**
16   * This class validate the email/ftp addresses.This is an utility class.
17   * validation methods for ftp is yet not implemented.
18   * @author Mahesh Gidwani
19   * 02/08/2001
20   */
21  public class EmailFtpAddressUtil
22  {
23      public static String INVALID = "invalid";
24      private static int MIN_EMAIL_ADD_LENGTH = 6;
25      
26      
27     /**
28      * Gets the userId from email address. (eg. 'abc' from abc@flexstornet.com)
29      * @param String email address
30      */
31      public static String getUserId(String address)    
32      {
33          if( !isCountOfAtTheRateValid(address) )
34              return INVALID;
35          String tAddress = address.trim();
36          if(tAddress.indexOf(' ') > 0)
37              return INVALID;
38          int nIndex = tAddress.indexOf('@');
39          if(nIndex > 0)
40              return tAddress.substring(0,nIndex-1);    
41          else return INVALID;
42      }
43      
44      /**
45      * Gets the host+domain name email address. (eg. 'flexstornet.com' from abc@flexstornet.com)
46      * @param String email address.
47      */
48      public static String getDomainName(String address)
49      {
50          if( !isCountOfAtTheRateValid(address) )
51              return INVALID;
52          String tAddress = address.trim();
53          int nIndex = tAddress.indexOf('@');
54          String sDomain = tAddress.substring(nIndex+1,tAddress.length());
55          
56          if(nIndex > 0 && sDomain.length() > 3 && isDomainNameVaild(sDomain) )
57              return sDomain;    
58          else return INVALID;
59      }
60      
61     /**
62      * returns true if there is only one '@' character in email address.
63      * @param String email address
64      * @return boolean
65      */
66      private static boolean isCountOfAtTheRateValid(String address) 
67      {
68          char array[] = address.toCharArray();
69          int count = 0;
70          for(int i = 0; i < array.length; i++)
71          {
72              if(array[i]=='@')    
73              {
74                  count++;
75                  if(count > 1)
76                      return false;
77              }
78          }
79          if( count == 0 )
80              return false;
81          return true;
82          
83      }
84      private static boolean isDomainNameVaild(String sDomain)
85      {
86          if( sDomain == null )
87              return false;
88          int at_index = sDomain.indexOf('@');
89          int dot_index = sDomain.indexOf('.');
90          //If . comes just after @ 
91          if( dot_index == at_index+1)
92              return false;
93          int last_dot_index = sDomain.lastIndexOf('.');
94          
95          //If its not ending with tree-letter domain or two-letter country, return false.
96          //a@b.c is not valid.
97          String tString = sDomain.substring(last_dot_index+1);
98          int length = tString.length();
99          if( length < 2 || length >3 )
100             return false;
101         return true;
102     }
103    /**
104     * This method returns true, if the email address is valid.
105     * @param String email address.
106     * @return boolean true if address passed is valid email address else false.
107     */
108     public static boolean isEmailAddressValid(String address)
109     {
110         String tAdd = address.trim();
111         if( tAdd.length() < MIN_EMAIL_ADD_LENGTH || tAdd.indexOf(' ') > 0
112              || !isValidStartEnd(address) || isConsecutiveDots(address)
113              || !isCountOfAtTheRateValid(address) || isSpecialCharContains(address)
114              || !isDomainNameVaild(address))
115             return false;
116         else return true;
117     }
118 
119 
120     /** Siraj
121       Verify if comma seperated "addresses" are valid
122     */
123     public static boolean isEmailAddressesValid(String addresses)
124     {
125        try
126        {
127          Vector v = new Vector();
128          if (addresses.indexOf(",")== -1)
129          {  System.out.println(" Only one ");
130            v.addElement(addresses);
131          }
132          else
133          {
134            StringTokenizer st = new StringTokenizer(addresses, ", ");
135            while (st.hasMoreTokens())
136            {
137               System.out.println(" more ");
138               v.addElement(st.nextToken());
139            }
140          }
141          for(int i=0;i<v.size();i++)
142          {
143            if(isEmailAddressValid((String)v.elementAt(i))== false)
144             return (false);
145          }
146        }
147        catch(Exception e)
148        {
149          return false;
150        }
151 
152        return true;
153     }
154 
155     /** Siraj
156       Get invalid address from comma seperated "addresses" are valid
157     */
158     public static String getInvalidAddress(String addresses)
159     {
160        try
161        {
162          Vector v = new Vector();
163          if (addresses.indexOf(",")==-1)
164          {
165            v.addElement(addresses);
166          }
167          else
168          {
169            StringTokenizer st = new StringTokenizer(addresses, ", ");
170            while (st.hasMoreTokens())
171              v.addElement(st.nextToken());
172          }
173          for(int i=0;i<v.size();i++)
174          {
175            if(isEmailAddressValid((String)v.elementAt(i))== false)
176             return (String)v.elementAt(i);
177          }
178        }
179        catch(Exception e)
180        {
181          return "";
182        }
183 
184        return ""; // in fact this should never happen
185 
186     }
187     // This method checks if special characters which are not allowed, are there.If so, it
188     // returns true.
189     private static boolean isSpecialCharContains(String address)
190     {
191         char array[] = address.toCharArray();
192         for(int i = 0; i < array.length; i++)
193         {
194             if(array[i] == '<' || array[i] == '>' || array[i] == ')' || array[i] == '('
195                 ||array[i] == ',' || array[i] == ';' || array[i] == '"' || array[i] == '['
196                 ||  array[i] == ']' || array[i] == ':' || array[i] == '\\')    
197             {
198                 
199                 return true;
200             }
201         }
202         return false;
203     }
204     
205     public static boolean isConsecutiveDots(String address)
206     {
207         char array[] = address.toCharArray();
208         for(int i = 0; i < array.length; i++)
209         {
210             if( array[i]== '.' )    
211             {
212                 i++;
213                 if( array[i]== '.' )
214                     return true;
215             }
216         }
217         return false;
218     }
219     
220     private static boolean isValidStartEnd(String address)
221     {
222          return  (  ( !isStringStartsWith( address, '@' ) )
223                   && ( !isStringStartsWith ( address, '.' ) )
224                   && ( !isStringEndsWith   ( address, '@' ) )
225                   && ( !isStringEndsWith   ( address, '.' ) ) ) ;
226     }
227     public static boolean isStringStartsWith(String s,char c)
228     {
229         if(s == null)
230             return true;
231         if( s.indexOf(c) == 0) 
232             return true;
233         else return false;
234     }
235     
236     public static boolean isStringEndsWith(String s,char c)
237     {
238         if( s == null )
239             return true;
240         if( s.indexOf(c) == ( s.length()-1 ) ) 
241             return true;
242         else return false;
243     }
244     
245     /*public static boolean isFtpAddressValid(String address)
246     {
247         
248     }*/
249     
250 }