View Javadoc

1   /*
2    * $Id: ConfigurationValidator.java,v 1.7 2005/06/01 19:55:51 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.core.libraries;
10  
11  import java.util.ArrayList;
12  import java.util.Iterator;
13  import java.util.List;
14  import java.util.Map.Entry;
15  import java.util.regex.Pattern;
16  
17  /***
18   * Implementation of IConfigurationValidator that provides 
19   * validation for LibraryEntry and Library configurations.
20   * 
21   * Provide the constructor with one of the two constants, either 
22   * VALIDATE_ENTRY or VALIDATE_LIBRARY to determine the behavior 
23   * of the class. 
24   * 
25   * @author aperezlo
26   */
27  public class ConfigurationValidator implements IConfigurationValidator
28  {
29     /***
30      * Mode constant for creating ConfigurationValidators to operate on instances
31      * of <code>LibraryEntry</code>.
32      */
33     public static final int VALIDATE_ENTRY = 1;
34     /***
35      * Mode constant for creating ConfigurationValidators to operate on instances
36      * of <code>Library</code>.
37      */
38     public static final int VALIDATE_LIBRARY = 2;
39     
40     /***
41      * Inner validator that does the actual work.  This will be either an
42      * EntryValidator or a LibraryValidator depending on the mode specified at
43      * construction.
44      */
45     protected IConfigurationValidator _validator;
46     
47     /***
48      * Creates a new configuration validator to validate an entry or a library.
49      * @param mode Either <code>VALIDATE_ENTRY</code> or 
50      *             <code>VALIDATE_LIBRARY</code>.  If any other mode is 
51      *             specified, the resulting object will return <code>false</code>
52      *             for all validation attempts.
53      */
54     public ConfigurationValidator(int mode)
55     {
56        if(mode == VALIDATE_ENTRY)
57        {
58           _validator = new EntryValidator();
59        }
60        else
61        {
62           _validator = new LibraryValidator();
63        }
64     }
65     /*
66      *  (non-Javadoc)
67      * @see com.bbn.swede.core.libraries.IConfigurationValidator#getErrorMessages()
68      */
69     public String[] getErrorMessages()
70     {
71        return (_validator == null) ? new String[0] : _validator.getErrorMessages();
72     }
73     /*
74      *  (non-Javadoc)
75      * @see com.bbn.swede.core.libraries.IConfigurationValidator#isValid(java.util.Map.Entry)
76      */
77     public boolean isValid(Entry property)
78     {
79        return (_validator == null) ? false : _validator.isValid(property);
80     }
81     /*
82      *  (non-Javadoc)
83      * @see com.bbn.swede.core.libraries.IConfigurationValidator#isValid(com.bbn.swede.core.libraries.IConfiguration)
84      */
85     public boolean isValid(IConfiguration props)
86     {
87        return (_validator == null) ? false : _validator.isValid(props);
88     }
89  
90     private final class EntryValidator implements IConfigurationValidator
91     {
92        private List _errorMessages;
93        
94        private EntryValidator()
95        {
96           _errorMessages = new ArrayList();
97        }
98        
99        /*
100        *  (non-Javadoc)
101        * @see com.bbn.swede.core.libraries.IConfigurationValidator#getErrorMessages()
102        */
103       public String[] getErrorMessages()
104       {
105          String[] toReturn = new String[_errorMessages.size()];
106          Iterator errorI = _errorMessages.iterator();
107          int i = 0; 
108          while(errorI.hasNext())
109          {
110             toReturn[i] = (String) errorI.next();
111             i++;
112          }
113          return toReturn;
114       }
115     
116       /*
117        *  (non-Javadoc)
118        * @see com.bbn.swede.core.libraries.IConfigurationValidator#isValid(java.util.Map.Entry)
119        */
120       public boolean isValid(Entry property)
121       {
122          boolean toReturn = true;
123          return toReturn;
124       }
125       
126       /*
127        *  (non-Javadoc)
128        * @see com.bbn.swede.core.libraries.IConfigurationValidator#isValid(com.bbn.swede.core.libraries.IConfiguration)
129        */
130       public boolean isValid(IConfiguration props)
131       {
132          boolean toReturn = true;
133          LibraryEntryConfiguration lec = (LibraryEntryConfiguration) props;
134          Iterator i = props.getSystemProperties().entrySet().iterator();
135          _errorMessages.clear();
136          while(i.hasNext())
137          {
138             if(!isValid((Entry) i.next()))
139             {
140                toReturn = false;
141                break;
142             }
143          }
144          if(toReturn)
145          {
146             if((null == lec.getURI()) || (lec.getURI().length() < 1))
147             {
148                toReturn = false;
149                _errorMessages
150                   .add("You must enter a new piece of metadata called '"
151                      + LibraryEntryConfiguration.ENTRY_URI_PROPERTY
152                      + "' with a value which is the URI that should be used to reference '"
153                      + lec.getName() + "'.");
154             }
155             if((null == lec.getName()) || (lec.getName().length() < 1))
156             {
157                toReturn = false;
158                _errorMessages
159                   .add("One of your resources does not have a name.  "
160                      + "You must enter a new piece of metadata for it called '"
161                      + LibraryEntryConfiguration.ENTRY_NAME_PROPERTY
162                      + "' and give it a name.");
163             }
164             if(null != lec.getUpdateSchedule())
165             {
166                if(lec.getUpdateSchedule().equals(Boolean.TRUE.toString()))
167                {
168                   if((null == lec.getURL() 
169                      || (lec.getURL().length() < 1) 
170                      || !Pattern.matches(
171                         "//w*:/+?([a-zA-Z]:)?/+?([//w//-]++//.?)+?([//w//-]++)?/"
172                         + "([//w//-//.]++/?)*?[//w//-]*?(//.[//w//-]++)*?",
173                         lec.getURL())))
174                   {
175                      toReturn = false;
176                      _errorMessages
177                         .add("You have chosen to allow updates for a resource, "
178                               + "but not provided a valid URL.");
179                   }
180                }
181             }
182          }
183          return toReturn;
184       }      
185       
186    }
187    
188    private final class LibraryValidator implements IConfigurationValidator
189    {
190       private List _errorMessages;
191       
192       private LibraryValidator()
193       {
194          _errorMessages = new ArrayList();
195       }
196 
197       /*
198        *  (non-Javadoc)
199        * @see com.bbn.swede.core.libraries.IConfigurationValidator#getErrorMessages()
200        */
201       public String[] getErrorMessages()
202       {
203          String[] toReturn = new String[_errorMessages.size()];
204          Iterator errorI = _errorMessages.iterator();
205          int i = 0; 
206          while(errorI.hasNext())
207          {
208             toReturn[i] = (String) errorI.next();
209             i++;
210          }
211          return toReturn;
212       }
213 
214       /*
215        *  (non-Javadoc)
216        * @see com.bbn.swede.core.libraries.IConfigurationValidator#isValid(java.util.Map.Entry)
217        */
218       public boolean isValid(Entry property)
219       {
220          boolean toReturn = false;
221          _errorMessages.clear();
222 
223          return toReturn;
224       }
225       
226       /*
227        *  (non-Javadoc)
228        * @see com.bbn.swede.core.libraries.IConfigurationValidator#isValid(com.bbn.swede.core.libraries.IConfiguration)
229        */
230       public boolean isValid(IConfiguration props)
231       {
232          boolean toReturn = true;
233          Iterator i = props.getSystemProperties().entrySet().iterator();
234          _errorMessages.clear();
235          while(i.hasNext())
236          {
237             if(!isValid((Entry) i.next()))
238             {
239                toReturn = false;
240                break;
241             }
242          }
243          return toReturn;
244       }
245    }
246 }