1 /*
   2  * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.corba.se.idl.toJavaPortable ;
  27 
  28 import com.sun.tools.corba.se.idl.toJavaPortable.NameModifier ;
  29 
  30 public class NameModifierImpl implements NameModifier {
  31     private String prefix ;
  32     private String suffix ;
  33 
  34     public NameModifierImpl( )
  35     {
  36         this.prefix = null ;
  37         this.suffix = null ;
  38     }
  39 
  40     public NameModifierImpl( String prefix, String suffix )
  41     {
  42         this.prefix = prefix ;
  43         this.suffix = suffix ;
  44     }
  45 
  46     /** Construct a NameModifier from a pattern of the form xxx%xxx.
  47     * The pattern must consist of characters chosen from the
  48     * set [A-Za-z0-9%$_]. In addition, the pattern must contain
  49     * exactly one % character.  Finally, if % is not the first char in
  50     * the pattern, the pattern must not start with a number.
  51     * <p>
  52     * The semantics of makeName are very simply: just replace the
  53     * % character with the base in the pattern and return the result.
  54     */
  55     public NameModifierImpl( String pattern )
  56     {
  57         int first = pattern.indexOf( '%' ) ;
  58         int last  = pattern.lastIndexOf( '%' ) ;
  59 
  60         if (first != last)
  61             throw new IllegalArgumentException(
  62                 Util.getMessage( "NameModifier.TooManyPercent" ) ) ;
  63 
  64         if (first == -1)
  65             throw new IllegalArgumentException(
  66                 Util.getMessage( "NameModifier.NoPercent" ) ) ;
  67 
  68         for (int ctr = 0; ctr<pattern.length(); ctr++) {
  69             char ch = pattern.charAt( ctr ) ;
  70             if (invalidChar( ch, ctr==0 )) {
  71                 char[] chars = new char[] { ch } ;
  72                 throw new IllegalArgumentException(
  73                     Util.getMessage( "NameModifier.InvalidChar",
  74                         new String( chars )) ) ;
  75             }
  76         }
  77 
  78         // at this point, 0 <= first && first < pattern.length()
  79         prefix = pattern.substring( 0, first ) ;
  80         suffix = pattern.substring( first+1 ) ;
  81     }
  82 
  83     /** Return true if ch is invalid as a character in an
  84     * identifier.  If ch is a number, it is invalid only if
  85     * isFirst is true.
  86     */
  87     private boolean invalidChar( char ch, boolean isFirst )
  88     {
  89         if (('A'<=ch) && (ch<='Z'))
  90             return false ;
  91         else if (('a'<=ch) && (ch<='z'))
  92             return false ;
  93         else if (('0'<=ch) && (ch<='9'))
  94             return isFirst ;
  95         else if (ch=='%')
  96             return false ;
  97         else if (ch=='$')
  98             return false ;
  99         else if (ch=='_')
 100             return false ;
 101         else
 102             return true ;
 103     }
 104 
 105     public String makeName( String base )
 106     {
 107         StringBuffer sb = new StringBuffer() ;
 108 
 109         if (prefix != null)
 110             sb.append( prefix ) ;
 111 
 112         sb.append( base ) ;
 113 
 114         if (suffix != null)
 115             sb.append( suffix ) ;
 116 
 117         return sb.toString() ;
 118     }
 119 }