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