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                    return HtmlUtil.buildData(data);
142            }
143    
144            public static String buildLabel(
145                    String baseType, boolean inlineField, boolean showForLabel,
146                    String forLabel) {
147    
148                    StringBundler sb = new StringBundler(7);
149    
150                    if (baseType.equals("boolean")) {
151                            baseType = "checkbox";
152                    }
153    
154                    if (baseType.equals("checkbox") || baseType.equals("radio")) {
155                            if (inlineField) {
156                                    sb.append("class=\"");
157                                    sb.append(baseType);
158                                    sb.append("-inline");
159                                    sb.append("\" ");
160                            }
161                    }
162                    else {
163                            sb.append("class=\"control-label\" ");
164                    }
165    
166                    if (showForLabel) {
167                            sb.append("for=\"");
168                            sb.append(HtmlUtil.escapeAttribute(forLabel));
169                            sb.append("\"");
170                    }
171    
172                    return sb.toString();
173            }
174    
175            /**
176             * @deprecated As of 6.2.0, replaced by {@link #buildLabel(String, boolean,
177             *             boolean, String)}
178             */
179            @Deprecated
180            public static String buildLabel(
181                    String inlineLabel, boolean showForLabel, String forLabel,
182                    boolean choiceField) {
183    
184                    return buildLabel(StringPool.BLANK, false, showForLabel, forLabel);
185            }
186    
187            public static Object getAttribute(
188                    HttpServletRequest request, String namespace, String key) {
189    
190                    Map<String, Object> dynamicAttributes =
191                            (Map<String, Object>)request.getAttribute(
192                                    namespace.concat("dynamicAttributes"));
193                    Map<String, Object> scopedAttributes =
194                            (Map<String, Object>)request.getAttribute(
195                                    namespace.concat("scopedAttributes"));
196    
197                    if (((dynamicAttributes != null) &&
198                             dynamicAttributes.containsKey(key)) ||
199                            ((scopedAttributes != null) && scopedAttributes.containsKey(key))) {
200    
201                            return request.getAttribute(namespace.concat(key));
202                    }
203    
204                    return null;
205            }
206    
207            public static String getNamespace(HttpServletRequest request) {
208                    return GetterUtil.getString(
209                            request.getAttribute("aui:form:portletNamespace"));
210            }
211    
212            public static String getNamespace(
213                    PortletRequest portletRequest, PortletResponse portletResponse) {
214    
215                    String namespace = StringPool.BLANK;
216    
217                    if (portletRequest == null) {
218                            return namespace;
219                    }
220    
221                    boolean auiFormUseNamespace = GetterUtil.getBoolean(
222                            (String)portletRequest.getAttribute("aui:form:useNamespace"), true);
223    
224                    if ((portletResponse != null) && auiFormUseNamespace) {
225                            namespace = GetterUtil.getString(
226                                    portletRequest.getAttribute("aui:form:portletNamespace"),
227                                    portletResponse.getNamespace());
228                    }
229    
230                    return namespace;
231            }
232    
233            public static boolean isOpensNewWindow(String target) {
234                    if ((target != null) &&
235                            (target.equals("_blank") || target.equals("_new"))) {
236    
237                            return true;
238                    }
239    
240                    return false;
241            }
242    
243            public static String normalizeId(String name) {
244                    Matcher matcher = _friendlyURLPattern.matcher(name);
245    
246                    return matcher.replaceAll(StringPool.DASH);
247            }
248    
249            private static final Pattern _friendlyURLPattern = Pattern.compile(
250                    "[^A-Za-z0-9/_-]");
251    
252    }