PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/portal-impl/src/com/liferay/portlet/wiki/util/WikiUtil.java

https://github.com/spreddy/liferay-portal
Java | 576 lines | 424 code | 135 blank | 17 comment | 55 complexity | 93bf144e3525773b53a68b8de8a7ee22 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portlet.wiki.util;
  15. import com.liferay.portal.kernel.configuration.Filter;
  16. import com.liferay.portal.kernel.exception.PortalException;
  17. import com.liferay.portal.kernel.exception.SystemException;
  18. import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
  19. import com.liferay.portal.kernel.portlet.LiferayPortletURL;
  20. import com.liferay.portal.kernel.util.DiffHtmlUtil;
  21. import com.liferay.portal.kernel.util.GetterUtil;
  22. import com.liferay.portal.kernel.util.HttpUtil;
  23. import com.liferay.portal.kernel.util.InstancePool;
  24. import com.liferay.portal.kernel.util.ListUtil;
  25. import com.liferay.portal.kernel.util.OrderByComparator;
  26. import com.liferay.portal.kernel.util.PropsKeys;
  27. import com.liferay.portal.kernel.util.StringPool;
  28. import com.liferay.portal.kernel.util.StringUtil;
  29. import com.liferay.portal.kernel.util.Validator;
  30. import com.liferay.portal.security.permission.ActionKeys;
  31. import com.liferay.portal.security.permission.PermissionChecker;
  32. import com.liferay.portal.theme.ThemeDisplay;
  33. import com.liferay.portal.util.PortalUtil;
  34. import com.liferay.portal.util.PropsUtil;
  35. import com.liferay.portal.util.WebKeys;
  36. import com.liferay.portlet.wiki.PageContentException;
  37. import com.liferay.portlet.wiki.WikiFormatException;
  38. import com.liferay.portlet.wiki.engines.WikiEngine;
  39. import com.liferay.portlet.wiki.model.WikiNode;
  40. import com.liferay.portlet.wiki.model.WikiPage;
  41. import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
  42. import com.liferay.portlet.wiki.service.permission.WikiNodePermission;
  43. import com.liferay.portlet.wiki.util.comparator.PageCreateDateComparator;
  44. import com.liferay.portlet.wiki.util.comparator.PageTitleComparator;
  45. import com.liferay.portlet.wiki.util.comparator.PageVersionComparator;
  46. import com.liferay.util.ContentUtil;
  47. import java.io.IOException;
  48. import java.util.ArrayList;
  49. import java.util.Arrays;
  50. import java.util.Collections;
  51. import java.util.Iterator;
  52. import java.util.List;
  53. import java.util.Map;
  54. import java.util.concurrent.ConcurrentHashMap;
  55. import java.util.regex.Matcher;
  56. import java.util.regex.Pattern;
  57. import javax.portlet.PortletPreferences;
  58. import javax.portlet.PortletRequest;
  59. import javax.portlet.PortletURL;
  60. /**
  61. * @author Brian Wing Shun Chan
  62. * @author Jorge Ferrer
  63. */
  64. public class WikiUtil {
  65. public static String convert(
  66. WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
  67. String attachmentURLPrefix)
  68. throws PageContentException, WikiFormatException {
  69. return _instance._convert(
  70. page, viewPageURL, editPageURL, attachmentURLPrefix);
  71. }
  72. public static String diffHtml (
  73. WikiPage sourcePage, WikiPage targetPage, PortletURL viewPageURL,
  74. PortletURL editPageURL, String attachmentURLPrefix)
  75. throws Exception {
  76. String sourceContent = StringPool.BLANK;
  77. String targetContent = StringPool.BLANK;
  78. if (sourcePage != null) {
  79. sourceContent = WikiUtil.convert(
  80. sourcePage, viewPageURL, editPageURL,attachmentURLPrefix);
  81. }
  82. if (targetPage != null) {
  83. targetContent = WikiUtil.convert(
  84. targetPage, viewPageURL, editPageURL, attachmentURLPrefix);
  85. }
  86. return DiffHtmlUtil.diff(
  87. new UnsyncStringReader(sourceContent),
  88. new UnsyncStringReader(targetContent));
  89. }
  90. public static String getEditPage(String format) {
  91. return _instance._getEditPage(format);
  92. }
  93. public static String getEmailFromAddress(
  94. PortletPreferences preferences, long companyId)
  95. throws SystemException {
  96. return PortalUtil.getEmailFromAddress(
  97. preferences, companyId, PropsKeys.WIKI_EMAIL_FROM_ADDRESS);
  98. }
  99. public static String getEmailFromName(
  100. PortletPreferences preferences, long companyId)
  101. throws SystemException {
  102. return PortalUtil.getEmailFromName(
  103. preferences, companyId, PropsKeys.WIKI_EMAIL_FROM_NAME);
  104. }
  105. public static String getEmailPageAddedBody(PortletPreferences preferences) {
  106. String emailPageAddedBody = preferences.getValue(
  107. "emailPageAddedBody", StringPool.BLANK);
  108. if (Validator.isNotNull(emailPageAddedBody)) {
  109. return emailPageAddedBody;
  110. }
  111. else {
  112. return ContentUtil.get(PropsUtil.get(
  113. PropsKeys.WIKI_EMAIL_PAGE_ADDED_BODY));
  114. }
  115. }
  116. public static boolean getEmailPageAddedEnabled(
  117. PortletPreferences preferences) {
  118. String emailPageAddedEnabled = preferences.getValue(
  119. "emailPageAddedEnabled", StringPool.BLANK);
  120. if (Validator.isNotNull(emailPageAddedEnabled)) {
  121. return GetterUtil.getBoolean(emailPageAddedEnabled);
  122. }
  123. else {
  124. return GetterUtil.getBoolean(PropsUtil.get(
  125. PropsKeys.WIKI_EMAIL_PAGE_ADDED_ENABLED));
  126. }
  127. }
  128. public static String getEmailPageAddedSignature(
  129. PortletPreferences preferences) {
  130. String emailPageAddedSignature = preferences.getValue(
  131. "emailPageAddedSignature", StringPool.BLANK);
  132. if (Validator.isNotNull(emailPageAddedSignature)) {
  133. return emailPageAddedSignature;
  134. }
  135. else {
  136. return ContentUtil.get(PropsUtil.get(
  137. PropsKeys.WIKI_EMAIL_PAGE_ADDED_SIGNATURE));
  138. }
  139. }
  140. public static String getEmailPageAddedSubjectPrefix(
  141. PortletPreferences preferences) {
  142. String emailPageAddedSubjectPrefix = preferences.getValue(
  143. "emailPageAddedSubjectPrefix", StringPool.BLANK);
  144. if (Validator.isNotNull(emailPageAddedSubjectPrefix)) {
  145. return emailPageAddedSubjectPrefix;
  146. }
  147. else {
  148. return ContentUtil.get(PropsUtil.get(
  149. PropsKeys.WIKI_EMAIL_PAGE_ADDED_SUBJECT_PREFIX));
  150. }
  151. }
  152. public static String getEmailPageUpdatedBody(
  153. PortletPreferences preferences) {
  154. String emailPageUpdatedBody = preferences.getValue(
  155. "emailPageUpdatedBody", StringPool.BLANK);
  156. if (Validator.isNotNull(emailPageUpdatedBody)) {
  157. return emailPageUpdatedBody;
  158. }
  159. else {
  160. return ContentUtil.get(PropsUtil.get(
  161. PropsKeys.WIKI_EMAIL_PAGE_UPDATED_BODY));
  162. }
  163. }
  164. public static boolean getEmailPageUpdatedEnabled(
  165. PortletPreferences preferences) {
  166. String emailPageUpdatedEnabled = preferences.getValue(
  167. "emailPageUpdatedEnabled", StringPool.BLANK);
  168. if (Validator.isNotNull(emailPageUpdatedEnabled)) {
  169. return GetterUtil.getBoolean(emailPageUpdatedEnabled);
  170. }
  171. else {
  172. return GetterUtil.getBoolean(PropsUtil.get(
  173. PropsKeys.WIKI_EMAIL_PAGE_UPDATED_ENABLED));
  174. }
  175. }
  176. public static String getEmailPageUpdatedSignature(
  177. PortletPreferences preferences) {
  178. String emailPageUpdatedSignature = preferences.getValue(
  179. "emailPageUpdatedSignature", StringPool.BLANK);
  180. if (Validator.isNotNull(emailPageUpdatedSignature)) {
  181. return emailPageUpdatedSignature;
  182. }
  183. else {
  184. return ContentUtil.get(PropsUtil.get(
  185. PropsKeys.WIKI_EMAIL_PAGE_UPDATED_SIGNATURE));
  186. }
  187. }
  188. public static String getEmailPageUpdatedSubjectPrefix(
  189. PortletPreferences preferences) {
  190. String emailPageUpdatedSubject = preferences.getValue(
  191. "emailPageUpdatedSubjectPrefix", StringPool.BLANK);
  192. if (Validator.isNotNull(emailPageUpdatedSubject)) {
  193. return emailPageUpdatedSubject;
  194. }
  195. else {
  196. return ContentUtil.get(PropsUtil.get(
  197. PropsKeys.WIKI_EMAIL_PAGE_UPDATED_SUBJECT_PREFIX));
  198. }
  199. }
  200. public static WikiNode getFirstNode(PortletRequest portletRequest)
  201. throws PortalException, SystemException {
  202. ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
  203. WebKeys.THEME_DISPLAY);
  204. long groupId = themeDisplay.getScopeGroupId();
  205. PermissionChecker permissionChecker =
  206. themeDisplay.getPermissionChecker();
  207. List<WikiNode> nodes = WikiNodeLocalServiceUtil.getNodes(groupId);
  208. PortletPreferences preferences = portletRequest.getPreferences();
  209. String[] visibleNodeNames =
  210. StringUtil.split(preferences.getValue("visibleNodes", null));
  211. nodes = orderNodes(nodes, visibleNodeNames);
  212. String[] hiddenNodes = StringUtil.split(
  213. preferences.getValue("hiddenNodes", StringPool.BLANK));
  214. Arrays.sort(hiddenNodes);
  215. for (WikiNode node : nodes) {
  216. if ((Arrays.binarySearch(hiddenNodes, node.getName()) < 0) &&
  217. (WikiNodePermission.contains(permissionChecker, node,
  218. ActionKeys.VIEW))) {
  219. return node;
  220. }
  221. }
  222. return null;
  223. }
  224. public static String getHelpPage(String format) {
  225. return _instance._getHelpPage(format);
  226. }
  227. public static String getHelpURL(String format) {
  228. return _instance._getHelpURL(format);
  229. }
  230. public static Map<String, Boolean> getLinks(WikiPage page)
  231. throws PageContentException {
  232. return _instance._getLinks(page);
  233. }
  234. public static List<String> getNodeNames(List<WikiNode> nodes) {
  235. List<String> nodeNames = new ArrayList<String>(nodes.size());
  236. for (WikiNode node : nodes) {
  237. nodeNames.add(node.getName());
  238. }
  239. return nodeNames;
  240. }
  241. public static List<WikiNode> getNodes(
  242. List<WikiNode> nodes, String[] hiddenNodes,
  243. PermissionChecker permissionChecker) {
  244. nodes = ListUtil.copy(nodes);
  245. Arrays.sort(hiddenNodes);
  246. Iterator<WikiNode> itr = nodes.iterator();
  247. while (itr.hasNext()) {
  248. WikiNode node = itr.next();
  249. if (!(Arrays.binarySearch(hiddenNodes, node.getName()) < 0) ||
  250. !WikiNodePermission.contains(
  251. permissionChecker, node, ActionKeys.VIEW)) {
  252. itr.remove();
  253. }
  254. }
  255. return nodes;
  256. }
  257. public static OrderByComparator getPageOrderByComparator(
  258. String orderByCol, String orderByType) {
  259. boolean orderByAsc = false;
  260. if (orderByType.equals("asc")) {
  261. orderByAsc = true;
  262. }
  263. OrderByComparator orderByComparator = null;
  264. if (orderByCol.equals("modifiedDate")) {
  265. orderByComparator = new PageCreateDateComparator(orderByAsc);
  266. }
  267. else if (orderByCol.equals("title")) {
  268. orderByComparator = new PageTitleComparator(orderByAsc);
  269. }
  270. else if (orderByCol.equals("version")) {
  271. orderByComparator = new PageVersionComparator(orderByAsc);
  272. }
  273. return orderByComparator;
  274. }
  275. public static List<WikiNode> orderNodes(
  276. List<WikiNode> nodes, String[] visibleNodeNames) {
  277. if ((visibleNodeNames == null) || (visibleNodeNames.length == 0)) {
  278. return nodes;
  279. }
  280. nodes = ListUtil.copy(nodes);
  281. List<WikiNode> orderedNodes = new ArrayList<WikiNode>(nodes.size());
  282. for (String visibleNodeName : visibleNodeNames) {
  283. for (WikiNode node : nodes) {
  284. if (node.getName().equals(visibleNodeName)) {
  285. orderedNodes.add(node);
  286. nodes.remove(node);
  287. break;
  288. }
  289. }
  290. }
  291. orderedNodes.addAll(nodes);
  292. return orderedNodes;
  293. }
  294. public static String processContent(String content) {
  295. content = content.replaceAll("</p>", "</p>\n");
  296. content = content.replaceAll("</br>", "</br>\n");
  297. content = content.replaceAll("</div>", "</div>\n");
  298. return content;
  299. }
  300. public static boolean validate(
  301. long nodeId, String content, String format)
  302. throws WikiFormatException {
  303. return _instance._validate(nodeId, content, format);
  304. }
  305. private String _convert(
  306. WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
  307. String attachmentURLPrefix)
  308. throws PageContentException, WikiFormatException {
  309. LiferayPortletURL liferayViewPageURL = (LiferayPortletURL)viewPageURL;
  310. LiferayPortletURL liferayEditPageURL = (LiferayPortletURL)editPageURL;
  311. WikiEngine engine = _getEngine(page.getFormat());
  312. String content = engine.convert(
  313. page, viewPageURL, editPageURL, attachmentURLPrefix);
  314. String editPageURLString = StringPool.BLANK;
  315. if (editPageURL != null) {
  316. liferayEditPageURL.setParameter("title", "__REPLACEMENT__", false);
  317. editPageURLString = editPageURL.toString();
  318. editPageURLString = StringUtil.replace(
  319. editPageURLString, "__REPLACEMENT__", "$1");
  320. }
  321. Matcher matcher = _editPageURLPattern.matcher(content);
  322. content = _convertURLs(editPageURLString, matcher);
  323. String viewPageURLString = StringPool.BLANK;
  324. if (viewPageURL != null) {
  325. liferayViewPageURL.setParameter("title", "__REPLACEMENT__", false);
  326. viewPageURLString = viewPageURL.toString();
  327. viewPageURLString = StringUtil.replace(
  328. viewPageURLString, "__REPLACEMENT__", "$1");
  329. }
  330. matcher = _viewPageURLPattern.matcher(content);
  331. content = _convertURLs(viewPageURLString, matcher);
  332. content = _replaceAttachments(
  333. content, page.getTitle(), attachmentURLPrefix);
  334. return content;
  335. }
  336. private String _convertURLs(String url, Matcher matcher) {
  337. StringBuffer sb = new StringBuffer();
  338. while (matcher.find()) {
  339. String replacement = null;
  340. if (matcher.groupCount() >= 1) {
  341. String encodedTitle = HttpUtil.encodeURL(matcher.group(1));
  342. replacement = url.replace("$1", encodedTitle);
  343. }
  344. else {
  345. replacement = url;
  346. }
  347. matcher.appendReplacement(sb, replacement);
  348. }
  349. return matcher.appendTail(sb).toString();
  350. }
  351. private String _getEditPage(String format) {
  352. return PropsUtil.get(
  353. PropsKeys.WIKI_FORMATS_EDIT_PAGE, new Filter(format));
  354. }
  355. private WikiEngine _getEngine(String format) throws WikiFormatException {
  356. WikiEngine engine = _engines.get(format);
  357. if (engine != null) {
  358. return engine;
  359. }
  360. synchronized (_engines) {
  361. engine = _engines.get(format);
  362. if (engine != null) {
  363. return engine;
  364. }
  365. try {
  366. String engineClass = PropsUtil.get(
  367. PropsKeys.WIKI_FORMATS_ENGINE, new Filter(format));
  368. if (engineClass == null) {
  369. throw new WikiFormatException(format);
  370. }
  371. if (!InstancePool.contains(engineClass)) {
  372. engine = (WikiEngine)InstancePool.get(engineClass);
  373. engine.setMainConfiguration(
  374. _readConfigurationFile(
  375. PropsKeys.WIKI_FORMATS_CONFIGURATION_MAIN, format));
  376. engine.setInterWikiConfiguration(
  377. _readConfigurationFile(
  378. PropsKeys.WIKI_FORMATS_CONFIGURATION_INTERWIKI,
  379. format));
  380. }
  381. else {
  382. engine = (WikiEngine)InstancePool.get(engineClass);
  383. }
  384. _engines.put(format, engine);
  385. return engine;
  386. }
  387. catch (Exception e) {
  388. throw new WikiFormatException(e);
  389. }
  390. }
  391. }
  392. private String _getHelpPage(String format) {
  393. return PropsUtil.get(
  394. PropsKeys.WIKI_FORMATS_HELP_PAGE, new Filter(format));
  395. }
  396. private String _getHelpURL(String format) {
  397. return PropsUtil.get(
  398. PropsKeys.WIKI_FORMATS_HELP_URL, new Filter(format));
  399. }
  400. private Map<String, Boolean> _getLinks(WikiPage page)
  401. throws PageContentException {
  402. try {
  403. return _getEngine(page.getFormat()).getOutgoingLinks(page);
  404. }
  405. catch (WikiFormatException wfe) {
  406. return Collections.emptyMap();
  407. }
  408. }
  409. private String _readConfigurationFile(String propertyName, String format)
  410. throws IOException {
  411. ClassLoader classLoader = getClass().getClassLoader();
  412. String configurationFile = PropsUtil.get(
  413. propertyName, new Filter(format));
  414. if (Validator.isNotNull(configurationFile)) {
  415. return HttpUtil.URLtoString(
  416. classLoader.getResource(configurationFile));
  417. }
  418. else {
  419. return StringPool.BLANK;
  420. }
  421. }
  422. private String _replaceAttachments(
  423. String content, String title, String attachmentURLPrefix) {
  424. content = StringUtil.replace(content, "[$WIKI_PAGE_NAME$]", title);
  425. content = StringUtil.replace(
  426. content, "[$ATTACHMENT_URL_PREFIX$]", attachmentURLPrefix);
  427. return content;
  428. }
  429. private boolean _validate(long nodeId, String content, String format)
  430. throws WikiFormatException {
  431. return _getEngine(format).validate(nodeId, content);
  432. }
  433. private static WikiUtil _instance = new WikiUtil();
  434. private static Pattern _editPageURLPattern = Pattern.compile(
  435. "\\[\\$BEGIN_PAGE_TITLE_EDIT\\$\\](.*?)" +
  436. "\\[\\$END_PAGE_TITLE_EDIT\\$\\]");
  437. private static Pattern _viewPageURLPattern = Pattern.compile(
  438. "\\[\\$BEGIN_PAGE_TITLE\\$\\](.*?)\\[\\$END_PAGE_TITLE\\$\\]");
  439. private Map<String, WikiEngine> _engines =
  440. new ConcurrentHashMap<String, WikiEngine>();
  441. }