1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.wiki.util;
24  
25  import com.efsol.friki.PageRepository;
26  
27  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.util.Http;
31  
32  import java.io.IOException;
33  import java.io.StringReader;
34  import java.io.StringWriter;
35  
36  import java.util.HashMap;
37  import java.util.Iterator;
38  import java.util.Map;
39  import java.util.StringTokenizer;
40  
41  import javax.portlet.PortletURL;
42  
43  import org.stringtree.factory.memory.MapStringRepository;
44  
45  /**
46   * <a href="WikiUtil.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class WikiUtil {
52  
53      public static String convert(NodeFilter filter, String content)
54          throws IOException {
55  
56          return _instance._convert(filter, content);
57      }
58  
59      public static NodeFilter getFilter(long nodeId) {
60          return _instance._getFilter(null, nodeId);
61      }
62  
63      public static NodeFilter getFilter(PortletURL portletURL, long nodeId) {
64          return _instance._getFilter(portletURL, nodeId);
65      }
66  
67      private WikiUtil() {
68          try {
69              ClassLoader classLoader = getClass().getClassLoader();
70  
71              _spec = Http.URLtoString(classLoader.getResource("wiki.transform"));
72              _remoteNames = _buildRemoteNamesMap(Http.URLtoString(
73                  classLoader.getResource("intermap.txt")));
74          }
75          catch (IOException ioe) {
76              ioe.printStackTrace();
77          }
78      }
79  
80      private Map _buildRemoteNamesMap(String names) {
81          Map remoteNames = new HashMap();
82  
83          StringTokenizer st = new StringTokenizer(names, "\n");
84  
85          while (st.hasMoreTokens()) {
86              String line = st.nextToken().trim();
87  
88              int sep = line.indexOf(StringPool.SPACE);
89  
90              if (sep > 0) {
91                  String name = line.substring(0, sep);
92                  String url = line.substring(sep + 1);
93  
94                  remoteNames.put(name, url);
95              }
96          }
97  
98          return remoteNames;
99      }
100 
101     private String _convert(NodeFilter filter, String content)
102         throws IOException {
103 
104         if (content == null) {
105             return StringPool.BLANK;
106         }
107 
108         StringWriter out = new StringWriter();
109 
110         filter.filter(new StringReader(content), out);
111 
112         String newContent = out.toString();
113 
114         String portletURLToString = StringPool.BLANK;
115 
116         LiferayPortletURL portletURL =
117             (LiferayPortletURL)filter.getPortletURL();
118 
119         if (portletURL != null) {
120             portletURL.setParameter(
121                 "nodeId", String.valueOf(filter.getNodeId()));
122 
123             Iterator itr = filter.getTitles().keySet().iterator();
124 
125             while (itr.hasNext()) {
126                 String title = (String)itr.next();
127 
128                 portletURL.setParameter("title", title, false);
129 
130                 portletURLToString = portletURL.toString();
131 
132                 newContent = StringUtil.replace(
133                     newContent,
134                     "[$BEGIN_PAGE_TITLE$]" + title + "[$END_PAGE_TITLE$]",
135                     portletURLToString);
136             }
137         }
138 
139         return newContent;
140     }
141 
142     private NodeFilter _getFilter(PortletURL portletURL, long nodeId) {
143         MapStringRepository context = new MapStringRepository();
144         NodeRepository nodeRepository = new NodeRepository(nodeId);
145         PageRepository pageRepository = new PageRepository(nodeRepository);
146 
147         NodeFilter filter = new NodeFilter(
148             context, pageRepository, _remoteNames, _spec, nodeRepository,
149             portletURL, nodeId);
150 
151         return filter;
152     }
153 
154     private static WikiUtil _instance = new WikiUtil();
155 
156     private String _spec;
157     private Map _remoteNames;
158 
159 }