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.myfaces.tobago.internal.util;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.faces.context.FacesContext;
26  import java.util.HashSet;
27  import java.util.Locale;
28  import java.util.Map;
29  
30  public final class AccessKeyMap {
31  
32    private static final Logger LOG = LoggerFactory.getLogger(AccessKeyMap.class);
33  
34    private static final char[] KEYS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
35        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
36        'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
37  
38    private static final String REQUEST_MAP_KEY = "accessKeysRequestMapKey";
39  
40    private HashSet set;
41    private StringBuilder duplicated = new StringBuilder();
42  
43  
44    public static AccessKeyMap getInstance(final FacesContext facesContext) {
45      final Map requestMap = facesContext.getExternalContext().getRequestMap();
46      AccessKeyMap keyMap = (AccessKeyMap) requestMap.get(REQUEST_MAP_KEY);
47      if (keyMap == null) {
48        keyMap = new AccessKeyMap();
49        requestMap.put(REQUEST_MAP_KEY, keyMap);
50      }
51      return keyMap;
52    }
53  
54    private AccessKeyMap() {
55      set = new HashSet();
56    }
57  
58    private HashSet getSet() {
59      return set;
60    }
61  
62    private String getDuplicated() {
63      return duplicated.toString();
64    }
65  
66    private void addDublicated(final char key) {
67      duplicated = duplicated.append(key);
68    }
69  
70    public static boolean addAccessKey(final FacesContext facesContext, Character key) {
71      key = key.toString().toLowerCase(Locale.ENGLISH).charAt(0);
72      final AccessKeyMap instance = getInstance(facesContext);
73      if (instance.getSet().contains(key)) {
74        instance.addDublicated(key);
75        return false;
76      } else {
77        instance.getSet().add(key);
78        return true;
79      }
80    }
81  
82    public static String getDublicatedKeys(final FacesContext facesContext) {
83      return getInstance(facesContext).getDuplicated();
84    }
85  
86    public static String getUnusedKeys(final FacesContext facesContext) {
87      final HashSet set = getInstance(facesContext).getSet();
88      final StringBuilder sb = new StringBuilder();
89      for (final char key : KEYS) {
90        if (!set.contains(Character.valueOf(key))) {
91          sb.append(key);
92        }
93      }
94      return sb.toString();
95    }
96  }