elemnumber.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,968 行 · 第 1/5 页

JAVA
1,968
字号
      if (letterVal != null              && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))        formattedNumber.append(tradAlphaCount(listElement, thisBundle));      else  //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))        int2alphaCount(listElement,                       (char[]) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET),                       formattedNumber);      break;    }    case 0x03B1 :    {      XResourceBundle thisBundle;      thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(        org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("el", ""));      if (letterVal != null              && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))        formattedNumber.append(tradAlphaCount(listElement, thisBundle));      else  //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))        int2alphaCount(listElement,                       (char[]) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET),                       formattedNumber);      break;    }    case 0x0430 :    {      XResourceBundle thisBundle;      thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(        org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("cy", ""));      if (letterVal != null              && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))        formattedNumber.append(tradAlphaCount(listElement, thisBundle));      else  //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))        int2alphaCount(listElement,                       (char[]) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET),                       formattedNumber);      break;    }    default :  // "1"      DecimalFormat formatter = getNumberFormatter(transformer, contextNode);      String padString = formatter == null ? String.valueOf(0) : formatter.format(0);          String numString = formatter == null ? String.valueOf(listElement) : formatter.format(listElement);      int nPadding = numberWidth - numString.length();      for (int k = 0; k < nPadding; k++)      {        formattedNumber.append(padString);      }      formattedNumber.append(numString);    }  }    /**   * Get a string value for zero, which is not really defined by the 1.0 spec,    * thought I think it might be cleared up by the erreta.   */   String getZeroString()   {     return ""+0;   }  /**   * Convert a long integer into alphabetic counting, in other words   * count using the sequence A B C ... Z.   *    * @param val Value to convert -- must be greater than zero.   * @param table a table containing one character for each digit in the radix   * @return String representing alpha count of number.   * @see TransformerImpl#DecimalToRoman   *   * Note that the radix of the conversion is inferred from the size   * of the table.   */  protected String int2singlealphaCount(long val, char[] table)  {    int radix = table.length;    // TODO:  throw error on out of range input    if (val > radix)    {      return getZeroString();    }    else      return (new Character(table[(int)val - 1])).toString();  // index into table is off one, starts at 0  }  /**   * Convert a long integer into alphabetic counting, in other words   * count using the sequence A B C ... Z AA AB AC.... etc.   *    * @param val Value to convert -- must be greater than zero.   * @param table a table containing one character for each digit in the radix   * @param aTable Array of alpha characters representing numbers   * @param stringBuf Buffer where to save the string representing alpha count of number.   *    * @see TransformerImpl#DecimalToRoman   *   * Note that the radix of the conversion is inferred from the size   * of the table.   */  protected void int2alphaCount(long val, char[] aTable,                                FastStringBuffer stringBuf)  {    int radix = aTable.length;    char[] table = new char[aTable.length];    // start table at 1, add last char at index 0. Reason explained above and below.    int i;    for (i = 0; i < aTable.length - 1; i++)    {      table[i + 1] = aTable[i];    }    table[0] = aTable[i];    // Create a buffer to hold the result    // TODO:  size of the table can be detereined by computing    // logs of the radix.  For now, we fake it.    char buf[] = new char[100];    //some languages go left to right(ie. english), right to left (ie. Hebrew),    //top to bottom (ie.Japanese), etc... Handle them differently    //String orientation = thisBundle.getString(org.apache.xml.utils.res.XResourceBundle.LANG_ORIENTATION);    // next character to set in the buffer    int charPos;    charPos = buf.length - 1;  // work backward through buf[]      // index in table of the last character that we stored    int lookupIndex = 1;  // start off with anything other than zero to make correction work    //                                          Correction number    //    //  Correction can take on exactly two values:    //    //          0       if the next character is to be emitted is usual    //    //      radix - 1    //                  if the next char to be emitted should be one less than    //                  you would expect    //                      // For example, consider radix 10, where 1="A" and 10="J"    //    // In this scheme, we count: A, B, C ...   H, I, J (not A0 and certainly    // not AJ), A1    //    // So, how do we keep from emitting AJ for 10?  After correctly emitting the    // J, lookupIndex is zero.  We now compute a correction number of 9 (radix-1).    // In the following line, we'll compute (val+correction) % radix, which is,    // (val+9)/10.  By this time, val is 1, so we compute (1+9) % 10, which    // is 10 % 10 or zero.  So, we'll prepare to emit "JJ", but then we'll    // later suppress the leading J as representing zero (in the mod system,    // it can represent either 10 or zero).  In summary, the correction value of    // "radix-1" acts like "-1" when run through the mod operator, but with the    // desireable characteristic that it never produces a negative number.    long correction = 0;    // TODO:  throw error on out of range input    do    {      // most of the correction calculation is explained above,  the reason for the      // term after the "|| " is that it correctly propagates carries across      // multiple columns.      correction =        ((lookupIndex == 0) || (correction != 0 && lookupIndex == radix - 1))        ? (radix - 1) : 0;      // index in "table" of the next char to emit      lookupIndex = (int)(val + correction) % radix;      // shift input by one "column"      val = (val / radix);      // if the next value we'd put out would be a leading zero, we're done.      if (lookupIndex == 0 && val == 0)        break;      // put out the next character of output      buf[charPos--] = table[lookupIndex];  // left to right or top to bottom       }    while (val > 0);    stringBuf.append(buf, charPos + 1, (buf.length - charPos - 1));  }  /**   * Convert a long integer into traditional alphabetic counting, in other words   * count using the traditional numbering.   *    * @param val Value to convert -- must be greater than zero.   * @param table a table containing one character for each digit in the radix   * @param thisBundle Resource bundle to use   *    * @return String representing alpha count of number.   * @see XSLProcessor#DecimalToRoman   *   * Note that the radix of the conversion is inferred from the size   * of the table.   */  protected String tradAlphaCount(long val, XResourceBundle thisBundle)  {    // if this number is larger than the largest number we can represent, error!    if (val > Long.MAX_VALUE)    {      this.error(XSLTErrorResources.ER_NUMBER_TOO_BIG);      return XSLTErrorResources.ERROR_STRING;    }    char[] table = null;    // index in table of the last character that we stored    int lookupIndex = 1;  // start off with anything other than zero to make correction work    // Create a buffer to hold the result    // TODO:  size of the table can be detereined by computing    // logs of the radix.  For now, we fake it.    char buf[] = new char[100];    //some languages go left to right(ie. english), right to left (ie. Hebrew),    //top to bottom (ie.Japanese), etc... Handle them differently    //String orientation = thisBundle.getString(org.apache.xml.utils.res.XResourceBundle.LANG_ORIENTATION);    // next character to set in the buffer    int charPos;    charPos = 0;  //start at 0    // array of number groups: ie.1000, 100, 10, 1    int[] groups = (int[]) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_NUMBERGROUPS);    // array of tables of hundreds, tens, digits...    String[] tables =      (String[]) (thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_NUM_TABLES));    //some languages have additive alphabetical notation,    //some multiplicative-additive, etc... Handle them differently.    String numbering = thisBundle.getString(org.apache.xml.utils.res.XResourceBundle.LANG_NUMBERING);    // do multiplicative part first    if (numbering.equals(org.apache.xml.utils.res.XResourceBundle.LANG_MULT_ADD))    {      String mult_order = thisBundle.getString(org.apache.xml.utils.res.XResourceBundle.MULT_ORDER);      long[] multiplier =        (long[]) (thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_MULTIPLIER));      char[] zeroChar = (char[]) thisBundle.getObject("zero");      int i = 0;      // skip to correct multiplier      while (i < multiplier.length && val < multiplier[i])      {        i++;      }      do      {        if (i >= multiplier.length)          break;  //number is smaller than multipliers        // some languages (ie chinese) put a zero character (and only one) when        // the multiplier is multiplied by zero. (ie, 1001 is 1X1000 + 0X100 + 0X10 + 1)        // 0X100 is replaced by the zero character, we don't need one for 0X10        if (val < multiplier[i])        {          if (zeroChar.length == 0)          {            i++;          }          else          {            if (buf[charPos - 1] != zeroChar[0])              buf[charPos++] = zeroChar[0];            i++;          }        }        else if (val >= multiplier[i])        {          long mult = val / multiplier[i];          val = val % multiplier[i];  // save this.          int k = 0;          while (k < groups.length)          {            lookupIndex = 1;  // initialize for each table            if (mult / groups[k] <= 0)  // look for right table              k++;            else            {              // get the table              char[] THEletters = (char[]) thisBundle.getObject(tables[k]);              table = new char[THEletters.length + 1];              int j;              for (j = 0; j < THEletters.length; j++)              {                table[j + 1] = THEletters[j];              }              table[0] = THEletters[j - 1];  // don't need this                                                                                       // index in "table" of the next char to emit              lookupIndex = (int)mult / groups[k];              //this should not happen              if (lookupIndex == 0 && mult == 0)                break;              char multiplierChar = ((char[]) (thisBundle.getObject(                org.apache.xml.utils.res.XResourceBundle.LANG_MULTIPLIER_CHAR)))[i];              // put out the next character of output                 if (lookupIndex < table.length)              {                if (mult_order.equals(org.apache.xml.utils.res.XResourceBundle.MULT_PRECEDES))                {                  buf[charPos++] = multiplierChar;                  buf[charPos++] = table[lookupIndex];                }                else                {                  // don't put out 1 (ie 1X10 is just 10)                  if (lookupIndex == 1 && i == multiplier.length - 1){}                  else                    buf[charPos++] = table[lookupIndex];                  buf[charPos++] = multiplierChar;                }                break;  // all done!              }              else                return XSLTErrorResources.ERROR_STRING;            }  //end else          }  // end while                  i++;        }  // end else if      }  // end do while      while (i < multiplier.length);    }    // Now do additive part...    int count = 0;    String tableName;    // do this for each table of hundreds, tens, digits...    while (count < groups.length)    {      if (val / groups[count] <= 0)  // look for correct table        count++;      else      {        char[] theletters = (char[]) thisBundle.getObject(tables[count]);        table = new char[theletters.length + 1];        int j;        // need to start filling the table up at index 1        for (j = 0; j < theletters.length; j++)        {          table[j + 1] = theletters[j];        }        table[0] = theletters[j - 1];  // don't need this        // index in "table" of the next char to emit        lookupIndex = (int)val / groups[count];        // shift input by one "column"        val = val % groups[count];        // this should not happen        if (lookupIndex == 0 && val == 0)      

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?