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 void appendSibling(TemplateNode sibling) {
70          _siblings.add(sibling);
71      }
72  
73      public TemplateNode getChild(String name) {
74          return _children.get(name);
75      }
76  
77      public List<TemplateNode> getChildren() {
78          return new ArrayList<TemplateNode>(_children.values());
79      }
80  
81      public String getData() {
82          if (getType().equals("link_to_layout")) {
83              String data = (String)get("data");
84  
85              int pos = data.indexOf(StringPool.AT);
86  
87              if (pos != -1) {
88                  data = data.substring(0, pos);
89              }
90  
91              return data;
92          }
93          else {
94              return (String)get("data");
95          }
96      }
97  
98      public String getName() {
99          return (String)get("name");
100     }
101 
102     public List<String> getOptions() {
103         return (List<String>)get("options");
104     }
105 
106     public List<TemplateNode> getSiblings() {
107         return _siblings;
108     }
109 
110     public String getType() {
111         return (String)get("type");
112     }
113 
114     public String getUrl() {
115         if (getType().equals("link_to_layout")) {
116             String layoutLink = (String)get("data");
117             String layoutId = layoutLink;
118 
119             int pos = layoutId.indexOf(StringPool.AT);
120 
121             if (pos != -1) {
122                 layoutId = layoutId.substring(0, pos);
123             }
124 
125             StringBuilder sb = new StringBuilder();
126 
127             if (layoutLink.endsWith("@public")) {
128                 sb.append(PortalUtil.getPathFriendlyURLPublic());
129             }
130             else if (layoutLink.endsWith("@private-group")) {
131                 sb.append(PortalUtil.getPathFriendlyURLPrivateGroup());
132             }
133             else if (layoutLink.endsWith("@private-user")) {
134                 sb.append(PortalUtil.getPathFriendlyURLPrivateUser());
135             }
136             else {
137                 sb.append("@friendly_url_current@");
138             }
139 
140             sb.append(StringPool.SLASH);
141             sb.append("@group_id@");
142             sb.append(StringPool.SLASH);
143             sb.append(layoutId);
144 
145             return sb.toString();
146         }
147         else {
148             return StringPool.BLANK;
149         }
150     }
151 
152     private Map<String, TemplateNode> _children =
153         new LinkedHashMap<String, TemplateNode>();
154     private List<TemplateNode> _siblings = new ArrayList<TemplateNode>();
155 
156 }