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.Image; 022import com.vladsch.flexmark.ast.Link; 023import com.vladsch.flexmark.parser.PostProcessor; 024import com.vladsch.flexmark.parser.block.NodePostProcessor; 025import com.vladsch.flexmark.util.ast.Document; 026import com.vladsch.flexmark.util.ast.Node; 027import com.vladsch.flexmark.util.ast.NodeTracker; 028import com.vladsch.flexmark.util.sequence.CharSubSequence; 029import org.apache.commons.lang3.Strings; 030import org.apache.oro.text.regex.Pattern; 031import org.apache.wiki.api.core.Context; 032import org.apache.wiki.api.core.ContextEnum; 033import org.apache.wiki.attachment.AttachmentManager; 034import org.apache.wiki.markdown.nodes.JSPWikiLink; 035import org.apache.wiki.parser.LinkParsingOperations; 036import org.apache.wiki.parser.MarkupParser; 037import org.apache.wiki.util.TextUtil; 038 039import java.util.List; 040 041 042/** 043 * {@link NodePostProcessor} to convert {@link Link}s into {@link JSPWikiLink}s, and to point {@link Image}s of attachments to their URL. 044 * <p> 045 * Acts as a factory of {@link NodePostProcessorState}, which are the classes generating the extra markup for each concrete type of link. 046 */ 047public class JSPWikiLinkNodePostProcessor extends NodePostProcessor { 048 049 protected final Context m_context; 050 protected final LinkParsingOperations linkOperations; 051 private final boolean isImageInlining; 052 private final List< Pattern > inlineImagePatterns; 053 protected boolean m_useOutlinkImage = true; 054 protected final Document document; 055 056 public JSPWikiLinkNodePostProcessor( final Context m_context, 057 final Document document, 058 final boolean isImageInlining, 059 final List< Pattern > inlineImagePatterns ) { 060 this.m_context = m_context; 061 this.document = document; 062 linkOperations = new LinkParsingOperations( m_context ); 063 this.isImageInlining = isImageInlining; 064 this.inlineImagePatterns = inlineImagePatterns; 065 m_useOutlinkImage = m_context.getBooleanWikiProperty( MarkupParser.PROP_USEOUTLINKIMAGE, m_useOutlinkImage ); 066 } 067 068 /** 069 * {@inheritDoc} 070 * 071 * @see PostProcessor#process(NodeTracker, Node) 072 */ 073 @Override 074 public void process( final NodeTracker state, final Node node ) { 075 if( node instanceof Link ) { 076 final JSPWikiLink link = replaceLinkWithJSPWikiLink( state, node ); 077 078 final NodePostProcessorState< JSPWikiLink > linkPostProcessor; 079 if( linkOperations.isAccessRule( link.getUrl().toString() ) ) { 080 linkPostProcessor = new AccessRuleLinkNodePostProcessorState( m_context ); 081 } else if( linkOperations.isMetadata( link.getUrl().toString() ) ) { 082 linkPostProcessor = new MetadataLinkNodePostProcessorState( m_context ); 083 } else if( linkOperations.isPluginLink( link.getUrl().toString() ) ) { 084 linkPostProcessor = new PluginLinkNodePostProcessorState( m_context ); 085 } else if( linkOperations.isVariableLink( link.getUrl().toString() ) ) { 086 linkPostProcessor = new VariableLinkNodePostProcessorState( m_context ); 087 } else if( linkOperations.isExternalLink( link.getUrl().toString() ) ) { 088 linkPostProcessor = new ExternalLinkNodePostProcessorState( m_context, isImageInlining, inlineImagePatterns ); 089 } else if( linkOperations.isInterWikiLink( link.getUrl().toString() ) ) { 090 linkPostProcessor = new InterWikiLinkNodePostProcessorState( m_context, document, isImageInlining, inlineImagePatterns ); 091 } else if( Strings.CS.startsWith( link.getUrl().toString(), "#" ) ) { 092 linkPostProcessor = new LocalFootnoteLinkNodePostProcessorState( m_context ); 093 } else if( TextUtil.isNumber( link.getUrl().toString() ) ) { 094 linkPostProcessor = new LocalFootnoteRefLinkNodePostProcessorState( m_context ); 095 } else { 096 linkPostProcessor = new LocalLinkNodePostProcessorState( m_context, isImageInlining, inlineImagePatterns ); 097 } 098 linkPostProcessor.process( state, link ); 099 } else if( node instanceof Image image ) { 100 final String attachment = m_context.getEngine().getManager( AttachmentManager.class ) 101 .getAttachmentInfoName( m_context, image.getUrl().toString() ); 102 if( attachment != null ) { 103 image.setUrl( CharSubSequence.of( m_context.getURL( ContextEnum.PAGE_ATTACH.getRequestContext(), attachment ) ) ); 104 } 105 } 106 } 107 108 JSPWikiLink replaceLinkWithJSPWikiLink( final NodeTracker state, final Node node ) { 109 final JSPWikiLink link = new JSPWikiLink( ( Link )node ); 110 final Node previous = node.getPrevious(); 111 final Node parent = node.getParent(); 112 113 link.takeChildren( node ); 114 node.unlink(); 115 116 if( previous != null ) { 117 previous.insertAfter( link ); 118 } else if( parent != null ) { 119 parent.appendChild( link ); 120 } 121 122 state.nodeRemoved( node ); 123 state.nodeAddedWithChildren( link ); 124 return link; 125 } 126 127}