1   package com.stateofflow.eclipse.metrics.export.html;
2   
3   import java.io.File;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import org.eclipse.jdt.core.IJavaProject;
8   import org.eclipse.jdt.core.IPackageFragmentRoot;
9   import org.eclipse.jdt.core.JavaModelException;
10  
11  import com.java2html.BadOptionException;
12  import com.java2html.Java2HTML;
13  import com.stateofflow.eclipse.metrics.util.ProgressMonitor;
14  
15  class Java2HtmlWriter implements ProgressMonitor.Runnable {
16      private final File directory;
17      private final IJavaProject project;
18  
19      public Java2HtmlWriter(final File directory, final IJavaProject project) {
20          this.directory = directory;
21          this.project = project;
22      }
23  
24      public void run(final ProgressMonitor progressMonitor) throws Exception {
25          initialiseDirectory();
26  
27          progressMonitor.newChild(1).beginTask("Exporting Java", 1);
28          createJava2Html(getPackageFragments()).buildJava2HTML();
29      }
30  
31      private void initialiseDirectory() {
32          if (!directory.exists()) {
33              directory.mkdir();
34          }
35      }
36  
37      private String[] getPackageFragments() throws JavaModelException {
38          final List<String> fragments = new ArrayList<String>();
39          for (final IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
40              if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
41                  fragments.add(root.getCorrespondingResource().getLocation().toOSString());
42              }
43          }
44          return fragments.toArray(new String[fragments.size()]);
45      }
46  
47      private Java2HTML createJava2Html(final String[] roots) throws JavaModelException, BadOptionException {
48          final Java2HTML java2HTML = new Java2HTML();
49  
50          java2HTML.setDestination(directory.getAbsolutePath());
51          java2HTML.setMarginSize(4);
52          java2HTML.setTabSize(4);
53          java2HTML.setHeader(true);
54          java2HTML.setFooter(false);
55          java2HTML.setJavaDirectorySource(roots);
56  
57          return java2HTML;
58      }
59  }
60