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.util.ast.Node;
022import com.vladsch.flexmark.util.ast.NodeTracker;
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 NodePostProcessorState} which further post processes local links.
037 */
038public class LocalLinkNodePostProcessorState implements NodePostProcessorState< JSPWikiLink > {
039
040    private final Context wikiContext;
041    private final LinkParsingOperations linkOperations;
042    private final boolean isImageInlining;
043    private final List< Pattern > inlineImagePatterns;
044
045    public LocalLinkNodePostProcessorState( final Context wikiContext,
046                                            final boolean isImageInlining,
047                                            final List< Pattern > inlineImagePatterns ) {
048        this.wikiContext = wikiContext;
049        this.linkOperations = new LinkParsingOperations( wikiContext );
050        this.isImageInlining = isImageInlining;
051        this.inlineImagePatterns = inlineImagePatterns;
052    }
053
054    /**
055     * {@inheritDoc}
056     *
057     * @see NodePostProcessorState#process(NodeTracker, Node) 
058     */
059    @Override
060    public void process( final NodeTracker state, final JSPWikiLink link ) {
061        final int hashMark = link.getUrl().toString().indexOf( '#' );
062        final String attachment = wikiContext.getEngine().getManager( AttachmentManager.class ).getAttachmentInfoName( wikiContext, link.getUrl().toString() );
063        if( attachment != null  ) {
064            if( !linkOperations.isImageLink( link.getUrl().toString(), isImageInlining, inlineImagePatterns ) ) {
065                final String attlink = wikiContext.getURL( ContextEnum.PAGE_ATTACH.getRequestContext(), link.getUrl().toString() );
066                link.setUrl( CharSubSequence.of( attlink ) );
067                link.removeChildren();
068                final WikiHtmlInline content = WikiHtmlInline.of( link.getText().toString(), wikiContext );
069                link.appendChild( content );
070                state.nodeAddedWithChildren( content );
071                addAttachmentLink( state, link );
072            } else {
073                final String attlink = wikiContext.getURL( ContextEnum.PAGE_ATTACH.getRequestContext(), attachment );
074                new ImageLinkNodePostProcessorState( wikiContext, attlink, link.hasRef() ).process( state, link );
075            }
076        } else if( hashMark != -1 ) { // It's an internal Wiki link, but to a named section
077            final String namedSection = link.getUrl().toString().substring( hashMark + 1 );
078            link.setUrl( CharSubSequence.of( link.getUrl().toString().substring( 0, hashMark ) ) );
079            final String matchedLink = linkOperations.linkIfExists( link.getUrl().toString() );
080            if( matchedLink != null ) {
081                String sectref = "#section-" + wikiContext.getEngine().encodeName( matchedLink + "-" + MarkupParser.wikifyLink( namedSection ) );
082                sectref = sectref.replace( '%', '_' );
083                link.setUrl( CharSubSequence.of( wikiContext.getURL( ContextEnum.PAGE_VIEW.getRequestContext(), link.getUrl().toString() + sectref ) ) );
084            } else {
085                link.setUrl( CharSubSequence.of( wikiContext.getURL( ContextEnum.PAGE_EDIT.getRequestContext(), link.getUrl().toString() ) ) );
086            }
087        } else {
088            if( linkOperations.linkExists( link.getUrl().toString() ) ) {
089                link.setUrl( CharSubSequence.of( wikiContext.getURL( ContextEnum.PAGE_VIEW.getRequestContext(), link.getUrl().toString() ) ) );
090            } else {
091                link.setUrl( CharSubSequence.of( wikiContext.getURL( ContextEnum.PAGE_EDIT.getRequestContext(), link.getUrl().toString() ) ) );
092            }
093        }
094    }
095
096    void addAttachmentLink( final NodeTracker state, final JSPWikiLink link ) {
097        final String infolink = wikiContext.getURL( ContextEnum.PAGE_INFO.getRequestContext(), link.getWikiLink() );
098        final String imglink = wikiContext.getURL( ContextEnum.PAGE_NONE.getRequestContext(), "images/attachment_small.png" );
099        final WikiHtmlInline aimg = WikiHtmlInline.of( "<a href=\""+ infolink + "\" class=\"infolink\">" +
100                                                              "<img src=\""+ imglink + "\" border=\"0\" alt=\"(info)\" />" +
101                                                           "</a>" ) ;
102        link.insertAfter( aimg );
103        state.nodeAdded( aimg );
104    }
105
106}