View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
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  package org.settings4j.util;
19  
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  import org.apache.commons.collections4.Transformer;
24  
25  /**
26   * This Transformer implements the function for a {@link org.apache.commons.collections4.map.LazyMap}
27   * with Key=String (=RegEx) and Value=Boolean (=Result).
28   * 
29   * <pre>
30   * Example:
31   * 
32   * &lt;%
33   * request.setAttribute("matchPatternMap", LazyMap.decorate(new HashMap(), new MatchPatternTransformer("testString")));
34   * %&gt;
35   * &lt;html&gt;
36   *   &lt;head&gt;&lt;/head&gt;
37   *   &lt;body&gt;
38   *     ${matchPatternMap['testString']} == true &lt;br /&gt;
39   *     ${matchPatternMap['.*String']} == true &lt;br /&gt;
40   *     ${matchPatternMap['test.*']} == true &lt;br /&gt;
41   *     ${matchPatternMap['.*string']} == false &lt;br /&gt;
42   *   &lt;/body&gt;
43   * &lt;html&gt;
44   * </pre>
45   * 
46   * @see java.util.regex.Pattern
47   * @author Harald.Brabenetz
48   */
49  public class MatchPatternTransformer implements Transformer<String, Boolean> {
50  
51      /** General Logger for this Class. */
52      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(MatchPatternTransformer.class);
53  
54      private final String compareValue;
55  
56      /**
57       * @param compareValue The String which should be Compared with a RegEx. see {@link Pattern}.
58       */
59      public MatchPatternTransformer(final String compareValue) {
60          super();
61          this.compareValue = compareValue;
62      }
63  
64  
65      /** {@inheritDoc} */
66      public Boolean transform(final String input) {
67          Boolean result = Boolean.FALSE;
68          if (input != null) {
69              final String patternString = input.toString();
70              try {
71                  final Pattern pattern = Pattern.compile(patternString);
72                  final Matcher matcher = pattern.matcher(this.compareValue);
73                  if (matcher.matches()) {
74                      result = Boolean.TRUE;
75                      LOG.debug("TRUE - found '{}' in '{}'", patternString, compareValue);
76                  } else {
77                      result = Boolean.FALSE;
78                      LOG.debug("FALSE - do not found '{}' in '{}'", patternString, compareValue);
79                  }
80              } catch (final Exception e) {
81                  LOG.warn("Cann't matche Pattern '" + patternString + "' with compareValue '" + this.compareValue + "'",
82                      e);
83                  result = Boolean.FALSE;
84              }
85          }
86          return result;
87      }
88  
89  }