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.htmltowiki.syntax; 020 021import org.apache.wiki.htmltowiki.XHtmlElementToWikiTranslator; 022import org.apache.wiki.htmltowiki.XHtmlToWikiConfig; 023import org.jdom2.Element; 024import org.jdom2.JDOMException; 025 026import java.io.PrintWriter; 027import java.util.Map; 028 029 030/** 031 * Translates to wiki syntax from an {@code A} element. 032 */ 033public abstract class ADecorator { 034 035 final protected PrintWriter out; 036 final protected XHtmlToWikiConfig config; 037 final protected XHtmlElementToWikiTranslator chain; 038 039 protected ADecorator( final PrintWriter out, final XHtmlToWikiConfig config, final XHtmlElementToWikiTranslator chain ) { 040 this.out = out; 041 this.config = config; 042 this.chain = chain; 043 } 044 045 /** 046 * Translates the given XHTML element into wiki markup. 047 * 048 * @param e XHTML element being translated. 049 */ 050 public void decorate( final Element e, final String ref ) throws JDOMException { 051 final Map< String, String > augmentedWikiLinkAttributes = MarkupHelper.getAugmentedWikiLinkAttributes( e ); 052 if( containsAdditionalLinkAttributes( augmentedWikiLinkAttributes ) ) { 053 final String augmentedWikiLink = MarkupHelper.augmentedWikiLinkMapToString( augmentedWikiLinkAttributes ); 054 linkMarkup( e, ref, augmentedWikiLink ); 055 } else if( !e.getTextTrim().equalsIgnoreCase( ref ) ) { 056 linkMarkup( e, ref ); 057 } else { 058 linkMarkup( e ); 059 } 060 } 061 062 boolean containsAdditionalLinkAttributes( final Map< String, String > augmentedWikiLinkAttributes ) { 063 return !augmentedWikiLinkAttributes.isEmpty(); 064 } 065 066 /** 067 * Translate a link to a footnote to wiki syntax. 068 * 069 * @param text link's text 070 * @param href link's href 071 */ 072 public void decorateFootnote( final String text, final String href ) { 073 linkMarkup( text, href ); 074 } 075 076 /** 077 * Translate an undefined link to wiki syntax. 078 * 079 * @param e element being translated. 080 */ 081 public void decorateUndefinedLink( final Element e ) throws JDOMException { 082 linkMarkup( e ); 083 } 084 085 /** 086 * Provide wiki markup for the XHTML element being translated. 087 * 088 * @param e element being translated. 089 * @throws JDOMException error parsing the element being translated. 090 */ 091 protected abstract void linkMarkup( Element e ) throws JDOMException; 092 093 /** 094 * Provide wiki markup for the XHTML element being translated. 095 * 096 * @param e element being translated. 097 * @param ref link's href. 098 * @throws JDOMException error parsing the element being translated. 099 */ 100 protected abstract void linkMarkup( Element e, String ref ) throws JDOMException; 101 102 /** 103 * Provide wiki markup for the XHTML element being translated. 104 * 105 * @param e element being translated. 106 * @param ref link's href. 107 * @param additionalAttrs link's additional attributes. 108 * @throws JDOMException error parsing the element being translated. 109 */ 110 protected abstract void linkMarkup( Element e, String ref, String additionalAttrs ) throws JDOMException; 111 112 /** 113 * Provide wiki markup for the XHTML element being translated. 114 * 115 * @param text link's text 116 * @param ref link's href - might be equals to {@code text}! 117 */ 118 protected abstract void linkMarkup( String text, String ref ); 119 120}