1
22
23 package com.liferay.portlet.wiki.engines.jspwiki;
24
25 import com.ecyrd.jspwiki.WikiContext;
26 import com.ecyrd.jspwiki.WikiException;
27 import com.ecyrd.jspwiki.WikiPage;
28
29 import com.liferay.portal.SystemException;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portal.kernel.util.Validator;
32 import com.liferay.portlet.wiki.PageContentException;
33 import com.liferay.portlet.wiki.engines.WikiEngine;
34 import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
35
36 import java.io.ByteArrayInputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39
40 import java.util.Collection;
41 import java.util.HashMap;
42 import java.util.Map;
43 import java.util.Properties;
44
45 import javax.portlet.PortletURL;
46
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49
50
56 public class JSPWikiEngine implements WikiEngine {
57
58 public String convert(
59 com.liferay.portlet.wiki.model.WikiPage page, PortletURL portletURL)
60 throws PageContentException {
61
62 try {
63 return convert(page);
64 }
65 catch (WikiException we) {
66 throw new PageContentException(we);
67 }
68 }
69
70 public Map<String, Boolean> getOutgoingLinks(
71 com.liferay.portlet.wiki.model.WikiPage page)
72 throws PageContentException {
73
74 try {
75 LiferayJSPWikiEngine engine = getEngine(page.getNodeId());
76
77 WikiPage jspWikiPage = LiferayPageProvider.toJSPWikiPage(
78 page, engine);
79
80 Collection<String> titles = engine.scanWikiLinks(
81 jspWikiPage, page.getContent());
82
83 Map<String, Boolean> links = new HashMap<String, Boolean>();
84
85 for (String title : titles) {
86 if (title.startsWith("[[")) {
87 title = title.substring(2);
88 }
89 else if (title.startsWith("[")) {
90 title = title.substring(1);
91 }
92
93 if (title.endsWith("]]")) {
94 title = title.substring(title.length() - 2, title.length());
95 }
96 else if (title.startsWith("[")) {
97 title = title.substring(title.length() - 1, title.length());
98 }
99
100 Boolean existsObj = links.get(title);
101
102 if (existsObj == null) {
103 if (WikiPageLocalServiceUtil.getPagesCount(
104 page.getNodeId(), title, true) > 0) {
105
106 existsObj = Boolean.TRUE;
107 }
108 else {
109 existsObj = Boolean.FALSE;
110 }
111
112 links.put(title, existsObj);
113 }
114 }
115
116 return links;
117 }
118 catch (SystemException se) {
119 throw new PageContentException(se);
120 }
121 catch (WikiException we) {
122 throw new PageContentException(we);
123 }
124 }
125
126 public boolean isLinkedTo(
127 com.liferay.portlet.wiki.model.WikiPage page, String targetTitle)
128 throws PageContentException {
129
130 Map<String, Boolean> links = getOutgoingLinks(page);
131
132 Boolean link = links.get(targetTitle);
133
134 if (link != null) {
135 return true;
136 }
137 else {
138 return false;
139 }
140 }
141
142 public void setInterWikiConfiguration(String interWikiConfiguration) {
143 }
144
145 public void setMainConfiguration(String mainConfiguration) {
146 setProperties(mainConfiguration);
147 }
148
149 public boolean validate(long nodeId, String newContent) {
150 return true;
151 }
152
153 protected String convert(com.liferay.portlet.wiki.model.WikiPage page)
154 throws WikiException {
155
156 String content = page.getContent();
157
158 if (Validator.isNull(content)) {
159 return StringPool.BLANK;
160 }
161
162 com.ecyrd.jspwiki.WikiEngine engine = getEngine(page.getNodeId());
163
164 WikiPage jspWikiPage = LiferayPageProvider.toJSPWikiPage(page, engine);
165
166 WikiContext wikiContext = new WikiContext(engine, jspWikiPage);
167
168 return engine.textToHTML(wikiContext, content);
169 }
170
171 protected LiferayJSPWikiEngine getEngine(long nodeId)
172 throws WikiException {
173
174 LiferayJSPWikiEngine engine = _engines.get(nodeId);
175
176 if (engine == null) {
177 Properties nodeProps = new Properties(_props);
178
179 nodeProps.setProperty("nodeId", String.valueOf(nodeId));
180
181 String appName = nodeProps.getProperty("jspwiki.applicationName");
182
183 nodeProps.setProperty(
184 "jspwiki.applicationName", appName + " for node " + nodeId);
185
186 engine = new LiferayJSPWikiEngine(nodeProps);
187
188 _engines.put(nodeId, engine);
189 }
190
191 return engine;
192 }
193
194 protected synchronized void setProperties(String configuration) {
195 _props = new Properties();
196
197 InputStream is = new ByteArrayInputStream(configuration.getBytes());
198
199 try {
200 _props.load(is);
201 }
202 catch (IOException ioe) {
203 _log.error(ioe, ioe);
204 }
205 }
206
207 private static Log _log = LogFactory.getLog(JSPWikiEngine.class);
208
209 private Properties _props;
210 private Map<Long, LiferayJSPWikiEngine> _engines =
211 new HashMap<Long, LiferayJSPWikiEngine>();
212
213 }