1
2
3
4
5 package hoplugins.conv;
6
7
8
9
10
11 import java.io.File;
12 import java.util.Calendar;
13 import java.util.Date;
14
15 import javax.swing.JOptionPane;
16
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.NodeList;
20 import org.w3c.dom.Text;
21
22
23
24 /***
25 * @author Thorsten Dietz
26 * 01.03.2005
27 */
28 final class Buddy extends HrfMaker{
29
30 boolean isLeague = false;
31
32 private String currentLeague = "0";
33
34 protected Buddy(){
35 type = "Buddy";
36 }
37
38 protected void start(File[] selectedFiles,File targetDir) {
39
40 try {
41
42 String htString = null;
43 String[] filter = null;
44 File[][] xmls = null;
45 filter = getName(selectedFiles);
46
47 xmls = getFiles(filter, selectedFiles);
48 for (int i = 0; i < xmls.length; i++) {
49 if (xmls[i][0] == null || xmls[i][1] == null
50 || xmls[i][2] == null || xmls[i][3] == null
51 || xmls[i][4] == null || xmls[i][5] == null) {
52 handleException(null, RSC.PROP_FILE_NOT_FOUND + " :"
53 + filter + RSC.BUDDY_EXTENSION[i]);
54 return;
55 }
56 }
57
58 if (targetDir != null) {
59
60 JOptionPane.showMessageDialog(null, RSC
61 .getProperty("download_supplemental_files"));
62 htString = new String(
63 RSC.MINIMODEL
64 .getDownloadHelper()
65 .getHattrickXMLFile(
66 "/Common/worldDetails.asp?outputType=XML&actionType=leagues")
67 .getBytes(), "UTF-8");
68 Document doc = null;
69
70 for (int i = 0; i < xmls.length; i++) {
71
72 clearArrays();
73
74 addBasics();
75 doc = getDocument(getUTF8String(xmls[i][1]));
76
77 analyzeClub(doc.getDocumentElement().getChildNodes());
78 doc = getDocument(getUTF8String(xmls[i][0]));
79
80 analyzeArena(doc.getDocumentElement().getChildNodes());
81 doc = getDocument(getUTF8String(xmls[i][5]));
82
83 analyzeTraining(doc.getDocumentElement()
84 .getChildNodes());
85 doc = getDocument(getUTF8String(xmls[i][2]));
86
87 analyzeEconomy(doc.getDocumentElement().getChildNodes());
88 doc = getDocument(getUTF8String(xmls[i][4]));
89
90 analyzeTeamDetails(doc.getDocumentElement()
91 .getChildNodes());
92 doc = getDocument(getUTF8String(xmls[i][3]));
93
94
95 initPlayersArray(doc.getDocumentElement()
96 .getChildNodes());
97
98 doc = getDocument(htString);
99 analyzeWorldDetails(doc.getDocumentElement()
100 .getChildNodes());
101
102 writeHrf(filter[i], targetDir);
103 }
104 }
105
106 JOptionPane.showMessageDialog(null, RSC.getProperty("finished"));
107
108 } catch (Exception e1) {
109 handleException(e1, RSC.PROP_DEFAULT_ERROR_MESSAGE);
110 }
111
112 }
113
114 private static File[][] getFiles(String[] filter, File[] selectedFiles) {
115 File[][] xmls = new File[selectedFiles.length][6];
116 File[] tmp = new File[0];
117
118 File dir = selectedFiles[0].getParentFile();
119 tmp = dir.listFiles();
120 for (int i = 0; i < selectedFiles.length; i++) {
121 for (int j = 0; j < tmp.length; j++) {
122 if (tmp[j].getName().equals(filter[i] + RSC.BUDDY_EXTENSION[0]))
123 xmls[i][0] = tmp[j];
124 if (tmp[j].getName().equals(filter[i] + RSC.BUDDY_EXTENSION[1]))
125 xmls[i][1] = tmp[j];
126 if (tmp[j].getName().equals(filter[i] + RSC.BUDDY_EXTENSION[2]))
127 xmls[i][2] = tmp[j];
128 if (tmp[j].getName().equals(filter[i] + RSC.BUDDY_EXTENSION[3]))
129 xmls[i][3] = tmp[j];
130 if (tmp[j].getName().equals(filter[i] + RSC.BUDDY_EXTENSION[4]))
131 xmls[i][4] = tmp[j];
132 if (tmp[j].getName().equals(filter[i] + RSC.BUDDY_EXTENSION[5]))
133 xmls[i][5] = tmp[j];
134 }
135 }
136 return xmls;
137 }
138
139 private final String[] getName(File[] f) {
140 String[] names = new String[f.length];
141 int index = 0;
142 for (int i = 0; i < names.length; i++) {
143 index = f[i].getName().indexOf(".");
144 if (index > 0)
145 names[i] = f[i].getName().substring(0, index);
146 }
147 return names;
148 }
149
150 private final void analyzeWorldDetails(NodeList nodelist) {
151 for (int i = 0; i < nodelist.getLength(); i++) {
152 if (nodelist.item(i).hasChildNodes()) {
153 analyzeWorldDetails(nodelist.item(i).getChildNodes());
154 }
155
156 if (nodelist.item(i) instanceof Element) {
157 Element element = (Element) nodelist.item(i);
158 Text txt = (Text) element.getFirstChild();
159
160 if (txt == null)
161 continue;
162 String tagValue = txt.getData();
163
164 if (tagValue == null)
165 continue;
166
167 if (element.getTagName().equals("LeagueID")) {
168 if (basicsValue[13][1].equals(tagValue)) {
169 currentLeague = tagValue;
170
171 }
172 continue;
173 }
174 if (element.getTagName().equals("CountryID")) {
175 if (basicsValue[13][1].equals(currentLeague)) {
176 basicsValue[12][1] = tagValue;
177 }
178 continue;
179 }
180 if (element.getTagName().equals("Season")) {
181 if (basicsValue[13][1].equals(currentLeague)) {
182 basicsValue[4][1] = tagValue;
183 }
184 continue;
185 }
186 if (element.getTagName().equals("MatchRound")) {
187 if (basicsValue[13][1].equals(currentLeague)) {
188 basicsValue[5][1] = tagValue;
189 }
190 continue;
191 }
192 if (element.getTagName().equals("CurrencyName")) {
193 if (basicsValue[13][1].equals(currentLeague)) {
194 xtraValue[4][1] = tagValue;
195
196 }
197 continue;
198 }
199 if (element.getTagName().equals("CurrencyRate")) {
200 if (basicsValue[13][1].equals(currentLeague)) {
201 xtraValue[5][1] = tagValue.replaceAll(",", ".");
202 }
203 continue;
204 }
205 if (element.getTagName().equals("TrainingDate")) {
206 if (basicsValue[13][1].equals(currentLeague)) {
207
208 boolean otherDay = true;
209 boolean first = true;
210 Date nextTrainingsDate = createDate(tagValue);
211 Date hrfNextTraining = createDate(basicsValue[3][1]);
212
213 while (otherDay) {
214 if (!first
215 && hrfNextTraining.getDay() == nextTrainingsDate
216 .getDay())
217 otherDay = false;
218 else
219 hrfNextTraining.setDate(hrfNextTraining
220 .getDate() + 1);
221 first = false;
222 }
223 Calendar calendar = Calendar.getInstance();
224 calendar.setTime(hrfNextTraining);
225 xtraValue[1][1] = hrfNextTraining.getYear()
226 + "-"
227 + convertNumber((hrfNextTraining.getMonth() + 1))
228 + "-"
229 + convertNumber(calendar
230 .get(Calendar.DAY_OF_MONTH)) + ""
231 + tagValue.substring(10, 19);
232 }
233 continue;
234 }
235 if (element.getTagName().equals("EconomyDate")) {
236
237 if (basicsValue[13][1].equals(currentLeague)) {
238
239 boolean otherDay = true;
240 boolean first = true;
241 Date nextEconomyDate = createDate(tagValue);
242 Date hrfNextEconomy = createDate(basicsValue[3][1]);
243
244 while (otherDay) {
245 if (!first
246 && hrfNextEconomy.getDay() == nextEconomyDate
247 .getDay())
248 otherDay = false;
249 else
250 hrfNextEconomy
251 .setDate(hrfNextEconomy.getDate() + 1);
252 first = false;
253 }
254 Calendar calendar = Calendar.getInstance();
255 calendar.setTime(hrfNextEconomy);
256 xtraValue[2][1] = hrfNextEconomy.getYear()
257 + "-"
258 + convertNumber((hrfNextEconomy.getMonth() + 1))
259 + "-"
260 + convertNumber(calendar
261 .get(Calendar.DAY_OF_MONTH)) + ""
262 + tagValue.substring(10, 19);
263
264 }
265 continue;
266 }
267 if (element.getTagName().equals("SeriesMatchDate")) {
268 if (basicsValue[13][1].equals(currentLeague)) {
269 currentLeague = "0";
270 boolean otherDay = true;
271 boolean first = true;
272 Date nextSeriesMatchDate = createDate(tagValue);
273 Date hrfNextMatch = createDate(basicsValue[3][1]);
274
275 while (otherDay) {
276 if (!first
277 && hrfNextMatch.getDay() == nextSeriesMatchDate
278 .getDay())
279 otherDay = false;
280 else
281 hrfNextMatch
282 .setDate(hrfNextMatch.getDate() + 1);
283 first = false;
284 }
285 Calendar calendar = Calendar.getInstance();
286 calendar.setTime(hrfNextMatch);
287 xtraValue[3][1] = hrfNextMatch.getYear()
288 + "-"
289 + convertNumber((hrfNextMatch.getMonth() + 1))
290 + "-"
291 + convertNumber(calendar
292 .get(Calendar.DAY_OF_MONTH)) + ""
293 + tagValue.substring(10, 19);
294 }
295 continue;
296 }
297 }
298 isLeague = false;
299 }
300 }
301
302
303 private static Date createDate(String input) {
304 int day = Integer.parseInt(input.substring(8, 10));
305 int month = Integer.parseInt(input.substring(5, 7));
306 int year = Integer.parseInt(input.substring(0, 4));
307 int hour = Integer.parseInt(input.substring(11, 13));
308 int minutes = Integer.parseInt(input.substring(14, 16));
309 int seconds = Integer.parseInt(input.substring(17, 19));
310
311 Calendar calendar = Calendar.getInstance();
312
313 calendar.set(Calendar.YEAR, year + 1900);
314 calendar.set(Calendar.DAY_OF_MONTH, day);
315 calendar.set(Calendar.MONTH, month - 1);
316 calendar.set(Calendar.HOUR_OF_DAY, hour);
317 calendar.set(Calendar.MINUTE, minutes);
318 calendar.set(Calendar.SECOND, seconds);
319 return calendar.getTime();
320 }
321 }
322