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.antlrwiki;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.parsers.creole.ast.ASTNode;
021    import com.liferay.portal.parsers.creole.ast.WikiPageNode;
022    import com.liferay.portal.parsers.creole.ast.link.LinkNode;
023    import com.liferay.portal.parsers.creole.parser.Creole10Lexer;
024    import com.liferay.portal.parsers.creole.parser.Creole10Parser;
025    import com.liferay.portal.parsers.creole.visitor.impl.LinkNodeCollectorVisitor;
026    import com.liferay.portlet.wiki.PageContentException;
027    import com.liferay.portlet.wiki.engines.WikiEngine;
028    import com.liferay.portlet.wiki.engines.antlrwiki.translator.XhtmlTranslator;
029    import com.liferay.portlet.wiki.model.WikiPage;
030    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
031    
032    import java.util.HashMap;
033    import java.util.List;
034    import java.util.Map;
035    
036    import javax.portlet.PortletURL;
037    
038    import org.antlr.runtime.ANTLRStringStream;
039    import org.antlr.runtime.CommonTokenStream;
040    import org.antlr.runtime.RecognitionException;
041    
042    /**
043     * @author Miguel Pastor
044     */
045    public class CreoleWikiEngine implements WikiEngine {
046    
047            @Override
048            public String convert(
049                    WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
050                    String attachmentURLPrefix) {
051    
052                    XhtmlTranslator xhtmlTranslator = new XhtmlTranslator();
053    
054                    return xhtmlTranslator.translate(
055                            page, viewPageURL, editPageURL, attachmentURLPrefix,
056                            parse(page.getContent()));
057            }
058    
059            @Override
060            public Map<String, Boolean> getOutgoingLinks(WikiPage page)
061                    throws PageContentException {
062    
063                    Map<String, Boolean> outgoingLinks = new HashMap<String, Boolean>();
064    
065                    LinkNodeCollectorVisitor linkNodeCollectorVisitor =
066                            new LinkNodeCollectorVisitor();
067    
068                    List<ASTNode> astNodes = linkNodeCollectorVisitor.collect(
069                            parse(page.getContent()));
070    
071                    try {
072                            for (ASTNode astNode : astNodes) {
073                                    LinkNode linkNode = (LinkNode)astNode;
074    
075                                    String title = linkNode.getLink();
076    
077                                    boolean existingLink = false;
078    
079                                    if (WikiPageLocalServiceUtil.getPagesCount(
080                                                    page.getNodeId(), title, true) > 0) {
081    
082                                            existingLink = true;
083                                    }
084    
085                                    if (existingLink) {
086                                            title = title.toLowerCase();
087                                    }
088    
089                                    outgoingLinks.put(title, existingLink);
090                            }
091                    }
092                    catch (SystemException se) {
093                            throw new PageContentException(se);
094                    }
095    
096                    return outgoingLinks;
097            }
098    
099            @Override
100            public void setInterWikiConfiguration(String interWikiConfiguration) {
101            }
102    
103            @Override
104            public void setMainConfiguration(String mainConfiguration) {
105            }
106    
107            @Override
108            public boolean validate(long nodeId, String content) {
109                    return true;
110            }
111    
112            protected Creole10Parser build(String creoleCode) {
113                    ANTLRStringStream antlrStringStream = new ANTLRStringStream(creoleCode);
114    
115                    Creole10Lexer creole10Lexer = new Creole10Lexer(antlrStringStream);
116    
117                    CommonTokenStream commonTokenStream = new CommonTokenStream(
118                            creole10Lexer);
119    
120                    return new Creole10Parser(commonTokenStream);
121            }
122    
123            protected WikiPageNode parse(String creoleCode) {
124                    Creole10Parser creole10Parser = build(creoleCode);
125    
126                    try {
127                            creole10Parser.wikipage();
128                    }
129                    catch (RecognitionException re) {
130                            if (_log.isDebugEnabled()) {
131                                    _log.debug("Unable to parse:\n" + creoleCode, re);
132    
133                                    for (String error : creole10Parser.getErrors()) {
134                                            _log.debug(error);
135                                    }
136                            }
137                    }
138    
139                    return creole10Parser.getWikiPageNode();
140            }
141    
142            private static Log _log = LogFactoryUtil.getLog(CreoleWikiEngine.class);
143    
144    }