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;
026
027import com.vladsch.flexmark.util.html.Attributes;
028
029
030/**
031 * {@link NodeAttributeProviderState} which sets the attributes for external links.
032 */
033public class ExternalLinkAttributeProviderState implements NodeAttributeProviderState< JSPWikiLink > {
034
035    private final boolean hasRef;
036    private final boolean m_useRelNofollow;
037    private final WikiContext wikiContext;
038    private final LinkParsingOperations linkOperations;
039
040    public ExternalLinkAttributeProviderState( final WikiContext wikiContext, final boolean hasRef ) {
041        this.hasRef = hasRef;
042        this.wikiContext = wikiContext;
043        this.linkOperations = new LinkParsingOperations( wikiContext );
044        this.m_useRelNofollow = wikiContext.getBooleanWikiProperty( MarkupParser.PROP_USERELNOFOLLOW, false );
045    }
046
047    /**
048     * {@inheritDoc}
049     *
050     * @see NodeAttributeProviderState#setAttributes(Attributes, Node)
051     */
052    @Override
053    public void setAttributes( final Attributes attributes, final JSPWikiLink link ) {
054        if( linkOperations.isImageLink( link.getUrl().toString() ) ) {
055            new ImageLinkAttributeProviderState( wikiContext, link.getText().toString(), hasRef ).setAttributes( attributes, link );
056        } else {
057            attributes.replaceValue( "class", MarkupParser.CLASS_EXTERNAL );
058            attributes.replaceValue( "href", link.getUrl().toString() );
059        }
060        if( m_useRelNofollow ) {
061            attributes.replaceValue( "rel", "nofollow" );
062        }
063    }
064
065}