View Javadoc

1   /*
2    *  Copyright 2006 Stephen Colebourne
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  package org.joda.time.contrib.i18n;
17  
18  import java.io.BufferedInputStream;
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.ObjectInputStream;
24  
25  import org.joda.time.DateTimeZone;
26  
27  /**
28   * Provides time data for a specific territory, typically a country.
29   * <p>
30   * Many pieces of data used in dates and times varies by territory.
31   * This class provides access to that data.
32   */
33  public class CLDRTerritory extends Territory {
34  
35      /** The raw CLDR data. */
36      private static final byte[] cRawData = loadRawData();
37  
38      /** The territory id, as per CLDR. */
39      private String iID;
40      /** The zones. */
41      private DateTimeZone[] iZones;
42      /** The first day of week. */
43      private int iFirstDOW;
44      /** The first day of the business week. */
45      private int iFirstBusinessDOW;
46      /** The last day of the business week. */
47      private int iLastBusinessDOW;
48      /** The first day of the weekend. */
49      private int iFirstWeekendDOW;
50      /** The last day of the weekend. */
51      private int iLastWeekendDOW;
52  
53      //-----------------------------------------------------------------------
54      /**
55       * Loads the data from file.
56       *
57       * @return the array of bytes
58       */
59      private static byte[] loadRawData() {
60          String path = "org/joda/time/contrib/i18n/CLDRTerritoryData.dat";
61          InputStream baseStream = null;
62          ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
63          try {
64              // open the file
65              baseStream = CLDRTerritory.class.getClassLoader().getResourceAsStream(path);
66              if (baseStream == null) {
67                  throw new IOException("Resource not found " + path);
68              }
69              byte[] bytes = new byte[1024];
70              int result = 0;
71              while (result != -1) {
72                  baos.write(bytes, 0, result);
73                  result = baseStream.read(bytes, 0, 1024);
74              }
75              return baos.toByteArray();
76              
77          } catch (IOException ex) {
78              throw new IllegalArgumentException("Territory data could not be loaded: " + ex.getMessage());
79          } finally {
80              if (baseStream != null) {
81                  try {
82                      baseStream.close();
83                  } catch (IOException ex) {
84                      // ignore
85                  }
86              }
87          }
88      }
89  
90      //-----------------------------------------------------------------------
91      /**
92       * Constructor.
93       *
94       * @param id  the territory id, not null
95       */
96      CLDRTerritory(String id) {
97          super();
98          init(id);
99      }
100 
101     //-----------------------------------------------------------------------
102     /**
103      * Initialises the data for this id.
104      *
105      * @param id  the territory id, not null
106      * @throws IOException if an error occurs
107      */
108     private void init(String id) {
109         try {
110             // open the file
111             ByteArrayInputStream bais = new ByteArrayInputStream(cRawData);
112             ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bais));
113             byte version = in.readByte();
114             if (version != 1) {
115                 throw new IllegalArgumentException("Unknown file version: " + version);
116             }
117             
118             // loop through file to find the id
119             String inputId = in.readUTF();
120             while (inputId.equals(id) == false) {
121                 byte zoneCount = in.readByte();
122                 for (int i = 0; i < zoneCount; i++) {
123                     in.readUTF();
124                 }
125                 in.skip(5);
126                 inputId = in.readUTF();
127                 if (inputId.length() == 0) {
128                     throw new IllegalArgumentException("Territory " + id + " could not be loaded");
129                 }
130             }
131             
132             // found matching id
133             iID = id;
134             byte zoneCount = in.readByte();
135             iZones = new DateTimeZone[zoneCount];
136             for (int i = 0; i < zoneCount; i++) {
137                 String zoneID = in.readUTF();
138                 try {
139                     iZones[i] = DateTimeZone.forID(zoneID);
140                 } catch (IllegalArgumentException ex) {
141                     // ignore unless primary, allowing different timezone data files to work
142                     if (i == 0) {
143                         throw ex;
144                     }
145                 }
146             }
147             iFirstDOW = in.readByte();
148             iFirstBusinessDOW = in.readByte();
149             iLastBusinessDOW = in.readByte();
150             iFirstWeekendDOW = in.readByte();
151             iLastWeekendDOW = in.readByte();
152             
153         } catch (IOException ex) {
154             throw new IllegalArgumentException("Territory " + id + " could not be loaded: " + ex.getMessage());
155         }
156     }
157 
158     //-----------------------------------------------------------------------
159     public String getID() {
160         return iID;
161     }
162 
163     //-----------------------------------------------------------------------
164 	public DateTimeZone[] getZones() {
165         return (DateTimeZone[]) iZones.clone();
166     }
167 
168     public int getFirstDayOfWeek() {
169         return iFirstDOW;
170     }
171 
172     public int getBusinessWeekStart() {
173         return iFirstBusinessDOW;
174     }
175 
176     public int getBusinessWeekEnd() {
177         return iLastBusinessDOW;
178     }
179 
180     public int getWeekendStart() {
181         return iFirstWeekendDOW;
182     }
183 
184     public int getWeekendEnd() {
185         return iLastWeekendDOW;
186     }
187 
188 }