# SaxEntityImpl.java

    /*
    ** Module   : SaxEntityImpl
    ** Abstract : SAX attributes, reader, writer common fatures iplementation.
    **
    ** Copyright (c) 2013-2023, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ---------------------------------------Description---------------------------------------
    ** 001 EVL 20130208 Created initial version. Implementation is limited to the stubs to get the
    **                  converted java code compiled.
    ** 002 EVK 20130903 Moved valid() and unknown() amd removeAttribute() from XCommonImpl.
    ** 003 CA  20130927 Removed unknown() API, as by default the resource is known.
    ** 004 EVK 20131001 Implement methods and class logic. Added methods to process some SAX errors.
    ** 005 EVK 20140113 Moved method valid() to XCommonImpl class.
    ** 006 OM  20201030 Invalid attribute API support for getters/setters.
    ** 007 CA  20221006 UNIQUE-ID is kept as Java type instead of BDT.  Refs #6827
    **     CA  20230116 Avoid using handle.unwrap, handle.getReference or other BDT usage from within FWD runtime.
    */

    /*
    ** 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.xml;

    import com.goldencode.p2j.util.*;

    import static com.goldencode.p2j.xml.SaxWriter.WriteStatus;

    /**
     * Implementation the common features for SAX Reader, SAX Writer and SAX Attributes Progress
     * support objects.
     */
    public class SaxEntityImpl
    extends XCommonImpl
    implements SaxEntity
    {
       /** unique value for a given handle object type within an 4gl session. */
       private Long uniqueId = null;

       /** amd-data variable. */
       private String admData = null;

       /**
        * Default constructor.
        */
       public SaxEntityImpl()
       {
          // no-op
       }

       /**
        * Gets the the unique ID number associated to this object by the underlying system. Not the
        * same as the handle value itself.
        *
        * @return   The unique integer value for a given handle object type within an 4gl session.
        */
       @Override
       public integer getUniqueID()
       {
          if (uniqueId == null)
          {
             uniqueId = UniqueIdGenerator.getUniqueId(UniqueIdGenerator.IdKind.SAX_OBJECT);
          }
          return new integer(uniqueId);
       }

       /**
        * Get the value of the ADM-DATA attribute.
        *
        * @return   See above.
        */
       @Override
       public character getADMData()
       {
          return new character(admData);
       }

       /**
        * Set the value of the ADM-DATA attribute.
        *
        * @param    value
        *           The new value.
        */
       @Override
       public void setADMData(String value)
       {
          setADMData(new character(value));
       }

       /**
        * Set the value of the ADM-DATA attribute.
        *
        * @param    value
        *           The new value.
        */
       @Override
       public void setADMData(character value)
       {
          admData = value.getValue();
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        * @param    uri
        *           The URI of the attribute to insert or '?' for unknown value.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(String name, Text value, Text uri)
       {
          return insertAttribute(new character(name), value, uri);
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        * @param    uri
        *           The URI of the attribute to insert or '?' for unknown value.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(Text name, String value, Text uri)
       {
          return insertAttribute(name, new character(value), uri);
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        * @param    uri
        *           The URI of the attribute to insert or '?' for unknown value.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(Text name, Text value, String uri)
       {
          return insertAttribute(name, value, new character(uri));
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        * @param    uri
        *           The URI of the attribute to insert or '?' for unknown value.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(String name, String value, Text uri)
       {
          return insertAttribute(new character(name), new character(value), uri);
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        * @param    uri
        *           The URI of the attribute to insert or '?' for unknown value.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(String name, Text value, String uri)
       {
          return insertAttribute(new character(name), value, new character(uri));
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        * @param    uri
        *           The URI of the attribute to insert or '?' for unknown value.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(Text name, String value, String uri)
       {
          return insertAttribute(name, new character(value), new character(uri));
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        * @param    uri
        *           The URI of the attribute to insert or '?' for unknown value.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(String name, String value, String uri)
       {
          return insertAttribute(new character(name), new character(value), new character(uri));
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(String name, Text value)
       {
          return insertAttribute(new character(name), value, new character());
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(Text name, String value)
       {
          return insertAttribute(name, new character(value), new character());
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(String name, String value)
       {
          return insertAttribute(new character(name), new character(value), new character());
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(Text name, Text value)
       {
          return insertAttribute(name, value, new character());
       }

       /**
        * Adding the single attribute to the start tag in the XML document if applied for SAX writer
        * object. For SAX attributes object inserts attribute and its name into SAX attributes object.
        *
        * @param    name
        *           The attribute name to insert.
        * @param    value
        *           The attribute value to insert.
        * @param    uri
        *           The URI of the attribute to insert or '?' for unknown value.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical insertAttribute(Text name, Text value, Text uri)
       {
          invalidAttribute("INSERT-ATTRIBUTE", false);
          return new logical(true);
       }

       /**
        * Removes the specified attribute.
        *
        * @param    name
        *           The name of the attribute to remove.
        * @param    uri
        *           The URI of the attribute to remove.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical removeAttribute(character name, character uri)
       {
          invalidAttribute("REMOVE-ATTRIBUTE", false);
          return new logical(true);
       }

       /**
        * Removes the specified attribute.
        *
        * @param    index
        *           The 1-based index of the attribute to remove.
        * @param    uri
        *           The URI of the attribute to remove.
        *
        * @return   The <code>true</code> in case of success <code>false</code> otherwise.
        */
       @Override
       public logical removeAttribute(NumberType index, character uri)
       {
          invalidAttribute("REMOVE-ATTRIBUTE", false);
          return new logical(true);
       }

       /**
        * Process and display invalid read/write mode error. It displays appropriate error message.
        *
        * @param    attribute
        *           The read-only attribute.
        */
       protected void invalidReadWriteModeError(String attribute)
       {
          if (attribute == null)
          {
             throw new IllegalArgumentException("The attribute is null !");
          }
          String err = "Argument for SET-OUTPUT-DESTINATION must be a 'file', 'stream', " +
                       "'stream-handle','memptr', or 'longchar'";
          ErrorManager.recordOrShowError(14607, err, false, false, false);
       }

       /**
        * Process and display invalid status error. It displays appropriate error message.
        *
        * @param    attribute
        *           The read-only attribute.
        * @param    status
        *           Current status of SAX-WRITER.
        */
       protected void invalidStatusAttributeError(String attribute, WriteStatus status)
       {
          if (attribute == null)
          {
             throw new IllegalArgumentException("The attribute is null !");
          }
          String msg = "Error: setting attribute %s invalid while WRITE-STATUS is %s";
          String err = String.format(msg, attribute.toUpperCase(), status);
          ErrorManager.recordOrShowError(14630, err, false, false, false);
       }

       /**
        * Process and display invalid status error. It displays appropriate error message.
        *
        * @param    method
        *           The 4gl method name..
        */
       protected void writerOutputError(String method)
       {
          if (method == null)
          {
             throw new IllegalArgumentException("The method is null !");
          }
          String msg = "%s encountered an error with the output destination or encoding";
          String err = String.format(msg, method.toUpperCase());
          ErrorManager.recordOrShowError(14613, err, false, false, false);
       }

       /**
        * Process and display invalid status error. It displays appropriate error message.
        *
        * @param    method
        *           The 4gl method name.
        * @param    status
        *           Current status of SAX-WRITER.
        */
       protected void invalidStatusMethodError(String method, WriteStatus status)
       {
          if (method == null)
          {
             throw new IllegalArgumentException("The method is null !");
          }
          String msg = "%s method invalid while WRITE-STATUS is %s";
          String err = String.format(msg, method.toUpperCase(), status);
          ErrorManager.recordOrShowError(14609, err, false, false, false);
       }
    }