ConverterHelper.java

/*
** Module   : ConverterHelper.java
** Abstract : generic Progress to Java conversion helper routines
**
** Copyright (c) 2005-2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- --JPRM-- ----------------------------------Description------------------------------------
** 001 GES 20050406   @20938 Created initial version which provides a
**                           simple helper to create import statements
**                           that aren't duplicated.
** 002 GES 20050603   @21400 Exposed the createImport() static to rule
**                           sets via a pattern engine worker.
** 003 GES 20050928   @22872 Added safety code and change signature to
**                           match JavaPatternWorker.
** 004 ECF 20060204   @24342 Added user functions to access AstKey public
**                           constants. Necessary to work around
**                           expression engine's inability to access
**                           static variables/methods directly.
** 005 ECF 20060407   @25423 Added another AstKey public constant user
**                           function. Exposes AstKey.KEY_WILDCARD.
** 006 GES 20080701   @39053 Added static import support.
** 007 GES 20090518   @42378 Import change.
** 008 CA  20201008          Added createImport(JavaAst, import) to create an import statement to a specific
**                           COMPILE_UNIT.
** 009 CA  20211214          The AST manager plugin must be context-local, for runtime conversion, as the 
**                           InMemoryRegistryPlugin is not thread-safe.
*/
/*
** 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.convert;

import com.goldencode.ast.*;
import com.goldencode.p2j.uast.*;
import com.goldencode.p2j.pattern.*;


/**
 * A simple implementation of a helper to centralize some conversion services
 * that are generic for multiple workers.
 *
 * @author   GES
 * @version  1.0.0
 */
public final class ConverterHelper
extends AbstractPatternWorker
implements JavaTokenTypes
{      
   /**
    * Default constructor which initializes libraries.
    */
   public ConverterHelper()
   {
      super();
      setLibrary(new Helper());
   }
   
   /**
    * Creates an import statement (<code>KW_IMPORT</code> with text as the
    * package/class to import) or a static import statement 
    * (<code>STATIC_IMPORT</code> with text as the class members to import)
    * and attaches it to the specified parent, IF an equivalent import
    * statement does not already exist.
    *
    * @param    text
    *           The package/class to import.
    * @param    stat
    *           <code>true</code> to create a <code>STATIC_IMPORT</code> node
    *           otherwise create a <code>KW_IMPORT</code>.
    * @param    jroot
    *           The root node of the Java tree in which to create the import
    *           statement.
    * @param    resolver
    *           The current instance of the {@link AstSymbolResolver} in use
    *           in the pattern engine.
    *
    * @return   The created AST or <code>null</code> if text is
    *           <code>null</code>, jroot is not a valid Java root AST node,
    *           if an equivalent import statement already exists or if there
    *           are errors during creation of the new Java AST.
    */
   public static JavaAst createImport(String            text,
                                      boolean           stat,
                                      JavaAst           jroot,
                                      AstSymbolResolver resolver)
   throws IllegalArgumentException
   {
      if (text == null || jroot == null || resolver == null)
         return null;
      
      boolean exists = false;
      
      // make sure this is the root node for this tree
      if (!jroot.isRoot())
      {
         // get the root node
         jroot = (JavaAst) jroot.getAncestor(-1);
      }
      
      int ttype = stat ? STATIC_IMPORT : KW_IMPORT;
      
      // search for an existing statement that matches
      JavaAst child = (JavaAst) jroot.getImmediateChild(ttype, null);
      
      while (child != null)
      {
         if (text.equals(child.getText()))
         {
            exists = true;
            break;
         }
         
         child = (JavaAst) jroot.getImmediateChild(ttype, child);
      }
      
      JavaAst ast = null;
      
      // create it if needed
      if (!exists)
      {
         ast = JavaPatternWorker.createAst(ttype, text, jroot, resolver, -1);
      }
      
      return ast;
   }            
   
   /**
    * Exports helper user-functions to pattern engine rules for Progress to
    * Java conversion.
    */
   public class Helper
   {
      /**
       * Create an import statement in the specified {@link JavaTokenTypes#COMPILE_UNIT}.
       * 
       * @param    jroot
       *           The root JAST.
       * @param    text
       *           The package/class to import.
       *           
       * @return   The created AST or <code>null</code> if text is <code>null</code>, jparent is not a valid 
       *           Java AST node, if an equivalent import statement already exists or if there are errors 
       *           during creation of the new Java AST.
       */
      public Aast createImport(JavaAst jroot, String text)
      {
         return ConverterHelper.createImport(text, false, jroot, getResolver());
      }
      
      /**
       * Creates an import statement (<code>KW_IMPORT</code> with text as the
       * package/class to import) and attaches it into the target tree, IF
       * an equivalent import statement does not already exist.
       *
       * @param    text
       *           The package/class to import.
       *
       * @return   The created AST or <code>null</code> if text is
       *           <code>null</code>, jparent is not a valid Java AST node,
       *           if an equivalent import statement already exists or if
       *           there are errors during creation of the new Java AST.
       */
      public Aast createImport(String text)
      {
         JavaAst jroot = (JavaAst) getResolver().getTargetRootAst();
         
         return ConverterHelper.createImport(text, false, jroot, getResolver());
      }
      
      /**
       * Creates a static import statement (<code>STATIC_IMPORT</code> with
       * text as the class members to import) and attaches it into the target
       * tree, IF an equivalent import statement does not already exist.
       *
       * @param    text
       *           The package/class to import.
       *
       * @return   The created AST or <code>null</code> if text is
       *           <code>null</code>, jparent is not a valid Java AST node,
       *           if an equivalent import statement already exists or if
       *           there are errors during creation of the new Java AST.
       */
      public Aast createStaticImport(String text)
      {
         JavaAst jroot = (JavaAst) getResolver().getTargetRootAst();
         
         return ConverterHelper.createImport(text, true, jroot, getResolver());
      }
      
      /**
       * Get the constant which defines {@link com.goldencode.p2j.uast.AstKey
       * AstKey}'s case-insensitive token text comparison flag.
       *
       * @return  An <code>AstKey</code> constant.
       *
       * @see     AstKey#TEXT_KEY_IGNORE_CASE
       */
      public Object getTextKeyIgnoreCase()
      {
         return AstKey.TEXT_KEY_IGNORE_CASE;
      }
      
      /**
       * Get the constant which defines {@link com.goldencode.p2j.uast.AstKey
       * AstKey}'s case-sensitive token text comparison flag.
       *
       * @return  An <code>AstKey</code> constant.
       *
       * @see     AstKey#TEXT_KEY_CASE_SENS
       */
      public Object getTextKeyCaseSensitive()
      {
         return AstKey.TEXT_KEY_CASE_SENS;
      }
      
      /**
       * Get the constant which defines {@link com.goldencode.p2j.uast.AstKey
       * AstKey}'s wildcard token type.
       *
       * @return  An <code>AstKey</code> constant.
       *
       * @see     AstKey#KEY_WILDCARD
       */
      public Object getWildcardKey()
      {
         return AstKey.KEY_WILDCARD;
      }
   }
}