001
014
015 package com.liferay.portlet.wiki.engines;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
020 import com.liferay.portal.kernel.portlet.Router;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.model.Portlet;
024 import com.liferay.portal.service.PortletLocalServiceUtil;
025 import com.liferay.portal.util.Portal;
026 import com.liferay.portal.util.PortletKeys;
027 import com.liferay.portlet.wiki.NoSuchNodeException;
028 import com.liferay.portlet.wiki.PageContentException;
029 import com.liferay.portlet.wiki.model.WikiPage;
030 import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
031
032 import java.util.Collections;
033 import java.util.HashMap;
034 import java.util.List;
035 import java.util.Map;
036
037 import javax.portlet.PortletURL;
038
039 import net.htmlparser.jericho.Source;
040 import net.htmlparser.jericho.StartTag;
041
042
046 public class HtmlEngine implements WikiEngine {
047
048 public HtmlEngine() {
049 Portlet portlet = PortletLocalServiceUtil.getPortletById(
050 PortletKeys.WIKI);
051
052 _friendlyURLMapping =
053 Portal.FRIENDLY_URL_SEPARATOR + portlet.getFriendlyURLMapping();
054
055 FriendlyURLMapper friendlyURLMapper =
056 portlet.getFriendlyURLMapperInstance();
057
058 _router = friendlyURLMapper.getRouter();
059 }
060
061 public String convert(
062 WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
063 String attachmentURLPrefix) {
064
065 return page.getContent();
066 }
067
068 public Map<String, Boolean> getOutgoingLinks(WikiPage page)
069 throws PageContentException {
070
071 try {
072 return _getOutgoingLinks(page);
073 }
074 catch (Exception e) {
075 throw new PageContentException(e);
076 }
077 }
078
079 public void setInterWikiConfiguration(String interWikiConfiguration) {
080 }
081
082 public void setMainConfiguration(String mainConfiguration) {
083 }
084
085 public boolean validate(long nodeId, String newContent) {
086 return true;
087 }
088
089 private Map<String, Boolean> _getOutgoingLinks(WikiPage page)
090 throws Exception {
091
092 if (Validator.isNull(page.getContent())) {
093 return Collections.emptyMap();
094 }
095
096 Map<String, Boolean> links = new HashMap<String, Boolean>();
097
098 Source source = new Source(page.getContent());
099
100 List<StartTag> startTags = source.getAllStartTags("a");
101
102 for (StartTag startTag : startTags) {
103 String href = startTag.getAttributeValue("href");
104
105 if (Validator.isNull(href)) {
106 continue;
107 }
108
109 int pos = href.lastIndexOf(_friendlyURLMapping);
110
111 String friendlyURL = href.substring(
112 pos + _friendlyURLMapping.length());
113
114 if (friendlyURL.endsWith(StringPool.SLASH)) {
115 friendlyURL = friendlyURL.substring(
116 0, friendlyURL.length() - 1);
117 }
118
119 Map<String, String> routeParameters = new HashMap<String, String>();
120
121 if (!_router.urlToParameters(friendlyURL, routeParameters)) {
122 if (_log.isWarnEnabled()) {
123 _log.warn(
124 "No route could be found to match URL " + friendlyURL);
125 }
126
127 continue;
128 }
129
130 String title = routeParameters.get("title");
131 String nodeName = routeParameters.get("nodeName");
132
133 if (Validator.isNull(title) || Validator.isNull(nodeName)) {
134 continue;
135 }
136
137 try {
138 WikiNodeLocalServiceUtil.getNode(page.getGroupId(), nodeName);
139
140 links.put(title.toLowerCase(), Boolean.TRUE);
141 }
142 catch (NoSuchNodeException nsne) {
143 if (_log.isWarnEnabled()) {
144 _log.warn(nsne.getMessage());
145 }
146 }
147 }
148
149 return links;
150 }
151
152 private static Log _log = LogFactoryUtil.getLog(HtmlEngine.class);
153
154 private String _friendlyURLMapping;
155 private Router _router;
156
157 }