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