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.XHtmlToWikiConfig;
022import org.jdom2.Element;
023
024import java.io.PrintWriter;
025import java.util.Map;
026
027/**
028 * Translates to wiki syntax from an XHTML Image.
029 */
030public abstract class ImageDecorator {
031
032    final protected PrintWriter out;
033    final protected XHtmlToWikiConfig config;
034
035    protected ImageDecorator( final PrintWriter out, final XHtmlToWikiConfig config ) {
036        this.out = out;
037        this.config = config;
038    }
039
040    /**
041     * Translates the given XHTML element into wiki markup.
042     *
043     * @param src image source.
044     * @param imageAttrs image attributes.
045     */
046    public void decorate( final String src, final Map< String, Object > imageAttrs ) {
047        if( imageAttrs.isEmpty() ) {
048            out.print( markupImageSimpleOpen() + src + markupImageSimpleClose() );
049        } else {
050            markupImageWithAttributes( src, imageAttrs );
051        }
052    }
053
054    /**
055     * Translates the given XHTML element into wiki markup.
056     *
057     * @param e XHTML element being translated.
058     */
059    public void decorate( final Element e ) {
060        if( config.isNotIgnorableWikiMarkupLink( e ) ) {
061            out.print( markupImageSimpleOpen() );
062            MarkupHelper.printUnescaped( out, config.trimLink( e.getAttributeValue( "src" ) ) );
063            out.print( markupImageSimpleClose() );
064        }
065    }
066
067    /**
068     * Opening wiki markup for a simple (no image attributes) link image.
069     *
070     * @return Opening wiki markup for a simple (no image attributes) link image.
071     */
072    protected abstract String markupImageSimpleOpen();
073
074    /**
075     * Closing wiki markup for a simple (no image attributes) link image.
076     *
077     * @return Closing wiki markup for a simple (no image attributes) link image.
078     */
079    protected abstract String markupImageSimpleClose();
080
081    /**
082     * Wiki markup for a link image with image attributes.
083     *
084     * @param src image source.
085     * @param imageAttrs image attributes.
086     */
087    protected abstract void markupImageWithAttributes( String src, Map< String, Object > imageAttrs );
088
089}