1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.util;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.util.PortalUtil;
20  
21  import java.util.ArrayList;
22  import java.util.LinkedHashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  /**
27   * <a href="TemplateNode.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Alexander Chow
30   * @author Raymond Augé
31   */
32  public class TemplateNode extends LinkedHashMap<String, Object> {
33  
34      public TemplateNode(String name, String data, String type) {
35          super();
36  
37          put("name", name);
38          put("data", data);
39          put("type", type);
40          put("options", new ArrayList<String>());
41      }
42  
43      public void appendChild(TemplateNode child) {
44          _children.put(child.getName(), child);
45          put(child.getName(), child);
46      }
47  
48      public void appendChildren(List<TemplateNode> children) {
49          for (TemplateNode child : children) {
50              appendChild(child);
51          }
52      }
53  
54      public void appendOption(String option) {
55          getOptions().add(option);
56      }
57  
58      public void appendOptions(List<String> options) {
59          getOptions().addAll(options);
60      }
61  
62      public void appendSibling(TemplateNode sibling) {
63          _siblings.add(sibling);
64      }
65  
66      public TemplateNode getChild(String name) {
67          return _children.get(name);
68      }
69  
70      public List<TemplateNode> getChildren() {
71          return new ArrayList<TemplateNode>(_children.values());
72      }
73  
74      public String getData() {
75          if (getType().equals("link_to_layout")) {
76              String data = (String)get("data");
77  
78              int pos = data.indexOf(StringPool.AT);
79  
80              if (pos != -1) {
81                  data = data.substring(0, pos);
82              }
83  
84              return data;
85          }
86          else {
87              return (String)get("data");
88          }
89      }
90  
91      public String getName() {
92          return (String)get("name");
93      }
94  
95      public List<String> getOptions() {
96          return (List<String>)get("options");
97      }
98  
99      public List<TemplateNode> getSiblings() {
100         return _siblings;
101     }
102 
103     public String getType() {
104         return (String)get("type");
105     }
106 
107     public String getUrl() {
108         if (getType().equals("link_to_layout")) {
109             String layoutLink = (String)get("data");
110             String layoutId = layoutLink;
111 
112             int pos = layoutId.indexOf(StringPool.AT);
113 
114             if (pos != -1) {
115                 layoutId = layoutId.substring(0, pos);
116             }
117 
118             StringBundler sb = new StringBundler(5);
119 
120             if (layoutLink.endsWith("@public")) {
121                 sb.append(PortalUtil.getPathFriendlyURLPublic());
122             }
123             else if (layoutLink.endsWith("@private-group")) {
124                 sb.append(PortalUtil.getPathFriendlyURLPrivateGroup());
125             }
126             else if (layoutLink.endsWith("@private-user")) {
127                 sb.append(PortalUtil.getPathFriendlyURLPrivateUser());
128             }
129             else {
130                 sb.append("@friendly_url_current@");
131             }
132 
133             sb.append(StringPool.SLASH);
134             sb.append("@group_id@");
135             sb.append(StringPool.SLASH);
136             sb.append(layoutId);
137 
138             return sb.toString();
139         }
140         else {
141             return StringPool.BLANK;
142         }
143     }
144 
145     private Map<String, TemplateNode> _children =
146         new LinkedHashMap<String, TemplateNode>();
147     private List<TemplateNode> _siblings = new ArrayList<TemplateNode>();
148 
149 }