This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.java
322 lines (284 loc) · 12.7 KB
/
Main.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class Main {
public static void main(String[] args) throws Exception {
// creates a File object representing the "export.xml" file
File xmlFile = new File("export.xml");
// creates a new DocumentBuilderFactory
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
// creates a new DocumentBuilder using the DocumentBuilderFactory
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
// parses the xmlFile and creates a new Document object
Document doc = dBuilder.parse(xmlFile);
// normalizes the document
doc.getDocumentElement().normalize();
// gets all the elements in the document and stores them in a NodeList
NodeList nodeList = doc.getElementsByTagName("*");
// creates a new list to store animes
List<String> animedbID = new ArrayList<>();
List<String> names = new ArrayList<>();
List<String> completedAnime = new ArrayList<>();
List<String> onHoldAnime = new ArrayList<>();
List<String> watchingAnime = new ArrayList<>();
List<String> planToWatchAnime = new ArrayList<>();
List<String> droppedAnime = new ArrayList<>();
// iterates through the NodeList
for (int i = 0; i < nodeList.getLength(); i++) {
// gets the current node
Node node = nodeList.item(i);
// checks if the node name is "name"
if (node.getNodeName().equals("name"))
{
// if it is, adds the node's text content to the names list
names.add(node.getTextContent());
}
if (node.getNodeName().equals("link"))
{
String link = node.getTextContent();
String[] linkParts = link.split("/");
String ID = linkParts[linkParts.length - 1];
animedbID.add(ID);
}
}
// flag variable to track when "On-Hold" is encountered
boolean onHold = false;
boolean watching = false;
boolean planToWatch = false;
boolean dropped = false;
// iterates through the names list
for (String name : names)
{
// checks if the flag is set to true
if(dropped)
{
droppedAnime.add(name);
}
else if(planToWatch)
{
if(name.equals("Dropped"))
{
dropped = true;
continue;
}
planToWatchAnime.add(name);
}
else if(watching)
{
if(name.equals("Plan to watch"))
{
planToWatch = true;
continue;
}
watchingAnime.add(name);
}
else if (onHold)
{
if(name.equals("Watching"))
{
watching = true;
continue;
}
onHoldAnime.add(name);
}
// checks if the name is "On-Hold"
else if (name.equals("On-Hold"))
{
// if it is, sets the flag to true
onHold = true;
}
else
{
if(names.get(0).equals(name))
continue;
// adds the name to the completed list
completedAnime.add(name);
}
}
Scanner input = new Scanner(System.in);
System.out.println("\nPlease be aware that the converted MyAnimeList.xml file does not include information on the\n"
+ "number of watched episodes. This may affect anime in the \"watching\" \"on-hold\" and \"dropped\" categories\n"
+ "and may overwrite existing entries on MyAnimeList with the default value of 0 episodes watched.\n\n"
+ "To avoid this would you like to enter in the amount of episodes watched for these anime? (Yes = 1 / No = -1)");
int x = input.nextInt();
System.out.println("");
// create an empty list to store the watched episode numbers
List<Integer> watchedEpisodes = new ArrayList<>();
if(x != -1)
{
// add 0 for the length of the completedAnime list
for (int i = 0; i < completedAnime.size(); i++)
{
watchedEpisodes.add(0);
}
// prompt the user to enter the watched episode numbers for onHoldAnime and watchingAnime
for (int i = 0; i < onHoldAnime.size(); i++)
{
System.out.println("Enter the watched episode number for " + onHoldAnime.get(i) + ": ");
int watched = input.nextInt();
watchedEpisodes.add(watched);
}
for (int i = 0; i < watchingAnime.size(); i++)
{
System.out.println("Enter the watched episode number for " + watchingAnime.get(i) + ": ");
int watched = input.nextInt();
watchedEpisodes.add(watched);
}
// add 0 for the length of the planToWatchAnime list
for (int i = 0; i < planToWatchAnime.size(); i++)
{
watchedEpisodes.add(0);
}
// add the watched episode numbers for the droppedAnime list
for (int i = 0; i < droppedAnime.size(); i++)
{
System.out.println("Enter the watched episode number for " + droppedAnime.get(i) + ": ");
int watched = input.nextInt();
watchedEpisodes.add(watched);
}
}
else
{
int zero = 0;
for(int i = 0; i < names.size(); i++)
{
watchedEpisodes.add(zero);
}
}
input.close();
prtList(completedAnime, onHoldAnime, watchingAnime, planToWatchAnime, droppedAnime);
generateXML(completedAnime, onHoldAnime, watchingAnime, planToWatchAnime, droppedAnime, animedbID, watchedEpisodes);
}
public static void generateXML(List<String> completedAnime, List<String> onHoldAnime,
List<String> watchingAnime, List<String> planToWatchAnime, List<String> droppedAnime,
List<String> animedbID, List<Integer> watchedEpisodes)throws Exception
{
int listsize1 = completedAnime.size();
int listsize2 = listsize1 + onHoldAnime.size();
int listsize3 = listsize2 + watchingAnime.size();
int listsize4 = listsize3 + planToWatchAnime.size();
int totalsize = listsize4 + droppedAnime.size();
// create a new XML document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
// create the root element "myanimelist"
Element root = doc.createElement("myanimelist");
doc.appendChild(root);
Element myinfo = doc.createElement("myinfo");
root.appendChild(myinfo);
Element userExportType = doc.createElement("user_export_type");
myinfo.appendChild(userExportType);
Text userExportTypeText = doc.createTextNode("1");
userExportType.appendChild(userExportTypeText);
// create the "anime" elements and their sub elements
for (int i = 0; i < totalsize; i++) {
Element anime = doc.createElement("anime");
root.appendChild(anime);
Element seriesAnimedbId = doc.createElement("series_animedb_id");
Text seriesAnimedbIdText = doc.createTextNode(animedbID.get(i));
seriesAnimedbId.appendChild(seriesAnimedbIdText);
anime.appendChild(seriesAnimedbId);
Element seriesTitle = doc.createElement("series_title");
if(listsize1 > i)
{
CDATASection seriesTitleCDATA = doc.createCDATASection(completedAnime.get(i));
seriesTitle.appendChild(seriesTitleCDATA);
}
else if(listsize2 > i)
{
CDATASection seriesTitleCDATA = doc.createCDATASection(onHoldAnime.get(i-listsize1));
seriesTitle.appendChild(seriesTitleCDATA);
}
else if(listsize3 > i)
{
CDATASection seriesTitleCDATA = doc.createCDATASection(watchingAnime.get(i-listsize2));
seriesTitle.appendChild(seriesTitleCDATA);
}
else if(listsize4 > i)
{
CDATASection seriesTitleCDATA = doc.createCDATASection(planToWatchAnime.get(i-listsize3));
seriesTitle.appendChild(seriesTitleCDATA);
}
else if(totalsize > i)
{
CDATASection seriesTitleCDATA = doc.createCDATASection(droppedAnime.get(i-listsize4));
seriesTitle.appendChild(seriesTitleCDATA);
}
anime.appendChild(seriesTitle);
Element myWatchedEpisodes = doc.createElement("my_watched_episodes");
Text myWatchedEpisodesText = doc.createTextNode(String.valueOf(watchedEpisodes.get(i)));
myWatchedEpisodes.appendChild(myWatchedEpisodesText);
anime.appendChild(myWatchedEpisodes);
Element myStatus = doc.createElement("my_status");
if(listsize1 > i)
{
Text myStatusText = doc.createTextNode("Completed");
myStatus.appendChild(myStatusText);
}
else if(listsize2 > i)
{
Text myStatusText = doc.createTextNode("On-Hold");
myStatus.appendChild(myStatusText);
}
else if(listsize3 > i)
{
Text myStatusText = doc.createTextNode("Watching");
myStatus.appendChild(myStatusText);
}
else if(listsize4 > i)
{
Text myStatusText = doc.createTextNode("Plan to Watch");
myStatus.appendChild(myStatusText);
}
else if(totalsize > i)
{
Text myStatusText = doc.createTextNode("Dropped");
myStatus.appendChild(myStatusText);
}
anime.appendChild(myStatus);
Element updateOnImport = doc.createElement("update_on_import");
Text updateOnImportText = doc.createTextNode("1");
updateOnImport.appendChild(updateOnImportText);
anime.appendChild(updateOnImport);
}
// create a Transformer object and transform the XML document into a stream of bytes
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult("MyAnimeList.xml");
transformer.transform(source, result);
}
public static void prtList(List<String> completedAnime, List<String> onHoldAnime,
List<String> watchingAnime, List<String> planToWatchAnime, List<String> droppedAnime)
{
// prints the completed list
System.out.println("\n\n---------------------------- Completed Anime ---------------------------- \n");
System.out.println(completedAnime);
System.out.println("\n\n----------------------------- On-Hold Anime ----------------------------- \n");
System.out.println(onHoldAnime);
System.out.println("\n\n---------------------------- Watching Anime ----------------------------- \n");
System.out.println(watchingAnime);
System.out.println("\n\n-------------------------- Plan to Watch Anime -------------------------- \n");
System.out.println(planToWatchAnime);
System.out.println("\n\n----------------------------- Dropped Anime ----------------------------- \n");
System.out.println(droppedAnime);
System.out.println("\n\n");
}
}