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.css;
21  
22  import org.apache.commons.collections.map.MultiKeyMap;
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.myfaces.tobago.component.SupportsMarkup;
25  import org.apache.myfaces.tobago.context.Markup;
26  import org.apache.myfaces.tobago.context.Theme;
27  import org.apache.myfaces.tobago.internal.util.Deprecation;
28  import org.apache.myfaces.tobago.util.VariableResolverUtils;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import javax.faces.component.UIComponent;
33  import javax.faces.context.FacesContext;
34  
35  /**
36   * Builds the CSS class attribute of tags.
37   * The names will be generated in a formal way, so generic name (and abbrevation) are possible.
38   * The class works like a factory, so caching will be possible.
39   * <p/>
40   * The default naming conventions allow these values:<br/>
41   *
42   * <ul>
43   * <li>tobago-&lt;rendererName></li>
44   * <li>tobago-&lt;rendererName>-markup-&lt;markupName></li>
45   * <li>tobago-&lt;rendererName>-&lt;subElement></li>
46   * <li>tobago-&lt;rendererName>-&lt;subElement>-markup-&lt;markupName></li>
47   * </ul>
48   *
49   * where
50   * <ul>
51   * <li>&lt;rendererName>, &lt;subElement> and &lt;markupName> must only contain ASCII-chars and -numbers</li>
52   * <li>&lt;rendererName> is the rendererType with a lower case char as first char</li>
53   * <li>&lt;subElement> is a sub element of the main tag in the output language (e.g. HTML)</li>
54   * <li>&lt;markupName> is the name of an existing markup</li>
55   * </ul>
56   * If the markup contains more than one name, there will be generated more than one output string.
57   * E.g.: UIIn with Markup [readonly, error] will get the class
58   * "tobago-in tobago-in-markup-readonly tobago-in-markup-error".
59   *
60   */
61  public final class Classes {
62  
63    private static final Logger LOG = LoggerFactory.getLogger(Classes.class);
64  
65    private static final MultiKeyMap CACHE = new MultiKeyMap();
66  
67    private final String stringValue;
68  
69    public static Classes create(UIComponent component) {
70      return create(component, true, null, null, false);
71    }
72  
73    public static Classes create(UIComponent component, String sub) {
74      return create(component, true, sub, null, false);
75    }
76  
77    public static Classes create(UIComponent component, Markup explicit) {
78      return create(component, false, null, explicit, false);
79    }
80  
81    public static Classes create(UIComponent component, String sub, Markup explicit) {
82      return create(component, false, sub, explicit, false);
83    }
84  
85    // XXX optimize synchronized
86    private static synchronized Classes create(
87        UIComponent component, boolean markupFromComponent, String sub, Markup explicit, boolean ignoreCheck) {
88      final String rendererName = StringUtils.uncapitalize(component.getRendererType());
89      final Markup markup = markupFromComponent ? ((SupportsMarkup) component).getCurrentMarkup() : explicit;
90      Classes value = (Classes) CACHE.get(rendererName, markup, sub);
91      if (value == null) {
92        value = new Classes(rendererName, markup, sub, ignoreCheck);
93        CACHE.put(rendererName, markup, sub, value);
94        if (LOG.isDebugEnabled()) {
95          LOG.debug("Element added (size={}) to cache (renderName='{}', markup='{}', sub='{}')",
96              new Object[] {CACHE.size(), rendererName, markup, sub});
97        }
98      }
99      return value;
100   }
101 
102   private Classes(String rendererName, Markup markup, String sub, boolean ignoreMarkupCheck) {
103 
104     assert sub == null || StringUtils.isAlphanumeric(sub) : "Invalid sub element name: '" + sub + "'";
105 
106     // These values are statistically tested length of the html class attribute
107     StringBuilder builder = new StringBuilder(markup != null ? 80 : 32);
108     builder.append("tobago-");
109     builder.append(rendererName);
110     if (sub != null) {
111       builder.append('-');
112       builder.append(sub);
113     }
114     if (markup != null) {
115       Theme theme = VariableResolverUtils.resolveClientProperties(FacesContext.getCurrentInstance()).getTheme();
116       for (String markupString : markup) {
117         if (ignoreMarkupCheck || theme.getRenderersConfig().isMarkupSupported(rendererName, markupString)) {
118           builder.append(' ');
119           builder.append("tobago-");
120           builder.append(rendererName);
121           if (sub != null) {
122             builder.append('-');
123             builder.append(sub);
124           }
125           builder.append("-markup-");
126           builder.append(markupString);
127         } else if ("none".equals(markupString)) {
128           Deprecation.LOG.warn("Markup 'none' is deprecated, please use a NULL pointer instead.");
129         } else {
130           LOG.warn("Ignoring unknown markup='" + markupString + "' for rendererName='" + rendererName + "'");
131         }
132       }
133     }
134     this.stringValue = builder.toString();
135   }
136 
137   public String getStringValue() {
138     return stringValue;
139   }
140 
141   /** @deprecated This workaround will be removed later */
142   @Deprecated
143   public static String requiredWorkaround(UIComponent component) {
144     final String rendererName = StringUtils.uncapitalize(component.getRendererType());
145     return "tobago-" + rendererName + "-markup-required";
146   }
147 
148   /**
149    * @deprecated This workaround will be removed later
150    */
151   @Deprecated
152   public static Classes createWorkaround(String rendererName, String sub, Markup explicit) {
153     return new Classes(rendererName, explicit, sub, false);
154   }
155 
156   /**
157    * @deprecated This workaround will be removed later
158    */
159   @Deprecated
160   public static Classes createWorkaround(String rendererName, Markup explicit) {
161     return new Classes(rendererName, explicit, null, false);
162   }
163 
164 }