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.Node; 023import com.vladsch.flexmark.util.ast.NodeTracker; 024import com.vladsch.flexmark.util.sequence.CharSubSequence; 025import org.apache.oro.text.regex.Pattern; 026import org.apache.wiki.api.core.Context; 027import org.apache.wiki.api.core.ContextEnum; 028import org.apache.wiki.attachment.AttachmentManager; 029import org.apache.wiki.markdown.nodes.JSPWikiLink; 030import org.apache.wiki.parser.LinkParsingOperations; 031import org.apache.wiki.parser.MarkupParser; 032 033import java.util.List; 034 035 036/** 037 * {@link NodePostProcessorState} which further post processes local links. 038 */ 039public class LocalLinkNodePostProcessorState implements NodePostProcessorState< JSPWikiLink > { 040 041 private final Context wikiContext; 042 private final LinkParsingOperations linkOperations; 043 private final boolean isImageInlining; 044 private final List< Pattern > inlineImagePatterns; 045 046 public LocalLinkNodePostProcessorState( final Context wikiContext, 047 final boolean isImageInlining, 048 final List< Pattern > inlineImagePatterns ) { 049 this.wikiContext = wikiContext; 050 this.linkOperations = new LinkParsingOperations( wikiContext ); 051 this.isImageInlining = isImageInlining; 052 this.inlineImagePatterns = inlineImagePatterns; 053 } 054 055 /** 056 * {@inheritDoc} 057 * 058 * @see NodePostProcessorState#process(NodeTracker, Node) 059 */ 060 @Override 061 public void process( final NodeTracker state, final JSPWikiLink link ) { 062 final int hashMark = link.getUrl().toString().indexOf( '#' ); 063 final String attachment = wikiContext.getEngine().getManager( AttachmentManager.class ).getAttachmentInfoName( wikiContext, link.getUrl().toString() ); 064 if( attachment != null ) { 065 if( !linkOperations.isImageLink( link.getUrl().toString(), isImageInlining, inlineImagePatterns ) ) { 066 final String attlink = wikiContext.getURL( ContextEnum.PAGE_ATTACH.getRequestContext(), link.getUrl().toString() ); 067 link.setUrl( CharSubSequence.of( attlink ) ); 068 link.removeChildren(); 069 final HtmlInline content = new HtmlInline( CharSubSequence.of( link.getText().toString() ) ); 070 link.appendChild( content ); 071 state.nodeAddedWithChildren( content ); 072 addAttachmentLink( state, link ); 073 } else { 074 new ImageLinkNodePostProcessorState( wikiContext, attachment, 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 HtmlInline aimg = new HtmlInline( CharSubSequence.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}