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.renderkit;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.SupportsAccessKey;
24  import org.apache.myfaces.tobago.util.ComponentUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import javax.faces.component.UIComponent;
29  import java.util.Locale;
30  
31  public final class LabelWithAccessKey {
32  
33    private static final Logger LOG = LoggerFactory.getLogger(LabelWithAccessKey.class);
34  
35    private final String label;
36    private final Character accessKey;
37    private final int pos;
38  
39    public LabelWithAccessKey(final SupportsAccessKey component) {
40      this(component, false);
41    }
42  
43    public LabelWithAccessKey(final SupportsAccessKey component, final boolean preferItemLabel) {
44  
45      String label0;
46      Character accessKey0;
47      int pos0 = -1;
48  
49      label0 = component.getLabel();
50  
51      // compatibility since TOBAGO-1093
52      if (preferItemLabel) {
53        final String itemLabel = ComponentUtils.getStringAttribute((UIComponent) component, Attributes.itemLabel);
54        if (itemLabel != null) {
55          label0 = itemLabel;
56        }
57      }
58  
59      accessKey0 = component.getAccessKey();
60      if (accessKey0 != null) {
61        accessKey0 = Character.toLowerCase(accessKey0);
62        if (!isPermitted(accessKey0)) {
63          LOG.warn("Ignoring illegal access key: " + accessKey0);
64          accessKey0 = null;
65        }
66      }
67  
68      if (accessKey0 != null && label0 != null) {
69        pos0 = label0.toLowerCase(Locale.ENGLISH).indexOf(accessKey0);
70      }
71  
72      label = label0;
73      accessKey = accessKey0;
74      pos = pos0;
75    }
76  
77    public String getLabel() {
78      return label;
79    }
80  
81    public Character getAccessKey() {
82      return accessKey;
83    }
84  
85    public int getPos() {
86      return pos;
87    }
88  
89    /**
90     * Ensures, that no illegal character will be write out.
91     * (If this is changed from only allowing normal letters and numbers, the renderers may change the escaping)
92     */
93    private boolean isPermitted(final Character accessKey) {
94      return accessKey == null || accessKey >= 'a' && accessKey <= 'z' || accessKey >= '0' && accessKey <= '9';
95    }
96  }