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