-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataModel.java
More file actions
395 lines (367 loc) · 19.8 KB
/
DataModel.java
File metadata and controls
395 lines (367 loc) · 19.8 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package p3;
import java.io.*;
import java.util.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import p3.*;
import java.net.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
//This class is in charge of collecting all the required data (countries, albums and songs). Moreover, in this class all the files are parsed and classified in error, fatalerror and correct files
class DataModel{
//The map of correct files
public static TreeMap<String,Document> mapDocs = new TreeMap<String,Document>(); //The year with its document (only the correct documents)
//The list with all names of MUML files found in the first document
public static ArrayList<String> allUrls = new ArrayList<String>();
//The list with all names of MUML files found in the rest of documents
public static ArrayList<String> allUrls2 = new ArrayList<String>();
//The lists of error and fatalerror files
public static ArrayList<String> errorsFile = new ArrayList<String>();
public static ArrayList<String> fatalerrorsFile = new ArrayList<String>();
//To know what files have to be parsed
public static LinkedList<String> pendientes = new LinkedList<String>();
//To parse the files
static DocumentBuilderFactory dbf=null;
static DocumentBuilder db=null;
//Method used to get all the countries that are in the documents
public static ArrayList<String> getQ1Countries() throws ParserConfigurationException, SAXException, IOException{ // We get the list of countries from the documents of the map
//The list of all countries
ArrayList<String> countriesMap= new ArrayList<String>();
try{
//To go all over the map with the correct files
for (String key : mapDocs.keySet()) {
//It gets the document that we need to read its information
Document docUrl = mapDocs.get(key);
//To collect the country elements it is used the getElementByTagName with the tag "Country"
NodeList nl = docUrl.getElementsByTagName("Country");
Element elemCountry;
String countryName;
//To collect all the country elements found
for (int x = 0; x < nl.getLength(); x++) {
elemCountry = (Element)nl.item(x);
//It gets the text (string) of the element
countryName = elemCountry.getTextContent();
//To not repeat the countries that are stored
if (!countriesMap.contains(countryName)) {
countriesMap.add(countryName);
}
}
}
//To order in reverse alphabetical order
Collections.sort(countriesMap);
Collections.reverse(countriesMap);
}catch(Exception e){
}
//It returns the list of all countries
return countriesMap;
}
//Method used to get all the albums of a given country that are in the documents. It is used XPATH
public static ArrayList<Album> getQ1Albums (String country) {
//The list of all albums
ArrayList<Album> albumsMap= new ArrayList<Album>();
try{
//To go all over the map with the correct files
for (String key : mapDocs.keySet()) {
//It gets the document that we need to read its information
Document docUrl = mapDocs.get(key);
//XPATH is used to get the year of the documents first, and then all the albums specifying the country
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String xPathTargetYear = "/Music/Year";
NodeList YearDoc = (NodeList)xpath.evaluate(xPathTargetYear,docUrl, XPathConstants.NODESET );
Element elemYear= (Element)YearDoc.item(0);
String YearString = elemYear.getTextContent().trim();
String xPathTarget = "//Album[Country='"+country+"']"; //albums of a given country
NodeList allAlbumsCountry= (NodeList)xpath.evaluate(xPathTarget,docUrl, XPathConstants.NODESET );
//To get all the found albums
for( int i=0; i< allAlbumsCountry.getLength(); i++){
Element elemAlbum= (Element)allAlbumsCountry.item(i); //album by album
//To get the Review
String xPathTargetReview = "text()[normalize-space()]";
String reviewAlbum= (String)xpath.evaluate(xPathTargetReview,elemAlbum, XPathConstants.STRING);
String reviewAlb = reviewAlbum.trim();
//To get the format (it is an attribute)
String format = elemAlbum.getAttribute("format").trim();
//To get the aid (it is and attribute)
String aid = elemAlbum.getAttribute("aid").trim();
//To get the Name
NodeList nameAlbum= elemAlbum.getElementsByTagName("Name");
Element elemNameAlbum = (Element)nameAlbum.item(0);
//It gets the text (string) of the element
String nameAlb = elemNameAlbum.getTextContent().trim();
//To get the Country
NodeList countryAlbum= elemAlbum.getElementsByTagName("Country");
Element elemCountryAlbum = (Element)countryAlbum.item(0);
//It gets the text (string) of the element
String countryAlb = elemCountryAlbum.getTextContent().trim();
//To get the Performer
NodeList singerAlbum =elemAlbum.getElementsByTagName("Singer");
String singerAlb= null;
Element elemSingerAlbum;
//The singer could be null because maybe the performer is a group
if((elemSingerAlbum = (Element)singerAlbum.item(0))!=null){
//It gets the text (string) of the element
singerAlb = elemSingerAlbum.getTextContent().trim();
}
NodeList groupAlbum= elemAlbum.getElementsByTagName("Group");
String groupAlb=null;
Element elemGroupAlbum;
//The group could be null because maybe the performer is a singer
if((elemGroupAlbum = (Element)groupAlbum.item(0))!=null){
//It gets the text (string) of the element
groupAlb = elemGroupAlbum.getTextContent().trim();
}
//To get the ISBN
NodeList ISBNAlbum= elemAlbum.getElementsByTagName("ISBN");
Element elemISBNAlbum = (Element)ISBNAlbum.item(0);
//It gets the text (string) of the element
String ISBNAlb = elemISBNAlbum.getTextContent().trim();
//To get the Company
NodeList companyAlbum= elemAlbum.getElementsByTagName("Company");
Element elemCompanyAlbum = (Element)companyAlbum.item(0);
//It gets the text (string) of the element
String companyAlb = elemCompanyAlbum.getTextContent().trim();
//To call the constructor with the singer or group we need to know which one is null
if(singerAlb!=null){
Album AlbumObj = new Album(nameAlb,countryAlb,singerAlb,ISBNAlb,companyAlb,aid,YearString,reviewAlb,format);
//The album is added to the list
albumsMap.add(AlbumObj);
}
if(groupAlb!=null){
Album AlbumObj = new Album(nameAlb,countryAlb,groupAlb,ISBNAlb,companyAlb,aid,YearString,reviewAlb,format);
//The album is added to the list
albumsMap.add(AlbumObj);
}
}
}
}catch(Exception e){
}
//The albums must be in alphabetical order
Collections.sort(albumsMap);
//It returns the list of all found albums
return albumsMap;
}
//Method used to get all the pop songs of a given country and a given album that are in the documents. It is used XPATH
public static ArrayList<Song> getQ1Songs (String country, String album) {
//List of all found songs
ArrayList<Song> songsMap= new ArrayList<Song>();
try{
//To go all over the map with the correct files
for (String key : mapDocs.keySet()) {
//It gets the document that we need to read its information
Document docUrl = mapDocs.get(key);
//XPATH is used to collect all the songs of a given country and a given album and that has a Pop genre
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String xPathTarget = "//Album[@aid='"+album+"'][Country='"+country+"']/Song[Genre='Pop']"; //asi tenemos todos las songs de un album
NodeList allSongsAlbums= (NodeList)xpath.evaluate(xPathTarget,docUrl, XPathConstants.NODESET );
//To get all the found songs
for( int i=0; i< allSongsAlbums.getLength(); i++){
Element elemSong= (Element)allSongsAlbums.item(i); //song by song
//To get the sid (it is an attribute)
String sid = elemSong.getAttribute("sid");
//To get the langage (it is an attribute)
String lang = elemSong.getAttribute("lang");
//To get the Title
NodeList titleSong= elemSong.getElementsByTagName("Title");
Element elemTitleSong = (Element)titleSong.item(0);
//It gets the text (string) of the element
String titleS = elemTitleSong.getTextContent().trim();
//To get the Duration
NodeList durationSong= elemSong.getElementsByTagName("Duration");
Element elemDurationSong = (Element)durationSong.item(0);
//It gets the text (string) of the element
String durationS = elemDurationSong.getTextContent().trim();
//To get the Genres
NodeList genreSong= elemSong.getElementsByTagName("Genre");
//To collect the genres because they could be more than one
String genreS[]=new String[genreSong.getLength()];
//To get all the genres
for(int x=0; x< genreSong.getLength();x++){
Element elemGenreSong = (Element)genreSong.item(x);
//It gets the text (string) of the element
genreS[x] = elemGenreSong.getTextContent().trim();
}
//An array is created to get a string with the genres separated with ','
ArrayList<String> list = new ArrayList<String>();
for(int index=0; index<genreS.length; index++){
list.add(genreS[index]);
}
//The genres into a string
String genresToString = String.join(", ", list);
//To get the Composer
NodeList composerSong= elemSong.getElementsByTagName("Composer");
String composerS=null;
Element elemComposerSong;
//It could be null because it is optional
if((elemComposerSong = (Element)composerSong.item(0))!=null){
//It gets the text (string) of the element
composerS = elemComposerSong.getTextContent().trim();
}
//To get the MUML
NodeList muMLSong= elemSong.getElementsByTagName("MuML");
Element elemmuMLSong;
String muMLS=null;
//It could be null because it is optional
if((elemmuMLSong= (Element)muMLSong.item(0))!=null){
//It gets the text (string) of the element
muMLS = elemmuMLSong.getTextContent().trim();
}
//Creating a song calling tha class Song
Song SongObj = new Song(composerS,titleS,durationS,genresToString,muMLS,lang,sid);
//Adding the song to the list
songsMap.add(SongObj);
}
}
}catch(Exception e){
}
//The songs must be in alphabetical order
Collections.sort(songsMap);
//It returns the list of all found songs
return songsMap;
}
//To find the year of the document using XPATH
public static String catchYear(Document documento){
String YearString=null;
try{
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
//To get the year of the file
String xPathTargetYear = "/Music/Year";
NodeList YearDoc = (NodeList)xpath.evaluate(xPathTargetYear,documento, XPathConstants.NODESET );
Element elemYear= (Element)YearDoc.item(0);
//It gets the text (string) of the element
YearString = elemYear.getTextContent().trim();
}catch(Exception e){
}
//The year is returned
return YearString;
}
//To find the mumls of the document using XPATH
public static ArrayList<String> catchUrls(Document documento){
//The list with all the mumls
ArrayList<String> allUrls= new ArrayList<String>();
try{
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
//To get all the mumls of the file
String xPathTargetMML = "//MuML";
NodeList MMLDoc = (NodeList)xpath.evaluate(xPathTargetMML,documento, XPathConstants.NODESET );
for( int x=0; x< MMLDoc.getLength();x++){
Element elemMML= (Element)MMLDoc.item(x);
String MMLString = elemMML.getTextContent().trim();
//To not repeat mumls
if(allUrls.indexOf(MMLString)<1){
allUrls.add(MMLString);
}
}
}catch(Exception e){}
//The mumls are returned
return allUrls;
}
//This method parsers the files and classifies them into error, fatalerror and correct documents
public static void parserDocs(){
//The parameters are initialized
String yearOfDoc="";
String urlToParse="";
Boolean wellform=false;
Document doc=null;
Document docNext=null;
//The general URL and the URL of the first document
String urlGeneral= "http://alberto.gil.webs.uvigo.es/SINT/22-23/";
String firstUrl="http://alberto.gil.webs.uvigo.es/SINT/22-23/muml2001.xml";
try{
//The first document is parsed
URL url = new URL(firstUrl);
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
doc = db.parse(url.openStream());
//To know if the document is wellformed or not
wellform=true;
}catch(Exception e){
//It is not wellformed, there is a fatalerror
//It is added to the list of fatalerror files
fatalerrorsFile.add(firstUrl);
wellform=false;
}
//It is wellformed. Now we have to verify if it is correct or it has an error in the year
if(wellform){
//To get all mumls of the file
allUrls= catchUrls(doc);
//To get the year of the file
yearOfDoc= catchYear(doc);
int yearOfDocInt = Integer.parseInt (yearOfDoc);
//To know if the year is correct
if(yearOfDocInt >= 1980 && yearOfDocInt <=2021){
//It is correct, so it is added to the map of correct files
mapDocs.put(yearOfDoc,doc);
//The found mumls are stored in the list of pendientes because they are going to be parsed
for(int index=0; index<allUrls.size(); index++){
//To not repeat
if(pendientes.indexOf(allUrls.get(index))<1){
pendientes.add(allUrls.get(index));
}
}
}else{
//The year is wrong, so the file is added to the list of error files
errorsFile.add(firstUrl);
}
}
//While there is a file to be parsed
while(!pendientes.isEmpty()) {
try{
//To get the url
urlToParse= urlGeneral+pendientes.get(0);
//The document is parsed
docNext= db.parse(urlToParse);
//To know if the document is wellformed or not
wellform=true;
}catch(Exception e){
//It is not wellformed, there is a fatalerror
//It is added to the list of fatalerror files
fatalerrorsFile.add(urlToParse);
//The file is removed from pendientes list because it is already analyzed
pendientes.pop();
wellform=false;
}
//It is wellformed. Now we have to verify if it is correct or it has an error in the year
if(wellform){
//To get all the MUMLs of a file
allUrls2= catchUrls(docNext);
//To get the year of a file
String yearOfDocNext= catchYear(docNext);
int yearOfDocNextInt = Integer.parseInt (yearOfDocNext);
if(yearOfDocNextInt >= 1980 && yearOfDocNextInt <=2021){
//It is correct, so it is added to the map of correct files
//The file is removed from pendientes list because it is already analyzed
pendientes.pop();
mapDocs.put(yearOfDocNext,docNext);
//The found mumls are stored in the list of pendientes because they are going to be parsed
for(int index=0; index<allUrls2.size(); index++){
//To not repeat
if(pendientes.indexOf(allUrls2.get(index))<1){
pendientes.add(allUrls2.get(index));
}
}
} else{
//The year is wrong, so the file is added to the list of error files
errorsFile.add(urlToParse);
//The file is removed from pendientes list because it is already analyzed
pendientes.pop();
}
}
}
}
//To get the list of files with errors (in browser mode)
public static ArrayList<String> getErrorsFiles(){
return errorsFile;
}
//To get the list of files with fatal errors (in browser mode)
public static ArrayList<String> getFatalErrorsFiles(){
return fatalerrorsFile;
}
}