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.GetterUtil;
018    import com.liferay.portal.kernel.util.HtmlUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.util.Map;
024    import java.util.regex.Matcher;
025    import java.util.regex.Pattern;
026    
027    import javax.portlet.PortletRequest;
028    import javax.portlet.PortletResponse;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class AUIUtil {
036    
037            /**
038             * @deprecated As of 6.2.0
039             */
040            @Deprecated
041            public static final String BUTTON_INPUT_PREFIX = "btn-input";
042    
043            public static final String BUTTON_PREFIX = "btn";
044    
045            public static final String FIELD_PREFIX = "field";
046    
047            /**
048             * @deprecated As of 6.2.0
049             */
050            @Deprecated
051            public static final String INPUT_PREFIX = "field-input";
052    
053            /**
054             * @deprecated As of 6.2.0
055             */
056            @Deprecated
057            public static final String LABEL_CHOICE_PREFIX = "choice-label";
058    
059            /**
060             * @deprecated As of 6.2.0
061             */
062            @Deprecated
063            public static final String LABEL_FIELD_PREFIX = "field-label";
064    
065            public static String buildControlGroupCss(
066                    boolean inlineField, String inlineLabel, String wrapperCssClass,
067                    String baseType) {
068    
069                    StringBundler sb = new StringBundler(9);
070    
071                    sb.append("form-group");
072    
073                    if (inlineField) {
074                            sb.append(" form-group-inline");
075                    }
076    
077                    if (Validator.isNotNull(inlineLabel)) {
078                            sb.append(" form-inline");
079                    }
080    
081                    if (Validator.isNotNull(wrapperCssClass)) {
082                            sb.append(StringPool.SPACE);
083                            sb.append(wrapperCssClass);
084                    }
085    
086                    if (Validator.isNotNull(baseType)) {
087                            sb.append(StringPool.SPACE);
088                            sb.append("input-");
089                            sb.append(baseType);
090                            sb.append("-wrapper");
091                    }
092    
093                    return sb.toString();
094            }
095    
096            public static String buildCss(
097                    String prefix, boolean disabled, boolean first, boolean last,
098                    String cssClass) {
099    
100                    StringBundler sb = new StringBundler(8);
101    
102                    sb.append(prefix);
103    
104                    if (disabled) {
105                            sb.append(StringPool.SPACE);
106                            sb.append("disabled");
107                    }
108    
109                    if (first) {
110                            sb.append(StringPool.SPACE);
111                            sb.append(prefix);
112                            sb.append("-first");
113                    }
114                    else if (last) {
115                            sb.append(StringPool.SPACE);
116                            sb.append(prefix);
117                            sb.append("-last");
118                    }
119    
120                    if (Validator.isNotNull(cssClass)) {
121                            sb.append(StringPool.SPACE);
122                            sb.append(cssClass);
123                    }
124    
125                    return sb.toString();
126            }
127    
128            /**
129             * @deprecated As of 6.2.0, replaced by {@link #buildCss(String, boolean,
130             *             boolean, boolean, String)}
131             */
132            @Deprecated
133            public static String buildCss(
134                    String prefix, String baseTypeCss, boolean disabled, boolean first,
135                    boolean last, String cssClass) {
136    
137                    return buildCss(prefix, disabled, first, last, cssClass);
138            }
139    
140            public static String buildData(Map<String, Object> data) {
141                    if ((data == null) || data.isEmpty()) {
142                            return StringPool.BLANK;
143                    }
144    
145                    StringBundler sb = new StringBundler(data.size() * 5);
146    
147                    for (Map.Entry<String, Object> entry : data.entrySet()) {
148                            String dataKey = entry.getKey();
149                            String dataValue = String.valueOf(entry.getValue());
150    
151                            sb.append("data-");
152                            sb.append(dataKey);
153                            sb.append("=\"");
154                            sb.append(HtmlUtil.escapeAttribute(dataValue));
155                            sb.append("\" ");
156                    }
157    
158                    return sb.toString();
159            }
160    
161            public static String buildLabel(
162                    String baseType, boolean inlineField, boolean showForLabel,
163                    String forLabel) {
164    
165                    StringBundler sb = new StringBundler(7);
166    
167                    if (baseType.equals("boolean")) {
168                            baseType = "checkbox";
169                    }
170    
171                    if (baseType.equals("checkbox") || baseType.equals("radio")) {
172                            if (inlineField) {
173                                    sb.append("class=\"");
174                                    sb.append(baseType);
175                                    sb.append("-inline");
176                                    sb.append("\" ");
177                            }
178                    }
179                    else {
180                            sb.append("class=\"control-label\" ");
181                    }
182    
183                    if (showForLabel) {
184                            sb.append("for=\"");
185                            sb.append(HtmlUtil.escapeAttribute(forLabel));
186                            sb.append("\"");
187                    }
188    
189                    return sb.toString();
190            }
191    
192            /**
193             * @deprecated As of 6.2.0, replaced by {@link #buildLabel(String, boolean,
194             *             boolean, String)}
195             */
196            @Deprecated
197            public static String buildLabel(
198                    String inlineLabel, boolean showForLabel, String forLabel,
199                    boolean choiceField) {
200    
201                    return buildLabel(StringPool.BLANK, false, showForLabel, forLabel);
202            }
203    
204            public static Object getAttribute(
205                    HttpServletRequest request, String namespace, String key) {
206    
207                    Map<String, Object> dynamicAttributes =
208                            (Map<String, Object>)request.getAttribute(
209                                    namespace.concat("dynamicAttributes"));
210                    Map<String, Object> scopedAttributes =
211                            (Map<String, Object>)request.getAttribute(
212                                    namespace.concat("scopedAttributes"));
213    
214                    if (((dynamicAttributes != null) &&
215                             dynamicAttributes.containsKey(key)) ||
216                            ((scopedAttributes != null) && scopedAttributes.containsKey(key))) {
217    
218                            return request.getAttribute(namespace.concat(key));
219                    }
220    
221                    return null;
222            }
223    
224            public static String getNamespace(
225                    PortletRequest portletRequest, PortletResponse portletResponse) {
226    
227                    String namespace = StringPool.BLANK;
228    
229                    if (portletRequest == null) {
230                            return namespace;
231                    }
232    
233                    boolean auiFormUseNamespace = GetterUtil.getBoolean(
234                            (String)portletRequest.getAttribute("aui:form:useNamespace"), true);
235    
236                    if ((portletResponse != null) && auiFormUseNamespace) {
237                            namespace = GetterUtil.getString(
238                                    portletRequest.getAttribute("aui:form:portletNamespace"),
239                                    portletResponse.getNamespace());
240                    }
241    
242                    return namespace;
243            }
244    
245            public static boolean isOpensNewWindow(String target) {
246                    if ((target != null) &&
247                            (target.equals("_blank") || target.equals("_new"))) {
248    
249                            return true;
250                    }
251    
252                    return false;
253            }
254    
255            public static String normalizeId(String name) {
256                    Matcher matcher = _friendlyURLPattern.matcher(name);
257    
258                    return matcher.replaceAll(StringPool.DASH);
259            }
260    
261            private static final Pattern _friendlyURLPattern = Pattern.compile(
262                    "[^A-Za-z0-9/_-]");
263    
264    }