Package KFM.GUI.Templates

Source Code of KFM.GUI.Templates.KfmTemplate

/*
*  This software and supporting documentation were developed by
*
*    Siemens Corporate Technology
*    Competence Center Knowledge Management and Business Transformation
*    D-81730 Munich, Germany
*
*    Authors (representing a really great team ;-) )
*            Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
*  This software is Open Source under GNU General Public License (GPL).
*  Read the text of this license in LICENSE.TXT
*  or look at www.opensource.org/licenses/
*
*  Once more we emphasize, that:
*  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  WITHOUT ANY WARRANTY
*  REGARDING  THE  SOFTWARE,  ITS  PERFORMANCE OR
*  FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
*  ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
*  PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/


// KfmTemplate

// ************ package ******************************************************
package KFM.GUI.Templates;

// ************ imports ******************************************************

import KFM.GUI.KFM_HttpServletRequest; // For `quoteFormField�.
import KFM.GUI.Page;
import KFM.Language;
import KFM.JavaScript.*;
import KFM.log.*;

import com.oroinc.text.regex.MatchResult;                   // Get rid of this when it's included in RegExp.

import java.io.*;
import java.lang.String;
import java.util.Properties;

/** Extend HTML Template with KFM specific things.
* <!-- Note: Javadoc 1.2 copies first sentence to the package index. -->
*
* @see Template
* Please see template.
*
* Definitions: A <B>field</B> is anything where a user can enter input, e.g. an INPUT entity
* with TYPE='TEXT'. Currently, they are called "INPUT" or "SEARCH" in the templates.
*
* Note that `KfmTemplate� has `replace� methods and `create� methods.
*
* <pre>
* ====================
* History:
*    creation date: 99.02.26
* --------------------
* Changes:
*    <none>
* </pre>
*
* @version 1.0 (99.02.27)
*/
public class KfmTemplate extends Template {

    //
    // Variables
    //

    /** Needed for automatic replacement of <KFM defaultStyleSheet> ... </KFM>. */
    String defaultStylesheet;

    /** Needed for automatic replacement of <KFM footer> ... </KFM>. */
    String footerUrl;

    /** A page that knows how to generate its footer. */
    Page mPage = null;

    //
    // Methods
    //

    /** Prepare a template without loading it.
     *
     * Should propably be deprecated, use the constructor with the Page argument instead.
     *
     *@param theNamePrefix  E.g. "NewProposal" for a template file
     *                      "NewProposal." + Language + templateExtension
     *                      for example "NewProposal.German.kfmt.htm".
     * @deprecated
     */
    public KfmTemplate(
        /* TemplateObserver observer,*/
        String theDir,
        String theNamePrefix)
    {
        this(theDir, theNamePrefix, "", "");
    }

    /** Prepare a template without loading it.
     *
     * Should propably be deprecated, use the constructor with the Page argument instead.
     *
     *@param theNamePrefix  E.g. "NewProposal" for a template file
     *                      "NewProposal." + Language + templateExtension
     *                      for example "NewProposal.German.kfmt.htm".
     * @deprecated
     */
    public KfmTemplate(
        /* TemplateObserver observer,*/
        String theDir,
        String theNamePrefix,
        String ddefaultStylesheet,
        String ffooterUrl)
    {
        super(theDir, theNamePrefix);
        defaultStylesheet = ddefaultStylesheet;
        footerUrl = ffooterUrl;
    }

    /** Prepare a template without loading it.
     *
     * This is the constructor you should use without the new logging concept (KFMLog)
     *
     * @param theNamePrefix  E.g. "NewProposal" for a template file
     *                      "NewProposal." + Language + templateExtension
     *                      for example "NewProposal.German.kfmt.htm".
     * @param aPage          A page that knows how to generate its footer.
     */
    public KfmTemplate(
        /* TemplateObserver observer,*/
        String theDir,
        String theNamePrefix,
        String ddefaultStylesheet,
        Page   aPage)
    {
        super(theDir, theNamePrefix);
        defaultStylesheet = ddefaultStylesheet;
        footerUrl = "";
        mPage = aPage;
    }

    /** Prepare a template without loading it.
     *
     * This is the constructor you should use with the new logging concept (KFMLog)
     *
     * @param theNamePrefix  E.g. "NewProposal" for a template file
     *                      "NewProposal." + Language + templateExtension
     *                      for example "NewProposal.German.kfmt.htm".
     * @param aPage a page that knows how to generate its footer.
     * @param aLog the KFMLog to log any output to
     */
    public KfmTemplate(
        /* TemplateObserver observer,*/
        String theDir,
        String theNamePrefix,
        String ddefaultStylesheet,
        Page   aPage,
        KFMLog aLog)
    {
        super(theDir, theNamePrefix, aLog);
        defaultStylesheet = ddefaultStylesheet;
        footerUrl = "";
        mPage = aPage;
    }


    // ************************************************************
    // * Replace methods for fields
    // ************************************************************

    /** Replace a KFM-command by a text input field for use in forms, i.e. an INPUT entity with TYPE='TEXT'.
     *
     * See replaceAllKfmTextInputField below.
     */
    public void replaceAllKfmTextInputField (String kfmLabel, String name,
                                             String value, String maxLength)
    {
        replaceAllKfmTextInputField(kfmLabel, name, value, maxLength, false);
    }

    /** Replace a KFM-command by a text input field for use in forms, i.e. an INPUT entity with TYPE='TEXT'.
     *
     * Needed params within the label:<BR>
     *     SIZE
     *
     * Example substitution:<BR>
     *     <KFM titleField size="32"> test </KFM>
     *
     * See `createTextInputField�.
     *
     *@param kfmLabel     Case insensitive label of the KFM-command, e.g. `titleField�, `emailField�.
     *@param name         Value of INPUTs parameter `NAME�, it is also the name of the HTTP parameter.
     *@param value        Value of INPUTs parameter `VALUE�, it is also a value for the HTTP parameter named `name�.
     *@param maxLength    maximum of characters to put into the field
     *@param highlighted  true if the TextInputField was HighLighted
     */
    public void replaceAllKfmTextInputField (String kfmLabel, String name,
                                             String value, String maxLength, boolean highlighted)
    {
        // A loop like this will only work correctly when the HTML code is wellformed and follows our restrictions.
        // All params must appear.
        String size;
        if(! highlighted)
            do {
                size  = getKfmParam(kfmLabel, "size");
            } while(replaceKfm(kfmLabel,
                           createTextInputField(kfmLabel, name, value, size, maxLength)));
        else
            do {
                size  = getKfmParam(kfmLabel, "size");
            } while(replaceKfm(kfmLabel,    "<a name=\"" + kfmLabel + "\">" +
                                            createTextInputField(kfmLabel, name, value, size, maxLength) +
                                            "</a>"));
    }

    /** Replace a KFM-command by a passord field for use in forms, i.e. an INPUT entity with TYPE='PASSWORD'.
     *
     * See replaceAllKfmInputPassword below.
     */
    public void replaceAllKfmInputPassword (String kfmLabel, String name,
                                             String value, String maxLength)
    {
        replaceAllKfmInputPassword(kfmLabel, name, value, maxLength, false);
    }

    /** Replace a KFM-command by a passord field for use in forms, i.e. an INPUT entity with TYPE='PASSWORD'.
     *
     * Needed params within the label:<BR>
     *     SIZE
     *
     * Example substitution:<BR>
     *     <KFM titleField size="32"> test </KFM>
     *
     * See `createInputPassword�.
     *
     *@param kfmLabel  Case insensitive label of the KFM-command, e.g. `titleField�, `emailField�.
     *@param name      Value of INPUTs parameter `NAME�, it is also the name of the HTTP parameter.
     *@param value     Value of INPUTs parameter `VALUE�, it is also a value for the HTTP parameter named `name�.
     *@param maxLength     maximum of characters to put into the field
     *@param highlighted   true if the Passwordbox should be highlighted
     */
    public void replaceAllKfmInputPassword (String kfmLabel, String name,
                                             String value, String maxLength, boolean highlighted)
    {
        // A loop like this will only work correctly when the HTML code is wellformed and follows our restrictions.
        // All params must appear.
        String size;
        if(! highlighted)
            do {
                size  = getKfmParam(kfmLabel, "size");
                } while(replaceKfm(     kfmLabel,
                                        createInputPassword(kfmLabel, name, value, size, maxLength)));
        else
            do {
                size  = getKfmParam(kfmLabel, "size");
            } while(replaceKfm(kfmLabel,    "<a name=\"" + kfmLabel + "\">" +
                                            createInputPassword(kfmLabel, name, value, size, maxLength) +
                                            "</a>"));
    }

    /** Replace a KFM-command by a text area field for use in forms, i.e. an TEXTAREA entity.
     *
     * See `createTextAreaField�.
     *
     *@param kfmLabel  Case insensitive label of the KFM-command, e.g. `titleField�, `emailField�.
     *@param name      Value of INPUTs parameter `NAME�, it is also the name of the HTTP parameter.
     *@param content   Value of TEXTAREAs content, it is also a value for the HTTP parameter named `name�.
     *                 Note: Analogous to the parameter `VALUE� of the entity INPUT with TYPE='TEXT'.
     */
    public void replaceAllKfmTextAreaField (String kfmLabel, String name, String content)
    {
        // A loop like this will only work correctly when the HTML code is wellformed and follows our restrictions.
        // All params must appear.
        String rows, cols;
        do {
            rows = getKfmParam(kfmLabel, "rows");
            cols = getKfmParam(kfmLabel, "cols");
        } while(replaceKfm(kfmLabel,
                           createTextAreaField(kfmLabel, name, content, rows, cols)));
    }

    /** Replace a KFM-command by a SELECT element for use in forms.
     *
     * See `createSelect�.
     */
    public void replaceAllKfmSelect (
        String kfmLabel,
        String name,
        String[] values,
        String[] texts,
        String selectedvalue)
    {
        // A loop like this will only work correctly when the HTML code is wellformed and follows our restrictions.
        // All params must appear.
        String size;
        do {
            size = getKfmParam(kfmLabel, "size");
        } while(replaceKfm(kfmLabel,
                           createSelect(kfmLabel, name, size, values, texts, selectedvalue)));
    }

    // ************************************************************
    // * Create methods.
    // ************************************************************

    /**
     *@param formtag       Will usually be the current `Page�s `newFormOpenTag()�.
     *@param hiddenParams  Will usually be the current `KFM_HttpServletRequest�s `toHiddenParams()�.
     */
  static public String createFormOpentag(String formtag, String hiddenParams)
  {
      return formtag + hiddenParams;
  }

    /** Build a text input field for use in forms, i.e. an INPUT entity with TYPE='TEXT'.
     *
     * A text input field has one line and no scroll bars. See also createTextAreaField.
     *
     * Since 99-12-13, quotes the value correctly.
     *
     *@param kfmLabel  Case insensitive label of the KFM-command, e.g. `titleField�, `emailField�.
     *@param name      Value of INPUT's parameter `NAME�, it is also the name of the HTTP parameter.
     *@param value     Value of INPUT's parameter `VALUE�, it is also a value for the HTTP parameter named `name�.
     *                 Angle brackets, single quotes, double quotes and ampersands are quoted correctly.
     *@param size      Value of INPUT's parameter `SIZE�, width of the text field in characters.
     *@param maxLength Value of INPUT's parameter `MAXLENGTH�, maximum number of chars the user may type.
     *@return          HTML code for INPUT entity.
     */
    static public String createTextInputField (
        String kfmLabel,
        String name,
        String value,
        String size,
        String maxLength)
    {
        return "<INPUT TYPE='text' NAME='" + name
            + "' SIZE='" + size
            + "' MAXLENGTH='" + maxLength
            + "' VALUE='" + KFM_HttpServletRequest.quoteFormField(value) + "'>";
    }

    /** Build a radio button.
     *
     * Since 99-12-13, quotes the value correctly.
     *
     *@param name      Value of INPUT's parameter `NAME�, it is also the name of the HTTP parameter.
     *@param value     Value of INPUT's parameter `VALUE�, it is also a value for the HTTP parameter named `name�.
     *                 Angle brackets, single quotes, double quotes and ampersands are quoted correctly.
     *@return          HTML code for INPUT entity.
     */
    static public String createRadioButton (
        String name,
        String value,
        boolean selected)
    {
        return "<INPUT TYPE='RADIO' NAME='" + name
                + "' VALUE='" + KFM_HttpServletRequest.quoteFormField(value) + "'"
                + (selected ? " CHECKED" : "") + ">";
    }

    /** Build a checkbutton.
     *
     *@param name      Value of INPUT's parameter `NAME�, it is also the name of the HTTP parameter.
     *@param value     Value of INPUT's parameter `VALUE�, it is also a value for the HTTP parameter named `name�.
     *                 Angle brackets, single quotes, double quotes and ampersands are quoted correctly.
     *@return          HTML code for INPUT entity.
     */
    static public String createCheckButton (
        String name,
        String value,
        boolean selected)
    {
        return "<INPUT TYPE='CHECKBOX' NAME='" + name
                + "' VALUE='" + KFM_HttpServletRequest.quoteFormField(value) + "'"
                + (selected ? " CHECKED" : "") + ">";
    }

    /** Build a SELECT element.
     *
     * Use this method when you may have several selected items.
     *
     * A SELECT element is displayed as a list where you can select one or several lines.
     * If the list has more elements than its size can display, you get a scroll bar.
     *
     * Example:
     *
     * <SELECT SIZE="3" NAME="jobrole">
     *     <OPTION VALUE="Projektmitarbeiter">Projektmitarbeiter</OPTION>
     *     <OPTION SELECTED VALUE="Projektleiter">Projektleiter</OPTION>
     *     <OPTION VALUE="Projektentscheider">Projektentscheider</OPTION>
     * </SELECT>
     *
     * Since 99-12-13, quotes the value correctly.
     *
     * @param kfmLabel   Case insensitive label of the KFM-command, e.g. `titleField�, `emailField�.
     * @param name       Name of HTTP parameter.
     * @param size       Number of lines to display.
     * @param value      Array of values for the HTTP parameter `name�.
     *                   Angle brackets, single quotes, double quotes and ampersands are quoted correctly.
     * @param texts      Array of HTML texts to be shown.
     * @param selecteds  Array with true for each item to be selected.
     */
    static public String createSelect (
        String kfmLabel,
        String name,
        String size,
        String[] values,
        String[] texts,
        boolean[] selecteds)
    {
        StringBuffer ret = new StringBuffer("<SELECT SIZE='" + size + "' NAME='" + name + "'>\n");
        for(int i = 0; i < values.length; ++ i) {
            ret.append(createOption(values[i], texts[i], selecteds[i]));
        }
        return ret.append("</SELECT>\n").toString();
    }

    /** Build a SELECT element.
     *
     * Use this method when you have exactly one selected item.
     *
     * For the rest of the documentation consult the method above.
     *
     * Since 99-12-13, quotes the value correctly.
     */
    static public String createSelect (
        String kfmLabel,
        String name,
        String size,
        String[] values,
        String[] texts,
        String selectedvalue)
    {
        StringBuffer ret = new StringBuffer("<SELECT SIZE='" + size + "' NAME='" + name + "'>\n");
        for(int i = 0; i < values.length; ++ i) {
            ret.append(createOption(values[i], texts[i], selectedvalue.equals(values[i])));
        }
        return ret.append("</SELECT>\n").toString();
    }

    /** Build one OPTION for a SELECT.
     *
     * See `createSelect� for an example.
     *
     * Since 99-12-13, quotes the value correctly.
     */
    static public String createOption (
        String value,
        String text,
        boolean selected)
    {
        return "<OPTION " + (selected ? "SELECTED " : "")
            + "VALUE='" + KFM_HttpServletRequest.quoteFormField(value) + "'>"
            + text + "</OPTION>\n";
    }

    /** Build a Password input field for use in forms, i.e. an INPUT entity with TYPE='PASSWORD'.
     *
     * A password input field has one line and no scroll bars. See also createTextAreaField.
     *
     * Since 99-12-13, quotes the value correctly.
     *
     *@param kfmLabel  Case insensitive label of the KFM-command, e.g. `titleField�, `emailField�.
     *@param name      Value of INPUT's parameter `NAME�, it is also the name of the HTTP parameter.
     *@param value     Value of INPUT's parameter `VALUE�, it is also a value for the HTTP parameter named `name�.
     *                 Angle brackets, single quotes, double quotes and ampersands are quoted correctly.
     *@param size      Value of INPUT's parameter `SIZE�, width of the text field in characters.
     *@param maxLength Value of INPUT's parameter `MAXLENGTH�, maximum number of chars the user may type.
     *@return          HTML code for INPUT entity.
     */
    static public String createInputPassword (
        String kfmLabel, /* not used??? */
        String name,
        String value,
        String size,
        String maxLength)
    {
        return "<INPUT TYPE='PASSWORD' NAME='" + name
            + "' SIZE='" + size
            + "' MAXLENGTH='" + maxLength
            + "' VALUE='" + KFM_HttpServletRequest.quoteFormField(value) + "'>";
    }

    /** Build a text area field for use in forms, i.e. an TEXTAREA entity.
     *
     * A text area field has several lines and scroll bars. See also createTextInputField.
     *
     * Since 99-12-13, quotes the value correctly.
     *
     *@param kfmLabel  Case insensitive label of the KFM-command, e.g. `abstractField� [dt. Zusammenfassung], `ideaConceptField�.
     *@param name      Value of TEXTAREA's parameter `NAME�, it is also the name of the HTTP parameter.
     *@param content   Value of TEXTAREA's content, it is also a value for the HTTP parameter named `name�.
     *                 Note: Analogous to the parameter `VALUE� of the entity INPUT with TYPE='TEXT'.
     *                 Angle brackets, single quotes, double quotes and ampersands are quoted correctly.
     *@param rows      Value of TEXTAREA's parameter `ROWS�, number of (character) rows of text area field.
     *@param cols      Value of TEXTAREA's parameter `COLS�, number of (character) columns of text area field.
     *@return          HTML code for TEXTAREA entity.
     */
    public String createTextAreaField (
        String kfmLabel,
        String name,
        String content,
        String rows,
        String cols)
    {
        return "<TEXTAREA NAME='" + name
            + "' ROWS='" + rows
            + "' COLS='" + cols
            + "' WRAP='physical'>"
            + KFM_HttpServletRequest.quoteFormField(content)
            + "</TEXTAREA>";
    }

    //
    // End Fields
    //

    /** Does all KFM-specific replacements.
     *
     * This function is intended to be called by all subclasses.
     *
     * Implements:
     *   <KFM rem>a design remark</KFM>   Use for remarks that should be displayed
     *                                    in WWW browser during design only.
     *   <KFM defaultStylesheet></KFM>
     *   <KFM goBack></KFM>
     *   <KFM footer></KFM>
     */
    public void execute ()
    {
        replaceAllKfm("rem", "");

        if (! defaultStylesheet.equals("")) {
            replaceAllKfm("defaultStylesheet",
                "<LINK REL=\"stylesheet\" HREF=\"" + defaultStylesheet + "\">");
        }

        replaceAllKfm("context", "/kfm-servlet");

        replaceAllKfm("goBack", GoBackButton.printGoBackFunction(currentLanguage));

        if(mPage != null) {
            replaceAllKfm("footer",
                mPage.createFooter(currentLanguage.toString()));
        } else if(! footerUrl.equals("")) {
            replaceAllKfm("footer",
                Page.createFooter(currentLanguage.toString(), footerUrl));
        }
    }

    //
    // Usage sample.
    //

    /**
     * Sample of usage.
     */
    public static void main (String args[])
         throws FileNotFoundException, IOException
    {
        KfmTemplate t = new KfmTemplate("o:/KFM/www-docs/ProjectIdC/protected/templates/",
                                        "Form_insertIdea");
        t.setDebug(true);

        /*D*/ System.out.println("Lese ein.");
        t.startWithLanguage(Language.German);

        /*D*/ System.out.println("Ersetze.");
        t.execute();
        t.markCommandsLeft();

        /*D*/ System.out.println("Gebe aus.");
        t.write(System.out);

        /*D*/ System.out.println();
        /*D*/ System.out.println("Fertig");
    }
}
TOP

Related Classes of KFM.GUI.Templates.KfmTemplate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.