# DataModelAst.java

    /*
    ** Module   : DataModelAst.java
    ** Abstract : A data model-specific AST.
    **
    ** Copyright (c) 2005-2022, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- --JPRM-- ----------------------------------Description------------------------------------
    ** 001 ECF 20050426   @21000 Created initial version.
    ** 002 ECF 20050526   @21302 Renamed resolveToLiteral to resolveConstant.
    ** 003 ECF 20050608   @21461 Removed unused token types.
    ** 004 ECF 20050616   @21538 Added relation types: MANY_TO_ONE, ONE_TO_ONE and ONE_TO_MANY.
    ** 005 ECF 20050628   @21603 Added COMPONENT type.
    ** 006 ECF 20050707   @21673 Added tokens needed for initial value processing. INITIAL, STRING,
    **                           NUM_LITERAL, DEC_LITERAL, BOOL_TRUE, BOOL_FALSE,
    **                           DATE_LITERAL, UNKNOWN.
    ** 007 ECF 20050720   @21775 Changed COMPONENT to COMPOSITE.
    ** 008 ECF 20060401   @25332 Added NULL_LITERAL.
    ** 009 GES 20090518   @42394 Import change.
    ** 010 OM  20121207          Added support for constructing sequence nodes into to p2o file.
    ** 011 GES 20130121          Added DATETIME_TZ_LITERAL.
    ** 012 OM  20130521          Added DATETIME_LITERAL.
    ** 013 AIL 20190722          Added TRIGGER.
    ** 014 OM  20200331          Added RELATION_COL.
    ** 015 CA  20221010          Avoid reflection when creating a new instance - added 'newInstance' method.
    **                           Refs #6813
    */

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

    import java.util.*;
    import antlr.*;
    import com.goldencode.ast.*;

    /**
     * Provides symbolic token name lookups that are specific to a data model
     * AST. All other function is implemented in the superclass.
     * <p>
     * Currently, line and column number and filename information is not used.
     *
     * @see  DataModelTokenTypes
     */
    public class DataModelAst
    extends AnnotatedAst
    implements DataModelTokenTypes
    {
       /** Map of token types to token names */
       private static final Map<Integer, String> TOKEN_NAMES = new HashMap<>();

       /** Map of token names to token types */
       private static final Map<String, Integer> TOKEN_TYPES = new HashMap<>();

       // Initialize token maps.
       static
       {
          Object[] mapping = new Object[] {
             DATA_MODEL,          "DATA_MODEL",
             TABLE,               "TABLE",
             PROPERTY,            "PROPERTY",
             CLASS,               "CLASS",
             GENERATOR,           "GENERATOR",
             PRIMARY,             "PRIMARY",
             INDEX,               "INDEX",
             UNIQUE,              "UNIQUE",
             INDEX_COL,           "INDEX_COL",
             CONSTR_COL,          "CONSTR_COL",
             FOREIGN,             "FOREIGN",
             MANY_TO_ONE,         "MANY_TO_ONE",
             ONE_TO_MANY,         "ONE_TO_MANY",
             ONE_TO_ONE,          "ONE_TO_ONE",
             COMPOSITE,           "COMPOSITE",
             INITIAL,             "INITIAL",
             STRING,              "STRING",
             NUM_LITERAL,         "NUM_LITERAL",
             DEC_LITERAL,         "DEC_LITERAL",
             BOOL_TRUE,           "BOOL_TRUE",
             BOOL_FALSE,          "BOOL_FALSE",
             DATE_LITERAL,        "DATE_LITERAL",
             DATETIME_LITERAL,    "DATETIME_LITERAL",
             DATETIME_TZ_LITERAL, "DATETIME_TZ_LITERAL",
             UNKNOWN,             "UNKNOWN",
             NULL_LITERAL,        "NULL_LITERAL",
             SEQUENCE,            "SEQUENCE",
             TRIGGER,             "TRIGGER",
             RELATION_COL,        "RELATION_COL",
          };

          // the following casts are a bit risky but as long as the [mapping] array is correctly
          // defined, everything should be just fine.
          int len = mapping.length;
          for (int i = 0; i < len; i += 2)
          {
             TOKEN_NAMES.put((Integer) mapping[i], (String) mapping[i + 1]);
             TOKEN_TYPES.put((String)mapping[i + 1], (Integer) mapping[i]);
          }
       }

       /**
        * Resolve the given token name to a numeric token type. The lookup is
        * case-insensitive.
        *
        * @param   tokenName
        *          String constant representing token name.
        *
        * @return  Integral token type or <code>null</code> if name could not
        *          be resolved.
        */
       public static Object resolveConstant(String tokenName)
       {
          return TOKEN_TYPES.get(tokenName.toUpperCase());
       }

       /**
        * Default constructor.
        */
       public DataModelAst()
       {
          super();
       }

       /**
        * Constructor which accepts and wrappers token.
        *
        * @param   token
        *          Token upon which this node is based.
        */
       public DataModelAst(Token token)
       {
          super(token);
       }

       /**
        * Translates a text representation of a token type into the
        * actual integer value.
        *
        * @param    tokenName
        *           The text representation of the token type.
        *
        * @return   A valid integer token type value or -1 if no match was found.
        */
       public int lookupTokenType(String tokenName)
       {
          Integer type = TOKEN_TYPES.get(tokenName);

          return (type != null) ? type : -1;
       }

       /**
        * Translates an integer value of a token type into the associated
        * human readable text representation.
        *
        * @param    type
        *           The integer token type.
        *
        * @return   A valid symbolic token type name or <code>null</code> if
        *           no match was found.
        */
       public String lookupTokenName(int type)
       {
          return TOKEN_NAMES.get(type);
       }

       /**
        * Returns the symbolic representation of this AST's token type.
        *
        * @return  Symbolic token name for this AST's token type.
        */
       public String getSymbolicTokenType()
       {
          return lookupTokenName(getType());
       }

       /**
        * Create a new instance of this AST implementation.
        *
        * @return   A new {@link DataModelAst} instance.
        */
       @Override
       protected AnnotatedAst newInstance()
       {
          return new DataModelAst();
       }
    }