    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 com.vladsch.flexmark.util.html.MutableAttributes;
    023import org.apache.oro.text.regex.Pattern;
    024import org.apache.wiki.api.core.Context;
    025import org.apache.wiki.markdown.nodes.JSPWikiLink;
    026import org.apache.wiki.parser.LinkParsingOperations;
    027import org.apache.wiki.parser.MarkupParser;
    028
    029import java.util.List;
    030
    031
    032/**
    033 * {@link NodeAttributeProviderState} which sets the attributes for external links.
    034 */
    035public class ExternalLinkAttributeProviderState implements NodeAttributeProviderState< JSPWikiLink > {
    036
    037    private final boolean hasRef;
    038    private final boolean m_useRelNofollow;
    039    private final Context wikiContext;
    040    private final LinkParsingOperations linkOperations;
    041    private final boolean isImageInlining;
    042    private final List< Pattern > inlineImagePatterns;
    043
    044    public ExternalLinkAttributeProviderState( final Context wikiContext,
    045                                               final boolean hasRef,
    046                                               final boolean isImageInlining,
    047                                               final List< Pattern > inlineImagePatterns ) {
    048        this.hasRef = hasRef;
    049        this.wikiContext = wikiContext;
    050        this.linkOperations = new LinkParsingOperations( wikiContext );
    051        this.isImageInlining = isImageInlining;
    052        this.inlineImagePatterns = inlineImagePatterns;
    053        this.m_useRelNofollow = wikiContext.getBooleanWikiProperty( MarkupParser.PROP_USERELNOFOLLOW, false );
    054    }
    055
    056    /**
    057     * {@inheritDoc}
    058     *
    059     * @see NodeAttributeProviderState#setAttributes(MutableAttributes, Node)
    060     */
    061    @Override
    062    public void setAttributes( final MutableAttributes attributes, final JSPWikiLink link ) {
    063        if( linkOperations.isImageLink( link.getUrl().toString(), isImageInlining, inlineImagePatterns ) ) {
    064            new ImageLinkAttributeProviderState( wikiContext, link.getText().toString(), hasRef ).setAttributes( attributes, link );
    065        } else {
    066            attributes.replaceValue( "class", MarkupParser.CLASS_EXTERNAL );
    067            attributes.replaceValue( "href", link.getUrl().toString() );
    068        }
    069        if( m_useRelNofollow ) {
    070            attributes.replaceValue( "rel", "nofollow" );
    071        }
    072    }
    073
    074}