1 /*
   2  * Copyright 2008 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package build.tools.charsetmapping;
  27 
  28 import java.io.*;
  29 import java.util.regex.*;
  30 import static build.tools.charsetmapping.CharsetMapping.*;
  31 
  32 public class GenerateMapping {
  33 
  34     public static void genMapping(String argv[]) throws IOException {
  35         genDataJIS0213(new FileInputStream(argv[0]),
  36                        new FileOutputStream(argv[1]));
  37     }
  38 
  39     // regex pattern to parse the "jis0213.map" file
  40     static Pattern sjis0213 = Pattern.compile("0x(\\p{XDigit}++)\\s++U\\+(\\p{XDigit}++)(?:\\+(\\p{XDigit}++))?\\s++#.*");
  41     private static void genDataJIS0213(InputStream in, OutputStream out)
  42     {
  43         int[] sb = new int[0x100];                         // singlebyte
  44         int[] db = new int[0x10000];                       // doublebyte
  45         int[] indexC2B = new int[256];
  46         Entry[] supp = new Entry[0x10000];
  47         Entry[] comp = new Entry[0x100];
  48         int suppTotal = 0;
  49         int compTotal = 0;
  50 
  51         int b1Min1 = 0x81;
  52         int b1Max1 = 0x9f;
  53         int b1Min2 = 0xe0;
  54         int b1Max2 = 0xfc;
  55         int b2Min = 0x40;
  56         int b2Max = 0xfe;
  57 
  58         //init
  59         for (int i = 0; i < 0x80; i++) sb[i] = i;
  60         for (int i = 0x80; i < 0x100; i++) sb[i] = UNMAPPABLE_DECODING;
  61         for (int i = 0; i < 0x10000; i++) db[i] = UNMAPPABLE_DECODING;
  62         try {
  63             Parser p = new Parser(in, sjis0213);
  64             Entry  e = null;
  65             while ((e = p.next()) != null) {
  66                 if (e.cp2 != 0) {
  67                     comp[compTotal++] = e;
  68                 } else {
  69                     if (e.cp <= 0xffff) {
  70                         if (e.bs <= 0xff)
  71                             sb[e.bs] = e.cp;
  72                         else
  73                             db[e.bs] = e.cp;
  74                         indexC2B[e.cp>>8] = 1;
  75                     } else {
  76                         supp[suppTotal++] = e;
  77                     }
  78                 }
  79             }
  80             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  81             // c2b Index Table, always the first one
  82             writeINDEXC2B(baos, indexC2B);
  83             writeSINGLEBYTE(baos, sb);
  84             writeDOUBLEBYTE1(baos, db, b1Min1, b1Max1, b2Min, b2Max);
  85             writeDOUBLEBYTE2(baos, db, b1Min2, b1Max2, b2Min, b2Max);
  86             writeSUPPLEMENT(baos, supp, suppTotal);
  87             writeCOMPOSITE(baos, comp, compTotal);
  88             writeSIZE(out, baos.size());
  89             baos.writeTo(out);
  90             out.close();
  91         } catch (Exception x) {
  92             x.printStackTrace();
  93         }
  94     }
  95 }