001
014
015 package com.liferay.portlet.wiki.engines.jspwiki;
016
017 import com.ecyrd.jspwiki.WikiContext;
018 import com.ecyrd.jspwiki.WikiException;
019 import com.ecyrd.jspwiki.WikiPage;
020
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
023 import com.liferay.portal.kernel.log.Log;
024 import com.liferay.portal.kernel.log.LogFactoryUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portlet.wiki.PageContentException;
028 import com.liferay.portlet.wiki.engines.WikiEngine;
029 import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
030 import com.liferay.portlet.wiki.util.WikiUtil;
031
032 import java.io.IOException;
033 import java.io.InputStream;
034
035 import java.util.Collection;
036 import java.util.Collections;
037 import java.util.HashMap;
038 import java.util.Map;
039 import java.util.Properties;
040
041 import javax.portlet.PortletURL;
042
043
046 public class JSPWikiEngine implements WikiEngine {
047
048 public String convert(
049 com.liferay.portlet.wiki.model.WikiPage page,
050 PortletURL viewPageURL, PortletURL editPageURL,
051 String attachmentURLPrefix)
052 throws PageContentException {
053
054 try {
055 return convert(page);
056 }
057 catch (WikiException we) {
058 throw new PageContentException(we);
059 }
060 }
061
062 public Map<String, Boolean> getOutgoingLinks(
063 com.liferay.portlet.wiki.model.WikiPage page)
064 throws PageContentException {
065
066 if (Validator.isNull(page.getContent())) {
067 return Collections.EMPTY_MAP;
068 }
069
070 try {
071 LiferayJSPWikiEngine engine = getEngine(page.getNodeId());
072
073 WikiPage jspWikiPage = LiferayPageProvider.toJSPWikiPage(
074 page, engine);
075
076 Collection<String> titles = engine.scanWikiLinks(
077 jspWikiPage, WikiUtil.encodeJSPWikiContent(page.getContent()));
078
079 Map<String, Boolean> links = new HashMap<String, Boolean>();
080
081 for (String title : titles) {
082 if (title.startsWith("[[")) {
083 title = title.substring(2);
084 }
085 else if (title.startsWith("[")) {
086 title = title.substring(1);
087 }
088
089 if (title.endsWith("]]")) {
090 title = title.substring(title.length() - 2, title.length());
091 }
092 else if (title.startsWith("[")) {
093 title = title.substring(title.length() - 1, title.length());
094 }
095
096 Boolean existsObj = links.get(title);
097
098 if (existsObj == null) {
099 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 }