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;
025
026import com.vladsch.flexmark.util.html.Attributes;
027
028
029/**
030 * {@link NodeAttributeProviderState} which sets the attributes for image links.
031 */
032public class ImageLinkAttributeProviderState implements NodeAttributeProviderState< JSPWikiLink > {
033
034    private final boolean isLinkFromText;
035    private final LinkParsingOperations linkOperations;
036    private final WikiContext wikiContext;
037    private final String urlRef;
038
039    public ImageLinkAttributeProviderState( final WikiContext wikiContext, final String urlRef, final boolean isLinkFromText ) {
040        this.isLinkFromText = isLinkFromText;
041        this.urlRef = urlRef;
042        this.wikiContext = wikiContext;
043        this.linkOperations = new LinkParsingOperations( wikiContext );
044    }
045
046    /**
047     * {@inheritDoc}
048     *
049     * @see NodeAttributeProviderState#setAttributes(Attributes, JSPWikiLink)
050     */
051    @Override
052    public void setAttributes( final Attributes attributes, final JSPWikiLink link ) {
053        if( isLinkFromText && linkOperations.isExternalLink( link.getText().toString() ) ) {
054            attributes.replaceValue( "class", MarkupParser.CLASS_EXTERNAL );
055            attributes.replaceValue( "href", urlRef );
056        } else if ( isLinkFromText && linkOperations.linkExists( link.getText().toString() ) ) {
057            final String pagelink = wikiContext.getURL( WikiContext.VIEW, link.getText().toString() );
058            attributes.replaceValue( "class", MarkupParser.CLASS_WIKIPAGE );
059            attributes.replaceValue( "href", pagelink );
060        }
061    }
062
063}