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 com.vladsch.flexmark.util.sequence.CharSubSequence;
024import org.apache.oro.text.regex.Pattern;
025import org.apache.wiki.api.core.Context;
026import org.apache.wiki.api.core.ContextEnum;
027import org.apache.wiki.attachment.AttachmentManager;
028import org.apache.wiki.markdown.nodes.JSPWikiLink;
029import org.apache.wiki.parser.LinkParsingOperations;
030import org.apache.wiki.parser.MarkupParser;
031
032import java.util.List;
033
034
035/**
036 * {@link NodeAttributeProviderState} which sets the attributes for local links.
037 */
038public class LocalLinkAttributeProviderState implements NodeAttributeProviderState< JSPWikiLink > {
039
040    private final boolean hasRef;
041    private final Context wikiContext;
042    private final LinkParsingOperations linkOperations;
043    private final boolean isImageInlining;
044    private final List< Pattern > inlineImagePatterns;
045
046    public LocalLinkAttributeProviderState( final Context wikiContext,
047                                            final boolean hasRef,
048                                            final boolean isImageInlining,
049                                            final List< Pattern > inlineImagePatterns ) {
050        this.hasRef = hasRef;
051        this.wikiContext = wikiContext;
052        this.linkOperations = new LinkParsingOperations( wikiContext );
053        this.isImageInlining = isImageInlining;
054        this.inlineImagePatterns = inlineImagePatterns;
055    }
056
057    /**
058     * {@inheritDoc}
059     *
060     * @see NodeAttributeProviderState#setAttributes(MutableAttributes, Node)
061     */
062    @Override
063    public void setAttributes( final MutableAttributes attributes, final JSPWikiLink link ) {
064        final int hashMark = link.getUrl().toString().indexOf( '#' );
065        final String attachment = wikiContext.getEngine().getManager( AttachmentManager.class ).getAttachmentInfoName( wikiContext, link.getWikiLink() );
066        if( attachment != null ) {
067            if( !linkOperations.isImageLink( link.getUrl().toString(), isImageInlining, inlineImagePatterns ) ) {
068                attributes.replaceValue( "class", MarkupParser.CLASS_ATTACHMENT );
069                final String attlink = wikiContext.getURL( ContextEnum.PAGE_ATTACH.getRequestContext(), link.getWikiLink() );
070                attributes.replaceValue( "href", attlink );
071            } else {
072                new ImageLinkAttributeProviderState( wikiContext, attachment, hasRef ).setAttributes( attributes, link );
073            }
074        } else if( hashMark != -1 ) { // It's an internal Wiki link, but to a named section
075            final String namedSection = link.getUrl().toString().substring( hashMark + 1 );
076            final String matchedLink = linkOperations.linkIfExists( link.getUrl().toString() );
077            if( matchedLink != null ) {
078                String sectref = "#section-" + wikiContext.getEngine().encodeName( matchedLink + "-" + MarkupParser.wikifyLink( namedSection ) );
079                sectref = sectref.replace('%', '_');
080                link.setUrl( CharSubSequence.of( link.getUrl().toString() + sectref ) );
081                new LocalReadLinkAttributeProviderState( wikiContext ).setAttributes( attributes, link );
082            } else {
083                new LocalEditLinkAttributeProviderState( wikiContext, link.getWikiLink() ).setAttributes( attributes, link );
084            }
085        } else {
086            if( linkOperations.linkExists( link.getWikiLink() ) ) {
087                new LocalReadLinkAttributeProviderState( wikiContext ).setAttributes( attributes, link );
088            } else {
089                new LocalEditLinkAttributeProviderState( wikiContext, link.getWikiLink() ).setAttributes( attributes, link );
090            }
091        }
092    }
093
094}