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