001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
043     * @author Jorge Ferrer
044     * @author Zsigmond Rab
045     */
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            @Override
062            public String convert(
063                    WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
064                    String attachmentURLPrefix) {
065    
066                    return page.getContent();
067            }
068    
069            @Override
070            public Map<String, Boolean> getOutgoingLinks(WikiPage page)
071                    throws PageContentException {
072    
073                    try {
074                            return _getOutgoingLinks(page);
075                    }
076                    catch (Exception e) {
077                            throw new PageContentException(e);
078                    }
079            }
080    
081            @Override
082            public void setInterWikiConfiguration(String interWikiConfiguration) {
083            }
084    
085            @Override
086            public void setMainConfiguration(String mainConfiguration) {
087            }
088    
089            @Override
090            public boolean validate(long nodeId, String newContent) {
091                    return true;
092            }
093    
094            private Map<String, Boolean> _getOutgoingLinks(WikiPage page)
095                    throws Exception {
096    
097                    if (Validator.isNull(page.getContent())) {
098                            return Collections.emptyMap();
099                    }
100    
101                    Map<String, Boolean> links = new HashMap<String, Boolean>();
102    
103                    Source source = new Source(page.getContent());
104    
105                    List<StartTag> startTags = source.getAllStartTags("a");
106    
107                    for (StartTag startTag : startTags) {
108                            String href = startTag.getAttributeValue("href");
109    
110                            if (Validator.isNull(href)) {
111                                    continue;
112                            }
113    
114                            int pos = href.lastIndexOf(_friendlyURLMapping);
115    
116                            if (pos == -1) {
117                                    continue;
118                            }
119    
120                            String friendlyURL = href.substring(
121                                    pos + _friendlyURLMapping.length());
122    
123                            if (friendlyURL.endsWith(StringPool.SLASH)) {
124                                    friendlyURL = friendlyURL.substring(
125                                            0, friendlyURL.length() - 1);
126                            }
127    
128                            Map<String, String> routeParameters = new HashMap<String, String>();
129    
130                            if (!_router.urlToParameters(friendlyURL, routeParameters)) {
131                                    if (_log.isWarnEnabled()) {
132                                            _log.warn(
133                                                    "No route could be found to match URL " + friendlyURL);
134                                    }
135    
136                                    continue;
137                            }
138    
139                            String title = routeParameters.get("title");
140                            String nodeName = routeParameters.get("nodeName");
141    
142                            if (Validator.isNull(title) || Validator.isNull(nodeName)) {
143                                    continue;
144                            }
145    
146                            try {
147                                    WikiNodeLocalServiceUtil.getNode(page.getGroupId(), nodeName);
148    
149                                    links.put(title.toLowerCase(), Boolean.TRUE);
150                            }
151                            catch (NoSuchNodeException nsne) {
152                                    if (_log.isWarnEnabled()) {
153                                            _log.warn(nsne.getMessage());
154                                    }
155                            }
156                    }
157    
158                    return links;
159            }
160    
161            private static Log _log = LogFactoryUtil.getLog(HtmlEngine.class);
162    
163            private String _friendlyURLMapping;
164            private Router _router;
165    
166    }