View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.mitosis.configuration;
21  
22  
23  import java.beans.PropertyEditor;
24  import java.beans.PropertyEditorSupport;
25  import java.util.regex.Pattern;
26  
27  import org.apache.directory.shared.ldap.util.StringTools;
28  
29  
30  /**
31   * A {@link PropertyEditor} that converts strings into ReplicaIds
32   * and vice versa.
33   *
34   * @author The Apache Directory Project (dev@directory.apache.org)
35   * @version $Rev: 95 $, $Date: 2006-09-16 13:04:28 +0200 (Sat, 16 Sep 2006) $
36   */
37  public class ReplicaIdPropertyEditor extends PropertyEditorSupport
38  {
39      /** The replica pattern. */
40      private static final Pattern REPLICA_ID_PATTERN = Pattern.compile( "[-_A-Z0-9]{1,16}" );
41      
42      public ReplicaIdPropertyEditor()
43      {
44          super();
45      }
46  
47  
48      public ReplicaIdPropertyEditor( Object source )
49      {
50          super( source );
51      }
52  
53  
54      /**
55       * Check a new instance of ReplicaId. The id must be a String 
56       * which respect the pattern :
57       * 
58       * [-_a-zA-Z0-9]*
59       * 
60       * and must be between 1 and 16 chars length
61       *
62       * @param id The replica to check
63       * @return true if the replicaId is well formed
64       */
65      public static boolean check( String id )
66      {
67          if ( StringTools.isEmpty( id ) )
68          {
69              throw new IllegalArgumentException( "Empty ID: " + id );
70          }
71  
72          String tmpId = id.trim().toUpperCase();
73  
74          if ( !REPLICA_ID_PATTERN.matcher( tmpId ).matches() )
75          {
76              return false;
77          }
78  
79          return true;
80      }
81  
82      
83      public String getAsText()
84      {
85          Object val = getValue();
86          
87          if ( val == null )
88          {
89              return "";
90          }
91          else
92          {
93              return val.toString();
94          }
95      }
96  
97  
98      public void setAsText( String text ) throws IllegalArgumentException
99      {
100         text = text.trim();
101         
102         if ( text.length() == 0 )
103         {
104             setValue( null );
105         }
106         else
107         {
108             if ( check( text ) )
109             {
110                 setValue( text );
111             }
112             else
113             {
114                 setValue( null );
115             }        
116         }
117     }
118 }