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   */
50  public class FrikiEngine implements WikiEngine {
51  
52      public FrikiEngine() {
53      }
54  
55      public String convert(WikiPage page, PortletURL portletURL)
56          throws PageContentException {
57  
58          try {
59              return convert(
60                  getFilter(portletURL, page.getNodeId()), page.getContent());
61          }
62          catch (IOException ioe) {
63              throw new PageContentException(ioe);
64          }
65      }
66  
67      public Map<String, Boolean> getOutgoingLinks(WikiPage page)
68          throws PageContentException {
69  
70          NodeFilter filter = getFilter(page.getNodeId());
71  
72          try {
73              convert(filter, page.getContent());
74  
75              return filter.getTitles();
76          }
77          catch (IOException ioe) {
78              throw new PageContentException(ioe);
79          }
80      }
81  
82      public void setInterWikiConfiguration(String interWikiConfiguration) {
83          _remoteNames = buildRemoteNamesMap(interWikiConfiguration);
84      }
85  
86      public void setMainConfiguration(String mainConfiguration) {
87          _mainConfiguration = mainConfiguration;
88      }
89  
90      public boolean validate(long nodeId, String newContent) {
91          try {
92              NodeFilter filter = getFilter(nodeId);
93  
94              convert(filter, newContent);
95  
96              return true;
97          }
98          catch (Exception e) {
99              return false;
100         }
101     }
102 
103     protected Map<String, String> buildRemoteNamesMap(String names) {
104         Map<String, String> remoteNames = new HashMap<String, String>();
105 
106         StringTokenizer st = new StringTokenizer(names, "\n");
107 
108         while (st.hasMoreTokens()) {
109             String line = st.nextToken().trim();
110 
111             int sep = line.indexOf(StringPool.SPACE);
112 
113             if (sep > 0) {
114                 String name = line.substring(0, sep);
115                 String url = line.substring(sep + 1);
116 
117                 remoteNames.put(name, url);
118             }
119         }
120 
121         return remoteNames;
122     }
123 
124     protected String convert(NodeFilter filter, String content)
125         throws IOException {
126 
127         if (content == null) {
128             return StringPool.BLANK;
129         }
130 
131         StringWriter writer = new StringWriter();
132 
133         filter.filter(new StringReader(content), writer);
134 
135         return writer.toString();
136     }
137 
138     protected NodeFilter getFilter(long nodeId) {
139         return getFilter(null, nodeId);
140     }
141 
142     protected 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, _mainConfiguration,
149             nodeRepository, portletURL, nodeId);
150 
151         return filter;
152     }
153 
154     private String _mainConfiguration;
155     private Map<String, String> _remoteNames;
156 
157 }