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