001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.util;
021    
022    import javax.faces.context.FacesContext;
023    import java.util.HashSet;
024    import java.util.Map;
025    import java.util.Locale;
026    
027    public class AccessKeyMap {
028    
029      private static final char[] KEYS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
030          'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
031          'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
032    
033      private static final String REQUEST_MAP_KEY = "accessKeysRequestMapKey";
034    
035      private HashSet set;
036      private StringBuilder duplicated = new StringBuilder();
037    
038    
039      public static AccessKeyMap getInstance(FacesContext facesContext) {
040        final Map requestMap = facesContext.getExternalContext().getRequestMap();
041        AccessKeyMap keyMap = (AccessKeyMap) requestMap.get(REQUEST_MAP_KEY);
042        if (keyMap == null) {
043          keyMap = new AccessKeyMap();
044          requestMap.put(REQUEST_MAP_KEY, keyMap);
045        }
046        return keyMap;
047      }
048    
049      private AccessKeyMap() {
050        set = new HashSet();
051      }
052    
053      private HashSet getSet() {
054        return set;
055      }
056    
057      private String getDuplicated() {
058        return duplicated.toString();
059      }
060    
061      private void addDublicated(char key) {
062        duplicated = duplicated.append(key);
063      }
064    
065      public static boolean addAccessKey(FacesContext facesContext, Character key) {
066        key = key.toString().toLowerCase(Locale.ENGLISH).charAt(0);
067        final AccessKeyMap instance = getInstance(facesContext);
068        if (instance.getSet().contains(key)) {
069          instance.addDublicated(key);
070          return false;
071        } else {
072          instance.getSet().add(key);
073          return true;
074        }
075      }
076    
077      public static String getDublicatedKeys(FacesContext facesContext) {
078        return getInstance(facesContext).getDuplicated();
079      }
080    
081      public static String getUnusedKeys(FacesContext facesContext) {
082        HashSet set = getInstance(facesContext).getSet();
083        StringBuilder sb = new StringBuilder();
084        for (char key : KEYS) {
085          if (!set.contains(Character.valueOf(key))) {
086            sb.append(key);
087          }
088        }
089        return sb.toString();
090      }
091    }