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.antlrwiki.translator;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.TreeNode;
025    import com.liferay.portal.parsers.creole.ast.CollectionNode;
026    import com.liferay.portal.parsers.creole.ast.HeadingNode;
027    import com.liferay.portal.parsers.creole.ast.ImageNode;
028    import com.liferay.portal.parsers.creole.ast.WikiPageNode;
029    import com.liferay.portal.parsers.creole.ast.extension.TableOfContentsNode;
030    import com.liferay.portal.parsers.creole.ast.link.LinkNode;
031    import com.liferay.portal.parsers.creole.visitor.impl.XhtmlTranslationVisitor;
032    import com.liferay.portlet.wiki.NoSuchPageException;
033    import com.liferay.portlet.wiki.engines.antlrwiki.translator.internal.UnformattedHeadingTextVisitor;
034    import com.liferay.portlet.wiki.engines.antlrwiki.translator.internal.UnformattedLinksTextVisitor;
035    import com.liferay.portlet.wiki.model.WikiPage;
036    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
037    
038    import java.util.List;
039    
040    import javax.portlet.PortletURL;
041    
042    /**
043     * @author Miguel Pastor
044     */
045    public class XhtmlTranslator extends XhtmlTranslationVisitor {
046    
047            public String translate(
048                    WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
049                    String attachmentURLPrefix, WikiPageNode wikiPageNode) {
050    
051                    _page = page;
052                    _viewPageURL = viewPageURL;
053                    _editPageURL = editPageURL;
054                    _attachmentURLPrefix = attachmentURLPrefix;
055                    _rootWikiPageNode = wikiPageNode;
056    
057                    return super.translate(wikiPageNode);
058            }
059    
060            @Override
061            public void visit(HeadingNode headingNode) {
062                    append("<h");
063                    append(headingNode.getLevel());
064    
065                    String unformattedText = getUnformattedHeadingText(headingNode);
066    
067                    String markup = getHeadingMarkup(_page.getTitle(), unformattedText);
068    
069                    append(" id=\"");
070                    append(markup);
071                    append("\">");
072    
073                    traverse(headingNode.getChildASTNodes());
074    
075                    append("<a class=\"hashlink\" href=\"");
076    
077                    if (_viewPageURL != null) {
078                            append(_viewPageURL.toString());
079                    }
080    
081                    append(StringPool.POUND);
082                    append(markup);
083                    append("\">#</a></h");
084                    append(headingNode.getLevel());
085                    append(">");
086            }
087    
088            @Override
089            public void visit(ImageNode imageNode) {
090                    append("<img");
091    
092                    if (imageNode.hasAltCollectionNode()) {
093                            append(" alt=\"");
094    
095                            CollectionNode altCollectionNode = imageNode.getAltNode();
096    
097                            traverse(altCollectionNode.getASTNodes());
098    
099                            append(StringPool.QUOTE);
100                    }
101    
102                    append(" src=\"");
103    
104                    if (imageNode.isAbsoluteLink()) {
105                            append(imageNode.getLink());
106                    }
107                    else {
108                            append(_attachmentURLPrefix);
109                            append(imageNode.getLink());
110                    }
111    
112                    append("\" />");
113            }
114    
115            @Override
116            public void visit(LinkNode linkNode) {
117                    append("<a href=\"");
118    
119                    appendHref(linkNode);
120    
121                    append("\">");
122    
123                    if (linkNode.hasAltCollectionNode()) {
124                            CollectionNode altCollectionNode = linkNode.getAltCollectionNode();
125    
126                            traverse(altCollectionNode.getASTNodes());
127                    }
128                    else {
129                            append(HtmlUtil.escape(linkNode.getLink()));
130                    }
131    
132                    append("</a>");
133            }
134    
135            @Override
136            public void visit(TableOfContentsNode tableOfContentsNode) {
137                    TableOfContentsVisitor tableOfContentsVisitor =
138                            new TableOfContentsVisitor();
139    
140                    TreeNode<HeadingNode> tableOfContents = tableOfContentsVisitor.compose(
141                            _rootWikiPageNode);
142    
143                    append("<div class=\"toc\">");
144                    append("<div class=\"collapsebox\">");
145                    append("<h4>Table of Contents");
146                    append(StringPool.NBSP);
147                    append("<a class=\"toc-trigger\" href=\"javascript:;\">[-]</a></h4>");
148                    append("<div class=\"toc-index\">");
149    
150                    appendTableOfContents(tableOfContents, 1);
151    
152                    append("</div>");
153                    append("</div>");
154                    append("</div>");
155            }
156    
157            protected void appendAbsoluteHref(LinkNode linkNode) {
158                    append(HtmlUtil.escape(linkNode.getLink()));
159            }
160    
161            protected void appendHref(LinkNode linkNode) {
162                    if (linkNode.getLink() == null) {
163                            UnformattedLinksTextVisitor unformattedLinksTextVisitor =
164                                    new UnformattedLinksTextVisitor();
165    
166                            linkNode.setLink(
167                                    unformattedLinksTextVisitor.getUnformattedText(linkNode));
168                    }
169    
170                    if (linkNode.isAbsoluteLink()) {
171                            appendAbsoluteHref(linkNode);
172                    }
173                    else {
174                            appendWikiHref(linkNode);
175                    }
176            }
177    
178            protected void appendTableOfContents(
179                    TreeNode<HeadingNode> tableOfContents, int depth) {
180    
181                    append("<ol>");
182    
183                    List<TreeNode<HeadingNode>> treeNodes = tableOfContents.getChildNodes();
184    
185                    if (treeNodes != null) {
186                            for (TreeNode<HeadingNode> treeNode : treeNodes) {
187                                    append("<li class=\"toc-level-");
188                                    append(depth);
189                                    append("\">");
190    
191                                    HeadingNode headingNode = treeNode.getValue();
192    
193                                    String content = getUnformattedHeadingText(headingNode);
194    
195                                    append("<a class=\"wikipage\" href=\"");
196    
197                                    if (_viewPageURL != null) {
198                                            append(_viewPageURL.toString());
199                                    }
200    
201                                    append(StringPool.POUND);
202                                    append(getHeadingMarkup(_page.getTitle(), content));
203                                    append("\">");
204                                    append(content);
205                                    append("</a>");
206    
207                                    appendTableOfContents(treeNode, depth + 1);
208    
209                                    append("</li>");
210                            }
211                    }
212    
213                    append("</ol>");
214            }
215    
216            protected void appendWikiHref(LinkNode linkNode) {
217                    WikiPage page = null;
218    
219                    try {
220                            page = WikiPageLocalServiceUtil.getPage(
221                                    _page.getNodeId(), linkNode.getLink());
222                    }
223                    catch (NoSuchPageException nspe) {
224                    }
225                    catch (Exception e) {
226                            _log.error(e, e);
227                    }
228    
229                    String attachmentLink = searchLinkInAttachments(linkNode);
230    
231                    if (attachmentLink != null) {
232    
233                            // Attachment links take precedence over pages
234    
235                            append(_attachmentURLPrefix + attachmentLink);
236    
237                            return;
238                    }
239    
240                    String pageTitle = linkNode.getLink();
241    
242                    if ((page != null) && (_viewPageURL != null)) {
243                            _viewPageURL.setParameter("title", pageTitle);
244    
245                            append(_viewPageURL.toString());
246    
247                            _viewPageURL.setParameter("title", _page.getTitle());
248                    }
249                    else if (_editPageURL != null) {
250                            _editPageURL.setParameter("title", pageTitle);
251    
252                            append(_editPageURL.toString());
253    
254                            _editPageURL.setParameter("title", _page.getTitle());
255                    }
256            }
257    
258            protected String getHeadingMarkup(String prefix, String text) {
259                    StringBundler sb = new StringBundler(5);
260    
261                    sb.append(_HEADING_ANCHOR_PREFIX);
262                    sb.append(prefix);
263                    sb.append(StringPool.DASH);
264                    sb.append(text.trim());
265    
266                    return StringUtil.replace(
267                            sb.toString(), StringPool.SPACE, StringPool.PLUS);
268            }
269    
270            protected String getUnformattedHeadingText(HeadingNode headingNode) {
271                    UnformattedHeadingTextVisitor unformattedHeadingTextVisitor =
272                            new UnformattedHeadingTextVisitor();
273    
274                    return unformattedHeadingTextVisitor.getUnformattedText(headingNode);
275            }
276    
277            protected String searchLinkInAttachments(LinkNode linkNode) {
278                    try {
279                            for (FileEntry fileEntry : _page.getAttachmentsFileEntries()) {
280                                    String title = fileEntry.getTitle();
281    
282                                    if (title.equals(linkNode.getLink())) {
283                                            return title;
284                                    }
285                            }
286                    }
287                    catch (Exception e) {
288                    }
289    
290                    return null;
291            }
292    
293            private static final String _HEADING_ANCHOR_PREFIX = "section-";
294    
295            private static Log _log = LogFactoryUtil.getLog(XhtmlTranslator.class);
296    
297            private String _attachmentURLPrefix;
298            private PortletURL _editPageURL;
299            private WikiPage _page;
300            private WikiPageNode _rootWikiPageNode;
301            private PortletURL _viewPageURL;
302    
303    }