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.postprocessor;
020
021import com.vladsch.flexmark.ast.HtmlInline;
022import com.vladsch.flexmark.util.ast.NodeTracker;
023import com.vladsch.flexmark.util.sequence.CharSubSequence;
024import org.apache.wiki.WikiContext;
025import org.apache.wiki.markdown.nodes.JSPWikiLink;
026import org.apache.wiki.parser.LinkParsingOperations;
027import org.apache.wiki.parser.MarkupParser;
028
029
030/**
031 * {@link NodePostProcessorState} which further post processes local links.
032 */
033public class LocalLinkNodePostProcessorState implements NodePostProcessorState< JSPWikiLink > {
034
035    private final WikiContext wikiContext;
036    private final LinkParsingOperations linkOperations;
037
038    public LocalLinkNodePostProcessorState( final WikiContext wikiContext ) {
039        this.wikiContext = wikiContext;
040        this.linkOperations = new LinkParsingOperations( wikiContext );
041    }
042
043    /**
044     * {@inheritDoc}
045     *
046     * @see NodePostProcessorState#process(NodeTracker, JSPWikiLink)
047     */
048    @Override
049    public void process( final NodeTracker state, final JSPWikiLink link ) {
050        final int hashMark = link.getUrl().toString().indexOf( '#' );
051        final String attachment = wikiContext.getEngine().getAttachmentManager().getAttachmentInfoName( wikiContext, link.getUrl().toString() );
052        if( attachment != null  ) {
053            if( !linkOperations.isImageLink( link.getUrl().toString() ) ) {
054                final String attlink = wikiContext.getURL( WikiContext.ATTACH, link.getUrl().toString() );
055                link.setUrl( CharSubSequence.of( attlink ) );
056                link.removeChildren();
057                final HtmlInline content = new HtmlInline( CharSubSequence.of( link.getText().toString() ) );
058                link.appendChild( content );
059                state.nodeAddedWithChildren( content );
060                addAttachmentLink( state, link );
061            } else {
062                new ImageLinkNodePostProcessorState( wikiContext, attachment, link.hasRef() ).process( state, link );
063            }
064        } else if( hashMark != -1 ) { // It's an internal Wiki link, but to a named section
065            final String namedSection = link.getUrl().toString().substring( hashMark + 1 );
066            link.setUrl( CharSubSequence.of( link.getUrl().toString().substring( 0, hashMark ) ) );
067            final String matchedLink = linkOperations.linkIfExists( link.getUrl().toString() );
068            if( matchedLink != null ) {
069                String sectref = "#section-" + wikiContext.getEngine().encodeName( matchedLink + "-" + MarkupParser.wikifyLink( namedSection ) );
070                sectref = sectref.replace('%', '_');
071                link.setUrl( CharSubSequence.of( wikiContext.getURL( WikiContext.VIEW, link.getUrl().toString() + sectref ) ) );
072            } else {
073                link.setUrl( CharSubSequence.of( wikiContext.getURL( WikiContext.EDIT, link.getUrl().toString() ) ) );
074            }
075        } else {
076            if( linkOperations.linkExists( link.getUrl().toString() ) ) {
077                link.setUrl( CharSubSequence.of( wikiContext.getURL( WikiContext.VIEW, link.getUrl().toString() ) ) );
078            } else {
079                link.setUrl( CharSubSequence.of( wikiContext.getURL( WikiContext.EDIT, link.getUrl().toString() ) ) );
080            }
081        }
082    }
083
084    void addAttachmentLink( final NodeTracker state, final JSPWikiLink link ) {
085        final String infolink = wikiContext.getURL( WikiContext.INFO, link.getWikiLink() );
086        final String imglink = wikiContext.getURL( WikiContext.NONE, "images/attachment_small.png" );
087        final HtmlInline aimg = new HtmlInline( CharSubSequence.of( "<a href=\""+ infolink + "\" class=\"infolink\">" +
088                                                                       "<img src=\""+ imglink + "\" border=\"0\" alt=\"(info)\" />" +
089                                                                     "</a>" ) );
090        link.insertAfter( aimg );
091        state.nodeAdded( aimg );
092    }
093
094}