1   /**
2    * Copyright (c) 2000-2009 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.engines.friki;
24  
25  import com.efsol.friki.PageRepository;
26  
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portlet.wiki.PageContentException;
29  import com.liferay.portlet.wiki.engines.WikiEngine;
30  import com.liferay.portlet.wiki.model.WikiPage;
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.Map;
38  import java.util.StringTokenizer;
39  
40  import javax.portlet.PortletURL;
41  
42  import org.stringtree.factory.memory.MapStringRepository;
43  
44  /**
45   * <a href="FrikiEngine.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   */
49  public class FrikiEngine implements WikiEngine {
50  
51      public FrikiEngine() {
52      }
53  
54      public String convert(WikiPage page, PortletURL portletURL)
55          throws PageContentException {
56  
57          try {
58              return convert(
59                  getFilter(portletURL, page.getNodeId()), page.getContent());
60          }
61          catch (IOException ioe) {
62              throw new PageContentException(ioe);
63          }
64      }
65  
66      public Map<String, Boolean> getOutgoingLinks(WikiPage page)
67          throws PageContentException {
68  
69          NodeFilter filter = getFilter(page.getNodeId());
70  
71          try {
72              convert(filter, page.getContent());
73  
74              return filter.getTitles();
75          }
76          catch (IOException ioe) {
77              throw new PageContentException(ioe);
78          }
79      }
80  
81      public void setInterWikiConfiguration(String interWikiConfiguration) {
82          _remoteNames = buildRemoteNamesMap(interWikiConfiguration);
83      }
84  
85      public void setMainConfiguration(String mainConfiguration) {
86          _mainConfiguration = mainConfiguration;
87      }
88  
89      public boolean validate(long nodeId, String newContent) {
90          try {
91              NodeFilter filter = getFilter(nodeId);
92  
93              convert(filter, newContent);
94  
95              return true;
96          }
97          catch (Exception e) {
98              return false;
99          }
100     }
101 
102     protected Map<String, String> buildRemoteNamesMap(String names) {
103         Map<String, String> remoteNames = new HashMap<String, String>();
104 
105         StringTokenizer st = new StringTokenizer(names, "\n");
106 
107         while (st.hasMoreTokens()) {
108             String line = st.nextToken().trim();
109 
110             int sep = line.indexOf(StringPool.SPACE);
111 
112             if (sep > 0) {
113                 String name = line.substring(0, sep);
114                 String url = line.substring(sep + 1);
115 
116                 remoteNames.put(name, url);
117             }
118         }
119 
120         return remoteNames;
121     }
122 
123     protected String convert(NodeFilter filter, String content)
124         throws IOException {
125 
126         if (content == null) {
127             return StringPool.BLANK;
128         }
129 
130         StringWriter writer = new StringWriter();
131 
132         filter.filter(new StringReader(content), writer);
133 
134         return writer.toString();
135     }
136 
137     protected NodeFilter getFilter(long nodeId) {
138         return getFilter(null, nodeId);
139     }
140 
141     protected NodeFilter getFilter(PortletURL portletURL, long nodeId) {
142         MapStringRepository context = new MapStringRepository();
143         NodeRepository nodeRepository = new NodeRepository(nodeId);
144         PageRepository pageRepository = new PageRepository(nodeRepository);
145 
146         NodeFilter filter = new NodeFilter(
147             context, pageRepository, _remoteNames, _mainConfiguration,
148             nodeRepository, portletURL, nodeId);
149 
150         return filter;
151     }
152 
153     private String _mainConfiguration;
154     private Map<String, String> _remoteNames;
155 
156 }