To Identifier String : Identifier « Reflection « Java






To Identifier String

     
/*
 * Copyright 2000,2005 wingS development team.
 *
 * This file is part of wingS (http://wingsframework.org).
 *
 * wingS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * Please see COPYING for the complete licence.
 */

import java.util.StringTokenizer;

/**
 * Some string manipulation utilities.
 *
 * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
 */
public class StringUtil {

  private final static char[] ALPHAS = {
      'a', 'b',
      'c', 'd', 'e', 'f', 'g', 'h',
      'i', 'j', 'k', 'l', 'm', 'n',
      'o', 'p', 'q', 'r', 's', 't',
      'u', 'v', 'w', 'x', 'y', 'z',
  };

  /**
   * All possible digits for representing a number as a String
   * This is conservative and does not include 'special'
   * characters since some browsers don't handle them right.
   * The IE for instance seems to be case insensitive in class
   * names for CSSs. Grrr.
   */
  private final static char[] DIGITS = {
      '0', '1', '2', '3', '4', '5',
      '6', '7', '8', '9', 'a', 'b',
      'c', 'd', 'e', 'f', 'g', 'h',
      'i', 'j', 'k', 'l', 'm', 'n',
      'o', 'p', 'q', 'r', 's', 't',
      'u', 'v', 'w', 'x', 'y', 'z',
      /* This %@&!-IE is case insensitive for certain
       * URLs and IDs
       * 'A' , 'B' ,
       * 'C' , 'D' , 'E' , 'F' , 'G' , 'H' ,
       * 'I' , 'J' , 'K' , 'L' , 'M' , 'N' ,
       * 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' ,
       * 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z'
       */
  };

  public static final int MAX_RADIX = DIGITS.length;

  /**
   * creates a shortest possible string representation of the given
   * long number that qualifies as an identifier in common programming
   * languages (and HTML-id's :-)
   * That is, it must start with a letter.
   *
   * @param val the long value to be encoded
   * @return a string represantation of the given value that qualifies
   *         as an identifier.
   */
  public static String toIdentifierString(long val) {
      char buf[] = new char[14];
      int i = 0;
      if (val < 0) {
          buf[i++] = '_';
          val = -(val + 1);
      }
      buf[i++] = ALPHAS[(int) (val % ALPHAS.length)];
      val /= ALPHAS.length;
      while (val != 0 && i < buf.length) {
          buf[i++] = DIGITS[(int) (val % DIGITS.length)];
          val /= DIGITS.length;
      }
      return new String(buf, 0, i);
  }

  
}

   
    
    
    
    
  








Related examples in the same category

1.Is Java Identifier and get Java Identifier
2.Escape Java Literal
3.Check whether the given String is a valid identifier according to the Java Language specifications.
4.Determine whether the supplied string represents a well-formed fully-qualified Java classname.
5.Determines if the specified string is permissible as a Java identifier.