View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /* 
17  
18   */
19  
20  package org.apache.pluto.core.impl;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Enumeration;
25  
26  import javax.portlet.PortletPreferences;
27  import javax.portlet.PreferencesValidator;
28  import javax.portlet.ValidatorException;
29  
30  public class PreferencesValidatorImpl implements PreferencesValidator
31  {
32  
33      public PreferencesValidatorImpl()
34      {
35      }
36  
37      public void validate(PortletPreferences preferences)
38          throws ValidatorException
39      {
40        Collection failedKeys = new ArrayList();
41        Enumeration names = preferences.getNames();
42  
43        String[] defValues = {"no values"};
44        String[] values = null;
45        String key = null;
46  
47        while (names.hasMoreElements())
48        {
49            key = names.nextElement().toString();
50            values = preferences.getValues(key, defValues);
51  
52            if (values != null) // null values are allowed
53            {
54                for (int i=0; i<values.length;i++)
55                {
56                    if (values[i] != null) // null values are allowed
57                    {
58                        //validates that the preferences do not start or end with white space
59                        if (!values[i].equalsIgnoreCase(values[i].trim()))
60                        {
61                            failedKeys.add(key);
62                            i=values.length;
63                        }
64                    }
65                }
66            }
67        }
68  
69        if (!failedKeys.isEmpty())
70        {
71            throw new ValidatorException("One or more preferences do not comply with the validation criteria",failedKeys);
72        }
73      }
74  }