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.util.XmlUtil;
023import org.jdom2.Element;
024import org.jdom2.JDOMException;
025
026import java.io.PrintWriter;
027
028
029/**
030 * Translates to wiki syntax from a {@code FORM} element.
031 */
032public abstract class FormDecorator {
033
034    final protected PrintWriter out;
035    final protected XHtmlElementToWikiTranslator chain;
036
037    protected FormDecorator( final PrintWriter out, final XHtmlElementToWikiTranslator chain ) {
038        this.out = out;
039        this.chain = chain;
040    }
041
042    /**
043     * Translates the given XHTML element into wiki markup.
044     *
045     * @param e XHTML element being translated.
046     */
047    public void decorate( final Element e ) throws JDOMException {
048        // remove the hidden input where name="formname" since a new one will be generated again when the xhtml is rendered.
049        final Element formName = XmlUtil.getXPathElement( e, "INPUT[@name='formname']" );
050        if( formName != null ) {
051            formName.detach();
052        }
053
054        final String name = e.getAttributeValue( "name" );
055        out.print( "\n" + markupFormOpen( name ) + "\n" );
056        chain.translate( e );
057        out.print( "\n" + markupFormClose() + "\n" );
058    }
059
060    /**
061     * Opening wiki markup for a {@code Form} element.
062     *
063     * @param name {@code Form}'s {@code name} attribute - may be {@code null}.
064     * @return Opening wiki markup for a {@code Form} element.
065     */
066    protected abstract String markupFormOpen( String name );
067
068    /**
069     * Closing wiki markup for a {@code Form} element.
070     *
071     * @return Closing wiki markup for a {@code Form} element.
072     */
073    protected abstract String markupFormClose();
074
075}