1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.engines.friki;
16  
17  import com.efsol.friki.PageRepository;
18  
19  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
20  import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portlet.wiki.PageContentException;
23  import com.liferay.portlet.wiki.engines.WikiEngine;
24  import com.liferay.portlet.wiki.model.WikiPage;
25  
26  import java.io.IOException;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  import java.util.StringTokenizer;
31  
32  import javax.portlet.PortletURL;
33  
34  import org.stringtree.factory.memory.MapStringRepository;
35  
36  /**
37   * <a href="FrikiEngine.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Jorge Ferrer
40   */
41  public class FrikiEngine implements WikiEngine {
42  
43      public FrikiEngine() {
44      }
45  
46      public String convert(WikiPage page, PortletURL portletURL)
47          throws PageContentException {
48  
49          try {
50              return convert(
51                  getFilter(portletURL, page.getNodeId()), page.getContent());
52          }
53          catch (IOException ioe) {
54              throw new PageContentException(ioe);
55          }
56      }
57  
58      public Map<String, Boolean> getOutgoingLinks(WikiPage page)
59          throws PageContentException {
60  
61          NodeFilter filter = getFilter(page.getNodeId());
62  
63          try {
64              convert(filter, page.getContent());
65  
66              return filter.getTitles();
67          }
68          catch (IOException ioe) {
69              throw new PageContentException(ioe);
70          }
71      }
72  
73      public void setInterWikiConfiguration(String interWikiConfiguration) {
74          _remoteNames = buildRemoteNamesMap(interWikiConfiguration);
75      }
76  
77      public void setMainConfiguration(String mainConfiguration) {
78          _mainConfiguration = mainConfiguration;
79      }
80  
81      public boolean validate(long nodeId, String newContent) {
82          try {
83              NodeFilter filter = getFilter(nodeId);
84  
85              convert(filter, newContent);
86  
87              return true;
88          }
89          catch (Exception e) {
90              return false;
91          }
92      }
93  
94      protected Map<String, String> buildRemoteNamesMap(String names) {
95          Map<String, String> remoteNames = new HashMap<String, String>();
96  
97          StringTokenizer st = new StringTokenizer(names, "\n");
98  
99          while (st.hasMoreTokens()) {
100             String line = st.nextToken().trim();
101 
102             int sep = line.indexOf(StringPool.SPACE);
103 
104             if (sep > 0) {
105                 String name = line.substring(0, sep);
106                 String url = line.substring(sep + 1);
107 
108                 remoteNames.put(name, url);
109             }
110         }
111 
112         return remoteNames;
113     }
114 
115     protected String convert(NodeFilter filter, String content)
116         throws IOException {
117 
118         if (content == null) {
119             return StringPool.BLANK;
120         }
121 
122         UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(true);
123 
124         filter.filter(new UnsyncStringReader(content), unsyncStringWriter);
125 
126         return unsyncStringWriter.toString();
127     }
128 
129     protected NodeFilter getFilter(long nodeId) {
130         return getFilter(null, nodeId);
131     }
132 
133     protected NodeFilter getFilter(PortletURL portletURL, long nodeId) {
134         MapStringRepository context = new MapStringRepository();
135         NodeRepository nodeRepository = new NodeRepository(nodeId);
136         PageRepository pageRepository = new PageRepository(nodeRepository);
137 
138         NodeFilter filter = new NodeFilter(
139             context, pageRepository, _remoteNames, _mainConfiguration,
140             nodeRepository, portletURL, nodeId);
141 
142         return filter;
143     }
144 
145     private String _mainConfiguration;
146     private Map<String, String> _remoteNames;
147 
148 }