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