001package org.apache.maven.doxia.module.docbook;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.doxia.sink.Sink;
023
024/**
025 * Utility methods for Doxia Docbook Parser and Sink.
026 *
027 * @author ltheussl
028 * @since 1.1.1
029 */
030public final class DocbookUtils
031{
032    /**
033     * Translate a given Docbook table frame attribute value to a valid
034     * Doxia table frame attribute value.
035     *
036     * <p>The input has to be one of <code>"all"</code>, <code>"bottom"</code>,
037     * <code>"none"</code>, <code>"sides"</code>, <code>"top"</code> or <code>"topbot"</code>,
038     * otherwise an IllegalArgumentException is thrown.</p>
039     *
040     * <p>The corresponding output values are <code>"box"</code>, <code>"below"</code>,
041     * <code>"void"</code>, <code>"vsides"</code>, <code>"above"</code> and <code>"hsides"</code>.</p>
042     *
043     * @param frame a valid docbook table frame attribute as specified above,
044     * otherwise an IllegalArgumentException is thrown.
045     * @return a valid Doxia table frame attribute as specified above.
046     */
047    public static String doxiaTableFrameAttribute( final String frame )
048    {
049        String fr = frame;
050
051        if ( "all".equals( fr ) )
052        {
053            fr = "box";
054        }
055        else if ( "bottom".equals( fr ) )
056        {
057            fr = "below";
058        }
059        else if ( "none".equals( fr ) )
060        {
061            fr = "void";
062        }
063        else if ( "sides".equals( fr ) )
064        {
065            fr = "vsides";
066        }
067        else if ( "top".equals( fr ) )
068        {
069            fr = "above";
070        }
071        else if ( "topbot".equals( fr ) )
072        {
073            fr = "hsides";
074        }
075        else
076        {
077            throw new IllegalArgumentException( "Not a valid frame attribute: " + fr );
078        }
079
080        return fr;
081    }
082
083    /**
084     * Convert a docbook ordered-list numbering style to a doxia numbering style.
085     *
086     * <p>The input has to be one of the style constants defined in {@link SimplifiedDocbookMarkup},
087     * otherwise an IllegalArgumentException is thrown.</p>
088     *
089     * <p>The output is one of the numbering constants defined in {@link Sink}.</p>
090     * @param style a docbook ordered-list numbering style.
091     * @return a doxia numbering style.
092     */
093    public static int doxiaListNumbering( final String style )
094    {
095        if ( SimplifiedDocbookMarkup.LOWERALPHA_STYLE.equals( style ) )
096        {
097            return Sink.NUMBERING_LOWER_ALPHA;
098        }
099        else if ( SimplifiedDocbookMarkup.LOWERROMAN_STYLE.equals( style ) )
100        {
101            return Sink.NUMBERING_LOWER_ROMAN;
102        }
103        else if ( SimplifiedDocbookMarkup.UPPERALPHA_STYLE.equals( style ) )
104        {
105            return Sink.NUMBERING_UPPER_ALPHA;
106        }
107        else if ( SimplifiedDocbookMarkup.UPPERROMAN_STYLE.equals( style ) )
108        {
109            return Sink.NUMBERING_UPPER_ROMAN;
110        }
111        else if ( SimplifiedDocbookMarkup.ARABIC_STYLE.equals( style ) )
112        {
113            return Sink.NUMBERING_DECIMAL;
114        }
115        else
116        {
117            throw new IllegalArgumentException( "Not a valid numbering style: " + style );
118        }
119    }
120
121    /**
122     * Convert a doxia numbering style to a docbook ordered-list numbering style.
123     *
124     * <p>The input has to be one of the numbering constants defined in {@link Sink},
125     * otherwise an IllegalArgumentException is thrown.</p>
126     *
127     * <p>The output is one of the style constants defined in {@link SimplifiedDocbookMarkup}.</p>
128     *
129     * @param numbering a doxia numbering style.
130     * @return a docbook ordered-list numbering style.
131     */
132    public static String docbookListNumbering( final int numbering )
133    {
134        switch ( numbering )
135        {
136            case Sink.NUMBERING_UPPER_ALPHA:
137                return SimplifiedDocbookMarkup.UPPERALPHA_STYLE;
138            case Sink.NUMBERING_LOWER_ALPHA:
139                return SimplifiedDocbookMarkup.LOWERALPHA_STYLE;
140            case Sink.NUMBERING_UPPER_ROMAN:
141                return SimplifiedDocbookMarkup.UPPERROMAN_STYLE;
142            case Sink.NUMBERING_LOWER_ROMAN:
143                return SimplifiedDocbookMarkup.LOWERROMAN_STYLE;
144            case Sink.NUMBERING_DECIMAL:
145                return SimplifiedDocbookMarkup.ARABIC_STYLE;
146            default:
147                throw new IllegalArgumentException( "Not a valid numbering: " + numbering );
148        }
149    }
150
151    /**
152     * Get a trademark character from a class attribute.
153     *
154     * <p>The input String has to be one of <code>"registered"</code>, <code>"copyright"</code>,
155     * <code>"service"</code> or <code>"trade"</code> otherwise an IllegalArgumentException is thrown.</p>
156     *
157     * <p>The corresponding output is <code>'\u00AE'</code>, <code>'\u00A9'</code>,
158     * <code>'\u2120'</code> or <code>'\u2122'</code>.</p>
159     *
160     * @param trade a valid class atribute for the docbook <code>&lt;trademark&gt;</code> tag.
161     * @return the corresponding unicode character.
162     */
163    public static char trademarkFromClass( final String trade )
164    {
165        if ( "registered".equals( trade ) )
166        {
167            return '\u00AE';
168        }
169        else if ( "copyright".equals( trade ) )
170        {
171            return '\u00A9';
172        }
173        else if ( "service".equals( trade ) )
174        {
175            return '\u2120';
176        }
177        else if ( "trade".equals( trade ) )
178        {
179            return '\u2122';
180        }
181        else
182        {
183            throw new IllegalArgumentException( "Not a trademark class: " + trade );
184        }
185    }
186
187    private DocbookUtils()
188    {
189        // utility class
190    }
191}