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;
028import com.vladsch.flexmark.util.sequence.CharSubSequence;
029
030
031/**
032 * {@link NodeAttributeProviderState} which sets the attributes for local links.
033 */
034public class LocalLinkAttributeProviderState implements NodeAttributeProviderState< JSPWikiLink > {
035
036    private final boolean hasRef;
037    private final WikiContext wikiContext;
038    private final LinkParsingOperations linkOperations;
039
040    public LocalLinkAttributeProviderState( final WikiContext wikiContext, final boolean hasRef ) {
041        this.hasRef = hasRef;
042        this.wikiContext = wikiContext;
043        this.linkOperations = new LinkParsingOperations( wikiContext );
044    }
045
046    /**
047     * {@inheritDoc}
048     *
049     * @see NodeAttributeProviderState#setAttributes(Attributes, Node)
050     */
051    @Override
052    public void setAttributes( final Attributes attributes, final JSPWikiLink link ) {
053        final int hashMark = link.getUrl().toString().indexOf( '#' );
054        final String attachment = wikiContext.getEngine().getAttachmentManager().getAttachmentInfoName( wikiContext, link.getWikiLink() );
055        if( attachment != null ) {
056            if( !linkOperations.isImageLink( link.getUrl().toString() ) ) {
057                attributes.replaceValue( "class", MarkupParser.CLASS_ATTACHMENT );
058                final String attlink = wikiContext.getURL( WikiContext.ATTACH, link.getWikiLink() );
059                attributes.replaceValue( "href", attlink );
060            } else {
061                new ImageLinkAttributeProviderState( wikiContext, attachment, hasRef ).setAttributes( attributes, link );
062            }
063        } else if( hashMark != -1 ) { // It's an internal Wiki link, but to a named section
064            final String namedSection = link.getUrl().toString().substring( hashMark + 1 );
065            final String matchedLink = linkOperations.linkIfExists( link.getUrl().toString() );
066            if( matchedLink != null ) {
067                String sectref = "#section-" + wikiContext.getEngine().encodeName( matchedLink + "-" + MarkupParser.wikifyLink( namedSection ) );
068                sectref = sectref.replace('%', '_');
069                link.setUrl( CharSubSequence.of( link.getUrl().toString() + sectref ) );
070                new LocalReadLinkAttributeProviderState( wikiContext ).setAttributes( attributes, link );
071            } else {
072                new LocalEditLinkAttributeProviderState( wikiContext, link.getWikiLink() ).setAttributes( attributes, link );
073            }
074        } else {
075            if( linkOperations.linkExists( link.getWikiLink() ) ) {
076                new LocalReadLinkAttributeProviderState( wikiContext ).setAttributes( attributes, link );
077            } else {
078                new LocalEditLinkAttributeProviderState( wikiContext, link.getWikiLink() ).setAttributes( attributes, link );
079            }
080        }
081    }
082
083}