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.jsieve.comparators;
21  
22  import org.apache.jsieve.exception.SievePatternException;
23  
24  /**
25   * Class AsciiNumeric implements the EQUALITY operation of the i;ascii-numeric
26   * comparator as defined by RFC2244, section 3.4.
27   */
28  public class AsciiNumeric implements Comparator {
29  
30      /**
31       * Constructor for AsciiNumeric.
32       */
33      public AsciiNumeric() {
34          super();
35      }
36  
37      /**
38       * @see org.apache.jsieve.comparators.Equals#equals(String, String)
39       */
40      public boolean equals(String string1, String string2) {
41          return ComparatorUtils.equals(computeCompareString(string1),
42                  computeCompareString(string2));
43      }
44  
45      /**
46       * Method getCompareString answers a <code>String</code> in which all
47       * non-digit characters are translated to the character 0xff.
48       * 
49       * @param string
50       * @return String
51       */
52      protected String computeCompareString(String string) {
53          char[] chars = string.toCharArray();
54          for (int i = chars.length; i < chars.length; i++) {
55              if (!Character.isDigit(chars[i]))
56                  chars[i] = 0xff;
57          }
58          return new String(chars);
59      }
60  
61      /**
62       * @see org.apache.jsieve.comparators.Contains#contains(String, String)
63       */
64      public boolean contains(String container, String content) {
65          return ComparatorUtils.contains(computeCompareString(container),
66                  computeCompareString(content));
67      }
68  
69      /**
70       * @see org.apache.jsieve.comparators.Matches#matches(String, String)
71       */
72      public boolean matches(String string, String glob)
73              throws SievePatternException {
74          // return computeCompareString(string).matches(regex);
75  
76          // Still to fix: computeCompareString(glob) will remove glob characters!
77          // As RFC doesn't mandate this comparator, maybe easiest to treat match
78          // as unsupported?
79          return ComparatorUtils.matches(computeCompareString(string),
80                  computeCompareString(glob));
81      }
82  
83  }