Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. package main;
  2.  
  3. import java.io.PrintWriter;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7.  
  8. import person.Author;
  9. import biblio.Bibliography;
  10. import biblio.Entry;
  11. import biblio.Query;
  12. import csv.CSV;
  13.  
  14. public class Main {
  15.  
  16. private static final String FILTER_TYPE = "filter_type";
  17. private static final String FILTER_VALUE = "filter_value";
  18. private static final String FORMAT = "format_string";
  19.  
  20. private static final String AUTHOR_VALUE = "author";
  21. private static final String PUBLISHER_VALUE = "publisher";
  22.  
  23. public static void main(String[] args) {
  24.  
  25. final CSV csv = readCSV();
  26. final Bibliography bibliography = new Bibliography(csv);
  27. final Map<String, String> properties = loadProperties(args);
  28. final Query query = getQuery(properties);
  29.  
  30. if (query != null) {
  31. bibliography.filter(query);
  32. }
  33.  
  34. final String result = bibliography.show(parseFormat(properties
  35. .get(FORMAT)));
  36. printResult(result);
  37. }
  38.  
  39. private static void printResult(String result) {
  40. try (final PrintWriter writer = new PrintWriter(System.out)) {
  41. writer.print(result);
  42. writer.flush();
  43. }
  44. }
  45.  
  46. private static Query getQuery(Map<String, String> properties) {
  47. Query query;
  48. String filterType = properties.get(FILTER_TYPE);
  49. String filterValue = properties.get(FILTER_VALUE);
  50. if (PUBLISHER_VALUE.equals(filterType)) {
  51. return Query.byPublisher(filterValue);
  52. } else if (AUTHOR_VALUE.equals(filterType)) {
  53. final Author author = Author.make(filterValue);
  54. if (author != null) {
  55. return Query.byAuthor(author);
  56. }
  57. }
  58. return null;
  59. }
  60.  
  61. private static CSV readCSV() {
  62. try (final Scanner scanner = new Scanner(System.in)) {
  63. return CSV.read(scanner);
  64. }
  65. }
  66.  
  67. private static Map<String, String> loadProperties(String[] args) {
  68. final Map<String, String> properties = new HashMap<>();
  69.  
  70. for (int i = 0; i < args.length; i = i + 2) {
  71. final String key = args[i];
  72. final String value = args[i + 1];
  73. if ("publisher=".equals(key)) {
  74. properties.put(FILTER_TYPE, PUBLISHER_VALUE);
  75. properties.put(FILTER_VALUE, value);
  76. } else if ("author=".equals(key)) {
  77. properties.put(FILTER_TYPE, AUTHOR_VALUE);
  78. properties.put(FILTER_VALUE, value);
  79. } else if ("format=".equals(key)) {
  80. properties.put(FORMAT, value);
  81. }
  82. }
  83.  
  84. return properties;
  85. }
  86.  
  87. private static int parseFormat(String formatString) {
  88.  
  89. if (formatString == null) {
  90. return Entry.FORMAT_RAW;
  91. }
  92.  
  93. switch (formatString) {
  94. case "authorYear":
  95. return Entry.FORMAT_AUTHOR_YEAR;
  96. case "authorYearCompact":
  97. return Entry.FORMAT_AUTHOR_YEAR_COMPACT;
  98. default:
  99. return Entry.FORMAT_RAW;
  100. }
  101. }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement