1   package eu.fbk.dkm.pikes.resources.wes;
2   
3   import ixa.kaflib.KAFDocument;
4   import org.apache.commons.io.FileUtils;
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
7   
8   import java.io.BufferedWriter;
9   import java.io.File;
10  import java.io.FileWriter;
11  import java.io.IOException;
12  import java.util.Iterator;
13  
14  /**
15   * Created by alessio on 06/12/15.
16   */
17  
18  public class Naf2Html {
19  
20      private static final Logger LOGGER = LoggerFactory.getLogger(Naf2Html.class);
21  
22      public static void main(String[] args) {
23  
24          String nafFolder = "/Users/alessio/Documents/Resources/wes/wes2015.naf";
25          String htmlFolder = "/Users/alessio/Documents/Resources/wes/html";
26          String[] extensions = new String[] { "naf" };
27  
28          File nafFolderFile = new File(nafFolder);
29          File htmlFolderFile = new File(htmlFolder);
30  
31          try {
32              Iterator<File> fileIterator = FileUtils.iterateFiles(nafFolderFile, extensions, true);
33  
34              if (!htmlFolderFile.exists()) {
35                  htmlFolderFile.mkdirs();
36              }
37  
38              File indexFile = new File(htmlFolder + File.separator + "index.html");
39              BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));
40  
41              writer.append("<html>\n");
42              writer.append("<body>\n");
43              writer.append("<h1>List of files</h1>\n");
44              writer.append("<ul>\n");
45  
46              fileIterator.forEachRemaining((File f) -> {
47                  File outputHtml = new File(htmlFolder + File.separator + f.getName() + ".html");
48  
49                  try {
50                      KAFDocument document = KAFDocument.createFromFile(f);
51                      String title = document.getFileDesc().title;
52                      String text = document.getRawText();
53  
54                      BufferedWriter insideWriter = new BufferedWriter(new FileWriter(outputHtml));
55                      insideWriter.append("<html>\n");
56                      insideWriter.append("<head>\n");
57                      insideWriter.append(String.format("<title>%s</title>", title));
58                      insideWriter.append("</head>\n");
59                      insideWriter.append("<body>\n");
60                      insideWriter.append(String.format("<h1>%s</h1>", title));
61                      insideWriter.append("<p>\n");
62                      insideWriter.append(text.replaceAll("\n", "<br />\n"));
63                      insideWriter.append("</p>\n");
64                      insideWriter.append("<p>\n");
65                      insideWriter.append(String.format("<p><a href='index.html'>Back to index</a></p>"));
66                      insideWriter.append("</p>\n");
67                      insideWriter.append("</body>\n");
68                      insideWriter.append("</html>\n");
69                      insideWriter.close();
70  
71                      writer.append(String.format("<li><a href='%s'>%s - %s</a></li>\n", outputHtml.getName(),
72                              outputHtml.getName(), title));
73                  } catch (Exception e) {
74                      e.printStackTrace();
75                  }
76              });
77  
78              writer.append("</ul>\n");
79              writer.append("</body>\n");
80              writer.append("</html>\n");
81              writer.close();
82  
83          } catch (Exception e) {
84              e.printStackTrace();
85          }
86      }
87  }