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