001    /**
002     * Copyright (c) 2000-2012 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            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                            if (pos == -1) {
112                                    continue;
113                            }
114    
115                            String friendlyURL = href.substring(
116                                    pos + _friendlyURLMapping.length());
117    
118                            if (friendlyURL.endsWith(StringPool.SLASH)) {
119                                    friendlyURL = friendlyURL.substring(
120                                            0, friendlyURL.length() - 1);
121                            }
122    
123                            Map<String, String> routeParameters = new HashMap<String, String>();
124    
125                            if (!_router.urlToParameters(friendlyURL, routeParameters)) {
126                                    if (_log.isWarnEnabled()) {
127                                            _log.warn(
128                                                    "No route could be found to match URL " + friendlyURL);
129                                    }
130    
131                                    continue;
132                            }
133    
134                            String title = routeParameters.get("title");
135                            String nodeName = routeParameters.get("nodeName");
136    
137                            if (Validator.isNull(title) || Validator.isNull(nodeName)) {
138                                    continue;
139                            }
140    
141                            try {
142                                    WikiNodeLocalServiceUtil.getNode(page.getGroupId(), nodeName);
143    
144                                    links.put(title.toLowerCase(), Boolean.TRUE);
145                            }
146                            catch (NoSuchNodeException nsne) {
147                                    if (_log.isWarnEnabled()) {
148                                            _log.warn(nsne.getMessage());
149                                    }
150                            }
151                    }
152    
153                    return links;
154            }
155    
156            private static Log _log = LogFactoryUtil.getLog(HtmlEngine.class);
157    
158            private String _friendlyURLMapping;
159            private Router _router;
160    
161    }