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.jdom2.Element;
023import org.jdom2.JDOMException;
024
025import java.io.PrintWriter;
026import java.util.Deque;
027
028
029/**
030 * Translates to wiki syntax from a plain text handling monospace.
031 */
032public abstract class PlainTextMonospaceDecorator {
033
034    final protected PrintWriter out;
035    final protected Deque< String > preStack;
036    final protected XHtmlElementToWikiTranslator chain;
037    final protected PlainTextCssSpecialDecorator ptcsd;
038
039    protected PlainTextMonospaceDecorator( final PlainTextCssSpecialDecorator ptcsd, final PrintWriter out, final Deque< String > preStack, final XHtmlElementToWikiTranslator chain ) {
040        this.out = out;
041        this.preStack = preStack;
042        this.chain = chain;
043        this.ptcsd = ptcsd;
044    }
045
046    /**
047     * Translates the given XHTML element into wiki markup.
048     *
049     * @param dto XHTML element being translated.
050     */
051    public void decorate( final XHtmlElementToWikiTranslator.ElementDecoratorData dto ) throws JDOMException {
052        if( dto.monospace ) {
053            out.print( markupMonospaceOpen() );
054            preStack.addFirst( markupMonospaceOpen() );
055        }
056        ptcsd.decorate( dto );
057        if( dto.monospace ) {
058            preStack.removeFirst();
059            out.print( markupMonospaceClose() );
060        }
061    }
062
063    /**
064     * Translates the given XHTML element into wiki markup.
065     *
066     * @param e XHTML element being translated.
067     */
068    public void decorate( final Element e ) throws JDOMException {
069        out.print( "\n" + markupMonospaceOpen() ); // start wiki "code blocks" on its own line
070
071        preStack.push( "\n" + markupMonospaceOpen() );
072        chain.translate( e );
073        preStack.pop();
074
075        // print a newline after the closing braces to avoid breaking any subsequent wiki markup that follows.
076        out.print( markupMonospaceClose() + "\n" );
077    }
078
079    /**
080     * Opening wiki markup for a monospace element.
081     *
082     * @return Opening wiki markup for a monospace element.
083     */
084    protected abstract String markupMonospaceOpen();
085
086    /**
087     * Closing wiki markup for a monospace element.
088     *
089     * @return Closing wiki markup for a monospace element.
090     */
091    protected abstract String markupMonospaceClose();
092
093}