    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.html.AttributeProvider;
    022import com.vladsch.flexmark.html.renderer.AttributablePart;
    023import com.vladsch.flexmark.util.ast.Node;
    024import com.vladsch.flexmark.util.html.MutableAttributes;
    025import org.apache.commons.lang3.StringUtils;
    026import org.apache.oro.text.regex.Pattern;
    027import org.apache.wiki.api.core.Context;
    028import org.apache.wiki.markdown.nodes.JSPWikiLink;
    029import org.apache.wiki.parser.LinkParsingOperations;
    030import org.apache.wiki.util.TextUtil;
    031
    032import java.util.List;
    033
    034
    035/**
    036 * {@link AttributeProvider} to decorate {@link JSPWikiLink}s.
    037 *
    038 * Acts as a factory of {@link NodeAttributeProviderState}s, which are the classes setting the attributes for each concrete type of link.
    039 */
    040public class JSPWikiLinkAttributeProvider implements AttributeProvider {
    041
    042    protected final Context wikiContext;
    043    protected final LinkParsingOperations linkOperations;
    044    private final boolean isImageInlining;
    045    private final List< Pattern > inlineImagePatterns;
    046
    047    public JSPWikiLinkAttributeProvider( final Context wikiContext,
    048                                         final boolean isImageInlining,
    049                                         final List< Pattern > inlineImagePatterns ) {
    050        this.wikiContext = wikiContext;
    051        this.linkOperations = new LinkParsingOperations( wikiContext );
    052        this.isImageInlining = isImageInlining;
    053        this.inlineImagePatterns = inlineImagePatterns;
    054    }
    055
    056    /**
    057     * {@inheritDoc}
    058     *
    059     * @see AttributeProvider#setAttributes(Node, AttributablePart, MutableAttributes)
    060     */
    061    @Override
    062    public void setAttributes( final Node node, final AttributablePart part, final MutableAttributes attributes ) {
    063        if( node instanceof JSPWikiLink ) {
    064            final JSPWikiLink link = ( JSPWikiLink )node;
    065            final NodeAttributeProviderState< JSPWikiLink > linkState;
    066            if( linkOperations.isExternalLink( link.getWikiLink() ) ) {
    067                linkState = new ExternalLinkAttributeProviderState( wikiContext, link.hasRef(), isImageInlining, inlineImagePatterns );
    068            } else if( linkOperations.isInterWikiLink( link.getWikiLink() ) ) {
    069                linkState = new InterWikiLinkAttributeProviderState( wikiContext, link.hasRef(), isImageInlining, inlineImagePatterns );
    070            } else if( StringUtils.startsWith( link.getWikiLink(), "#" ) ) {
    071                linkState = new LocalFootnoteLinkAttributeProviderState( wikiContext );
    072            } else if( TextUtil.isNumber( link.getWikiLink() ) ) {
    073                linkState = new LocalFootnoteRefLinkAttributeProviderState( wikiContext );
    074            } else {
    075                linkState = new LocalLinkAttributeProviderState( wikiContext, link.hasRef(), isImageInlining, inlineImagePatterns );
    076            }
    077            linkState.setAttributes( attributes, link );
    078        }
    079    }
    080
    081}