2017-10-05 56 views
0

我需要解析.feature文件並生成Json數據。我目前能夠閱讀使用正則表達式的步驟,但我想分析整個功能文件。如何在java或groovy中解析Cucumber功能文件?

Path path= Paths.get("path to feature file").toAbsolutePath(); 
    try { 
     File file = new File(path.toString()) 
        try { 
         List<String> data = Files.readAllLines(file); 

         data.forEach(line -> { 
          Matcher stepMatcher = Pattern.compile("\\b(Given|When|Then|And|But)(.*)").matcher(line); 
          if (stepMatcher.find()) { 
           System.out.println(line); 
          } 
         }); 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
+0

請仔細閱讀http://stackoverflow.com/help/how-to-ask和重新表述您的問題。你有什麼確切的問題?你確切的問題是什麼?如果你的問題只是「寫我的代碼做我想做的事」,那麼你的問題就是關於SO的話題。 – Vampire

回答

0

我在google表格中得到了答案。答案是

public void readFeatureFile() { 
    Path fpath= Paths.get("path to file").toAbsolutePath(); 
    String path = fpath.toString(); 
    String gherkin; 
    try { 

     gherkin = FixJava.readReader(new InputStreamReader(new FileInputStream(path), "UTF-8")); 
     System.out.println("gherkin...\n" + gherkin); 
     StringBuilder json = new StringBuilder(); 
     System.out.println("json: '" + json + "'"); 
     JSONFormatter formatter = new JSONFormatter(json); 
     System.out.println("formatter: " + formatter.toString()); 
     Parser parser = new Parser(formatter); 
     System.out.println("parser: " + parser.toString()); 
     parser.parse(gherkin, path, 0); 
     System.out.println("json: '" + json + "'"); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (RuntimeException e) { 
     e.printStackTrace(); 
    } 
} 

退房這個網址爲討論 discussion link