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.portal.kernel.templateparser;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.service.GroupLocalServiceUtil;
027    import com.liferay.portal.service.LayoutLocalServiceUtil;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    
031    import java.util.ArrayList;
032    import java.util.HashMap;
033    import java.util.LinkedHashMap;
034    import java.util.List;
035    import java.util.Map;
036    
037    /**
038     * @author Alexander Chow
039     * @author Raymond Aug??
040     */
041    public class TemplateNode extends LinkedHashMap<String, Object> {
042    
043            public TemplateNode(
044                    ThemeDisplay themeDisplay, String name, String data, String type,
045                    Map<String, String> attributes) {
046    
047                    _themeDisplay = themeDisplay;
048    
049                    put("attributes", attributes);
050                    put("name", name);
051                    put("data", data);
052                    put("type", type);
053                    put("options", new ArrayList<String>());
054                    put("optionsMap", new HashMap<String, String>());
055            }
056    
057            public void appendChild(TemplateNode templateNode) {
058                    _childTemplateNodes.put(templateNode.getName(), templateNode);
059    
060                    put(templateNode.getName(), templateNode);
061            }
062    
063            public void appendChildren(List<TemplateNode> templateNodes) {
064                    for (TemplateNode templateNode : templateNodes) {
065                            appendChild(templateNode);
066                    }
067            }
068    
069            public void appendOption(String option) {
070                    List<String> options = getOptions();
071    
072                    options.add(option);
073            }
074    
075            public void appendOptionMap(String value, String label) {
076                    Map<String, String> optionsMap = getOptionsMap();
077    
078                    optionsMap.put(value, label);
079            }
080    
081            public void appendOptions(List<String> options) {
082                    List<String> curOptions = getOptions();
083    
084                    curOptions.addAll(options);
085            }
086    
087            public void appendOptionsMap(Map<String, String> optionMap) {
088                    Map<String, String> optionsMap = getOptionsMap();
089    
090                    optionsMap.putAll(optionMap);
091            }
092    
093            public void appendSibling(TemplateNode templateNode) {
094                    _siblingTemplateNodes.add(templateNode);
095            }
096    
097            public String getAttribute(String name) {
098                    Map<String, String> attributes = getAttributes();
099    
100                    if (attributes == null) {
101                            return StringPool.BLANK;
102                    }
103    
104                    return attributes.get(name);
105            }
106    
107            public Map<String, String> getAttributes() {
108                    return (Map<String, String>)get("attributes");
109            }
110    
111            public TemplateNode getChild(String name) {
112                    return _childTemplateNodes.get(name);
113            }
114    
115            public List<TemplateNode> getChildren() {
116                    return new ArrayList<>(_childTemplateNodes.values());
117            }
118    
119            public String getData() {
120                    String type = getType();
121    
122                    if (type.equals("link_to_layout")) {
123                            String data = (String)get("data");
124    
125                            int pos = data.indexOf(CharPool.AT);
126    
127                            if (pos != -1) {
128                                    data = data.substring(0, pos);
129                            }
130    
131                            return data;
132                    }
133                    else {
134                            return (String)get("data");
135                    }
136            }
137    
138            public String getFriendlyUrl() {
139                    if (_themeDisplay == null) {
140                            return getUrl();
141                    }
142    
143                    String type = getType();
144    
145                    if (!type.equals("link_to_layout")) {
146                            return StringPool.BLANK;
147                    }
148    
149                    String layoutType = getLayoutType();
150    
151                    if (Validator.isNull(layoutType)) {
152                            return StringPool.BLANK;
153                    }
154    
155                    long groupId = getLayoutGroupId();
156    
157                    if (groupId == 0) {
158                            groupId = _themeDisplay.getScopeGroupId();
159                    }
160    
161                    boolean privateLayout = layoutType.startsWith("private");
162    
163                    try {
164                            Layout layout = LayoutLocalServiceUtil.getLayout(
165                                    groupId, privateLayout, getLayoutId());
166    
167                            return PortalUtil.getLayoutFriendlyURL(layout, _themeDisplay);
168                    }
169                    catch (Exception e) {
170                            if (_log.isDebugEnabled()) {
171                                    _log.debug(
172                                            "Error finding friendly URL on page " +
173                                                    _themeDisplay.getURLCurrent(),
174                                            e);
175                            }
176    
177                            return getUrl();
178                    }
179            }
180    
181            public String getName() {
182                    return (String)get("name");
183            }
184    
185            public List<String> getOptions() {
186                    return (List<String>)get("options");
187            }
188    
189            public Map<String, String> getOptionsMap() {
190                    return (Map<String, String>)get("optionsMap");
191            }
192    
193            public List<TemplateNode> getSiblings() {
194                    return _siblingTemplateNodes;
195            }
196    
197            public String getType() {
198                    return (String)get("type");
199            }
200    
201            public String getUrl() {
202                    String type = getType();
203    
204                    if (!type.equals("link_to_layout")) {
205                            return StringPool.BLANK;
206                    }
207    
208                    StringBundler sb = new StringBundler(5);
209    
210                    String layoutType = getLayoutType();
211    
212                    if (Validator.isNull(layoutType)) {
213                            return StringPool.BLANK;
214                    }
215    
216                    if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_GROUP)) {
217                            sb.append(PortalUtil.getPathFriendlyURLPrivateGroup());
218                    }
219                    else if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_USER)) {
220                            sb.append(PortalUtil.getPathFriendlyURLPrivateUser());
221                    }
222                    else if (layoutType.equals(_LAYOUT_TYPE_PUBLIC)) {
223                            sb.append(PortalUtil.getPathFriendlyURLPublic());
224                    }
225                    else {
226                            sb.append("@friendly_url_current@");
227                    }
228    
229                    sb.append(StringPool.SLASH);
230    
231                    try {
232                            Group group = GroupLocalServiceUtil.getGroup(getLayoutGroupId());
233    
234                            String name = group.getFriendlyURL();
235    
236                            name = name.substring(1);
237    
238                            sb.append(name);
239                    }
240                    catch (Exception e) {
241                            sb.append("@group_id@");
242                    }
243    
244                    sb.append(StringPool.SLASH);
245                    sb.append(getLayoutId());
246    
247                    return sb.toString();
248            }
249    
250            protected long getLayoutGroupId() {
251                    String data = (String)get("data");
252    
253                    int pos = data.lastIndexOf(CharPool.AT);
254    
255                    if (pos != -1) {
256                            data = data.substring(pos + 1);
257                    }
258    
259                    return GetterUtil.getLong(data);
260            }
261    
262            protected long getLayoutId() {
263                    String data = (String)get("data");
264    
265                    int pos = data.indexOf(CharPool.AT);
266    
267                    if (pos != -1) {
268                            data = data.substring(0, pos);
269                    }
270    
271                    return GetterUtil.getLong(data);
272            }
273    
274            protected String getLayoutType() {
275                    String data = (String)get("data");
276    
277                    int x = data.indexOf(CharPool.AT);
278                    int y = data.lastIndexOf(CharPool.AT);
279    
280                    if ((x != -1) && (y != -1)) {
281                            if (x == y) {
282                                    data = data.substring(x + 1);
283                            }
284                            else {
285                                    data = data.substring(x + 1, y);
286                            }
287                    }
288    
289                    return data;
290            }
291    
292            private static final String _LAYOUT_TYPE_PRIVATE_GROUP = "private-group";
293    
294            private static final String _LAYOUT_TYPE_PRIVATE_USER = "private-user";
295    
296            private static final String _LAYOUT_TYPE_PUBLIC = "public";
297    
298            private static final Log _log = LogFactoryUtil.getLog(TemplateNode.class);
299    
300            private final Map<String, TemplateNode> _childTemplateNodes =
301                    new LinkedHashMap<>();
302            private final List<TemplateNode> _siblingTemplateNodes = new ArrayList<>();
303            private ThemeDisplay _themeDisplay;
304    
305    }