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