001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.aui;
016    
017    import com.liferay.portal.kernel.util.HtmlUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.util.Map;
023    import java.util.regex.Matcher;
024    import java.util.regex.Pattern;
025    
026    import javax.servlet.http.HttpServletRequest;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class AUIUtil {
032    
033            /**
034             * @deprecated As of 6.2.0
035             */
036            @Deprecated
037            public static final String BUTTON_INPUT_PREFIX = "btn-input";
038    
039            public static final String BUTTON_PREFIX = "btn";
040    
041            public static final String FIELD_PREFIX = "field";
042    
043            /**
044             * @deprecated As of 6.2.0
045             */
046            @Deprecated
047            public static final String INPUT_PREFIX = "field-input";
048    
049            /**
050             * @deprecated As of 6.2.0
051             */
052            @Deprecated
053            public static final String LABEL_CHOICE_PREFIX = "choice-label";
054    
055            /**
056             * @deprecated As of 6.2.0
057             */
058            @Deprecated
059            public static final String LABEL_FIELD_PREFIX = "field-label";
060    
061            public static String buildControlGroupCss(
062                    boolean inlineField, String inlineLabel, String wrapperCssClass,
063                    String baseType) {
064    
065                    StringBundler sb = new StringBundler(9);
066    
067                    sb.append("form-group");
068    
069                    if (inlineField) {
070                            sb.append(" form-group-inline");
071                    }
072    
073                    if (Validator.isNotNull(inlineLabel)) {
074                            sb.append(" form-inline");
075                    }
076    
077                    if (Validator.isNotNull(wrapperCssClass)) {
078                            sb.append(StringPool.SPACE);
079                            sb.append(wrapperCssClass);
080                    }
081    
082                    if (Validator.isNotNull(baseType)) {
083                            sb.append(StringPool.SPACE);
084                            sb.append("input-");
085                            sb.append(baseType);
086                            sb.append("-wrapper");
087                    }
088    
089                    return sb.toString();
090            }
091    
092            public static String buildCss(
093                    String prefix, boolean disabled, boolean first, boolean last,
094                    String cssClass) {
095    
096                    StringBundler sb = new StringBundler(8);
097    
098                    sb.append(prefix);
099    
100                    if (disabled) {
101                            sb.append(StringPool.SPACE);
102                            sb.append("disabled");
103                    }
104    
105                    if (first) {
106                            sb.append(StringPool.SPACE);
107                            sb.append(prefix);
108                            sb.append("-first");
109                    }
110                    else if (last) {
111                            sb.append(StringPool.SPACE);
112                            sb.append(prefix);
113                            sb.append("-last");
114                    }
115    
116                    if (Validator.isNotNull(cssClass)) {
117                            sb.append(StringPool.SPACE);
118                            sb.append(cssClass);
119                    }
120    
121                    return sb.toString();
122            }
123    
124            /**
125             * @deprecated As of 6.2.0, replaced by {@link #buildCss(String, boolean,
126             *             boolean, boolean, String)}
127             */
128            @Deprecated
129            public static String buildCss(
130                    String prefix, String baseTypeCss, boolean disabled, boolean first,
131                    boolean last, String cssClass) {
132    
133                    return buildCss(prefix, disabled, first, last, cssClass);
134            }
135    
136            public static String buildData(Map<String, Object> data) {
137                    if ((data == null) || data.isEmpty()) {
138                            return StringPool.BLANK;
139                    }
140    
141                    StringBundler sb = new StringBundler(data.size() * 5);
142    
143                    for (Map.Entry<String, Object> entry : data.entrySet()) {
144                            String dataKey = entry.getKey();
145                            String dataValue = String.valueOf(entry.getValue());
146    
147                            sb.append("data-");
148                            sb.append(dataKey);
149                            sb.append("=\"");
150                            sb.append(HtmlUtil.escapeAttribute(dataValue));
151                            sb.append("\" ");
152                    }
153    
154                    return sb.toString();
155            }
156    
157            public static String buildLabel(
158                    String baseType, boolean inlineField, boolean showForLabel,
159                    String forLabel) {
160    
161                    StringBundler sb = new StringBundler(7);
162    
163                    if (baseType.equals("boolean")) {
164                            baseType = "checkbox";
165                    }
166    
167                    if (baseType.equals("checkbox") || baseType.equals("radio")) {
168                            if (inlineField) {
169                                    sb.append("class=\"");
170                                    sb.append(baseType);
171                                    sb.append("-inline");
172                                    sb.append("\" ");
173                            }
174                    }
175                    else {
176                            sb.append("class=\"control-label\" ");
177                    }
178    
179                    if (showForLabel) {
180                            sb.append("for=\"");
181                            sb.append(HtmlUtil.escapeAttribute(forLabel));
182                            sb.append("\"");
183                    }
184    
185                    return sb.toString();
186            }
187    
188            /**
189             * @deprecated As of 6.2.0, replaced by {@link #buildLabel(String, boolean,
190             *             boolean, String)}
191             */
192            @Deprecated
193            public static String buildLabel(
194                    String inlineLabel, boolean showForLabel, String forLabel,
195                    boolean choiceField) {
196    
197                    return buildLabel(StringPool.BLANK, false, showForLabel, forLabel);
198            }
199    
200            public static Object getAttribute(
201                    HttpServletRequest request, String namespace, String key) {
202    
203                    Map<String, Object> dynamicAttributes =
204                            (Map<String, Object>)request.getAttribute(
205                                    namespace.concat("dynamicAttributes"));
206                    Map<String, Object> scopedAttributes =
207                            (Map<String, Object>)request.getAttribute(
208                                    namespace.concat("scopedAttributes"));
209    
210                    if (((dynamicAttributes != null) &&
211                             dynamicAttributes.containsKey(key)) ||
212                            ((scopedAttributes != null) &&
213                             scopedAttributes.containsKey(key))) {
214    
215                            return request.getAttribute(namespace.concat(key));
216                    }
217    
218                    return null;
219            }
220    
221            public static boolean isOpensNewWindow(String target) {
222                    if ((target != null) &&
223                            (target.equals("_blank") || target.equals("_new"))) {
224    
225                            return true;
226                    }
227    
228                    return false;
229            }
230    
231            public static String normalizeId(String name) {
232                    Matcher matcher = _friendlyURLPattern.matcher(name);
233    
234                    return matcher.replaceAll(StringPool.DASH);
235            }
236    
237            private static final Pattern _friendlyURLPattern = Pattern.compile(
238                    "[^A-Za-z0-9/_-]");
239    
240    }