-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConvertTxtToTbl.java
197 lines (143 loc) · 5.35 KB
/
ConvertTxtToTbl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.io.*;
import java.util.*;
import qp.utils.*;
/*
assume that the first line of the file contain the names of the
attributes of the relation. each subsequent line represents 1
tuple of the relation. also assume that the fields of each line
is delimited by tabs ("\t")
*/
public class ConvertTxtToTbl {
public static void main(String[] args) throws IOException {
// check the arguments
if (args.length != 1) {
System.out.println("usage: java ConvertTxtToTbl <tablename> \n creats <tablename>.tbl files");
System.exit(1);
}
String tblname = args[0];
String mdfile = tblname + ".md";
String tblfile = tblname + ".tbl";
/** open the input and output streams **/
BufferedReader in = new BufferedReader(new FileReader(tblname + ".txt"));
//ObjectOutputStream outmd = new ObjectOutputStream(new FileOutputStream(mdfile));
ObjectOutputStream outtbl = new ObjectOutputStream(new FileOutputStream(tblfile));
/** First Line is METADATA **/
int linenum = 0;
String line;
/**
String line = in.readLine();
linenum++;
StringTokenizer tokenizer = new StringTokenizer(line);
int count = tokenizer.countTokens();
if(count!=1){
System.err.println("The first line shuld be 'METADATA'");
System.exit(1);
}
String title = tokenizer.nextToken();
if(!title.equals( "METADATA")){
System.err.println("ConvertTxtToTbl-line40: The first line shuld be 'METADATA'");
System.exit(1);
}
//Reading the meta data from text file and creating
// Schema of the table in Object format
boolean flag=false;
Vector attrlist = new Vector();
while(flag==false){
line = in.readLine();
linenum++;
tokenizer = new StringTokenizer(line);
if(tokenizer.countTokens()==1){
flag=true;
}else{
Attribute attr;
String attname= tokenizer.nextToken();
String datatype = tokenizer.nextToken();
int type;
int keytyp;
if(datatype.equals("INT")){
type= Attribute.INT;
// System.out.println("integer");
}else if(datatype.equals("STRING")){
type=Attribute.STRING;
// System.out.println("String");
}else if(datatype.equals("REAL")){
type=Attribute.REAL;
}else{
type=-1;
System.err.println("invalid data type");
System.exit(1);
}
if(tokenizer.hasMoreElements()){
String keytype = tokenizer.nextToken();
if(keytype.equals("PK"))
keytyp=Attribute.PK;
else if(keytype.equals("FK"))
keytyp=Attribute.FK;
else{
keytyp=-1;
System.err.println("invalid key type");
System.exit(1);
}
attr = new Attribute(tblname,attname,type,keytyp);
}else{
attr = new Attribute(tblname,attname,type);
}
attrlist.add(attr);
}
}
Schema schema = new Schema(attrlist);
outmd.writeObject(schema);
outmd.close();
// Starting of the data section
String header = tokenizer.nextToken();
if(header=="DATA"){
System.err.println("The data section should start with 'DATA'");
System.exit(1);
}
int numCols = schema.getNumCols();
**/
Schema schema = null;
try {
ObjectInputStream ins = new ObjectInputStream(new FileInputStream(mdfile));
schema = (Schema) ins.readObject();
} catch (ClassNotFoundException ce) {
System.out.println("class not found exception --- error in schema object file");
System.exit(1);
}
boolean flag = false;
StringTokenizer tokenizer;
while ((line = in.readLine()) != null) {
linenum++;
tokenizer = new StringTokenizer(line);
//int tokencount = tokenizer.countTokens();
//System.out.println("numtokens= "+tokenizer.countTokens()+"numcols="+numCols);
//if(tokencount != numCols){
//System.err.println("Incomplete tuple: linenum= "+linenum);
//System.exit(1);
//}
Vector data = new Vector();
int attrIndex = 0;
while (tokenizer.hasMoreElements()) {
String dataElement = tokenizer.nextToken();
int datatype = schema.typeOf(attrIndex);
//System.out.print("Convert :"+ dataElement+" "+datatype);
if (datatype == Attribute.INT) {
//System.out.println("Integer data:"+dataElement);
data.add(Integer.valueOf(dataElement));
} else if (datatype == Attribute.REAL) {
data.add(Float.valueOf(dataElement));
} else if (datatype == Attribute.STRING) {
data.add(dataElement);
} else {
System.err.println("Invalid data type");
System.exit(1);
}
attrIndex++;
}
Tuple tuple = new Tuple(data);
outtbl.writeObject(tuple);
}
outtbl.close();
in.close();
}
}