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.translator.internal;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.parsers.creole.ast.BoldTextNode;
019    import com.liferay.portal.parsers.creole.ast.FormattedTextNode;
020    import com.liferay.portal.parsers.creole.ast.ItalicTextNode;
021    import com.liferay.portal.parsers.creole.ast.NoWikiSectionNode;
022    import com.liferay.portal.parsers.creole.ast.UnformattedTextNode;
023    import com.liferay.portal.parsers.creole.ast.link.LinkNode;
024    import com.liferay.portal.parsers.creole.visitor.impl.BaseASTVisitor;
025    
026    /**
027     * @author Miguel Pastor
028     */
029    public abstract class UnformattedTextVisitor extends BaseASTVisitor {
030    
031            public String getText() {
032                    return _sb.toString();
033            }
034    
035            @Override
036            public void visit(BoldTextNode boldTextNode) {
037                    if (boldTextNode.getContent() != null) {
038                            write(boldTextNode.getContent());
039                    }
040                    else {
041                            super.visit(boldTextNode);
042                    }
043            }
044    
045            @Override
046            public void visit(FormattedTextNode formattedTextNode) {
047                    if (formattedTextNode.getContent() != null) {
048                            write(formattedTextNode.getContent());
049                    }
050                    else {
051                            super.visit(formattedTextNode);
052                    }
053            }
054    
055            @Override
056            public void visit(ItalicTextNode italicTextNode) {
057                    if (italicTextNode.getContent() != null) {
058                            write(italicTextNode.getContent());
059                    }
060                    else {
061                            super.visit(italicTextNode);
062                    }
063            }
064    
065            @Override
066            public void visit(LinkNode linkNode) {
067                    String link = linkNode.getLink();
068    
069                    if (link != null) {
070                            write(link);
071                    }
072    
073                    super.visit(linkNode);
074            }
075    
076            @Override
077            public void visit(NoWikiSectionNode noWikiSectionNode) {
078                    write(noWikiSectionNode.getContent());
079            }
080    
081            @Override
082            public void visit(UnformattedTextNode unformattedTextNode) {
083                    if (unformattedTextNode.hasContent()) {
084                            write(unformattedTextNode.getContent());
085                    }
086                    else {
087                            super.visit(unformattedTextNode);
088                    }
089            }
090    
091            protected void write(String text) {
092                    _sb.append(text);
093            }
094    
095            private StringBundler _sb = new StringBundler();
096    
097    }