1   /**
2    * Copyright (c) 2000-2008 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 boolean isLinkedTo(WikiPage page, String targetTitle)
83          throws PageContentException {
84  
85          NodeFilter filter = getFilter(page.getNodeId());
86  
87          try {
88              convert(filter, page.getContent());
89  
90              if (filter.getTitles().get(targetTitle) != null) {
91                  return true;
92              }
93          }
94          catch (IOException ioe) {
95              throw new PageContentException(ioe);
96          }
97  
98          return false;
99      }
100 
101     public void setInterWikiConfiguration(String interWikiConfiguration) {
102         _remoteNames = buildRemoteNamesMap(interWikiConfiguration);
103     }
104 
105     public void setMainConfiguration(String mainConfiguration) {
106         _mainConfiguration = mainConfiguration;
107     }
108 
109     public boolean validate(long nodeId, String newContent) {
110         try {
111             NodeFilter filter = getFilter(nodeId);
112 
113             convert(filter, newContent);
114 
115             return true;
116         }
117         catch (Exception e) {
118             return false;
119         }
120     }
121 
122     protected Map<String, String> buildRemoteNamesMap(String names) {
123         Map<String, String> remoteNames = new HashMap<String, String>();
124 
125         StringTokenizer st = new StringTokenizer(names, "\n");
126 
127         while (st.hasMoreTokens()) {
128             String line = st.nextToken().trim();
129 
130             int sep = line.indexOf(StringPool.SPACE);
131 
132             if (sep > 0) {
133                 String name = line.substring(0, sep);
134                 String url = line.substring(sep + 1);
135 
136                 remoteNames.put(name, url);
137             }
138         }
139 
140         return remoteNames;
141     }
142 
143     protected String convert(NodeFilter filter, String content)
144         throws IOException {
145 
146         if (content == null) {
147             return StringPool.BLANK;
148         }
149 
150         StringWriter writer = new StringWriter();
151 
152         filter.filter(new StringReader(content), writer);
153 
154         return writer.toString();
155     }
156 
157     protected NodeFilter getFilter(long nodeId) {
158         return getFilter(null, nodeId);
159     }
160 
161     protected NodeFilter getFilter(PortletURL portletURL, long nodeId) {
162         MapStringRepository context = new MapStringRepository();
163         NodeRepository nodeRepository = new NodeRepository(nodeId);
164         PageRepository pageRepository = new PageRepository(nodeRepository);
165 
166         NodeFilter filter = new NodeFilter(
167             context, pageRepository, _remoteNames, _mainConfiguration,
168             nodeRepository, portletURL, nodeId);
169 
170         return filter;
171     }
172 
173     private String _mainConfiguration;
174     private Map<String, String> _remoteNames;
175 
176 }