~ken-vandine/+junk/libphonenumber-7.1.0-4ubuntu1

« back to all changes in this revision

Viewing changes to .pc/0008-tools-jdk5.patch/tools/java/java-build/src/com/google/i18n/phonenumbers/buildtools/GenerateTimeZonesMapData.java

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2014-09-03 07:12:50 UTC
  • Revision ID: package-import@ubuntu.com-20140903071250-wzcgxhpc7w5k9pbb
Tags: 6.3~svn698-3
More patches for JDK 1.5 build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 The Libphonenumber Authors
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 * http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
package com.google.i18n.phonenumbers.buildtools;
 
18
 
 
19
import com.google.i18n.phonenumbers.prefixmapper.PrefixTimeZonesMap;
 
20
 
 
21
import java.io.BufferedInputStream;
 
22
import java.io.BufferedReader;
 
23
import java.io.File;
 
24
import java.io.FileInputStream;
 
25
import java.io.FileOutputStream;
 
26
import java.io.IOException;
 
27
import java.io.InputStream;
 
28
import java.io.InputStreamReader;
 
29
import java.io.ObjectOutputStream;
 
30
import java.io.OutputStream;
 
31
import java.nio.charset.Charset;
 
32
import java.util.SortedMap;
 
33
import java.util.TreeMap;
 
34
import java.util.logging.Level;
 
35
import java.util.logging.Logger;
 
36
 
 
37
/**
 
38
 * A utility that generates the binary serialization of the prefix/time zones mappings from
 
39
 * a human-readable text file.
 
40
 *
 
41
 * @author Walter Erquinigo
 
42
 */
 
43
public class GenerateTimeZonesMapData {
 
44
  private final File inputTextFile;
 
45
  private static final String MAPPING_DATA_FILE_NAME = "map_data";
 
46
  // The IO Handler used to output the generated binary file.
 
47
  private final AbstractPhonePrefixDataIOHandler ioHandler;
 
48
 
 
49
  private static final Logger LOGGER = Logger.getLogger(GenerateTimeZonesMapData.class.getName());
 
50
 
 
51
  public GenerateTimeZonesMapData(File inputTextFile, AbstractPhonePrefixDataIOHandler ioHandler)
 
52
      throws IOException {
 
53
    this.inputTextFile = inputTextFile;
 
54
    if (!inputTextFile.isFile()) {
 
55
      throw new IOException("The provided input text file does not exist.");
 
56
    }
 
57
    this.ioHandler = ioHandler;
 
58
  }
 
59
 
 
60
  /**
 
61
   * Reads phone prefix data from the provided input stream and returns a SortedMap with the
 
62
   * prefix to time zones mappings.
 
63
   */
 
64
  // @VisibleForTesting
 
65
  static SortedMap<Integer, String> parseTextFile(InputStream input)
 
66
      throws IOException, RuntimeException {
 
67
    final SortedMap<Integer, String> timeZoneMap = new TreeMap<Integer, String>();
 
68
    BufferedReader bufferedReader =
 
69
        new BufferedReader(new InputStreamReader(
 
70
            new BufferedInputStream(input), Charset.forName("UTF-8")));
 
71
    int lineNumber = 1;
 
72
 
 
73
    for (String line; (line = bufferedReader.readLine()) != null; lineNumber++) {
 
74
      line = line.trim();
 
75
      if (line.length() == 0 || line.startsWith("#")) {
 
76
        continue;
 
77
      }
 
78
      int indexOfPipe = line.indexOf('|');
 
79
      if (indexOfPipe == -1) {
 
80
        throw new RuntimeException(String.format("line %d: malformatted data, expected '|'",
 
81
                                                 lineNumber));
 
82
      }
 
83
      Integer prefix = Integer.parseInt(line.substring(0, indexOfPipe));
 
84
      String timezones = line.substring(indexOfPipe + 1);
 
85
      if (timezones.isEmpty()) {
 
86
        throw new RuntimeException(String.format("line %d: missing time zones", lineNumber));
 
87
      }
 
88
      if (timeZoneMap.put(prefix, timezones) != null) {
 
89
         throw new RuntimeException(String.format("duplicated prefix %d", prefix));
 
90
      }
 
91
    }
 
92
    return timeZoneMap;
 
93
  }
 
94
 
 
95
  /**
 
96
   * Writes the provided phone prefix/time zones map to the provided output stream.
 
97
   *
 
98
   * @throws IOException
 
99
   */
 
100
  // @VisibleForTesting
 
101
  static void writeToBinaryFile(SortedMap<Integer, String> sortedMap, OutputStream output)
 
102
      throws IOException {
 
103
    // Build the corresponding PrefixTimeZonesMap and serialize it to the binary format.
 
104
    PrefixTimeZonesMap prefixTimeZonesMap = new PrefixTimeZonesMap();
 
105
    prefixTimeZonesMap.readPrefixTimeZonesMap(sortedMap);
 
106
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(output);
 
107
    prefixTimeZonesMap.writeExternal(objectOutputStream);
 
108
    objectOutputStream.flush();
 
109
  }
 
110
 
 
111
  /**
 
112
   * Runs the prefix to time zones map data generator.
 
113
   *
 
114
   * @throws IOException
 
115
   */
 
116
  public void run() throws IOException {
 
117
    FileInputStream fileInputStream = null;
 
118
    FileOutputStream fileOutputStream = null;
 
119
    try {
 
120
      fileInputStream = new FileInputStream(inputTextFile);
 
121
      SortedMap<Integer, String> mappings = parseTextFile(fileInputStream);
 
122
      File outputBinaryFile = ioHandler.createFile(MAPPING_DATA_FILE_NAME);
 
123
      try {
 
124
        fileOutputStream = new FileOutputStream(outputBinaryFile);
 
125
        writeToBinaryFile(mappings, fileOutputStream);
 
126
        ioHandler.addFileToOutput(outputBinaryFile);
 
127
      } finally {
 
128
        ioHandler.closeFile(fileOutputStream);
 
129
      }
 
130
    } catch (RuntimeException e) {
 
131
      LOGGER.log(Level.SEVERE,
 
132
                 "Error processing file " + inputTextFile.getAbsolutePath());
 
133
      throw e;
 
134
    } catch (IOException e) {
 
135
      LOGGER.log(Level.SEVERE, e.getMessage());
 
136
    } finally {
 
137
      ioHandler.closeFile(fileInputStream);
 
138
      ioHandler.closeFile(fileOutputStream);
 
139
      ioHandler.close();
 
140
    }
 
141
    LOGGER.log(Level.INFO, "Time zone data successfully generated.");
 
142
  }
 
143
}