/[classpath]/cp-tools/src/gnu/currencygen/Main.java
ViewVC logotype

Diff of /cp-tools/src/gnu/currencygen/Main.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by cbj, Sun Jan 30 01:52:04 2005 UTC revision 1.4 by cbj, Sun Jan 30 02:21:07 2005 UTC
# Line 12  import java.io.BufferedWriter; Line 12  import java.io.BufferedWriter;
12  import java.io.FileInputStream;  import java.io.FileInputStream;
13  import java.util.HashMap;  import java.util.HashMap;
14    
15    public class Main
16    {
17    
18      static public void main(String args[]) throws Exception
19      {
20        XMLReader reader;
21        SupplementalHandler handler;
22        InputSource source;
23        printVersion();
24        if (args.length != 1)
25          {
26            printUsage();
27            return;
28          }
29        reader = XMLReaderFactory.createXMLReader();
30        source = new InputSource(new FileInputStream(args[0]));
31        FileWriter currencyFile = new FileWriter("iso4217.properties");
32        BufferedWriter bWriter = new BufferedWriter(currencyFile);
33        PrintWriter output = new PrintWriter(bWriter, true);
34        handler = new SupplementalHandler(output);
35        reader.setContentHandler(handler);
36        reader.parse(source);
37        bWriter.flush();
38      }
39    
40      static void printUsage()
41      {
42        System.out.println(" Usage: [filename]");
43        System.out.println();
44      }
45      static void printVersion()
46      {
47        System.out
48          .println(" This is the LDML to GNU Classpath converter (currency part)");
49        System.out.println("   Copyright (C) 2004 The Free Software Foundation.");
50        System.out.println();
51      }
52    }
53  class SupplementalHandler extends DefaultHandler  class SupplementalHandler extends DefaultHandler
54  {  {
   static final int STATE_ZERO = 0;  
   static final int STATE_SUPPLEMENTAL = 1;  
   static final int STATE_IGNORING = 2;  
   static final int STATE_REGION = 3;  
   static final int STATE_CURRENCY = 4;  
   static final int STATE_CURRENCYDATA = 5;  
   static final int STATE_ALTERNATE = 6;  
   static final int STATE_SEENCURRENCY = 7;  
   static final int STATE_FRACTIONS = 8;  
   static final int STATE_INFO = 9;  
55    
56    static class CurrencyInfo    static class CurrencyInfo
57    {    {
# Line 36  class SupplementalHandler extends Defaul Line 64  class SupplementalHandler extends Defaul
64        this.rounding = rounding;        this.rounding = rounding;
65      }      }
66    }    }
67      static final int STATE_ALTERNATE = 6;
68    int state;    static final int STATE_CURRENCY = 4;
69    int oldState;    static final int STATE_CURRENCYDATA = 5;
70    int ignoreLevel;    static final int STATE_FRACTIONS = 8;
71      static final int STATE_IGNORING = 2;
72      static final int STATE_INFO = 9;
73      static final int STATE_REGION = 3;
74      static final int STATE_SEENCURRENCY = 7;
75      static final int STATE_SUPPLEMENTAL = 1;
76      static final int STATE_ZERO = 0;
77    HashMap currencyInfo = new HashMap();    HashMap currencyInfo = new HashMap();
78    String currentCurrency;    String currentCurrency;
79    String currentRegion;    String currentRegion;
80      int ignoreLevel;
81      int oldState;
82    PrintWriter output;    PrintWriter output;
83      
84      int state;
85    
86    public SupplementalHandler(PrintWriter output)    public SupplementalHandler(PrintWriter output)
87    {    {
88      this.output = output;      this.output = output;
89    }    }
90    
91    public void startDocument()    void checkMultiState(int[] currentStates, int newState) throws SAXException
92    {    {
93      output.println("# This document is automatically generated by gnu.currencygen");      int i;
94      output.println();      for (i = 0; i < currentStates.length; i++)
   
     state = STATE_ZERO;  
     ignoreLevel = 0;  
   }  
   
   public void startElement(String uri, String localName, String qName, Attributes atts)  
     throws SAXException  
   {  
     if (ignoreLevel > 0)  
       {  
         ignoreLevel++;  
         return;  
       }  
   
     if (localName.equals("supplementalData"))  
       checkState(STATE_ZERO, STATE_SUPPLEMENTAL);  
     else if (localName.equals("currencyData"))  
       checkState(STATE_SUPPLEMENTAL, STATE_CURRENCYDATA);  
     else if (localName.equals("region"))  
       checkState(STATE_CURRENCYDATA, STATE_REGION);  
     else if (localName.equals("currency"))  
       checkMultiState(new int[] { STATE_SEENCURRENCY, STATE_REGION }, STATE_CURRENCY);  
     else if (localName.equals("alternate"))  
       checkState(STATE_CURRENCY, STATE_ALTERNATE);  
     else if (localName.equals("fractions"))  
       checkState(STATE_CURRENCYDATA, STATE_FRACTIONS);  
     else if (localName.equals("info"))  
       checkState(STATE_FRACTIONS, STATE_INFO);  
     else {  
       ignoreLevel++;  
       return;  
     }  
   
     if (state == STATE_REGION)  
95        {        {
96          String tRegion = (String)atts.getValue("iso3166");          if (currentStates[i] == state)
97                      break;
         if (tRegion == null)  
           throw new SAXException("region must have a iso3166 attribute");  
   
         currentRegion = tRegion;  
   
         output.print(tRegion + ".currency=");  
98        }        }
99        if (i == currentStates.length)
100          throw new SAXException("Invalid current state " + state);
101        oldState = state;
102        state = newState;
103      }
104    
105      if (state == STATE_INFO)    void checkState(int currentState, int newState) throws SAXException
106        {    {
107          String currencyCode = (String)atts.getValue("iso4217");      if (currentState != state)
108          String digits = (String)atts.getValue("digits");        throw new SAXException("Invalid current state " + currentState
109          String rounding = (String)atts.getValue("rounding");                               + " (was expecting " + state + ")");
110        oldState = state;
111          if (currencyCode == null || digits == null || rounding == null)      state = newState;
           throw new SAXException("currency info must have an iso4217, a digits and a rounding attribute (here we get " +  
                                  currencyCode + "," + digits + "," + rounding + ")");  
   
         currencyInfo.put(currencyCode, new CurrencyInfo(Integer.parseInt(digits), Integer.parseInt(rounding)));  
       }  
   
     if (state == STATE_CURRENCY || state == STATE_ALTERNATE)  
       {  
         String tName = (String)atts.getValue("iso4217");  
   
         if (tName == null)  
           throw new SAXException("currency must have a iso 4217 attribute");  
   
         if (state == STATE_CURRENCY)  
           currentCurrency = tName;  
   
         // We only treat current currencies.  
         if (atts.getValue("before") == null)  
           {  
             if (oldState == STATE_SEENCURRENCY || state == STATE_ALTERNATE)  
               output.print(',');  
             output.print(tName);  
           }  
         else  
           {  
             System.err.println("WARNING: before not supported (value="+atts.getValue("before")+")");  
           }  
       }  
112    }    }
113    
114    public void endElement(String uri, String localName, String qName)    public void endElement(String uri, String localName, String qName)
# Line 143  class SupplementalHandler extends Defaul Line 116  class SupplementalHandler extends Defaul
116    {    {
117      if (ignoreLevel > 0)      if (ignoreLevel > 0)
118        {        {
119          ignoreLevel--;          ignoreLevel--;
120          return;          return;
121        }        }
   
122      if (state == STATE_SEENCURRENCY || state == STATE_REGION)      if (state == STATE_SEENCURRENCY || state == STATE_REGION)
123        {        {
124          output.println();          output.println();
125            CurrencyInfo info = (CurrencyInfo) currencyInfo.get(currentCurrency);
126          CurrencyInfo info = (CurrencyInfo)currencyInfo.get(currentCurrency);          if (info == null)
127          if (info == null)            info = (CurrencyInfo) currencyInfo.get("DEFAULT");
128            info = (CurrencyInfo)currencyInfo.get("DEFAULT");          if (info != null)
129                      {
130          if (info != null)              output.println(currentRegion + ".fractionDigits=" + info.digits);
131            {            }
             output.println(currentRegion + ".fractionDigits=" + info.digits);  
           }  
132        }        }
   
133      if (localName.equals("supplementalData"))      if (localName.equals("supplementalData"))
134        checkState(STATE_SUPPLEMENTAL, STATE_ZERO);        checkState(STATE_SUPPLEMENTAL, STATE_ZERO);
135      else if (localName.equals("currencyData"))      else if (localName.equals("currencyData"))
136        checkState(STATE_CURRENCYDATA, STATE_SUPPLEMENTAL);        checkState(STATE_CURRENCYDATA, STATE_SUPPLEMENTAL);
137      else if (localName.equals("region"))      else if (localName.equals("region"))
138        checkMultiState(new int[] { STATE_SEENCURRENCY, STATE_REGION }, STATE_CURRENCYDATA);        checkMultiState(new int[] { STATE_SEENCURRENCY, STATE_REGION },
139                          STATE_CURRENCYDATA);
140      else if (localName.equals("currency"))      else if (localName.equals("currency"))
141        checkState(STATE_CURRENCY, STATE_SEENCURRENCY);        checkState(STATE_CURRENCY, STATE_SEENCURRENCY);
142      else if (localName.equals("alternate"))      else if (localName.equals("alternate"))
# Line 177  class SupplementalHandler extends Defaul Line 147  class SupplementalHandler extends Defaul
147        checkState(STATE_INFO, STATE_FRACTIONS);        checkState(STATE_INFO, STATE_FRACTIONS);
148    }    }
149    
150    void checkState(int currentState, int newState)    public void startDocument()
     throws SAXException  
151    {    {
152      if (currentState != state)      output
153        throw new SAXException("Invalid current state " + currentState + " (was expecting " + state + ")");        .println("# This document is automatically generated by gnu.currencygen");
154        output.println();
155      oldState = state;      state = STATE_ZERO;
156      state = newState;      ignoreLevel = 0;
157    }    }
158    
159    void checkMultiState(int[] currentStates, int newState)    public void startElement(String uri, String localName, String qName,
160      throws SAXException                             Attributes atts) throws SAXException
161    {    {
162      int i;      if (ignoreLevel > 0)
   
     for (i = 0; i < currentStates.length; i++)  
163        {        {
164          if (currentStates[i] == state)          ignoreLevel++;
165            break;          return;
166        }        }
167            if (localName.equals("supplementalData"))
168      if (i == currentStates.length)        checkState(STATE_ZERO, STATE_SUPPLEMENTAL);
169        throw new SAXException("Invalid current state " + state);      else if (localName.equals("currencyData"))
170          checkState(STATE_SUPPLEMENTAL, STATE_CURRENCYDATA);
171      oldState = state;      else if (localName.equals("region"))
172      state = newState;        checkState(STATE_CURRENCYDATA, STATE_REGION);
173    }      else if (localName.equals("currency"))
174          checkMultiState(new int[] { STATE_SEENCURRENCY, STATE_REGION },
175  }                        STATE_CURRENCY);
176        else if (localName.equals("alternate"))
177  public class Main        checkState(STATE_CURRENCY, STATE_ALTERNATE);
178  {      else if (localName.equals("fractions"))
179          checkState(STATE_CURRENCYDATA, STATE_FRACTIONS);
180    static void printVersion()      else if (localName.equals("info"))
181    {        checkState(STATE_FRACTIONS, STATE_INFO);
182      System.out.println(" This is the LDML to GNU Classpath converter (currency part)");      else
     System.out.println("   Copyright (C) 2004 The Free Software Foundation.");  
     System.out.println();  
   }  
   
   static void printUsage()  
   {  
     System.out.println(" Usage: [filename]");  
     System.out.println();  
   }  
     
   static public void main(String args[]) throws Exception  
   {  
     XMLReader reader;  
     SupplementalHandler handler;  
     InputSource source;  
   
     printVersion();  
   
     if (args.length != 1)  
183        {        {
184          printUsage();          ignoreLevel++;
185          return;          return;
186          }
187        if (state == STATE_REGION)
188          {
189            String tRegion = (String) atts.getValue("iso3166");
190            if (tRegion == null)
191              throw new SAXException("region must have a iso3166 attribute");
192            currentRegion = tRegion;
193            output.print(tRegion + ".currency=");
194          }
195        if (state == STATE_INFO)
196          {
197            String currencyCode = (String) atts.getValue("iso4217");
198            String digits = (String) atts.getValue("digits");
199            String rounding = (String) atts.getValue("rounding");
200            if (currencyCode == null || digits == null || rounding == null)
201              throw new SAXException(
202                                     "currency info must have an iso4217, a digits and a rounding attribute (here we get "
203                                                                      + currencyCode
204                                                                      + ","
205                                                                      + digits
206                                                                      + ","
207                                                                      + rounding
208                                                                      + ")");
209            currencyInfo.put(currencyCode, new CurrencyInfo(Integer
210              .parseInt(digits), Integer.parseInt(rounding)));
211          }
212        if (state == STATE_CURRENCY || state == STATE_ALTERNATE)
213          {
214            String tName = (String) atts.getValue("iso4217");
215            if (tName == null)
216              throw new SAXException("currency must have a iso 4217 attribute");
217            if (state == STATE_CURRENCY)
218              currentCurrency = tName;
219            // We only treat current currencies.
220            if (atts.getValue("before") == null)
221              {
222                if (oldState == STATE_SEENCURRENCY || state == STATE_ALTERNATE)
223                  output.print(',');
224                output.print(tName);
225              }
226            else
227              {
228                System.err.println("WARNING: before not supported (value="
229                                   + atts.getValue("before") + ")");
230              }
231        }        }
   
     reader = XMLReaderFactory.createXMLReader();  
       
     source = new InputSource(new FileInputStream(args[0]));  
   
     FileWriter currencyFile = new FileWriter("iso4217.properties");  
     BufferedWriter bWriter = new BufferedWriter(currencyFile);  
     PrintWriter output = new PrintWriter(bWriter, true);  
   
   
     handler = new SupplementalHandler(output);  
     reader.setContentHandler(handler);  
       
     reader.parse(source);  
       
     bWriter.flush();  
       
232    }    }
   
233  }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26