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