# BlueDiamondHelper.java

    /*
    ** Module   : BlueDiamondHelper.java
    ** Abstract : creates Blue Diamond specific output text
    **
    ** Copyright (c) 2007-2017, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- -T- --JPRM-- ----------------Description-----------------
    ** 001 GES 20070822 NEW   @34963 Creates Blue Diamond specific output text.
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.e4gl;

    /**
     * Assists in creating Blue Diamond specific output text.
     * <p>
     * Compatibility notes:
     * <p>
     * <ul>
     *    <li> Blue Diamond does not escape semicolons or tildes when converting
     *         raw HTML to strings, that is a bug so it is not reproduced
     *    <li> Blue Diamond does not honor the keep-meta-content-type option but
     *         there seems no reason not to do so
     *    <li> Blue Diamond has flaws in its (META or WSMETA element) options
     *         parsing that are not reproduced (it may not find options that are
     *         actually coded so it will silently ignored these options)
     *    <li> Blue Diamond drops blank raw HTML lines but this would cause
     *         problems in PRE elements so this is not duplicated (they tried to
     *         do this for efficiency but it seems that this was an unexpected
     *         result)
     *    <li> Blue Diamond has parsing limitations with handling some input
     *         spread across multiple lines so it is expected that all such
     *         problems have been eliminated from input source, the fact that
     *         the {@link E4GLParser} doesn't duplicate these limitations should
     *         not cause any issues (e.g. statement and expression escapes must
     *         be on their own lines in Blue Diamond, there can't be a mixture of
     *         raw HTML and partial escapes on the same line)
     *    <li> Blue Diamond does not recognize all valid WebSpeed statement and
     *         expression escapes, but this is not duplicated since these other
     *         escapes should not be present in code that works with Blue Diamond
     *    <li> Blue Diamond does not handle the entity replacement and URL
     *         character encoding that is done by some HTML authoring tools when
     *         those tools are used to input E4GL in embedded expressions and/or
     *         embedded statements; these conversions are done since the input
     *         source code should not have those features otherwise it would have
     *         already failed
     * </ul>
     */
    public class BlueDiamondHelper
    extends BaseHelper
    {
       /** Standard Blue Diamond header for procedure files. */
       private static final String HDR =
          "/* DO NOT EDIT - THIS IS A GENERATED FILE */" +
          NEWLINE                                        +
          NEWLINE                                        +
          "{sga_util.i}"                                 +
          NEWLINE                                        +
          "{setcontent.i}"                               +
          NEWLINE                                        +
          NEWLINE;

       /**
        * Provides the known configuration to the helper and allows it to
        * prepare for use. This must be called before any other method can be
        * used.
        *
        * @param    opts
        *           The configuration.
        */
       public void initialize(Options opts)
       {
          super.initialize(opts);

          unsafe.put("\\\"", "~\"");

          if (unsafe.remove("\\\\") != null)
             unsafe.put("\\\\", "~\\\\");
       }

       /**
        * Return the filename suffix (extension) to be used.
        *
        * @return   The filename extension to use based on the known options.
        */
       public String suffix()
       {
          return opts.isInclude() ? ".i" : ".p";
       }

       /**
        * Return the text for the header of the generated file. This is output
        * before any parsed content.
        *
        * @return   The text to use based on the known options.
        */
       public String header()
       {
          return opts.isInclude() ? "" : HDR;
       }

       /**
        * Return the text used to start a line of raw HTML output.
        *
        * @param    content
        *           <code>true</code> if there is HTML content that is about to
        *           be emitted on the line, <code>false</code> to handle the
        *           no content case (just start the line, without any
        *           content prefix text).
        *
        * @return   The text to use based on the known options.
        */
       public String openHTML(boolean content)
       {
          return String.format("PUT STREAM StdOut UNFORMATTED %s",
                               content ? "'" : "");
       }

       /**
        * Return the text used to start the output of an embedded 4GL expression.
        *
        * @return   The text to use based on the known options.
        */
       public String openExprEscape()
       {
          return "' ";
       }

       /**
        * Return the text used to end the output of an embedded 4GL expression.
        *
        * @return   The text to use based on the known options.
        */
       public String closeExprEscape()
       {
          return " '";
       }

       /**
        * Return the text used to start a comment that removes the start or end
        * tag for a statement or expression escape.
        *
        * @param    expr
        *           <code>true</code> if this is an expression escape,
        *           <code>false</code> for a statement escape.
        *
        * @return   The text to use based on the known options. <code>null</code>
        *           if the escape should be removed instead of commented.
        */
       public String openComment(boolean expr)
       {
          return expr ? null : super.openComment(expr);
       }

       /**
        * Return the text used to end a comment that removes the start or end tag
        * for a statement or expression escape.
        *
        * @param    expr
        *           <code>true</code> if this is an expression escape,
        *           <code>false</code> for a statement escape.
        *
        * @return   The text to use based on the known options. <code>null</code>
        *           if the escape should be removed instead of commented.
        */
       public String closeComment(boolean expr)
       {
          return expr ? null : super.closeComment(expr);
       }

       /**
        * Return the text for the footer of the generated file. This is output
        * after all parsed content.
        *
        * @return   The text to use based on the known options.
        */
       public String footer()
       {
          return NEWLINE;
       }
    }