001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.markdown.extensions.jspwikilinks.postprocessor;
020
021import com.vladsch.flexmark.ast.Link;
022import com.vladsch.flexmark.parser.PostProcessor;
023import com.vladsch.flexmark.parser.block.NodePostProcessor;
024import com.vladsch.flexmark.util.ast.Document;
025import com.vladsch.flexmark.util.ast.Node;
026import com.vladsch.flexmark.util.ast.NodeTracker;
027import org.apache.commons.lang3.StringUtils;
028import org.apache.wiki.WikiContext;
029import org.apache.wiki.markdown.nodes.JSPWikiLink;
030import org.apache.wiki.parser.LinkParsingOperations;
031import org.apache.wiki.parser.MarkupParser;
032import org.apache.wiki.util.TextUtil;
033
034
035/**
036 * {@link NodePostProcessor} to convert {@link Link}s into {@link JSPWikiLink}s.
037 *
038 * Acts as a factory of {@link NodePostProcessorState}, which are the classes generating the extra markup for each concrete type of link.
039 */
040public class JSPWikiLinkNodePostProcessor extends NodePostProcessor {
041
042    protected final WikiContext m_context;
043    protected final LinkParsingOperations linkOperations;
044    protected boolean m_useOutlinkImage = true;
045    protected final Document document;
046
047    public JSPWikiLinkNodePostProcessor( final WikiContext m_context, final Document document ) {
048        this.m_context = m_context;
049        this.document = document;
050        linkOperations = new LinkParsingOperations( m_context );
051        m_useOutlinkImage = m_context.getBooleanWikiProperty( MarkupParser.PROP_USEOUTLINKIMAGE, m_useOutlinkImage );
052    }
053
054    /**
055     * {@inheritDoc}
056     *
057     * @see PostProcessor#process(NodeTracker, Node)
058     */
059    @Override
060    public void process( final NodeTracker state, final Node node ) {
061        if( node instanceof Link ) {
062            JSPWikiLink link = replaceLinkWithJSPWikiLink( state, node );
063
064            final NodePostProcessorState< JSPWikiLink > linkPostProcessor;
065            if( linkOperations.isAccessRule( link.getUrl().toString() ) ) {
066                linkPostProcessor = new AccessRuleLinkNodePostProcessorState( m_context );
067            } else if( linkOperations.isMetadata( link.getUrl().toString() ) ) {
068                linkPostProcessor = new MetadataLinkNodePostProcessorState( m_context );
069            } else if( linkOperations.isPluginLink( link.getUrl().toString() ) ) {
070                linkPostProcessor = new PluginLinkNodePostProcessorState( m_context );
071            } else if( linkOperations.isVariableLink( link.getUrl().toString() ) ) {
072                linkPostProcessor = new VariableLinkNodePostProcessorState( m_context );
073            } else if( linkOperations.isExternalLink( link.getUrl().toString() ) ) {
074                linkPostProcessor = new ExternalLinkNodePostProcessorState( m_context );
075            } else if( linkOperations.isInterWikiLink( link.getUrl().toString() ) ) {
076                linkPostProcessor = new InterWikiLinkNodePostProcessorState( m_context, document );
077            } else if( StringUtils.startsWith( link.getUrl().toString(), "#" ) ) {
078                linkPostProcessor = new LocalFootnoteLinkNodePostProcessorState( m_context );
079            } else if( TextUtil.isNumber( link.getUrl().toString() ) ) {
080                linkPostProcessor = new LocalFootnoteRefLinkNodePostProcessorState( m_context );
081            } else {
082                linkPostProcessor = new LocalLinkNodePostProcessorState( m_context );
083            }
084            linkPostProcessor.process( state, link );
085        }
086    }
087
088    JSPWikiLink replaceLinkWithJSPWikiLink( final NodeTracker state, final Node node ) {
089        JSPWikiLink link = new JSPWikiLink( ( Link )node );
090        Node previous = node.getPrevious();
091        Node parent = node.getParent();
092
093        link.takeChildren( node );
094        node.unlink();
095
096        if( previous != null ) {
097            previous.insertAfter( link );
098        } else {
099            parent.appendChild( link );
100        }
101
102        state.nodeRemoved( node );
103        state.nodeAddedWithChildren( link );
104        return link;
105    }
106
107}