Home | History | Annotate | Download | only in layout
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *******************************************************************************
      5  * Copyright (C) 1998-2008, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8  *
      9  * Created on Dec 3, 2003
     10  *
     11  *******************************************************************************
     12  */
     13 
     14 package com.ibm.icu.dev.tool.layout;
     15 
     16 import java.io.BufferedReader;
     17 import java.io.FileOutputStream;
     18 import java.io.FileReader;
     19 import java.io.IOException;
     20 import java.io.PrintStream;
     21 import java.util.ArrayList;
     22 import java.util.Date;
     23 
     24 import com.ibm.icu.text.MessageFormat;
     25 
     26 public class ModuleWriter
     27 {
     28     private static final String BUILDER_FILE_PATH="src/com/ibm/icu/dev/tool/layout/";
     29 
     30     public ModuleWriter()
     31     {
     32         wroteDefine = false;
     33         output = null;
     34     }
     35 
     36     public void openFile(String outputFileName) {
     37         try
     38         {
     39             output = new PrintStream(
     40                 new FileOutputStream(BUILDER_FILE_PATH+outputFileName));
     41         } catch (IOException e) {
     42             System.out.println("? Could not open " + outputFileName + " for writing.");
     43             return;
     44         }
     45 
     46         wroteDefine = false;
     47         System.out.println("Writing module " + outputFileName + "...");
     48     }
     49 
     50     public void writeHeader(String define, String[] includeFiles)
     51     {
     52         writeHeader(define, includeFiles, null);
     53     }
     54 
     55     public void writeHeader(String define, String[] includeFiles, String brief)
     56     {
     57         MessageFormat format = new MessageFormat(moduleHeader);
     58         Object args[] = {new Date(System.currentTimeMillis())};
     59 
     60         output.print(format.format(args));
     61 
     62         if (define != null) {
     63             wroteDefine = true;
     64             output.print("#ifndef ");
     65             output.println(define);
     66 
     67             output.print("#define ");
     68             output.println(define);
     69 
     70             output.println();
     71         }
     72 
     73         if (includeFiles != null) {
     74             for (int i = 0; i < includeFiles.length; i += 1) {
     75                 output.print("#include \"");
     76                 output.print(includeFiles[i]);
     77                 output.println("\"");
     78             }
     79 
     80             output.println();
     81         }
     82 
     83         if (brief != null) {
     84             output.print(brief);
     85         }
     86 
     87         output.println(moduleBegin);
     88     }
     89 
     90     public void writeTrailer() {
     91         output.print(moduleTrailer);
     92 
     93         if (wroteDefine) {
     94             output.println("#endif");
     95 
     96         }
     97     }
     98 
     99     public void closeFile() {
    100         System.out.println("Done.");
    101         output.close();
    102     }
    103 
    104     protected boolean wroteDefine;
    105 
    106     protected PrintStream output;
    107 
    108     protected BufferedReader reader;
    109     protected PrintStream updateFile;
    110     protected int previousTotalScripts;
    111     protected int previousTotalLanguages;
    112     protected ArrayList scriptVersionNumber = new ArrayList();
    113     protected ArrayList languageVersionNumber = new ArrayList();
    114 
    115     public void openScriptAndLanguages(String name){
    116         try
    117         {
    118             updateFile = new PrintStream(new FileOutputStream(BUILDER_FILE_PATH+name));
    119         } catch (IOException e) {
    120             System.out.println("? Could not open " + name + " for writing.");
    121             return;
    122         }
    123     }
    124 
    125     public void readFile(String file, String what){
    126         try
    127         {
    128            reader = new BufferedReader(new FileReader(BUILDER_FILE_PATH+file));
    129            String inputText = "";
    130            String versionToAdd = "";
    131            while((inputText=reader.readLine())!=null){
    132                if(what.equals("script") && inputText.indexOf("Script=") >= 0){
    133                    previousTotalScripts = Integer.parseInt(inputText.substring(inputText.indexOf("=")+1));
    134                }else if(what.equals("languages") && inputText.indexOf("Language=") >= 0){
    135                    previousTotalLanguages = Integer.parseInt(inputText.substring(inputText.indexOf("=")+1));
    136                }else if(what.equals("script") && inputText.indexOf("Scripts={") >= 0){
    137                    while((versionToAdd=reader.readLine()).indexOf("}") == -1){
    138                        scriptVersionNumber.add(versionToAdd);
    139                    }
    140                }else if(what.equals("languages") && inputText.indexOf("Languages={") >= 0){
    141                    while((versionToAdd=reader.readLine()).indexOf("}") == -1){
    142                        languageVersionNumber.add(versionToAdd);
    143                    }
    144                }
    145            }
    146            reader.close();
    147 
    148         } catch (IOException e) {
    149             System.out.println("? Could not open " + file + " for reading.");
    150             return;
    151         }
    152     }
    153 
    154 
    155 
    156     protected static final String moduleHeader =
    157         "/*\n" +
    158         " *\n" +
    159         " * (C) Copyright IBM Corp. 1998-{0,date,yyyy}. All Rights Reserved.\n" +
    160         " *\n" +
    161         " * WARNING: THIS FILE IS MACHINE GENERATED. DO NOT HAND EDIT IT UNLESS\n" +
    162         " * YOU REALLY KNOW WHAT YOU''RE DOING.\n" +
    163         " *\n" +
    164         " * Generated on: {0,date,MM/dd/yyyy hh:mm:ss a z}\n" +
    165         " */\n" +
    166         "\n";
    167 
    168     protected static final String moduleBegin = "U_NAMESPACE_BEGIN\n";
    169 
    170     protected static final String moduleTrailer = "U_NAMESPACE_END\n";
    171 
    172 }
    173