2017-10-05 8 views
0

J'ai besoin d'analyser .feature fichiers et produire des données Json. Je suis actuellement capable de lire les étapes en utilisant des expressions régulières, mais je veux analyser tout le fichier de caractéristiques.Comment analyser le fichier de fonction Cucumber dans java ou groovy?

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

S'il vous plaît lire http://stackoverflow.com/help/how-to-ask et reformuler votre question. Quel problème exact avez-vous? Quelle est votre question exacte? Si votre question est simplement «écrivez-moi le code pour faire ce que je veux», alors votre question est hors sujet sur SO. – Vampire

Répondre

0

J'ai obtenu la réponse dans google forms. la réponse est

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(); 
    } 
} 

vérifier cette URL pour la discussion discussion link