Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.08 KB | None | 0 0
  1. import java.awt.print.PrinterAbortException;
  2. import java.io.*;
  3. import java.util.Scanner;
  4.  
  5. public class BibCreator {
  6.  
  7.     public static void processFilesForValidation(Scanner[] scanners) {
  8.         String field = null;
  9.         String line = null;
  10.         int invalidFileCounter = 0;
  11.         File file = null;
  12.         File file2 = null;
  13.         File file3 = null;
  14.  
  15.  
  16.  
  17.         for(int scanner = 0; scanner < scanners.length; scanner++) {
  18.  
  19.             int articleCount = 0;
  20.  
  21.             try {
  22.  
  23.                 // file is valid, start reading articles
  24.  
  25.                 String author = null;
  26.                 String journal = null;
  27.                 String title = null;
  28.                 String year = null;
  29.                 String volume = null;
  30.                 String number = null;
  31.                 String pages = null;
  32.                 String keywords = null;
  33.                 String doi = null;
  34.                 String ISSN = null;
  35.                 String month = null;
  36.  
  37.                 while (scanners[scanner].hasNextLine()) {
  38.  
  39.                     line = scanners[scanner].nextLine();
  40.  
  41.                     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
  42.  
  43.                         articleCount++;
  44.  
  45.                         author = null;
  46.                         journal = null;
  47.                         title = null;
  48.                         year = null;
  49.                         volume = null;
  50.                         number = null;
  51.                         pages = null;
  52.                         keywords = null;
  53.                         doi = null;
  54.                         ISSN = null;
  55.                         month = null;
  56.  
  57.                         //consume another line
  58.                         line = scanners[scanner].nextLine();
  59.                     }
  60.  
  61.                     // if the line contains the specific word, split the next line into two segments
  62.                     // everything after the regex is saved as the information pertaining to the contained word
  63.  
  64.                     if (line.contains("author=")) {
  65.                         field = "author";
  66.                         author = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
  67.                     }
  68.  
  69.                     if (line.contains("journal=")) {
  70.                         field = "journal";
  71.                         journal = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
  72.                     }
  73.  
  74.                     if (line.contains("title=")) {
  75.                         field = "title";
  76.                         title = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
  77.                     }
  78.  
  79.                     if (line.contains("year=")) {
  80.                         field = "year";
  81.                         year = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
  82.                     }
  83.  
  84.                     if (line.contains("volume=")) {
  85.                         field = "volume";
  86.                         volume = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
  87.                     }
  88.  
  89.                     if (line.contains("number=")) {
  90.                         field = "number";
  91.                         number = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
  92.                     }
  93.  
  94.                     if (line.contains("pages=")) {
  95.                         field = "pages";
  96.                         pages = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
  97.                     }
  98.  
  99.                     if (line.contains("keywords=")) {
  100.                         field = "keywords";
  101.                         keywords = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
  102.                     }
  103.  
  104.                     if (line.contains("doi=")) {
  105.                         field = "doi";
  106.                         doi = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
  107.                     }
  108.  
  109.                     if (line.contains("ISSN=")) {
  110.                         field = "ISSN";
  111.                         ISSN = line.substring(line.indexOf("{") + 1, line.indexOf("}"));
  112.                     }
  113.  
  114.                     if (line.contains("month=")) {
  115.                         field = "month";
  116.                         month = line.split("month=")[1];
  117.                     }
  118.  
  119.                     if (line.equals("}")) {
  120.  
  121.                         // recording to an IEEE file
  122.                         file = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\IEEE" + (scanner + 1) + ".json");
  123.                         try {
  124.                             PrintWriter pw;
  125.  
  126.                             if (articleCount == 1) {
  127.                                 pw = new PrintWriter(new FileOutputStream(file, false), true);
  128.                             } else {
  129.                                 pw = new PrintWriter(new FileOutputStream(file, true), true);
  130.                             }
  131.  
  132.                             pw.println(author.replace("and", ".") + "," + journal + "," + title + "," + year + "," + volume + "," + number + "," + pages + "," + keywords + "," + doi + "," + ISSN + "," + month);
  133.                             pw.flush();
  134.                             pw.close();
  135.                         } catch (FileNotFoundException e) {
  136.                             System.out.print("There was an error processing IEEE" + (scanner + 1) + ".json, the program will terminate.");
  137.                             System.exit(0);
  138.                         }
  139.  
  140.                         // recording to an ACM file
  141.                         file2 = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\ACM" + (scanner + 1) + ".json");
  142.                         try {
  143.                             PrintWriter pw;
  144.  
  145.                             if (articleCount == 1) {
  146.                                 pw = new PrintWriter(new FileOutputStream(file2, false), true);
  147.                             } else {
  148.                                 pw = new PrintWriter(new FileOutputStream(file2, true), true);
  149.                             }
  150.  
  151.                             pw.println(author.replace("and", "et al") + "," + journal + "," + title + "," + year + "," + volume + "," + number + "," + pages + "," + keywords + "," + doi + "," + ISSN + "," + month);
  152.                             pw.flush();
  153.                             pw.close();
  154.                         } catch (FileNotFoundException e) {
  155.                             System.out.print("There was an error processing ACM" + (scanner + 1) + ".json, the program will terminate.");
  156.                             System.exit(0);
  157.                         }
  158.  
  159.                         // recording to an NJ file
  160.                         file3 = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\NJ" + (scanner + 1) + ".json");
  161.                         try {
  162.                             PrintWriter pw;
  163.  
  164.                             if (articleCount == 1) {
  165.                                 pw = new PrintWriter(new FileOutputStream(file3, false), true);
  166.                             } else {
  167.                                 pw = new PrintWriter(new FileOutputStream(file3, true), true);
  168.                             }
  169.  
  170.                             pw.println(author.replace("and", "&") + "," + journal + "," + title + "," + year + "," + volume + "," + number + "," + pages + "," + keywords + "," + doi + "," + ISSN + "," + month);
  171.                             pw.flush();
  172.                             pw.close();
  173.                         } catch (FileNotFoundException e) {
  174.                             System.out.print("There was an error processing NJ" + (scanner + 1) + ".json, the program will terminate.");
  175.                             System.exit(0);
  176.                         }
  177.                     }
  178.  
  179.                     // if the content is empty, throw the exception
  180.                     if (line.contains("{}")) {
  181.                         throw new FileInvalidException();
  182.                     }
  183.  
  184.  
  185.                 } //done reading file
  186.  
  187.  
  188.             } // end of try
  189.             catch (FileInvalidException e) {
  190.  
  191.                 file.delete();
  192.                 file2.delete();
  193.                 file3.delete();
  194.  
  195.                 invalidFileCounter++;
  196.  
  197.                 System.out.println("Error: Detected empty field!");
  198.                 System.out.println("****************************");
  199.                 System.out.println("Problem detected with input file:" + " Latex" + (scanner + 1) + ".bib");
  200.                 System.out.println("File is invalid: Field " + field + " is empty. Processing stopped at this point. Other empty fields may be present as well.");
  201.                 System.out.println("\n");
  202.  
  203.  
  204.             } // end of catch
  205.         }
  206.         System.out.print("A total of " + invalidFileCounter + " files were invalid, and could not be processed. All other " + (10-invalidFileCounter) + " 'valid' files have been created.");
  207.     }
  208.  
  209.  
  210.     public static void main(String[] args) {
  211.  
  212.         System.out.println("Welcome to the BibCreator program!\n");
  213.  
  214.         int i=0; // save the index of the file if one does not exist
  215.  
  216.         Scanner[] fileScanners = new Scanner[10];
  217.  
  218.         for (; i < 10; i++) {
  219.             try {
  220.                 fileScanners[i] = new Scanner(new FileInputStream("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\Latex" + (i + 1) + ".bib"));
  221.             } // end of try
  222.  
  223.             catch (FileNotFoundException e) {
  224.                 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.");
  225.                 fileScanners[i].close();
  226.                 System.exit(0); // if the file does not exist, close any open files and exit the program
  227.             } // end of catch
  228.         }
  229.  
  230.         //fileScanners[i].close(); // close the scanner
  231.  
  232.  
  233.  
  234.         processFilesForValidation(fileScanners);
  235.  
  236.         PrintWriter[] fileWriters = new PrintWriter[10];
  237.  
  238.         int j = 0;
  239.  
  240.         // create the IEEE files
  241.         try {
  242.             for (; j < fileWriters.length; j++) {
  243.                 fileWriters[j] = new PrintWriter(new FileOutputStream("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\IEEE" + (j + 1) + ".json", true));
  244.             }
  245.         }
  246.         catch (FileNotFoundException e) {
  247.             System.out.print("Error: Could not open/create IEEE" + j + ".json.");
  248.             File file = null;
  249.             for (; j < 10; j++) {
  250.                 file = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\IEEE" + j + ".json");
  251.             }
  252.             if(file.delete()) {
  253.                 System.out.print("All files in the directory will now be deleted");
  254.                 System.exit(0);
  255.             }
  256.         }
  257.  
  258.         // create the ACM files
  259.         j = 0;
  260.  
  261.         try {
  262.             for (; j < fileWriters.length; j++) {
  263.                 fileWriters[j] = new PrintWriter(new FileOutputStream("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\ACM" + (j + 1) + ".json", true));
  264.             }
  265.         }
  266.         catch (FileNotFoundException e) {
  267.             System.out.print("Error: Could not open/create ACM" + j + ".json.");
  268.             File file = null;
  269.             File file2 = null;
  270.             for (; j < 10; j++) {
  271.                 file = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\IEEE" + j + ".json");
  272.                 file2 = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\ACM" + j + ".json");
  273.             }
  274.             if(file.delete() && file2.delete()) {
  275.                 System.out.print("All files in the directory will now be deleted");
  276.                 System.exit(0);
  277.             }
  278.         }
  279.  
  280.  
  281.         // create the NJ files
  282.         j = 0;
  283.  
  284.         try {
  285.             for (; j < fileWriters.length; j++) {
  286.                 fileWriters[j] = new PrintWriter(new FileOutputStream("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\NJ" + (j + 1) + ".json", true));
  287.             }
  288.         }
  289.         catch (FileNotFoundException e) {
  290.             System.out.print("Error: Could not open/create NJ" + j + ".json.");
  291.             File file = null;
  292.             File file2 = null;
  293.             File file3 = null;
  294.             for (; j < 10; j++) {
  295.                 file = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\IEEE" + j + ".json");
  296.                 file2 = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\ACM" + j + ".json");
  297.                 file3 = new File("C:\\Users\\kimbe\\Desktop\\COMP249\\Assignment3\\src\\Files\\CreatedFiles\\NJ" + j + ".json");
  298.             }
  299.             if(file.delete() && file2.delete() && file3.delete()) {
  300.                 System.out.print("All files in the directory will now be deleted");
  301.                 System.exit(0);
  302.             }
  303.         }
  304.  
  305.       // fileWriters[j].close(); // close the printWriter
  306.  
  307.     } // end of main
  308.  
  309. } // end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement