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.Document; 022import com.vladsch.flexmark.util.ast.NodeTracker; 023import com.vladsch.flexmark.util.sequence.CharSubSequence; 024import org.apache.log4j.Logger; 025import org.apache.wiki.WikiContext; 026import org.apache.wiki.i18n.InternationalizationManager; 027import org.apache.wiki.markdown.nodes.JSPWikiLink; 028import org.apache.wiki.parser.LinkParsingOperations; 029import org.apache.wiki.parser.MarkupParser; 030import org.apache.wiki.preferences.Preferences; 031import org.apache.wiki.render.RenderingManager; 032import org.apache.wiki.util.TextUtil; 033 034import java.text.MessageFormat; 035import java.util.ResourceBundle; 036 037 038/** 039 * {@link NodePostProcessorState} which further post processes interwiki links. 040 */ 041public class InterWikiLinkNodePostProcessorState implements NodePostProcessorState< JSPWikiLink > { 042 043 private static final Logger LOG = Logger.getLogger( InterWikiLinkNodePostProcessorState.class ); 044 private final WikiContext wikiContext; 045 private final LinkParsingOperations linkOperations; 046 private final Document document; 047 private final boolean m_wysiwygEditorMode; 048 private boolean m_useOutlinkImage = true; 049 050 public InterWikiLinkNodePostProcessorState( final WikiContext wikiContext, final Document document ) { 051 this.wikiContext = wikiContext; 052 this.linkOperations = new LinkParsingOperations( wikiContext ); 053 this.document = document; 054 this.m_useOutlinkImage = wikiContext.getBooleanWikiProperty( MarkupParser.PROP_USEOUTLINKIMAGE, m_useOutlinkImage ); 055 final Boolean wysiwygVariable = ( Boolean )wikiContext.getVariable( RenderingManager.WYSIWYG_EDITOR_MODE ); 056 m_wysiwygEditorMode = wysiwygVariable != null ? wysiwygVariable.booleanValue() : false; 057 } 058 059 /** 060 * {@inheritDoc} 061 * 062 * @see NodePostProcessorState#process(NodeTracker, JSPWikiLink) 063 */ 064 @Override 065 public void process( final NodeTracker state, final JSPWikiLink link ) { 066 final String[] refAndPage = link.getUrl().toString().split( ":" ); 067 if( !m_wysiwygEditorMode ) { 068 String urlReference = wikiContext.getEngine().getInterWikiURL( refAndPage[ 0 ] ); 069 if( urlReference != null ) { 070 urlReference = TextUtil.replaceString( urlReference, "%s", refAndPage[ 1 ] ); 071 if( linkOperations.isImageLink( urlReference ) ) { 072 new ImageLinkNodePostProcessorState( wikiContext, urlReference, link.hasRef() ).process( state, link ); 073 } else { 074 link.setUrl( CharSubSequence.of( urlReference ) ); 075 } 076 if( linkOperations.isExternalLink( urlReference ) ) { 077 NodePostProcessorStateCommonOperations.addOutlinkImage( state, link, wikiContext, m_useOutlinkImage ); 078 } 079 } else { 080 LOG.debug( refAndPage[0] + " not recognized as InterWiki link [document node: " + document + "]" ); 081 final Object[] args = { refAndPage[ 0 ] }; 082 final ResourceBundle rb = Preferences.getBundle( wikiContext, InternationalizationManager.CORE_BUNDLE ); 083 final String errMsg = MessageFormat.format( rb.getString( "markupparser.error.nointerwikiref" ), args ); 084 NodePostProcessorStateCommonOperations.makeError( state, link, errMsg ); 085 } 086 } else { 087 link.setUrl( CharSubSequence.of( refAndPage[0] + ":" + refAndPage[1] ) ); 088 } 089 } 090 091}