    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.util.ast.Document;
    022import com.vladsch.flexmark.util.ast.Node;
    023import com.vladsch.flexmark.util.ast.NodeTracker;
    024import com.vladsch.flexmark.util.sequence.CharSubSequence;
    025import org.apache.logging.log4j.LogManager;
    026import org.apache.logging.log4j.Logger;
    027import org.apache.oro.text.regex.Pattern;
    028import org.apache.wiki.api.core.Context;
    029import org.apache.wiki.i18n.InternationalizationManager;
    030import org.apache.wiki.markdown.nodes.JSPWikiLink;
    031import org.apache.wiki.parser.LinkParsingOperations;
    032import org.apache.wiki.parser.MarkupParser;
    033import org.apache.wiki.preferences.Preferences;
    034import org.apache.wiki.util.TextUtil;
    035
    036import java.text.MessageFormat;
    037import java.util.List;
    038import java.util.ResourceBundle;
    039
    040
    041/**
    042 * {@link NodePostProcessorState} which further post processes interwiki links.
    043 */
    044public class InterWikiLinkNodePostProcessorState implements NodePostProcessorState< JSPWikiLink > {
    045
    046    private static final Logger LOG = LogManager.getLogger( InterWikiLinkNodePostProcessorState.class );
    047    private final Context wikiContext;
    048    private final LinkParsingOperations linkOperations;
    049    private final boolean isImageInlining;
    050    private final List< Pattern > inlineImagePatterns;
    051    private final Document document;
    052    private final boolean m_wysiwygEditorMode;
    053    private boolean m_useOutlinkImage = true;
    054
    055    public InterWikiLinkNodePostProcessorState( final Context wikiContext,
    056                                                final Document document,
    057                                                final boolean isImageInlining,
    058                                                final List< Pattern > inlineImagePatterns ) {
    059        this.wikiContext = wikiContext;
    060        this.linkOperations = new LinkParsingOperations( wikiContext );
    061        this.isImageInlining = isImageInlining;
    062        this.inlineImagePatterns = inlineImagePatterns;
    063        this.document = document;
    064        this.m_useOutlinkImage = wikiContext.getBooleanWikiProperty( MarkupParser.PROP_USEOUTLINKIMAGE, m_useOutlinkImage );
    065        final Boolean wysiwygVariable = wikiContext.getVariable( Context.VAR_WYSIWYG_EDITOR_MODE );
    066        m_wysiwygEditorMode = wysiwygVariable != null ? wysiwygVariable : false;
    067    }
    068
    069    /**
    070     * {@inheritDoc}
    071     *
    072     * @see NodePostProcessorState#process(NodeTracker, Node)
    073     */
    074    @Override
    075    public void process( final NodeTracker state, final JSPWikiLink link ) {
    076        final String[] refAndPage = link.getUrl().toString().split( ":" );
    077        if( !m_wysiwygEditorMode ) {
    078            String urlReference = wikiContext.getEngine().getInterWikiURL( refAndPage[ 0 ] );
    079            if( urlReference != null ) {
    080                urlReference = TextUtil.replaceString( urlReference, "%s", refAndPage[ 1 ] );
    081                if( linkOperations.isImageLink( urlReference, isImageInlining, inlineImagePatterns ) ) {
    082                    new ImageLinkNodePostProcessorState( wikiContext, urlReference, link.hasRef() ).process( state, link );
    083                } else {
    084                    link.setUrl( CharSubSequence.of( urlReference ) );
    085                }
    086                if( linkOperations.isExternalLink( urlReference ) ) {
    087                    NodePostProcessorStateCommonOperations.addOutlinkImage( state, link, wikiContext, m_useOutlinkImage );
    088                }
    089            } else {
    090                LOG.debug( refAndPage[0] + " not recognized as InterWiki link [document node: " + document + "]" );
    091                final Object[] args = { refAndPage[ 0 ] };
    092                final ResourceBundle rb = Preferences.getBundle( wikiContext, InternationalizationManager.CORE_BUNDLE );
    093                final String errMsg = MessageFormat.format( rb.getString( "markupparser.error.nointerwikiref" ), args );
    094                NodePostProcessorStateCommonOperations.makeError( state, link, errMsg );
    095            }
    096        } else {
    097            link.setUrl( CharSubSequence.of( refAndPage[0] + ":" + refAndPage[1] ) );
    098        }
    099    }
    100
    101}