Repository navigation
Expand file tree
/
Copy pathBibliographyFactory.java
More file actions
executable file
·713 lines (655 loc) · 22.3 KB
/
Copy pathBibliographyFactory.java
File metadata and controls
executable file
·713 lines (655 loc) · 22.3 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//----------------------------------------------------
//Assignment #3
//Written by: Arsany Fahmy (40157267)
//----------------------------------------------------
//-----------------------------------------------------------
//Important note: the use of BufferedReader has been
//implemented instead of Scanner because of an unexpected
//problem that occurred when loading files (the scanner would
//stop reading a file unexpectedly without any reason)
//-----------------------------------------------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* This class reads a series of 10 bibliography files, checks if they are valid and create 3 files
* containing the following created reference styles: IEE, ACM and NJ
* @author Arsany Fahmy
*/
public class BibliographyFactory
{
static File[] files = new File[10]; //10 files
static BufferedReader[] buff = new BufferedReader[10]; //10 scanners to read 10 files
static BufferedReader[] buff2 = new BufferedReader[10]; //10 more scanners for IEE usage
static BufferedReader[] buff3 = new BufferedReader[10]; //10 more scanners for ACM usage
static BufferedReader[] buff4 = new BufferedReader[10]; //10 more scanners for NJ usage
static String line; //to display a line of a file
static File[] outputFiles = new File[30]; //30 output files
static PrintWriter[] pw = new PrintWriter[30]; //a printWriter for each output file
static int numOfInvalidFiles = 0; //to keep track of invalid files
/**
* Main method were the Bibliography is created for each file
* @param Keyboard Scanner that takes input from the keyboard
*/
public static void main(String[] args)
{
//Scanner Keyboard
Scanner Keyboard = new Scanner(System.in);
//welcome message
System.out.println("Welcome to BibliographyFactory!");
System.out.println(" ");
//Creating the 10 files
for(int i = 0; i < 10; i++)
{
files[i] = new File("Latex" + (i+1) + ".bib");
}
//Creating 10 bufferedReader (4 times)
for(int i = 0; i < 10; i++)
{
//if we want to display the content of all 10 files:
/*
try
{
buff[i] = new BufferedReader(new FileReader(files[i]));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
System.out.println("");
System.out.println("**************************************************");
System.out.println("Latex" + (i+1) + ".bib: ");
System.out.println("");
try {
while(buff[i].readLine() != null)
{
try
{
System.out.println(buff[i].readLine());
} catch (IOException e)
{
e.printStackTrace();
}
}
} catch (IOException e)
{
e.printStackTrace();
}
*/
try
{
buff[i] = new BufferedReader(new FileReader(files[i]));
buff2[i] = new BufferedReader(new FileReader(files[i]));
buff3[i] = new BufferedReader(new FileReader(files[i]));
buff4[i] = new BufferedReader(new FileReader(files[i]));
} catch (FileNotFoundException e)
{
//if file not found: error message and closing files and terminating program
System.out.println("Could not open file " + "Latex" + (i+1) + ".bib for"
+ " reading.\n Please check if file exists!! Program will terminate after"
+ "closing any opened files.");
for (int j = 0; j < i; j++)
{
try {
buff[j].close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
System.exit(0);
}
}
//Creating 30 output files
/* outputFiles[0-9]: IEE reference Files (10)
* outputFiles[10-19]: ACM reference Files (10)
* outputFiles[20-29]: NJ reference Files (10)
*/
for (int i = 0; i < 30; i++)
{
//Creating 10 IEEEi files
if (i < 10)
{
outputFiles[i] = new File("IEEE" + (i+1) + ".json");
try {
pw[i] = new PrintWriter(outputFiles[i]);
} catch (FileNotFoundException e) {
System.out.println("Error: cannot open or create The following"
+ " file: " + outputFiles[i] + ". Deleting all other files!");
for (int j = 0; j < i; j++)
{
outputFiles[j].delete();
}
System.exit(0);
}
}
//Creating 10 ACMi files
if (i >= 10 && i < 20)
{
int fileNumber = i-9;
outputFiles[i] = new File("ACM" + (fileNumber) + ".json");
try {
pw[i] = new PrintWriter(outputFiles[i]);
} catch (FileNotFoundException e) {
System.out.println("Error: cannot open or create The following"
+ " file: " + outputFiles[i] + ". Deleting all other files!");
for (int j = 0; j < i; j++)
{
outputFiles[j].delete();
}
System.exit(0);
}
}
//Creating 10 Nii files
if (i >= 20 && i < 30)
{
int fileNumber = i-19;
outputFiles[i] = new File("NJ" + (fileNumber) + ".json");
try {
pw[i] = new PrintWriter(outputFiles[i]);
} catch (FileNotFoundException e) {
System.out.println("Error: cannot open or create The following"
+ " file: " + outputFiles[i] + ". Deleting all other files!");
for (int j = 0; j < i; j++)
{
outputFiles[j].delete();
}
System.exit(0);
}
}
}
//To check if a file if valid or not and deleting its corresponding reference files if not valid
processFilesForValidation();
//Summary of the execution
System.out.println(" ");
System.out.println("########################################################################################################################################");
System.out.println(" A total of " + numOfInvalidFiles + " files were invalid, and could nnot be processed."
+ " All other " + (10-numOfInvalidFiles) + " valid files have been created.");
//Asking user if they want to review a certain file and displaying the content of that file
boolean secondChance = false; //user can have a second chance if first attempt was invalid
do
{
try
{
System.out.print("Please enter the name of one of the files that you need to review: ");
String review = Keyboard.nextLine();
BufferedReader reviewReader = new BufferedReader(new FileReader(review));
System.out.println("Here are the contents of the successfully created Jason file: " + review );
System.out.println(" ");
line = reviewReader.readLine() ;
while (line != null)
{
System.out.println(line);
line = reviewReader.readLine() ;
}
//Final goodbye
Keyboard.close();
reviewReader.close();
System.out.println(" ");
System.out.println("Goodbye! Hope you have enjoyed creating the needed files using BibiographyFactory.");
System.exit(0);
}catch(FileNotFoundException e)
{
if(secondChance == false) //if first time
{
secondChance = true; //give a second chance
System.out.println("Could not open input file. File does not exist; possibly it could not"
+ " be created!");
System.out.println(" ");
System.out.println("However, you will be allowed a second chance to enter another file name.");
}
else //if second chance already used
{
System.out.println(" ");
System.out.println(" Once again, could not open input file. File does not exist; possibly it could not"
+ " be created!");
System.out.println("Sorry! I am unable to display your desired files! Program will exit!");
System.exit(0);
}
} catch (IOException e)
{
e.printStackTrace();
}
}while (secondChance == true);
//closing Keyboard scanner
Keyboard.close();
}
/**
* This method process each file to check if it's a valid file or not and deletes its corresponding output files (reference files) if not valid
* @param isValid To check if a file is valid or not
* @param count To count how many articles in the file
* @param missingField To display to user which field makes the file invalid
*/
private static void processFilesForValidation()
{
boolean isValid = true;
int count = 0; //to count how many articles in a file
String missingField = "string"; //where the missing field of the invalid file is going to be stored
//Are the files valid?
for (int i = 0; i < 10; i++) //to go through the 10 files
{
try
{
count = 0;
isValid = true;
try {
while((line = buff[i].readLine()) != null)
{
if (line.contains("{}"))
{
missingField = line.substring(0,(line.indexOf('{')-1));
isValid = false;
}
if (line.contains("@ARTICLE"))
count++;
}
} catch (IOException e)
{
e.printStackTrace();
}
//Dealing with valid files
if (isValid == true)
{
//storing the IEE references created in the corresponding created files
IEE(i, count);
ACM(i, count);
NJ(i, count);
}
//dealing with invalid files
else if (isValid == false)
{
numOfInvalidFiles ++;
throw new FileInvalidException();
}
}catch(FileInvalidException e)
{
//displaying error message
System.out.println("Error: Detected empty field!");
System.out.println("=============================");
System.out.println(" ");
System.out.println("Problem detected with input file: " + files[i]);
System.out.println("File is invalid: Field \"" + missingField + "\" is empty. Processing"
+ " of this file has been stoped at this point. Other empty fields may be present as well!");
System.out.println(" ");
//deleting the invalid output files
outputFiles[i].delete(); //deleting IEE reference File
outputFiles[i+10].delete(); //deleting ACM reference File
outputFiles[i+20].delete(); //deleting NJ reference File
}
}
}
/**
* This method creates an IEE reference style for each article in a certain file in a separate folder
* @param index The index of the File being processed in the files[] array
* @param numArticles The number of articles in the file being processed
* @param articles A 2D array of Strings where rach article in the file will be stored each line on its own --> articles[articles][lines]
* @param iee An array of Strings where the final created IEE reference will be stored for later use
*/
private static void IEE(int index, int numArticles)
{
String[][] articles = new String[numArticles][14]; //where the whole file is going to be stored each article on its own
String[] iee = new String [numArticles]; //where the created iee references will be stored
//to fill the array of articles with content of file
for (int i = 0; i < numArticles; i++)
{
for (int j = 0; j < 13; j++)
{
//to avoid having empty lines in the array
try {
line = buff2[index].readLine();
while ((line.length() <= 1) && (line != "{") && (line != "}"))
{
line = buff2[index].readLine();
}
articles[i][j] = line;
} catch (IOException e) {
e.printStackTrace();
}
articles[i][13] = "}";
}
}
/* to see the content of the articles array
System.out.println("*************************************************** ");
System.out.println("File: " + (index+1));
for(int i = 0; i < numArticles; i++)
{
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@");
System.out.println("Article: " + (i+1));
for (int j = 0; j < 14; j++)
{
System.out.println(articles[i][j]);
}
}
String test = "\n";
System.out.println("which files? " + (index+1));
System.out.println("how many articles? " + numArticles);
System.out.println(articles[0][6].isEmpty());
System.out.println(test.length());*/
//Creating the reference
for (int i = 0; i < numArticles; i++) //loop going through each article
{
String author = "author";
String title = "title";
String journal = "journal";
String volume = "volume";
String number = "number";
String pages = "pages";
String month = "month";
String year = "year";
for(int y = 0; y < 14; y++) //loop going through each line (14 lines total)
{
if (articles[i][y].startsWith("author=") == true)
{
//the author part
author = articles[i][y].replaceAll("and", ",").substring(8);
author = author.substring(0, (author.length() - 3));
author = author.replace(" ,", ",");
}
else if (articles[i][y].startsWith("title=") == true)
{
//the title part
title = articles[i][y].substring(7,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("journal=") == true)
{
//the journal part
journal = articles[i][y].substring(9,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("volume=") == true)
{
//the volume part
volume = articles[i][y].substring(8,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("number=") == true)
{
//the number part
number = articles[i][y].substring(8,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("pages=") == true)
{
//the pages part
pages = articles[i][y].substring(7,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("month=") == true)
{
//the month part
month = articles[i][y].substring(7,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("year=") == true)
{
//the year part
year = articles[i][y].substring(6,(articles[i][y].indexOf('}')));
}
}
//all put together
String reference;
reference = author + ". \"" + title + "\", " +
journal + ", vol. " + volume + ", no. " + number + ", p. "
+ pages + ", " + month + " " + year + ".";
//storing the reference in the reference array
iee[i] = reference;
}
//Uploading the created references in their corresponding files
for(int i = 0; i < numArticles; i++)
{
pw[index].println(iee[i]);
pw[index].println(" ");
}
//closing printWriter
pw[index].close();
}
/**
* This method creates an ACM reference style for each article in a certain file in a separate folder
* @param index The index of the File being processed in the files[] array
* @param numArticles The number of articles in the file being processed
* @param articles A 2D array of Strings where each article in the file will be stored each line on its own --> articles[articles][lines]
* @param ACM An array of Strings where the final created ACM reference will be stored for later use
*/
private static void ACM(int index, int numArticles)
{
String[][] articles = new String[numArticles][14]; //where the whole file is going to be stored each article on its own
String[] ACM = new String [numArticles]; //where the created ACM references will be stored
//to fill the array of articles with content of file
for (int i = 0; i < numArticles; i++)
{
for (int j = 0; j < 13; j++)
{
//to avoid having empty lines in the array
try {
line = buff3[index].readLine();
while ((line.length() <= 1) && (line != "{") && (line != "}"))
{
line = buff3[index].readLine();
}
articles[i][j] = line;
} catch (IOException e) {
e.printStackTrace();
}
articles[i][13] = "}";
}
}
/* to see the content of the articles array
System.out.println("*************************************************** ");
System.out.println("File: " + (index+1));
for(int i = 0; i < numArticles; i++)
{
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@");
System.out.println("Article: " + (i+1));
for (int j = 0; j < 14; j++)
{
System.out.println(articles[i][j]);
}
}
String test = "\n";
System.out.println("which files? " + (index+1));
System.out.println("how many articles? " + numArticles);
System.out.println(articles[0][6].isEmpty());
System.out.println(test.length());*/
//Creating the reference
for (int i = 0; i < numArticles; i++) //loop going through each article
{
String author = "author";
String title = "title";
String journal = "journal";
String volume = "volume";
String number = "number";
String pages = "pages";
String month = "month";
String year = "year";
String DOI = "doi";
for(int y = 0; y < 14; y++) //loop going through each line (14 lines total)
{
if (articles[i][y].startsWith("author=") == true)
{
//the author part
if (articles[i][y].contains("and"))
{
author = articles[i][y].substring(8, (articles[i][y].indexOf("and")-1));
}
else
author = articles[i][y].substring(8, (articles[i][y].length()-3));
}
else if (articles[i][y].startsWith("title=") == true)
{
//the title part
title = articles[i][y].substring(7,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("journal=") == true)
{
//the journal part
journal = articles[i][y].substring(9,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("volume=") == true)
{
//the volume part
volume = articles[i][y].substring(8,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("number=") == true)
{
//the number part
number = articles[i][y].substring(8,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("pages=") == true)
{
//the pages part
pages = articles[i][y].substring(7,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("month=") == true)
{
//the month part
month = articles[i][y].substring(7,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("year=") == true)
{
//the year part
year = articles[i][y].substring(6,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("doi=") == true)
{
//the Doi part
DOI = articles[i][y].substring(5,(articles[i][y].indexOf('}')));
}
}
//all put together
String reference;
reference = "[" + (i+1) + "] " + author + " et al. " + year + ". " + title + ". " + journal + ". "
+ volume + ", " + number + " (" + year + "), " + pages + ". DOI:https://doi.org/" + DOI + ". ";
//storing the reference in the reference array
ACM[i] = reference;
}
//Uploading the created references in their corresponding files
for(int i = 0; i < numArticles; i++)
{
pw[(index+10)].println(ACM[i]);
pw[(index+10)].println(" ");
}
//closing printWriter
pw[(index+10)].close();
}
/**
* This method creates an NJ reference style for each article in a certain file in a separate folder
* @param index The index of the File being processed in the files[] array
* @param numArticles The number of articles in the file being processed
* @param articles A 2D array of Strings where each article in the file will be stored each line on its own --> articles[articles][lines]
* @param NJ An array of Strings where the final created NJ reference will be stored for later use
*/
private static void NJ(int index, int numArticles)
{
String[][] articles = new String[numArticles][14]; //where the whole file is going to be stored each article on its own
String[] NJ = new String [numArticles]; //where the created ACM references will be stored
//to fill the array of articles with content of file
for (int i = 0; i < numArticles; i++)
{
for (int j = 0; j < 13; j++)
{
//to avoid having empty lines in the array
try {
line = buff4[index].readLine();
while ((line.length() <= 1) && (line != "{") && (line != "}"))
{
line = buff4[index].readLine();
}
articles[i][j] = line;
} catch (IOException e) {
e.printStackTrace();
}
articles[i][13] = "}";
}
}
/* to see the content of the articles array
System.out.println("*************************************************** ");
System.out.println("File: " + (index+1));
for(int i = 0; i < numArticles; i++)
{
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@");
System.out.println("Article: " + (i+1));
for (int j = 0; j < 14; j++)
{
System.out.println(articles[i][j]);
}
}
String test = "\n";
System.out.println("which files? " + (index+1));
System.out.println("how many articles? " + numArticles);
System.out.println(articles[0][6].isEmpty());
System.out.println(test.length());*/
//Creating the reference
for (int i = 0; i < numArticles; i++) //loop going through each article
{
String author = "author";
String title = "title";
String journal = "journal";
String volume = "volume";
String number = "number";
String pages = "pages";
String month = "month";
String year = "year";
String DOI = "doi";
for(int y = 0; y < 14; y++) //loop going through each line (14 lines total)
{
if (articles[i][y].startsWith("author=") == true)
{
//the author part
author = articles[i][y].replaceAll("and", "&").substring(8);
author = author.substring(0, (author.length() - 3));
}
else if (articles[i][y].startsWith("title=") == true)
{
//the title part
title = articles[i][y].substring(7,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("journal=") == true)
{
//the journal part
journal = articles[i][y].substring(9,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("volume=") == true)
{
//the volume part
volume = articles[i][y].substring(8,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("number=") == true)
{
//the number part
number = articles[i][y].substring(8,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("pages=") == true)
{
//the pages part
pages = articles[i][y].substring(7,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("month=") == true)
{
//the month part
month = articles[i][y].substring(7,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("year=") == true)
{
//the year part
year = articles[i][y].substring(6,(articles[i][y].indexOf('}')));
}
else if (articles[i][y].startsWith("doi=") == true)
{
//the Doi part
DOI = articles[i][y].substring(5,(articles[i][y].indexOf('}')));
}
}
//all put together
String reference;
reference = author + ". " + title + ". " + journal + ". "
+ volume + ", " + pages + "(" + year + "). ";
//storing the reference in the reference array
NJ[i] = reference;
}
//Uploading the created references in their corresponding files
for(int i = 0; i < numArticles; i++)
{
pw[(index+20)].println(NJ[i]);
pw[(index+20)].println(" ");
}
//closing printWriter
pw[(index+20)].close();
}
}