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