001    /**
002     * Copyright (c) 2000-2013 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.alloy.util.ReservedAttributeUtil;
018    import com.liferay.portal.kernel.util.SetUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.taglib.aui.base.BaseComponentTag;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    import java.util.Set;
025    
026    import javax.servlet.http.HttpServletRequest;
027    
028    import org.apache.commons.lang.StringUtils;
029    
030    /**
031     * @author Eduardo Lundgren
032     * @author Bruno Basto
033     */
034    public class ComponentTag extends BaseComponentTag {
035    
036            protected boolean isEventAttribute(String key) {
037                    if (StringUtil.startsWith(key, "after") ||
038                            StringUtil.startsWith(key, "on")) {
039    
040                            return true;
041                    }
042    
043                    return false;
044            }
045    
046            protected boolean isValidAttribute(String key) {
047                    String excludeAttributes = getExcludeAttributes();
048    
049                    if (excludeAttributes == null) {
050                            return true;
051                    }
052    
053                    Set<String> excludeAttributesSet = SetUtil.fromArray(
054                            StringUtil.split(excludeAttributes));
055    
056                    if (key.equals("dynamicAttributes") ||
057                            excludeAttributesSet.contains(key)) {
058    
059                            return false;
060                    }
061    
062                    return true;
063            }
064    
065            protected void proccessAttributes(
066                    Map<String, Object> options, Map<String, Object> jsonifiedOptions) {
067    
068                    Map<String, String> afterEventOptions = new HashMap<String, String>();
069                    Map<String, String> onEventOptions = new HashMap<String, String>();
070    
071                    for (String key : options.keySet()) {
072                            if (!isValidAttribute(key)) {
073                                    continue;
074                            }
075    
076                            Object value = options.get(key);
077    
078                            String originalKey = ReservedAttributeUtil.getOriginalName(
079                                    getName(), key);
080    
081                            if (value instanceof Map) {
082                                    Map<String, Object> childOptions =
083                                            new HashMap<String, Object>();
084    
085                                    proccessAttributes((Map<String, Object>)value, childOptions);
086    
087                                    jsonifiedOptions.put(originalKey, childOptions);
088    
089                                    continue;
090                            }
091    
092                            if (isEventAttribute(key)) {
093                                    processEventAttribute(
094                                            key, String.valueOf(value), afterEventOptions,
095                                            onEventOptions);
096                            }
097                            else {
098                                    jsonifiedOptions.put(originalKey, value);
099                            }
100                    }
101    
102                    if (!afterEventOptions.isEmpty()) {
103                            jsonifiedOptions.put("after", afterEventOptions);
104                    }
105    
106                    if (!onEventOptions.isEmpty()) {
107                            jsonifiedOptions.put("on", onEventOptions);
108                    }
109            }
110    
111            protected void processEventAttribute(
112                            String key, String value, Map<String, String> afterEventOptions,
113                            Map<String, String> onEventsOptions) {
114    
115                    if (key.startsWith("after")) {
116                            String eventName = StringUtils.uncapitalize(key.substring(5));
117    
118                            afterEventOptions.put(eventName, value);
119                    }
120                    else {
121                            String eventName = StringUtils.uncapitalize(key.substring(2));
122    
123                            onEventsOptions.put(eventName, value);
124                    }
125            }
126    
127            @Override
128            protected void setAttributes(HttpServletRequest request) {
129                    Map<String, Object> options = getOptions();
130    
131                    Map<String, Object> jsonifiedOptions = new HashMap<String, Object>();
132    
133                    proccessAttributes(options, jsonifiedOptions);
134    
135                    super.setAttributes(request);
136    
137                    setNamespacedAttribute(request, "jsonifiedOptions", jsonifiedOptions);
138                    setNamespacedAttribute(request, "options", options);
139            }
140    
141    }