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