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