Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.print.PrinterAbortException;
- import java.io.*;
- import java.util.Scanner;
- public class BibCreator {
- public static void processFilesForValidation(Scanner[] scanners) {
- String field = null;
- String line = null;
- int invalidFileCounter = 0;
- File file = null;
- File file2 = null;
- File file3 = null;
- for(int scanner = 0; scanner < scanners.length; scanner++) {
- int articleCount = 0;
- try {
- // file is valid, start reading articles
- String author = null;
- String journal = null;
- String title = null;
- String year = null;
- String volume = null;
- String number = null;
- String pages = null;
- String keywords = null;
- String doi = null;
- String ISSN = null;
- String month = null;
- while (scanners[scanner].hasNextLine()) {
- line = scanners[scanner].nextLine();
- if (line.contains("@ARTICLE{")) { // if this is true, i am at the beginning of an article, and reset all the information to null on next article
- articleCount++;
- author = null;
- journal = null;
- title = null;
- year = null;
- volume = null;
- number = null;
- pages = null;
- keywords = null;
- doi = null;
- ISSN = null;
- month = null;
- //consume another line
- line = scanners[scanner].nextLine();
- }
- // if the line contains the specific word, split the next line into two segments
- // everything after the regex is saved as the information pertaining to the contained word
- if (line.contains("author=")) {
- field = "author";
- author = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
- }
- if (line.contains("journal=")) {
- field = "journal";
- journal = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
- }
- if (line.contains("title=")) {
- field = "title";
- title = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
- }
- if (line.contains("year=")) {
- field = "year";
- year = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
- }
- if (line.contains("volume=")) {
- field = "volume";
- volume = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
- }
- if (line.contains("number=")) {
- field = "number";
- number = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
- }
- if (line.contains("pages=")) {
- field = "pages";
- pages = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
- }
- if (line.contains("keywords=")) {
- field = "keywords";
- keywords = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
- }
- if (line.contains("doi=")) {
- field = "doi";
- doi = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
- }
- if (line.contains("ISSN=")) {
- field = "ISSN";
- ISSN = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
- }
- if (line.contains("month=")) {
- field = "month";
- month = line.split("month=")[1];
- }
- if (line.equals("}")) {
- // recording to an IEEE file
- file = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\IEEE" + (scanner + 1) + ".json");
- try {
- PrintWriter pw;
- if (articleCount == 1) {
- pw = new PrintWriter(new FileOutputStream(file, false), true);
- } else {
- pw = new PrintWriter(new FileOutputStream(file, true), true);
- }
- pw.println(author.replace("and", ".") + "," + journal + "," + title + "," + year + "," + volume + "," + number + "," + pages + "," + keywords + "," + doi + "," + ISSN + "," + month);
- pw.flush();
- pw.close();
- } catch (FileNotFoundException e) {
- System.out.print("There was an error processing IEEE" + (scanner + 1) + ".json, the program will terminate.");
- System.exit(0);
- }
- // recording to an ACM file
- file2 = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\ACM" + (scanner + 1) + ".json");
- try {
- PrintWriter pw;
- if (articleCount == 1) {
- pw = new PrintWriter(new FileOutputStream(file2, false), true);
- } else {
- pw = new PrintWriter(new FileOutputStream(file2, true), true);
- }
- pw.println(author.replace("and", "et al") + "," + journal + "," + title + "," + year + "," + volume + "," + number + "," + pages + "," + keywords + "," + doi + "," + ISSN + "," + month);
- pw.flush();
- pw.close();
- } catch (FileNotFoundException e) {
- System.out.print("There was an error processing ACM" + (scanner + 1) + ".json, the program will terminate.");
- System.exit(0);
- }
- // recording to an NJ file
- file3 = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\NJ" + (scanner + 1) + ".json");
- try {
- PrintWriter pw;
- if (articleCount == 1) {
- pw = new PrintWriter(new FileOutputStream(file3, false), true);
- } else {
- pw = new PrintWriter(new FileOutputStream(file3, true), true);
- }
- pw.println(author.replace("and", "&") + "," + journal + "," + title + "," + year + "," + volume + "," + number + "," + pages + "," + keywords + "," + doi + "," + ISSN + "," + month);
- pw.flush();
- pw.close();
- } catch (FileNotFoundException e) {
- System.out.print("There was an error processing NJ" + (scanner + 1) + ".json, the program will terminate.");
- System.exit(0);
- }
- }
- // if the content is empty, throw the exception
- if (line.contains("{}")) {
- throw new FileInvalidException();
- }
- } //done reading file
- } // end of try
- catch (FileInvalidException e) {
- file.delete();
- file2.delete();
- file3.delete();
- invalidFileCounter++;
- System.out.println("Error: Detected empty field!");
- System.out.println("****************************");
- System.out.println("Problem detected with input file:" + " Latex" + (scanner + 1) + ".bib");
- System.out.println("File is invalid: Field " + field + " is empty. Processing stopped at this point. Other empty fields may be present as well.");
- System.out.println("\n");
- } // end of catch
- }
- System.out.print("A total of " + invalidFileCounter + " files were invalid, and could not be processed. All other " + (10-invalidFileCounter) + " 'valid' files have been created.");
- }
- public static void main(String[] args) {
- System.out.println("Welcome to the BibCreator program!\n");
- int i=0; // save the index of the file if one does not exist
- Scanner[] fileScanners = new Scanner[10];
- for (; i < 10; i++) {
- try {
- fileScanners[i] = new Scanner(new FileInputStream("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\Latex" + (i + 1) + ".bib"));
- } // end of try
- catch (FileNotFoundException e) {
- System.out.print("Could not open input file Latex" + (i + 1) + ".bib for reading. Please check if file exists! Program will terminate after closing any opened files.");
- fileScanners[i].close();
- System.exit(0); // if the file does not exist, close any open files and exit the program
- } // end of catch
- }
- //fileScanners[i].close(); // close the scanner
- processFilesForValidation(fileScanners);
- PrintWriter[] fileWriters = new PrintWriter[10];
- int j = 0;
- // create the IEEE files
- try {
- for (; j < fileWriters.length; j++) {
- fileWriters[j] = new PrintWriter(new FileOutputStream("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\IEEE" + (j + 1) + ".json", true));
- }
- }
- catch (FileNotFoundException e) {
- System.out.print("Error: Could not open/create IEEE" + j + ".json.");
- File file = null;
- for (; j < 10; j++) {
- file = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\IEEE" + j + ".json");
- }
- if(file.delete()) {
- System.out.print("All files in the directory will now be deleted");
- System.exit(0);
- }
- }
- // create the ACM files
- j = 0;
- try {
- for (; j < fileWriters.length; j++) {
- fileWriters[j] = new PrintWriter(new FileOutputStream("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\ACM" + (j + 1) + ".json", true));
- }
- }
- catch (FileNotFoundException e) {
- System.out.print("Error: Could not open/create ACM" + j + ".json.");
- File file = null;
- File file2 = null;
- for (; j < 10; j++) {
- file = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\IEEE" + j + ".json");
- file2 = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\ACM" + j + ".json");
- }
- if(file.delete() && file2.delete()) {
- System.out.print("All files in the directory will now be deleted");
- System.exit(0);
- }
- }
- // create the NJ files
- j = 0;
- try {
- for (; j < fileWriters.length; j++) {
- fileWriters[j] = new PrintWriter(new FileOutputStream("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\NJ" + (j + 1) + ".json", true));
- }
- }
- catch (FileNotFoundException e) {
- System.out.print("Error: Could not open/create NJ" + j + ".json.");
- File file = null;
- File file2 = null;
- File file3 = null;
- for (; j < 10; j++) {
- file = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\IEEE" + j + ".json");
- file2 = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\ACM" + j + ".json");
- file3 = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\NJ" + j + ".json");
- }
- if(file.delete() && file2.delete() && file3.delete()) {
- System.out.print("All files in the directory will now be deleted");
- System.exit(0);
- }
- }
- // fileWriters[j].close(); // close the printWriter
- } // end of main
- } // end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement