001package Torello.Languages;
002
003import java.util.*;
004import java.io.*;
005
006/**
007 * HTMLWordTables for Mandarin Chinese Translations.
008 * 
009 * <BR /><BR /><EMBED CLASS='external-html' DATA-FILE-ID=HTMLWT>
010 */
011@Torello.JavaDoc.StaticFunctional
012public class HTMLWordTables
013{
014    private HTMLWordTables() { }
015
016    /**
017     * Builds a simple, short HTML version of the Chinese-Simplified / English / Spanish sentence
018     * DIV
019     * 
020     * @param simpSentence This may be a single word, a complete phrase, sentence, or even a
021     * paragraph        of simplified Mandarin Chinese scraped from a web-site like Gov.CN.  If this
022     * parameter is null, an exception      will be thrown.
023     * 
024     * @param englSentence This is intended to be the English-Translated version of the
025     * Simplified-Chinese sentence.           If this parameter is null, the output HTML-table will have
026     * <I><B>one less / fewer row</B></I> included.
027     * 
028     * @param espaSentence This is supposed to be the Spanish-translated version of the
029     * Simplified-Chinese sentence.           If this parameter is null, the output HTML-table will have
030     * <I><B>one less / fewer row</B></I> included.
031     * 
032     * @param tableClass If this table needs a CSS-Class selector, pass it here.
033     * 
034     * @param tableID If this table requires a CSS-ID selector, pass it here.
035     * 
036     * @param onClickListener This is a string of the form "callbackFuntion(params);" It is
037     * inserted
038     *      into the &lt;TABLE on_click="INSERTED-STRING-HERE"&gt; tag
039     * 
040     * @return An Table-HTML string.  Note: The Vocabulary tables *ARE NOT* included.
041     *      This only includes the English, Spanish &amp; Chinese sentences.
042     * 
043     * @throws NullPointerException If simpSentence is null.
044     */
045    public static String toHTML(
046            String simpSentence, String englSentence, String espaSentence,
047            String tableClass, String tableID, String onClickListener
048        )
049    {
050        if (simpSentence == null) throw new NullPointerException
051            ("simpSentence MAY NOT be NULL, engl and espa can be.");
052
053        String tableIncludeStr = "";
054
055        tableIncludeStr += (tableClass != null)
056            ? (" CLASS=\"" + tableClass + "\"")
057            : "";
058
059        tableIncludeStr += (tableID != null)
060            ? (" ID=\"" + tableID + "\"")
061            : "";
062
063        tableIncludeStr += (onClickListener != null)
064            ? (" onclick=\"" + onClickListener + "\"")
065            : "";
066
067        return  "<TABLE " + tableIncludeStr + "><TBODY>\n" +
068                ((simpSentence != null)
069                    ? ("<TR CLASS=\"ZHROW\" ><TD>" + simpSentence + "</TD></TR>\n") 
070                    : "") +
071                ((englSentence != null) 
072                    ? ("<TR CLASS=\"ENROW\" ><TD>" + englSentence + "</TD></TR>\n") 
073                    : "") +
074                ((espaSentence != null) 
075                    ? ("<TR CLASS=\"ESROW\" ><TD>" + espaSentence + "</TD></TR>\n") 
076                    : "") +
077                "</TBODY></TABLE>\n";
078    }
079
080    /**
081     * The purpose of this method is to generate the "Pop Up" vocabulary table as HTML
082     * 
083     * @param tableClass This contains the HTML-tag "class" String.  It is included in the
084     *      HTML &lt;TABLE CLASS="INSERT-TEXT-HERE&gt; String
085     * 
086     * @param tableID This contains the HTML-tag "id" String.  It is included in the
087     *      HTML &lt;TABLE ID="INSERT-TEXT-HERE"&gt; String
088     * 
089     * @param setNoDisplay When this variable is {@code TRUE}, a CSS tag is added to the opening
090     * &lt;TABLE&gt; tag, indicating: <BR /><CODE>STYLE="display: none;"</CODE> - which ensures
091     * that this vocabulary table remains hidden.
092     * 
093     * @param simpWords A Vector of simplified chinese character-words.  This parameter may not be
094     * null.
095     * 
096     * @param pronWords A Parallel Vector of the pronunciation of this input Simplified Mandarin
097     * words.  Can be left out / null.
098     * 
099     * @param tradWords Parallel Vector.  Traditional-Chinese Characters.  May be null.
100     * 
101     * @param englWords Parallel Vector.  English Words.  Can be null.
102     * 
103     * @param espaWords Parallel Vector.  Chinese in Spanish.  Also may be null.      
104     * 
105     * @return returns the Vocabulary Table as an HTML String of
106     * &lt;TABLE&gt;Vocab-Rows&lt;/TABLE&gt;
107     * 
108     * @throws NullPointerException If simpWords is null.
109     */
110    public static String getHTMLVocabTable(
111            String tableClass, String tableID, boolean setNoDisplay,
112            Vector<String>  simpWords,
113            Vector<String>  pronWords,
114            Vector<String>  tradWords,
115            Vector<String>  englWords,
116            Vector<String>  espaWords
117        )
118    {
119        if (simpWords== null) throw new NullPointerException
120            ("simpWords MAY NOT be NULL, engl and espa can be.");
121
122        StringBuilder sb = new StringBuilder();
123
124        // Build the <TABLE> tag.  It may require a CSS "CLASS=...", possibly a "ID=...", 
125        // or a CSS-styled "display: none;"
126
127        sb.append("<TABLE");
128        if (tableClass  != null)    sb.append(" CLASS=\""   + tableClass    + "\"");
129        if (tableID     != null)    sb.append(" ID=\""      + tableID       + "\"");
130        if (setNoDisplay)           sb.append(" STYLE=\"display: none;\"");
131        sb.append(">\n<TBODY>\n");
132
133        int len = simpWords.size();
134
135        for (int i = 0; i < len; i++)
136        {
137            sb.append(
138                "<TR>\n" +
139                ((simpWords != null) ? ("<TD>" + simpWords.elementAt(i) + "</TD>\n") : "")  +
140                ((pronWords != null) ? ("<TD>" + pronWords.elementAt(i) + "</TD>\n") : "")  +
141                ((tradWords != null) ? ("<TD>" + tradWords.elementAt(i) + "</TD>\n") : "")  +
142                ((englWords != null) ? ("<TD>" + englWords.elementAt(i) + "</TD>\n") : "")  +
143                ((espaWords != null) ? ("<TD>" + espaWords.elementAt(i) + "</TD>\n") : "")  +
144                "</TR>\n"   );
145        }
146
147        sb.append("</TBODY>\n</TABLE>\n");
148        return sb.toString();
149    }
150}