View Javadoc

1   /*
2    * $Id: BasicPatternDefinitionResolver.java 836180 2009-11-14 14:00:02Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.tiles.definition.pattern;
23  
24  import java.util.LinkedHashSet;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.tiles.Definition;
30  
31  /**
32   * A pattern definition resolver that stores {@link DefinitionPatternMatcher}
33   * separated by customization key. <br>
34   * It delegates creation of definition pattern matchers to a
35   * {@link DefinitionPatternMatcherFactory} and recgnizes patterns through the
36   * use of a {@link PatternRecognizer}.
37   *
38   * @param <T> The type of the customization key.
39   * @version $Rev: 836180 $ $Date: 2009-11-15 01:00:02 +1100 (Sun, 15 Nov 2009) $
40   * @since 2.2.0
41   */
42  public class BasicPatternDefinitionResolver<T> extends
43          AbstractPatternDefinitionResolver<T> {
44  
45      /**
46       * The factory of pattern matchers.
47       */
48      private DefinitionPatternMatcherFactory definitionPatternMatcherFactory;
49  
50      /**
51       * The pattern recognizer.
52       */
53      private PatternRecognizer patternRecognizer;
54  
55      /**
56       * Constructor.
57       *
58       * @param definitionPatternMatcherFactory The definition pattern matcher factory.
59       * @param patternRecognizer The pattern recognizer.
60       */
61      public BasicPatternDefinitionResolver(DefinitionPatternMatcherFactory definitionPatternMatcherFactory,
62              PatternRecognizer patternRecognizer) {
63          this.definitionPatternMatcherFactory = definitionPatternMatcherFactory;
64          this.patternRecognizer = patternRecognizer;
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      protected Map<String, Definition> addDefinitionsAsPatternMatchers(List<DefinitionPatternMatcher> matchers,
70              Map<String, Definition> defsMap) {
71          Set<String> excludedKeys = new LinkedHashSet<String>();
72          for (Map.Entry<String, Definition> de : defsMap.entrySet()) {
73              String key = de.getKey();
74              if (patternRecognizer.isPatternRecognized(key)) {
75                  matchers.add(definitionPatternMatcherFactory
76                          .createDefinitionPatternMatcher(key, de.getValue()));
77              } else {
78                  excludedKeys.add(key);
79              }
80          }
81          return PatternUtil.createExtractedMap(defsMap, excludedKeys);
82      }
83  }