    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.Attribute;
    023import org.jdom2.Element;
    024import org.jdom2.JDOMException;
    025
    026import java.io.PrintWriter;
    027
    028
    029/**
    030 * <p>Translates to wiki syntax from a {@code OPTION} element.</p>
    031 *
    032 * <p>Since this element outputs into the value of a parameter inside the {@code FormSelect} plugin, there shouldn't
    033 * be a reason to extend this class when developing a new wiki syntax (although you may do if you want to).</p>
    034 */
    035public class OptionDecorator {
    036
    037    final protected PrintWriter out;
    038    final protected XHtmlElementToWikiTranslator chain;
    039
    040    public OptionDecorator( final PrintWriter out, final XHtmlElementToWikiTranslator chain ) {
    041        this.out = out;
    042        this.chain = chain;
    043    }
    044
    045    /**
    046     * Translates the given XHTML element into wiki markup.
    047     *
    048     * @param base the parent of the XHTML element being translated.
    049     * @param e XHTML element being translated.
    050     */
    051    public void decorate( final Element base, final Element e ) throws JDOMException {
    052        // If this <option> element isn't the second child element within the parent <select>
    053        // element, then we need to print a semicolon as a separator. (The first child element
    054        // is expected to be a newline character which is at index of 0).
    055        if( base.indexOf( e ) != 1 ) {
    056            out.print( ";" );
    057        }
    058
    059        final Attribute selected = e.getAttribute( "selected" );
    060        if( selected != null ) {
    061            out.print( "*" );
    062        }
    063
    064        final String value = e.getAttributeValue( "value" );
    065        if( value != null ) {
    066            out.print( value );
    067        } else {
    068            chain.translate( e );
    069        }
    070    }
    071
    072}