PageRenderTime 46ms CodeModel.GetById 26ms app.highlight 17ms 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
 15package com.liferay.portlet.wiki.util;
 16
 17import com.liferay.portal.kernel.configuration.Filter;
 18import com.liferay.portal.kernel.exception.PortalException;
 19import com.liferay.portal.kernel.exception.SystemException;
 20import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
 21import com.liferay.portal.kernel.portlet.LiferayPortletURL;
 22import com.liferay.portal.kernel.util.DiffHtmlUtil;
 23import com.liferay.portal.kernel.util.GetterUtil;
 24import com.liferay.portal.kernel.util.HttpUtil;
 25import com.liferay.portal.kernel.util.InstancePool;
 26import com.liferay.portal.kernel.util.ListUtil;
 27import com.liferay.portal.kernel.util.OrderByComparator;
 28import com.liferay.portal.kernel.util.PropsKeys;
 29import com.liferay.portal.kernel.util.StringPool;
 30import com.liferay.portal.kernel.util.StringUtil;
 31import com.liferay.portal.kernel.util.Validator;
 32import com.liferay.portal.security.permission.ActionKeys;
 33import com.liferay.portal.security.permission.PermissionChecker;
 34import com.liferay.portal.theme.ThemeDisplay;
 35import com.liferay.portal.util.PortalUtil;
 36import com.liferay.portal.util.PropsUtil;
 37import com.liferay.portal.util.WebKeys;
 38import com.liferay.portlet.wiki.PageContentException;
 39import com.liferay.portlet.wiki.WikiFormatException;
 40import com.liferay.portlet.wiki.engines.WikiEngine;
 41import com.liferay.portlet.wiki.model.WikiNode;
 42import com.liferay.portlet.wiki.model.WikiPage;
 43import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
 44import com.liferay.portlet.wiki.service.permission.WikiNodePermission;
 45import com.liferay.portlet.wiki.util.comparator.PageCreateDateComparator;
 46import com.liferay.portlet.wiki.util.comparator.PageTitleComparator;
 47import com.liferay.portlet.wiki.util.comparator.PageVersionComparator;
 48import com.liferay.util.ContentUtil;
 49
 50import java.io.IOException;
 51
 52import java.util.ArrayList;
 53import java.util.Arrays;
 54import java.util.Collections;
 55import java.util.Iterator;
 56import java.util.List;
 57import java.util.Map;
 58import java.util.concurrent.ConcurrentHashMap;
 59import java.util.regex.Matcher;
 60import java.util.regex.Pattern;
 61
 62import javax.portlet.PortletPreferences;
 63import javax.portlet.PortletRequest;
 64import javax.portlet.PortletURL;
 65
 66/**
 67 * @author Brian Wing Shun Chan
 68 * @author Jorge Ferrer
 69 */
 70public class WikiUtil {
 71
 72	public static String convert(
 73			WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
 74			String attachmentURLPrefix)
 75		throws PageContentException, WikiFormatException {
 76
 77		return _instance._convert(
 78			page, viewPageURL, editPageURL, attachmentURLPrefix);
 79	}
 80
 81	public static String diffHtml (
 82			WikiPage sourcePage, WikiPage targetPage, PortletURL viewPageURL,
 83			PortletURL editPageURL, String attachmentURLPrefix)
 84		throws Exception {
 85
 86		String sourceContent = StringPool.BLANK;
 87		String targetContent = StringPool.BLANK;
 88
 89		if (sourcePage != null) {
 90			sourceContent = WikiUtil.convert(
 91				sourcePage, viewPageURL, editPageURL,attachmentURLPrefix);
 92		}
 93
 94		if (targetPage != null) {
 95			targetContent = WikiUtil.convert(
 96				targetPage, viewPageURL, editPageURL, attachmentURLPrefix);
 97		}
 98
 99		return DiffHtmlUtil.diff(
100			new UnsyncStringReader(sourceContent),
101			new UnsyncStringReader(targetContent));
102	}
103
104	public static String getEditPage(String format) {
105		return _instance._getEditPage(format);
106	}
107
108	public static String getEmailFromAddress(
109			PortletPreferences preferences, long companyId)
110		throws SystemException {
111
112		return PortalUtil.getEmailFromAddress(
113			preferences, companyId, PropsKeys.WIKI_EMAIL_FROM_ADDRESS);
114	}
115
116	public static String getEmailFromName(
117			PortletPreferences preferences, long companyId)
118		throws SystemException {
119
120		return PortalUtil.getEmailFromName(
121			preferences, companyId, PropsKeys.WIKI_EMAIL_FROM_NAME);
122	}
123
124	public static String getEmailPageAddedBody(PortletPreferences preferences) {
125		String emailPageAddedBody = preferences.getValue(
126			"emailPageAddedBody", StringPool.BLANK);
127
128		if (Validator.isNotNull(emailPageAddedBody)) {
129			return emailPageAddedBody;
130		}
131		else {
132			return ContentUtil.get(PropsUtil.get(
133				PropsKeys.WIKI_EMAIL_PAGE_ADDED_BODY));
134		}
135	}
136
137	public static boolean getEmailPageAddedEnabled(
138		PortletPreferences preferences) {
139
140		String emailPageAddedEnabled = preferences.getValue(
141			"emailPageAddedEnabled", StringPool.BLANK);
142
143		if (Validator.isNotNull(emailPageAddedEnabled)) {
144			return GetterUtil.getBoolean(emailPageAddedEnabled);
145		}
146		else {
147			return GetterUtil.getBoolean(PropsUtil.get(
148				PropsKeys.WIKI_EMAIL_PAGE_ADDED_ENABLED));
149		}
150	}
151
152	public static String getEmailPageAddedSignature(
153		PortletPreferences preferences) {
154
155		String emailPageAddedSignature = preferences.getValue(
156			"emailPageAddedSignature", StringPool.BLANK);
157
158		if (Validator.isNotNull(emailPageAddedSignature)) {
159			return emailPageAddedSignature;
160		}
161		else {
162			return ContentUtil.get(PropsUtil.get(
163				PropsKeys.WIKI_EMAIL_PAGE_ADDED_SIGNATURE));
164		}
165	}
166
167	public static String getEmailPageAddedSubjectPrefix(
168		PortletPreferences preferences) {
169
170		String emailPageAddedSubjectPrefix = preferences.getValue(
171			"emailPageAddedSubjectPrefix", StringPool.BLANK);
172
173		if (Validator.isNotNull(emailPageAddedSubjectPrefix)) {
174			return emailPageAddedSubjectPrefix;
175		}
176		else {
177			return ContentUtil.get(PropsUtil.get(
178				PropsKeys.WIKI_EMAIL_PAGE_ADDED_SUBJECT_PREFIX));
179		}
180	}
181
182	public static String getEmailPageUpdatedBody(
183		PortletPreferences preferences) {
184
185		String emailPageUpdatedBody = preferences.getValue(
186			"emailPageUpdatedBody", StringPool.BLANK);
187
188		if (Validator.isNotNull(emailPageUpdatedBody)) {
189			return emailPageUpdatedBody;
190		}
191		else {
192			return ContentUtil.get(PropsUtil.get(
193				PropsKeys.WIKI_EMAIL_PAGE_UPDATED_BODY));
194		}
195	}
196
197	public static boolean getEmailPageUpdatedEnabled(
198		PortletPreferences preferences) {
199
200		String emailPageUpdatedEnabled = preferences.getValue(
201			"emailPageUpdatedEnabled", StringPool.BLANK);
202
203		if (Validator.isNotNull(emailPageUpdatedEnabled)) {
204			return GetterUtil.getBoolean(emailPageUpdatedEnabled);
205		}
206		else {
207			return GetterUtil.getBoolean(PropsUtil.get(
208				PropsKeys.WIKI_EMAIL_PAGE_UPDATED_ENABLED));
209		}
210	}
211
212	public static String getEmailPageUpdatedSignature(
213		PortletPreferences preferences) {
214
215		String emailPageUpdatedSignature = preferences.getValue(
216			"emailPageUpdatedSignature", StringPool.BLANK);
217
218		if (Validator.isNotNull(emailPageUpdatedSignature)) {
219			return emailPageUpdatedSignature;
220		}
221		else {
222			return ContentUtil.get(PropsUtil.get(
223				PropsKeys.WIKI_EMAIL_PAGE_UPDATED_SIGNATURE));
224		}
225	}
226
227	public static String getEmailPageUpdatedSubjectPrefix(
228		PortletPreferences preferences) {
229
230		String emailPageUpdatedSubject = preferences.getValue(
231			"emailPageUpdatedSubjectPrefix", StringPool.BLANK);
232
233		if (Validator.isNotNull(emailPageUpdatedSubject)) {
234			return emailPageUpdatedSubject;
235		}
236		else {
237			return ContentUtil.get(PropsUtil.get(
238				PropsKeys.WIKI_EMAIL_PAGE_UPDATED_SUBJECT_PREFIX));
239		}
240	}
241
242	public static WikiNode getFirstNode(PortletRequest portletRequest)
243		throws PortalException, SystemException {
244
245		ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
246			WebKeys.THEME_DISPLAY);
247		long groupId = themeDisplay.getScopeGroupId();
248		PermissionChecker permissionChecker =
249			themeDisplay.getPermissionChecker();
250
251		List<WikiNode> nodes = WikiNodeLocalServiceUtil.getNodes(groupId);
252
253		PortletPreferences preferences = portletRequest.getPreferences();
254		String[] visibleNodeNames =
255			StringUtil.split(preferences.getValue("visibleNodes", null));
256		nodes = orderNodes(nodes, visibleNodeNames);
257
258		String[] hiddenNodes = StringUtil.split(
259			preferences.getValue("hiddenNodes", StringPool.BLANK));
260		Arrays.sort(hiddenNodes);
261
262		for (WikiNode node : nodes) {
263			if ((Arrays.binarySearch(hiddenNodes, node.getName()) < 0) &&
264				(WikiNodePermission.contains(permissionChecker, node,
265					ActionKeys.VIEW))) {
266				return node;
267			}
268		}
269		return null;
270	}
271
272	public static String getHelpPage(String format) {
273		return _instance._getHelpPage(format);
274	}
275
276	public static String getHelpURL(String format) {
277		return _instance._getHelpURL(format);
278	}
279
280	public static Map<String, Boolean> getLinks(WikiPage page)
281		throws PageContentException {
282
283		return _instance._getLinks(page);
284	}
285
286	public static List<String> getNodeNames(List<WikiNode> nodes) {
287		List<String> nodeNames = new ArrayList<String>(nodes.size());
288
289		for (WikiNode node : nodes) {
290			nodeNames.add(node.getName());
291		}
292
293		return nodeNames;
294	}
295
296	public static List<WikiNode> getNodes(
297		List<WikiNode> nodes, String[] hiddenNodes,
298		PermissionChecker permissionChecker) {
299
300		nodes = ListUtil.copy(nodes);
301
302		Arrays.sort(hiddenNodes);
303
304		Iterator<WikiNode> itr = nodes.iterator();
305
306		while (itr.hasNext()) {
307			WikiNode node = itr.next();
308
309			if (!(Arrays.binarySearch(hiddenNodes, node.getName()) < 0) ||
310				!WikiNodePermission.contains(
311					permissionChecker, node, ActionKeys.VIEW)) {
312
313				itr.remove();
314			}
315		}
316
317		return nodes;
318	}
319
320	public static OrderByComparator getPageOrderByComparator(
321		String orderByCol, String orderByType) {
322
323		boolean orderByAsc = false;
324
325		if (orderByType.equals("asc")) {
326			orderByAsc = true;
327		}
328
329		OrderByComparator orderByComparator = null;
330
331		if (orderByCol.equals("modifiedDate")) {
332			orderByComparator = new PageCreateDateComparator(orderByAsc);
333		}
334		else if (orderByCol.equals("title")) {
335			orderByComparator = new PageTitleComparator(orderByAsc);
336		}
337		else if (orderByCol.equals("version")) {
338			orderByComparator = new PageVersionComparator(orderByAsc);
339		}
340
341		return orderByComparator;
342	}
343
344	public static List<WikiNode> orderNodes(
345		List<WikiNode> nodes, String[] visibleNodeNames) {
346
347		if ((visibleNodeNames == null) || (visibleNodeNames.length == 0)) {
348			return nodes;
349		}
350
351		nodes = ListUtil.copy(nodes);
352
353		List<WikiNode> orderedNodes = new ArrayList<WikiNode>(nodes.size());
354
355		for (String visibleNodeName : visibleNodeNames) {
356			for (WikiNode node : nodes) {
357				if (node.getName().equals(visibleNodeName)) {
358					orderedNodes.add(node);
359
360					nodes.remove(node);
361
362					break;
363				}
364			}
365		}
366
367		orderedNodes.addAll(nodes);
368
369		return orderedNodes;
370	}
371
372	public static String processContent(String content) {
373		content = content.replaceAll("</p>", "</p>\n");
374		content = content.replaceAll("</br>", "</br>\n");
375		content = content.replaceAll("</div>", "</div>\n");
376
377		return content;
378	}
379
380	public static boolean validate(
381			long nodeId, String content, String format)
382		throws WikiFormatException {
383
384		return _instance._validate(nodeId, content, format);
385	}
386
387	private String _convert(
388			WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
389			String attachmentURLPrefix)
390		throws PageContentException, WikiFormatException {
391
392		LiferayPortletURL liferayViewPageURL = (LiferayPortletURL)viewPageURL;
393		LiferayPortletURL liferayEditPageURL = (LiferayPortletURL)editPageURL;
394
395		WikiEngine engine = _getEngine(page.getFormat());
396
397		String content = engine.convert(
398			page, viewPageURL, editPageURL, attachmentURLPrefix);
399
400		String editPageURLString = StringPool.BLANK;
401
402		if (editPageURL != null) {
403			liferayEditPageURL.setParameter("title", "__REPLACEMENT__", false);
404
405			editPageURLString = editPageURL.toString();
406
407			editPageURLString = StringUtil.replace(
408				editPageURLString, "__REPLACEMENT__", "$1");
409		}
410
411		Matcher matcher = _editPageURLPattern.matcher(content);
412
413		content = _convertURLs(editPageURLString, matcher);
414
415		String viewPageURLString = StringPool.BLANK;
416
417		if (viewPageURL != null) {
418			liferayViewPageURL.setParameter("title", "__REPLACEMENT__", false);
419
420			viewPageURLString = viewPageURL.toString();
421
422			viewPageURLString = StringUtil.replace(
423				viewPageURLString, "__REPLACEMENT__", "$1");
424		}
425
426		matcher = _viewPageURLPattern.matcher(content);
427
428		content = _convertURLs(viewPageURLString, matcher);
429
430		content = _replaceAttachments(
431			content, page.getTitle(), attachmentURLPrefix);
432
433		return content;
434	}
435
436	private String _convertURLs(String url, Matcher matcher) {
437		StringBuffer sb = new StringBuffer();
438
439		while (matcher.find()) {
440			String replacement = null;
441
442			if (matcher.groupCount() >= 1) {
443				String encodedTitle = HttpUtil.encodeURL(matcher.group(1));
444
445				replacement = url.replace("$1", encodedTitle);
446			}
447			else {
448				replacement = url;
449			}
450
451			matcher.appendReplacement(sb, replacement);
452		}
453
454		return matcher.appendTail(sb).toString();
455	}
456
457	private String _getEditPage(String format) {
458		return PropsUtil.get(
459			PropsKeys.WIKI_FORMATS_EDIT_PAGE, new Filter(format));
460	}
461
462	private WikiEngine _getEngine(String format) throws WikiFormatException {
463		WikiEngine engine = _engines.get(format);
464
465		if (engine != null) {
466			return engine;
467		}
468
469		synchronized (_engines) {
470			engine = _engines.get(format);
471
472			if (engine != null) {
473				return engine;
474			}
475
476			try {
477				String engineClass = PropsUtil.get(
478					PropsKeys.WIKI_FORMATS_ENGINE, new Filter(format));
479
480				if (engineClass == null) {
481					throw new WikiFormatException(format);
482				}
483
484				if (!InstancePool.contains(engineClass)) {
485					engine = (WikiEngine)InstancePool.get(engineClass);
486
487					engine.setMainConfiguration(
488						_readConfigurationFile(
489							PropsKeys.WIKI_FORMATS_CONFIGURATION_MAIN, format));
490
491					engine.setInterWikiConfiguration(
492						_readConfigurationFile(
493							PropsKeys.WIKI_FORMATS_CONFIGURATION_INTERWIKI,
494							format));
495				}
496				else {
497					engine = (WikiEngine)InstancePool.get(engineClass);
498				}
499
500				_engines.put(format, engine);
501
502				return engine;
503			}
504			catch (Exception e) {
505				throw new WikiFormatException(e);
506			}
507		}
508	}
509
510	private String _getHelpPage(String format) {
511		return PropsUtil.get(
512			PropsKeys.WIKI_FORMATS_HELP_PAGE, new Filter(format));
513	}
514
515	private String _getHelpURL(String format) {
516		return PropsUtil.get(
517			PropsKeys.WIKI_FORMATS_HELP_URL, new Filter(format));
518	}
519
520	private Map<String, Boolean> _getLinks(WikiPage page)
521		throws PageContentException {
522
523		try {
524			return _getEngine(page.getFormat()).getOutgoingLinks(page);
525		}
526		catch (WikiFormatException wfe) {
527			return Collections.emptyMap();
528		}
529	}
530
531	private String _readConfigurationFile(String propertyName, String format)
532		throws IOException {
533
534		ClassLoader classLoader = getClass().getClassLoader();
535
536		String configurationFile = PropsUtil.get(
537			propertyName, new Filter(format));
538
539		if (Validator.isNotNull(configurationFile)) {
540			return HttpUtil.URLtoString(
541				classLoader.getResource(configurationFile));
542		}
543		else {
544			return StringPool.BLANK;
545		}
546	}
547
548	private String _replaceAttachments(
549		String content, String title, String attachmentURLPrefix) {
550
551		content = StringUtil.replace(content, "[$WIKI_PAGE_NAME$]", title);
552
553		content = StringUtil.replace(
554			content, "[$ATTACHMENT_URL_PREFIX$]", attachmentURLPrefix);
555
556		return content;
557	}
558
559	private boolean _validate(long nodeId, String content, String format)
560		throws WikiFormatException {
561
562		return _getEngine(format).validate(nodeId, content);
563	}
564
565	private static WikiUtil _instance = new WikiUtil();
566
567	private static Pattern _editPageURLPattern = Pattern.compile(
568		"\\[\\$BEGIN_PAGE_TITLE_EDIT\\$\\](.*?)" +
569			"\\[\\$END_PAGE_TITLE_EDIT\\$\\]");
570	private static Pattern _viewPageURLPattern = Pattern.compile(
571		"\\[\\$BEGIN_PAGE_TITLE\\$\\](.*?)\\[\\$END_PAGE_TITLE\\$\\]");
572
573	private Map<String, WikiEngine> _engines =
574		new ConcurrentHashMap<String, WikiEngine>();
575
576}