001: //==============================================================================
002: //===
003: //=== DdfLoader (adapted class from druid project http://druid.sf.net)
004: //===
005: //=== Copyright (C) by Andrea Carboni.
006: //=== This file may be distributed under the terms of the GPL license.
007: //==============================================================================
008:
009: package org.fao.gast.lib.druid;
010:
011: import java.io.BufferedReader;
012: import java.io.FileNotFoundException;
013: import java.io.FileReader;
014: import java.io.IOException;
015: import java.util.ArrayList;
016: import java.util.List;
017: import org.dlib.tools.FullTokenizer;
018:
019: //==============================================================================
020:
021: public class DdfLoader {
022: public static interface Handler {
023: public void handleFields(List<ImportField> fields)
024: throws Exception;
025:
026: public void handleRow(List<String> values) throws Exception;
027:
028: public void cleanUp();
029: }
030:
031: //---------------------------------------------------------------------------
032: //---
033: //--- API methods
034: //---
035: //---------------------------------------------------------------------------
036:
037: public void setHandler(Handler h) {
038: handler = h;
039: }
040:
041: //---------------------------------------------------------------------------
042:
043: public void load(String fileName) throws FileNotFoundException,
044: IOException, Exception {
045: BufferedReader rdr = new BufferedReader(
046: new FileReader(fileName));
047:
048: String line;
049:
050: int status = START;
051:
052: ArrayList<ImportField> alFields = new ArrayList<ImportField>();
053:
054: try {
055: while ((line = rdr.readLine()) != null) {
056: //--- skip comments or blank lines
057:
058: if (line.equals("") || line.startsWith("#"))
059: continue;
060:
061: //--- start [FIELDS] section
062:
063: if (line.equals("[FIELDS]"))
064: status = FIELDS;
065:
066: //--- start [DATA] section and build prepared statement
067:
068: else if (line.equals("[DATA]")) {
069: status = DATA;
070: handler.handleFields(alFields);
071: } else {
072: if (status == FIELDS)
073: alFields.add(new ImportField(line));
074:
075: else if (status == DATA)
076: handleRow(line);
077:
078: else
079: throw new IllegalArgumentException(
080: "Data not allowed before [FIELDS] section");
081: }
082: }
083:
084: if (status != DATA)
085: throw new IllegalArgumentException(
086: "Unexpected EOF encountered");
087: } finally {
088: handler.cleanUp();
089: rdr.close();
090: }
091: }
092:
093: //---------------------------------------------------------------------------
094: //---
095: //--- Private methods
096: //---
097: //---------------------------------------------------------------------------
098:
099: private void handleRow(String line) throws Exception {
100: FullTokenizer ft = new FullTokenizer(line, "\t");
101:
102: String token;
103:
104: ArrayList<String> al = new ArrayList<String>();
105:
106: for (int i = 0; i < ft.countTokens(); i++) {
107: token = ft.nextToken();
108: al.add(token);
109: }
110:
111: handler.handleRow(al);
112: }
113:
114: //---------------------------------------------------------------------------
115: //---
116: //--- Variables
117: //---
118: //---------------------------------------------------------------------------
119:
120: private Handler handler;
121:
122: //---------------------------------------------------------------------------
123:
124: private static final int START = 0;
125: private static final int FIELDS = 1;
126: private static final int DATA = 2;
127: }
128:
129: //==============================================================================
|