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.attributeprovider;
020
021import com.vladsch.flexmark.ast.Link;
022import com.vladsch.flexmark.util.ast.Node;
023import com.vladsch.flexmark.util.html.MutableAttributes;
024import org.apache.oro.text.regex.Pattern;
025import org.apache.wiki.api.core.Context;
026import org.apache.wiki.markdown.nodes.JSPWikiLink;
027import org.apache.wiki.parser.LinkParsingOperations;
028import org.apache.wiki.parser.MarkupParser;
029import org.apache.wiki.util.TextUtil;
030
031import java.util.List;
032
033
034/**
035 * {@link NodeAttributeProviderState} which sets the attributes for interwiki links.
036 */
037public class InterWikiLinkAttributeProviderState implements NodeAttributeProviderState< JSPWikiLink > {
038
039    private final boolean hasRef;
040    private final boolean m_wysiwygEditorMode;
041    private final Context wikiContext;
042    private final LinkParsingOperations linkOperations;
043    private final boolean isImageInlining;
044    private final List< Pattern > inlineImagePatterns;
045
046    public InterWikiLinkAttributeProviderState( final Context wikiContext,
047                                                final boolean hasRef,
048                                                final boolean isImageInlining,
049                                                final List< Pattern > inlineImagePatterns ) {
050        this.hasRef = hasRef;
051        this.wikiContext = wikiContext;
052        this.linkOperations = new LinkParsingOperations( wikiContext );
053        this.isImageInlining = isImageInlining;
054        this.inlineImagePatterns = inlineImagePatterns;
055        final Boolean wysiwygVariable = wikiContext.getVariable( Context.VAR_WYSIWYG_EDITOR_MODE );
056        m_wysiwygEditorMode = wysiwygVariable != null ? wysiwygVariable : false;
057    }
058
059    /**
060     * {@inheritDoc}
061     *
062     * @see NodeAttributeProviderState#setAttributes(MutableAttributes, Node)
063     */
064    @Override
065    public void setAttributes( final MutableAttributes attributes, final JSPWikiLink link ) {
066        final String[] refAndPage = link.getWikiLink().split( ":" );
067        if( !m_wysiwygEditorMode ) {
068            String urlReference = wikiContext.getEngine().getInterWikiURL( refAndPage[ 0 ] );
069            if( urlReference != null ) {
070                urlReference = TextUtil.replaceString( urlReference, "%s", refAndPage[ 1 ] );
071                if( linkOperations.isImageLink( urlReference, isImageInlining, inlineImagePatterns ) ) {
072                    new ImageLinkAttributeProviderState( wikiContext, urlReference, hasRef ).setAttributes( attributes, link );
073                } else {
074                    setInterWikiLinkAttrs( attributes, link, urlReference );
075                }
076            }
077        } else {
078            setInterWikiLinkAttrs( attributes, link, refAndPage[0] + ":" + refAndPage[1] );
079        }
080    }
081
082    void setInterWikiLinkAttrs( final MutableAttributes attributes, final Link link, final String url ) {
083        attributes.replaceValue( "class", MarkupParser.CLASS_INTERWIKI );
084        attributes.replaceValue( "href", url );
085    }
086
087}