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