Advertisement
daegron

parser

Apr 14th, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. //CsvConverter
  2. public class CsvConverter {
  3.  
  4.     public static void convert(String file, List<Order> out) {
  5.         BlockingQueue<String> data = new LinkedBlockingQueue<>();
  6.         ReadService.readFile("src/main/resources/" + file, data);
  7.         CsvMapper mapper = new CsvMapper();
  8.         CsvSchema schema = CsvSchema.builder()
  9.                 .addColumn("orderId")
  10.                 .addColumn("amount")
  11.                 .addColumn("currency")
  12.                 .addColumn("comment")
  13.                 .build();
  14.  
  15.         data.parallelStream().forEachOrdered(s -> {
  16.             try {
  17.                 out.add((Order) mapper
  18.                         .readerFor(Order.class)
  19.                         .with(schema)
  20.                         .readValues(s).nextValue());
  21.             } catch (IOException e) {
  22.                 e.printStackTrace();
  23.             }
  24.         });
  25.     }
  26. }
  27.  
  28. //JsonConverter
  29. public class JsonConverter {
  30.  
  31.     public static void convert(String file, List<Order> out) {
  32.         BlockingQueue<String> data = new LinkedBlockingQueue<>();
  33.         ReadService.readFile("src/main/resources/" + file, data);
  34.         ObjectMapper mapper = new ObjectMapper();
  35.         data.parallelStream().forEachOrdered(s -> {
  36.             try {
  37.                 out.add(mapper.readValue(s, Order.class));
  38.             } catch (JsonProcessingException e) {
  39.                 e.printStackTrace();
  40.             }
  41.         });
  42.     }
  43. }
  44. //ReadService
  45. public class ReadService {
  46.  
  47.     public static void readFile(String fileName, BlockingQueue<String> data) {
  48.         try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
  49.             for (String line; (line = br.readLine()) != null; ) {
  50.                 data.add(line);
  51.             }
  52.         } catch (IOException e) {
  53.             e.printStackTrace();
  54.         }
  55.     }
  56. }
  57.  
  58. //App
  59. @SpringBootApplication
  60. public class ParsingProjApplication {
  61.  
  62.     public static void main(String[] args) {
  63.         SpringApplication.run(ParsingProjApplication.class, args);
  64.         BlockingQueue<String> data = new LinkedBlockingQueue<>();
  65.         List<String> permittedExt = new ArrayList<String>(Arrays.asList("json", "csv"));
  66.         List<Order> res = new ArrayList<>();
  67.         for (String arg : args) {
  68.             String ext = null;
  69.             try {
  70.                 ext = FilenameUtils.getExtension(arg);
  71.                 if (!permittedExt.contains(ext))
  72.                     throw new IllegalArgumentException();
  73.                 System.out.println(ext);
  74.             } catch (IllegalArgumentException e) {
  75.                 System.out.println("File format is not correct");
  76.             }
  77.             if (Objects.equals(ext, "json")){
  78.                 JsonConverter.convert(arg, res);
  79.             } else {
  80.                 CsvConverter.convert(arg, res);
  81.             }
  82.         }
  83.         System.out.println(res);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement