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.jspwiki;
020
021import org.apache.wiki.htmltowiki.SyntaxDecorator;
022import org.apache.wiki.htmltowiki.WhitespaceTrimWriter;
023import org.apache.wiki.htmltowiki.XHtmlElementToWikiTranslator;
024import org.apache.wiki.htmltowiki.XHtmlToWikiConfig;
025import org.jdom2.Element;
026import org.jdom2.JDOMException;
027import org.jdom2.Text;
028
029import java.io.PrintWriter;
030import java.util.Map;
031import java.util.Stack;
032
033
034/**
035 * Syntax decorator which translates to JSPWiki syntax. Delegates each kind of XHTML element to its specific decorator.
036 */
037public class JSPWikiSyntaxDecorator implements SyntaxDecorator {
038
039    ADecorator a;
040    BrDecorator br;
041    CodeDecorator code;
042    DdDecorator dd;
043    DlDecorator dl;
044    DtDecorator dt;
045    FormDecorator form;
046    HrDecorator hr;
047    H1Decorator h1;
048    H2Decorator h2;
049    H3Decorator h3;
050    H4Decorator h4;
051    ImageDecorator img;
052    InputDecorator input;
053    LiDecorator li;
054    OlDecorator ol;
055    OptionDecorator option;
056    PDecorator p;
057    PlainTextDecorator plainText;
058    PlainTextBoldDecorator strong;
059    PlainTextItalicDecorator em;
060    PlainTextMonospaceDecorator pre;
061    TableDecorator table;
062    TdDecorator td;
063    TextAreaDecorator textarea;
064    TextElementDecorator textElement;
065    ThDecorator th;
066    TrDecorator tr;
067    SelectDecorator select;
068    StrikeDecorator strike;
069    SubDecorator sub;
070    SupDecorator sup;
071    UlDecorator ul;
072    UnderlineDecorator underline;
073    WhitespaceTrimWriter outTrimmer;
074    XHtmlElementToWikiTranslator chain;
075    XHtmlToWikiConfig config;
076
077    /** {@inheritDoc} */
078    @Override
079    public void init( final PrintWriter out,
080                      final Stack< String > liStack,
081                      final Stack< String > preStack,
082                      final WhitespaceTrimWriter outTrimmer,
083                      final XHtmlToWikiConfig config,
084                      final XHtmlElementToWikiTranslator chain ) {
085        this.config = config;
086        this.outTrimmer = outTrimmer;
087        this.chain = chain;
088
089        this.a = new ADecorator( out, config, chain );
090        this.br = new BrDecorator( out, preStack, chain );
091        this.code = new CodeDecorator( out, preStack, chain );
092        this.dd = new DdDecorator( out, chain );
093        this.dl = new DlDecorator( out, chain );
094        this.dt = new DtDecorator( out, chain );
095        this.em = new PlainTextItalicDecorator( out, preStack, chain );
096        this.form = new FormDecorator( out, chain );
097        this.hr = new HrDecorator( out, chain );
098        this.h1 = new H1Decorator( out, chain );
099        this.h2 = new H2Decorator( out, chain );
100        this.h3 = new H3Decorator( out, chain );
101        this.h4 = new H4Decorator( out, chain );
102        this.img = new ImageDecorator( out, config );
103        this.input = new InputDecorator( out, chain );
104        this.li = new LiDecorator( out, liStack, chain );
105        this.ol = new OlDecorator( out, liStack, chain );
106        this.option = new OptionDecorator( out, chain );
107        this.p = new PDecorator( out, chain );
108        this.plainText = new PlainTextDecorator( out, preStack, chain );
109        this.pre = new PlainTextMonospaceDecorator( out, preStack, chain );
110        this.strong = new PlainTextBoldDecorator( out, preStack, chain );
111        this.table = new TableDecorator( out, outTrimmer, chain );
112        this.td = new TdDecorator( out, preStack, chain );
113        this.th = new ThDecorator( out, preStack, chain );
114        this.tr = new TrDecorator( out, chain );
115        this.textarea = new TextAreaDecorator( out, chain );
116        this.textElement = new TextElementDecorator( out, preStack );
117        this.select = new SelectDecorator( out, chain );
118        this.strike = new StrikeDecorator( out, chain );
119        this.sub = new SubDecorator( out, chain );
120        this.sup = new SupDecorator( out, chain );
121        this.ul = new UlDecorator( out, liStack, chain );
122        this.underline = new UnderlineDecorator( out, chain );
123    }
124
125    /** {@inheritDoc} */
126    @Override
127    public void a( final Element e ) throws JDOMException {
128        a.decorate( e );
129    }
130
131    /** {@inheritDoc} */
132    @Override
133    public void br( final Element base, final Element e ) throws JDOMException {
134        br.decorate( base, e );
135    }
136
137    /** {@inheritDoc} */
138    @Override
139    public void code( final Element e ) throws JDOMException {
140        code.decorate( e );
141    }
142
143    /** {@inheritDoc} */
144    @Override
145    public void dd( final Element e ) throws JDOMException {
146        dd.decorate( e );
147    }
148
149    /** {@inheritDoc} */
150    @Override
151    public void dl( final Element e ) throws JDOMException {
152        dl.decorate( e );
153    }
154
155    /** {@inheritDoc} */
156    @Override
157    public void dt( final Element e ) throws JDOMException {
158        dt.decorate( e );
159    }
160
161    /** {@inheritDoc} */
162    @Override
163    public void em( final Element e ) throws JDOMException {
164        em.decorate( e );
165    }
166
167    /** {@inheritDoc} */
168    @Override
169    public void form( final Element e ) throws JDOMException {
170        form.decorate( e );
171    }
172
173    /** {@inheritDoc} */
174    @Override
175    public void hr( final Element e ) throws JDOMException {
176        hr.decorate( e );
177    }
178
179    /** {@inheritDoc} */
180    @Override
181    public void h1( final Element e ) throws JDOMException {
182        h1.decorate( e );
183    }
184
185    /** {@inheritDoc} */
186    @Override
187    public void h2( final Element e ) throws JDOMException {
188        h2.decorate( e );
189    }
190
191    /** {@inheritDoc} */
192    @Override
193    public void h3( final Element e ) throws JDOMException {
194        h3.decorate( e );
195    }
196
197    /** {@inheritDoc} */
198    @Override
199    public void h4( final Element e ) throws JDOMException {
200        h4.decorate( e );
201    }
202
203    /** {@inheritDoc} */
204    @Override
205    public void image( final String src, final Map< String, Object > imageAttrs ) {
206        img.decorate( src, imageAttrs );
207    }
208
209    /** {@inheritDoc} */
210    @Override
211    public void img( final Element e ) {
212        img.decorate( e );
213    }
214
215    /** {@inheritDoc} */
216    @Override
217    public void input( final Element e ) throws JDOMException {
218        input.decorate( e );
219    }
220
221    /** {@inheritDoc} */
222    @Override
223    public void li( final Element base, final Element e ) throws JDOMException {
224        li.decorate( base, e );
225    }
226
227    /** {@inheritDoc} */
228    @Override
229    public void ol( final Element e ) throws JDOMException {
230        ol.decorate( e );
231    }
232
233    /** {@inheritDoc} */
234    @Override
235    public void option( final Element base, final Element e ) throws JDOMException {
236        option.decorate( base, e );
237    }
238
239    /** {@inheritDoc} */
240    @Override
241    public void p( final Element e ) throws JDOMException {
242        p.decorate( e );
243    }
244
245    /** {@inheritDoc} */
246    @Override
247    public void paragraph( final XHtmlElementToWikiTranslator.ElementDecoratorData dto ) throws JDOMException {
248        plainText.decorate( dto );
249    }
250
251    /** {@inheritDoc} */
252    @Override
253    public void pre( final Element e ) throws JDOMException {
254        pre.decorate( e );
255    }
256
257    /** {@inheritDoc} */
258    @Override
259    public void strong( final Element e ) throws JDOMException {
260        strong.decorate( e );
261    }
262
263    /** {@inheritDoc} */
264    @Override
265    public void table( final Element e ) throws JDOMException {
266        table.decorate( e );
267    }
268
269    /** {@inheritDoc} */
270    @Override
271    public void td( final Element e ) throws JDOMException {
272        td.decorate( e );
273    }
274
275    /** {@inheritDoc} */
276    @Override
277    public void text( final Text element ) {
278        textElement.decorate( element );
279    }
280
281    /** {@inheritDoc} */
282    @Override
283    public void textarea( final Element e ) throws JDOMException {
284        textarea.decorate( e );
285    }
286
287    /** {@inheritDoc} */
288    @Override
289    public void th( final Element e ) throws JDOMException {
290        th.decorate( e );
291    }
292
293    /** {@inheritDoc} */
294    @Override
295    public void tr( final Element e ) throws JDOMException {
296        tr.decorate( e );
297    }
298
299    /** {@inheritDoc} */
300    @Override
301    public void select( final Element e ) throws JDOMException {
302        select.decorate( e );
303    }
304
305    /** {@inheritDoc} */
306    @Override
307    public void strike( final Element e ) throws JDOMException {
308        strike.decorate( e );
309    }
310
311    /** {@inheritDoc} */
312    @Override
313    public void sub( final Element e ) throws JDOMException {
314        sub.decorate( e );
315    }
316
317    /** {@inheritDoc} */
318    @Override
319    public void sup( final Element e ) throws JDOMException {
320        sup.decorate( e );
321    }
322
323    /** {@inheritDoc} */
324    @Override
325    public void ul( final Element e ) throws JDOMException {
326        ul.decorate( e );
327    }
328
329    /** {@inheritDoc} */
330    @Override
331    public void underline( final Element e ) throws JDOMException {
332        underline.decorate( e );
333    }
334
335}