All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.wiki.engine.html.internal.HtmlEngine Maven / Gradle / Ivy

There is a newer version: 6.0.13
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.wiki.engine.html.internal;

import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
import com.liferay.portal.kernel.portlet.Router;
import com.liferay.portal.kernel.resource.bundle.ResourceBundleLoader;
import com.liferay.portal.kernel.resource.bundle.ResourceBundleLoaderUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.wiki.configuration.WikiGroupServiceConfiguration;
import com.liferay.wiki.constants.WikiPortletKeys;
import com.liferay.wiki.engine.BaseWikiEngine;
import com.liferay.wiki.engine.WikiEngine;
import com.liferay.wiki.exception.NoSuchNodeException;
import com.liferay.wiki.exception.PageContentException;
import com.liferay.wiki.model.WikiPage;
import com.liferay.wiki.service.WikiNodeLocalService;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.StartTag;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Jorge Ferrer
 * @author Zsigmond Rab
 */
@Component(service = WikiEngine.class)
public class HtmlEngine extends BaseWikiEngine {

	@Override
	public String getEditorName() {
		return _wikiGroupServiceConfiguration.getHTMLEditor();
	}

	@Override
	public String getFormat() {
		return "html";
	}

	@Override
	public String getHelpPageTitle(HttpServletRequest httpServletRequest) {
		return null;
	}

	@Override
	public String getHelpURL() {
		return null;
	}

	@Override
	public Map getOutgoingLinks(WikiPage page)
		throws PageContentException {

		try {
			return _getOutgoingLinks(page);
		}
		catch (PortalException portalException) {
			throw new PageContentException(portalException);
		}
	}

	@Override
	public String getToolbarSet() {
		return null;
	}

	@Activate
	protected void activate() {
		_friendlyURLMapping =
			Portal.FRIENDLY_URL_SEPARATOR + _friendlyURLMapper.getMapping();

		_router = _friendlyURLMapper.getRouter();
	}

	@Override
	protected ServletContext getEditPageServletContext() {
		return _servletContext;
	}

	@Override
	protected ServletContext getHelpPageServletContext() {
		return null;
	}

	@Override
	protected ResourceBundleLoader getResourceBundleLoader() {
		return ResourceBundleLoaderUtil.getPortalResourceBundleLoader();
	}

	private Map _getOutgoingLinks(WikiPage page)
		throws PortalException {

		if (Validator.isNull(page.getContent())) {
			return Collections.emptyMap();
		}

		Map links = new HashMap<>();

		Source source = new Source(page.getContent());

		List startTags = source.getAllStartTags("a");

		for (StartTag startTag : startTags) {
			String href = startTag.getAttributeValue("href");

			if (Validator.isNull(href)) {
				continue;
			}

			int pos = href.lastIndexOf(_friendlyURLMapping);

			if (pos == -1) {
				continue;
			}

			String friendlyURL = href.substring(
				pos + _friendlyURLMapping.length());

			if (friendlyURL.endsWith(StringPool.SLASH)) {
				friendlyURL = friendlyURL.substring(
					0, friendlyURL.length() - 1);
			}

			Map routeParameters = new HashMap<>();

			if (!_router.urlToParameters(friendlyURL, routeParameters)) {
				if (_log.isWarnEnabled()) {
					_log.warn(
						"No route could be found to match URL " + friendlyURL);
				}

				continue;
			}

			String title = routeParameters.get("title");
			String nodeName = routeParameters.get("nodeName");

			if (Validator.isNull(title) || Validator.isNull(nodeName)) {
				continue;
			}

			try {
				_wikiNodeLocalService.getNode(page.getGroupId(), nodeName);

				links.put(StringUtil.toLowerCase(title), Boolean.TRUE);
			}
			catch (NoSuchNodeException noSuchNodeException) {
				if (_log.isWarnEnabled()) {
					_log.warn(noSuchNodeException);
				}
			}
		}

		return links;
	}

	private static final Log _log = LogFactoryUtil.getLog(HtmlEngine.class);

	@Reference(target = "(javax.portlet.name=" + WikiPortletKeys.WIKI + ")")
	private FriendlyURLMapper _friendlyURLMapper;

	private String _friendlyURLMapping;
	private Router _router;

	@Reference(
		target = "(osgi.web.symbolicname=com.liferay.wiki.engine.input.editor.common)"
	)
	private ServletContext _servletContext;

	@Reference
	private WikiGroupServiceConfiguration _wikiGroupServiceConfiguration;

	@Reference
	private WikiNodeLocalService _wikiNodeLocalService;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy