My favorites | Sign in
Project Logo
                
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package org.quaternions.ipddump;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import org.quaternions.ipddump.data.InteractivePagerBackup;
import org.quaternions.ipddump.data.Record;

/**
* Parse an IPD file and populate a {@link InteractivePagerBackup} with the
* records.
*
* @author borkholder
* @date Jan 13, 2008
*/
public class IPDParser {
/**
* The name/path of the file to parse.
*/
protected final String fileName;

/**
* Specifies the state of the parser, that is, the current part of the IPD
* that the parser is reading.
*
* @author borkholder
* @date Jan 13, 2008
*/
protected enum ReadingState {
/**
* The file identification header - "Inter@ctive Pager Backup/Restore File".
*/
HEADER,

/**
* The character used to for line feeds in text fields - 0x0A.
*/
LINEFEED,

/**
* The IPD file version - 0x02.
*/
VERSION,

/**
* The number of databases in this file - 2 bytes in big endian.
*/
DATABASECOUNT,

/**
* The byte that separates database names - 0x00.
*/
DATABASENAMESEPARATOR,

/**
* The length of the next specified database name - 2 bytes in little
* endian. The length includes the terminating null of the name string.
*/
DATABASENAMELENGTH,

/**
* The name of the database - size as specified by the preceeding database
* name length.
*/
DATABASENAME,

/**
* The 0-based index of the database that contains this record - 2 bytes
* little endian.
*/
DATABASEID,

/**
* The length of the record in number of bytes that follow this value - 4
* bytes little endian.
*/
RECORDLENGTH,

/**
* The version of the database to which this record belongs - 1 byte.
*/
RECORDDBEVERSION,

/**
* A handle of the record within the database (this is a increasing sequence
* of integers with the IPD) - 2 bytes little endian.
*/
DATABASERECORDHANDLE,

/**
* The unique ID of the record within the database - 4 bytes.
*/
RECORDUNIQUEID,

/**
* The length of the field data in number of bytes - 2 bytes little endian.
*/
FIELDLENGTH,

/**
* The type of the field within the record - 1 byte.
*/
FIELDTYPE,

/**
* The field data - as long as is specified by the preceeding length.
*/
FIELDDATA;
}

/**
* Creates a new IPDParser that will parse the file at the given path.
*
* @param fileName The path of the file to parse
*/
public IPDParser( String fileName ) {
this.fileName = fileName;
}

/**
* Parses the provided IPD file into an {@link InteractivePagerBackup}.
*
* @return A new InteractivePagerBackup representing the IPD file
* @throws IOException Any error in reading the IPD file
*/
public InteractivePagerBackup parse() throws IOException {
// Temporary variables used in parsing
char lineFeed = 0;
int numberOfDatabases = 0;
int dbNameLength = 0;
int recordRead = 0;
int recordDBVersion = 0;
int databaseHandle = 0;
int fieldLength = 0;
int fieldType = 0;
int dbID = 0;
int recordLength = 0;
int uid = 0;

Record record = null;
InteractivePagerBackup database = null;
FileInputStream input = null;

try {
input = new FileInputStream( fileName );
FileChannel fc = input.getChannel();

// Start reading in the header state
ReadingState state = ReadingState.HEADER;
while ( fc.position() < fc.size() ) {
switch ( state ) {
case HEADER:
for ( int i = 0; i < "Inter@ctive Pager Backup/Restore File".length(); i++ ) {
input.read();
}
state = ReadingState.LINEFEED;
break;

case LINEFEED:
lineFeed = (char) input.read();
state = ReadingState.VERSION;
break;

case VERSION:
database = new InteractivePagerBackup( input.read(), lineFeed );
state = ReadingState.DATABASECOUNT;
break;

case DATABASECOUNT:
numberOfDatabases = input.read() << 8;
numberOfDatabases |= input.read();
state = ReadingState.DATABASENAMESEPARATOR;
break;

// Just eat it because we know the terminating null will be in
// the name anyway
case DATABASENAMESEPARATOR:
input.read();
state = ReadingState.DATABASENAMELENGTH;
break;

case DATABASENAMELENGTH:
dbNameLength = input.read();
dbNameLength |= input.read() << 8;
state = ReadingState.DATABASENAME;
break;

case DATABASENAME:
StringBuffer buffer = new StringBuffer();
// Read everything but the terminating null
for ( int i = 0; i < dbNameLength - 1; i++ ) {
buffer.append( (char) input.read() );
}

database.addDatabase( buffer.toString() );

// Eat null/separator
input.read();

if ( database.databaseNames().size() < numberOfDatabases ) {
state = ReadingState.DATABASENAMELENGTH;
} else {
state = ReadingState.DATABASEID;
}
break;

case DATABASEID:
dbID = input.read();
dbID |= input.read() << 8;
state = ReadingState.RECORDLENGTH;
break;

case RECORDLENGTH:
recordLength = input.read();
recordLength |= input.read() << 8;
recordLength |= input.read() << 16;
recordLength |= input.read() << 24;
recordRead = 0;
state = ReadingState.RECORDDBEVERSION;
break;

case RECORDDBEVERSION:
recordRead++;
recordDBVersion = input.read();
state = ReadingState.DATABASERECORDHANDLE;
break;

case DATABASERECORDHANDLE:
databaseHandle = input.read();
databaseHandle |= input.read() << 8;
recordRead += 2;
state = ReadingState.RECORDUNIQUEID;
break;

case RECORDUNIQUEID:
uid = input.read();
uid |= input.read() << 8;
uid |= input.read() << 16;
uid |= input.read() << 24;
recordRead += 4;
record = database.createRecord( dbID, recordDBVersion, uid, recordLength );
record.setRecordDBHandle( databaseHandle );
state = ReadingState.FIELDLENGTH;
break;

case FIELDLENGTH:
fieldLength = input.read();
fieldLength |= input.read() << 8;
recordRead += 2;
state = ReadingState.FIELDTYPE;
break;

case FIELDTYPE:
fieldType = input.read();
recordRead++;
state = ReadingState.FIELDDATA;
break;

case FIELDDATA:
char[] dataBuffer = new char[ fieldLength ];
for ( int i = 0; i < fieldLength; i++ ) {
dataBuffer[ i ] = (char) input.read();
}

record.addField( fieldType, dataBuffer );
recordRead += fieldLength;

if ( recordRead < record.getLength() ) {
state = ReadingState.FIELDLENGTH;
} else {
state = ReadingState.DATABASEID;
}
break;
}
}
} finally {
input.close();
}

database.organize();
return database;
}
}

Show details Hide details

Change log

r68 by borkhobs on Jun 24, 2009   Diff
Streamlined the Contact class and started
to clean up code.
Go to: 
Project members, sign in to write a code review

Older revisions

r28 by jimdaskalakis01 on Jun 04, 2009   Diff
Changes Made for:'If no argument
given, then the gui pops up' and 'Only
*.ipd Files will get parsed now'
r24 by borkhobs on Jun 01, 2009   Diff
Reformatted the entire project and
added project-specific code formatting
styles.
r20 by borkhobs on Jan 13, 2008   Diff
Renamed the Database to
InteractivePagerBackup.
Tried to clean up the rest of the code
and make the rest of the parser
significant.
All revisions of this file

File info

Size: 7707 bytes, 285 lines
Hosted by Google Code