1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.util;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.util.PortalUtil;
27  
28  import java.util.ArrayList;
29  import java.util.LinkedHashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * <a href="TemplateNode.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Alexander Chow
37   * @author Raymond Aug�
38   */
39  public class TemplateNode extends LinkedHashMap<String, Object> {
40  
41      public TemplateNode(String name, String data, String type) {
42          super();
43  
44          put("name", name);
45          put("data", data);
46          put("type", type);
47          put("options", new ArrayList<String>());
48      }
49  
50      public void appendChild(TemplateNode child) {
51          _children.put(child.getName(), child);
52          put(child.getName(), child);
53      }
54  
55      public void appendChildren(List<TemplateNode> children) {
56          for (TemplateNode child : children) {
57              appendChild(child);
58          }
59      }
60  
61      public void appendOption(String option) {
62          getOptions().add(option);
63      }
64  
65      public void appendOptions(List<String> options) {
66          getOptions().addAll(options);
67      }
68  
69      public TemplateNode getChild(String name) {
70          return _children.get(name);
71      }
72  
73      public List<TemplateNode> getChildren() {
74          return new ArrayList<TemplateNode>(_children.values());
75      }
76  
77      public String getData() {
78          return (String)get("data");
79      }
80  
81      public String getName() {
82          return (String)get("name");
83      }
84  
85      public List<String> getOptions() {
86          return (List<String>)get("options");
87      }
88  
89      public String getType() {
90          return (String)get("type");
91      }
92  
93      public String getUrl() {
94          if (getType().equals("link_to_layout")) {
95              String layoutLink = getData();
96              String layoutId = layoutLink;
97  
98              int pos = layoutId.indexOf(StringPool.AT);
99  
100             if (pos != -1) {
101                 layoutId = layoutId.substring(0, pos);
102             }
103 
104             StringBuilder sb = new StringBuilder();
105 
106             if (layoutLink.endsWith("@public")) {
107                 sb.append(PortalUtil.getPathFriendlyURLPublic());
108             }
109             else if (layoutLink.endsWith("@private-group")) {
110                 sb.append(PortalUtil.getPathFriendlyURLPrivateGroup());
111             }
112             else if (layoutLink.endsWith("@private-user")) {
113                 sb.append(PortalUtil.getPathFriendlyURLPrivateUser());
114             }
115             else {
116                 sb.append("@friendly_url_current@");
117             }
118 
119             sb.append(StringPool.SLASH);
120             sb.append("@group_id@");
121             sb.append(StringPool.SLASH);
122             sb.append(layoutId);
123 
124             return sb.toString();
125         }
126         else {
127             return StringPool.BLANK;
128         }
129     }
130 
131     private Map<String, TemplateNode> _children =
132         new LinkedHashMap<String, TemplateNode>();
133 
134 }