This repository was archived by the owner on May 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTransferUtils.java
More file actions
281 lines (234 loc) · 10.5 KB
/
TransferUtils.java
File metadata and controls
281 lines (234 loc) · 10.5 KB
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
package com.rubengees.vocables.utils;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Xml;
import com.rubengees.vocables.R;
import com.rubengees.vocables.pojo.MeaningList;
import com.rubengees.vocables.pojo.Unit;
import com.rubengees.vocables.pojo.Vocable;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
/**
* Created by Ruben Gees on 11.02.2015.
*/
public class TransferUtils {
private static final String TAG_UNITS = "units";
private static final String TAG_UNIT = "unit";
private static final String TAG_TITLE = "title";
private static final String TAG_VOCABLES = "vocables";
private static final String TAG_VOCABLE = "vocable";
private static final String TAG_FIRST_MEANING = "first_meaning";
private static final String TAG_SECOND_MEANING = "second_meaning";
private static final String TAG_VALUE = "value";
private static final String TAG_HINT = "hint";
private static final String EMPTY = "";
private static final String EXTENSION_CSV = ".csv";
private static final String EXTENSION_XML = ".xml";
public static boolean isFileSupported(@NonNull File file) {
String filename = file.getName();
return filename.endsWith(EXTENSION_CSV) || filename.endsWith(EXTENSION_XML);
}
public static void export(@NonNull List<Unit> units, @NonNull File toExport)
throws IOException {
XmlSerializer serializer = Xml.newSerializer();
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
BufferedWriter writer = new BufferedWriter(new FileWriter(toExport));
serializer.setOutput(writer);
writeXml(serializer, units);
}
private static void writeXml(@NonNull XmlSerializer serializer, @NonNull List<Unit> units)
throws IOException {
serializer.startDocument("UTF-16", true);
serializer.startTag(null, TAG_UNITS);
for (Unit unit : units) {
serializer.startTag(null, TAG_UNIT);
serializer.startTag(null, TAG_TITLE);
serializer.text(unit.getTitle());
serializer.endTag(null, TAG_TITLE);
serializer.startTag(null, TAG_VOCABLES);
for (Vocable vocable : unit.getVocables()) {
serializer.startTag(null, TAG_VOCABLE);
serializer.startTag(null, TAG_FIRST_MEANING);
insertMeaning(serializer, vocable.getFirstMeaningList());
serializer.endTag(null, TAG_FIRST_MEANING);
serializer.startTag(null, TAG_SECOND_MEANING);
insertMeaning(serializer, vocable.getSecondMeaningList());
serializer.endTag(null, TAG_SECOND_MEANING);
if(vocable.getHint() != null) {
serializer.startTag(null, TAG_HINT);
serializer.text(vocable.getHint());
serializer.endTag(null, TAG_HINT);
}
serializer.endTag(null, TAG_VOCABLE);
}
serializer.endTag(null, TAG_VOCABLES);
serializer.endTag(null, TAG_UNIT);
}
serializer.endTag(null, TAG_UNITS);
serializer.endDocument();
}
private static void insertMeaning(@NonNull XmlSerializer serializer,
@NonNull MeaningList meaningList) throws IOException {
for (String word : meaningList.getMeanings()) {
serializer.startTag(null, TAG_VALUE);
serializer.text(word);
serializer.endTag(null, TAG_VALUE);
}
}
public static List<Unit> getList(@NonNull Context context, @NonNull File file)
throws FormatException, IOException {
if (file.getName().endsWith(EXTENSION_CSV)) {
return parseCsv(context, file);
} else if (file.getName().endsWith(EXTENSION_XML)) {
return parseXml(context, file);
} else {
throw new FormatException(context.getString(R.string.transfer_import_format_error));
}
}
private static List<Unit> parseCsv(@NonNull Context context, @NonNull File file)
throws FormatException, IOException {
HashMap<String, Unit> unitMap = new HashMap<>();
BufferedReader reader = new BufferedReader(new FileReader(file));
long creationTime = System.currentTimeMillis();
int pos = 0;
String line = reader.readLine();
String unitTitle;
while (line != null) {
String[] split = line.split(",");
if (split.length == 2) {
unitTitle = context.getString(R.string.database_uni_title_default);
} else if (split.length == 3) {
unitTitle = split[2];
} else {
throw new FormatException(context.getString(R.string.dialog_import_error_message) +
" " + String.valueOf(pos));
}
String[] split1 = split[0].split(";");
String[] split2 = split[1].split(";");
if (split1.length >= 1 && split2.length >= 1) {
MeaningList first = new MeaningList(new ArrayList<>(Arrays.asList(split1)));
MeaningList second = new MeaningList(new ArrayList<>(Arrays.asList(split2)));
Vocable vocable = new Vocable(first, second, null, creationTime);
Unit unit;
if (unitMap.containsKey(unitTitle)) {
unit = unitMap.get(unitTitle);
} else {
unit = new Unit();
unit.setTitle(unitTitle);
unitMap.put(unitTitle, unit);
}
unit.add(vocable);
} else {
throw new FormatException(context.getString(R.string.dialog_import_error_message) +
" " + String.valueOf(pos));
}
line = reader.readLine();
pos++;
}
return new ArrayList<>(unitMap.values());
}
private static List<Unit> parseXml(Context context, File file)
throws FormatException, IOException {
List<Unit> result = new ArrayList<>();
XmlPullParser parser = null;
long creationTime = System.currentTimeMillis();
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
parser = factory.newPullParser();
parser.setInput(new BufferedReader(new FileReader(file)));
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, TAG_UNITS);
while (parser.nextTag() == XmlPullParser.START_TAG) {
Unit unit = new Unit();
parser.require(XmlPullParser.START_TAG, null, TAG_UNIT);
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, TAG_TITLE);
unit.setTitle(parser.nextText());
parser.require(XmlPullParser.END_TAG, null, TAG_TITLE);
parser.nextTag();
unit.addAll(getVocablesFromXml(parser, creationTime));
parser.require(XmlPullParser.END_TAG, null, TAG_UNIT);
result.add(unit);
}
parser.require(XmlPullParser.END_TAG, null, TAG_UNITS);
} catch (XmlPullParserException e) {
if (parser != null) {
throw new FormatException(context.getString(R.string.dialog_import_error_message)
+ " " + String.valueOf(parser.getLineNumber()));
}
}
return result;
}
private static List<Vocable> getVocablesFromXml(XmlPullParser parser, long creationTime)
throws IOException, XmlPullParserException {
List<Vocable> result = new ArrayList<>();
parser.require(XmlPullParser.START_TAG, null, TAG_VOCABLES);
while (parser.nextTag() != XmlPullParser.END_TAG) {
parser.require(XmlPullParser.START_TAG, null, TAG_VOCABLE);
Vocable vocable = null;
while (parser.nextTag() != XmlPullParser.END_TAG) {
MeaningList first;
MeaningList second;
if(vocable != null) {
String hint = readOptionalHint(parser);
if(hint != null) {
vocable.setHint(hint);
vocable = null;
continue;
}
}
parser.require(XmlPullParser.START_TAG, null, TAG_FIRST_MEANING);
first = getMeaningFromXml(parser);
parser.require(XmlPullParser.END_TAG, null, TAG_FIRST_MEANING);
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, TAG_SECOND_MEANING);
second = getMeaningFromXml(parser);
parser.require(XmlPullParser.END_TAG, null, TAG_SECOND_MEANING);
vocable = new Vocable(first, second, null, creationTime);
result.add(vocable);
}
}
parser.require(XmlPullParser.END_TAG, null, TAG_VOCABLES);
parser.nextTag();
return result;
}
private static String readOptionalHint(XmlPullParser parser) {
try {
parser.require(XmlPullParser.START_TAG, null, TAG_HINT);
String hint = parser.nextText();
parser.require(XmlPullParser.END_TAG, null, TAG_HINT);
return hint == null? EMPTY : hint;
} catch(IOException | XmlPullParserException e) {
return null;
}
}
private static MeaningList getMeaningFromXml(XmlPullParser parser)
throws IOException, XmlPullParserException {
List<String> words = new ArrayList<>();
parser.nextTag();
do {
parser.require(XmlPullParser.START_TAG, null, TAG_VALUE);
words.add(parser.nextText());
parser.require(XmlPullParser.END_TAG, null, TAG_VALUE);
} while (parser.nextTag() != XmlPullParser.END_TAG);
return new MeaningList(words);
}
public static class FormatException extends Exception {
public FormatException(String s) {
super(s);
}
}
}