/**
* A component of a library for
* Moses.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
package uk.ac.leeds.ccg.andyt.projects.moses.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.HashSet;
import uk.ac.leeds.ccg.andyt.projects.moses.utilities.StaticIO;
/**
* A class
for loading a list of Zone Code and Zone Names for
* England and Wales Local Authority Districts, Scotland Council Areas and
* Northern Ireland Distric Council regions.
*/
public class CASZoneCodeZoneNameDataHandler {
public HashSet _ZoneCode_HashSet;
/**
* Creates a new instance of CASZoneCodeZoneNameDataHandler.
*/
public CASZoneCodeZoneNameDataHandler(){}
/**
* Creates a new instance of CASZoneCodeZoneNameDataHandler with
* _ZoneCode_HashSet loaded from formattedFile.
* @param formattedFile
*/
public CASZoneCodeZoneNameDataHandler(File formattedFile) throws IOException {
_ZoneCode_HashSet = (HashSet) StaticIO.readObject(formattedFile);
}
/**
* @param args
* the command line arguments No arguments are used.
*/
public static void main(String[] args) throws IOException {
CASZoneCodeZoneNameDataHandler _CASZoneCodeZoneNameDataHandler = new CASZoneCodeZoneNameDataHandler();
_CASZoneCodeZoneNameDataHandler.run();
}
public void run() throws IOException {
loadSourceData(new File("C:/Work/data/Census/2001/CAS"));
File formattedFile = new File("C:/Work/projects/MoSeS/workspace/UKLAD_ZoneCode_HashSet.thisFile");
StaticIO.writeObject(
_ZoneCode_HashSet,
formattedFile );
CASZoneCodeZoneNameDataHandler testCASZoneCodeZoneNameDataHandler = new CASZoneCodeZoneNameDataHandler(
formattedFile);
System.out.println("" + _ZoneCode_HashSet.size()); // 371
}
/**
* Loads source data from directory
*/
protected void loadSourceData(
File directory)
throws IOException {
_ZoneCode_HashSet = new HashSet();
// Load from source
File sourceFile;
// Load England
sourceFile = new File(
directory,
"EnglandZoneCodeZoneNameLAD.csv");
format(sourceFile);
// Load Wales
sourceFile = new File(
directory,
"WalesZoneCodeZoneNameLAD.csv");
format(sourceFile);
// Load Scotland
sourceFile = new File(
directory,
"ScotlandZoneCodeZoneNameCouncilArea.csv");
format(sourceFile);
// Load Northern Ireland
sourceFile = new File(
directory,
"NorthernIrelandZoneCodeZoneNameDistrictCouncil.csv");
format(sourceFile);
}
protected void format(
File sourceFile)
throws IOException {
BufferedReader aBufferedReader = new BufferedReader(
new InputStreamReader(new FileInputStream(sourceFile)));
StreamTokenizer aStreamTokenizer = new StreamTokenizer(aBufferedReader);
StaticIO.setStreamTokenizerSyntax1(aStreamTokenizer);
String line = null;
CAS001DataRecord aCAS001DataRecord = new CAS001DataRecord();
// Skip the first line
int tokenType = aStreamTokenizer.nextToken();
while (tokenType != StreamTokenizer.TT_EOL) {
tokenType = aStreamTokenizer.nextToken();
}
tokenType = aStreamTokenizer.nextToken();
while (tokenType != StreamTokenizer.TT_EOF) {
switch (tokenType) {
case StreamTokenizer.TT_EOL:
System.out.println(line);
break;
case StreamTokenizer.TT_WORD:
line = aStreamTokenizer.sval;
String[] fields = line.split(",");
String aZone_Code = fields[0];
_ZoneCode_HashSet.add(aZone_Code);
tokenType = aStreamTokenizer.nextToken();
}
tokenType = aStreamTokenizer.nextToken();
}
}
}