View Javadoc

1   /*
2    * $Id: AbstractIndexer.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.tools.visualizer.postgraphers;
12  
13  import org.mule.tools.visualizer.components.PostGrapher;
14  import org.mule.tools.visualizer.config.GraphConfig;
15  import org.mule.tools.visualizer.config.GraphEnvironment;
16  import org.mule.tools.visualizer.config.VelocitySupport;
17  
18  import java.io.File;
19  import java.io.FileWriter;
20  import java.io.FilenameFilter;
21  import java.util.Arrays;
22  
23  import org.apache.velocity.Template;
24  import org.apache.velocity.VelocityContext;
25  import org.apache.velocity.runtime.RuntimeServices;
26  
27  public abstract class AbstractIndexer extends VelocitySupport implements PostGrapher
28  {
29  
30      protected AbstractIndexer(GraphEnvironment env) throws Exception
31      {
32          super(env);
33      }
34  
35      protected void doRendering(GraphEnvironment env, File[] htmlFiles, String template, String targetFile)
36      {
37          try
38          {
39              setEnv(env);
40              VelocityContext velocityContext = new VelocityContext();
41  
42              velocityContext.put("fileList", Arrays.asList(htmlFiles));
43              // TODO how to retrieve template using classpath ?
44              Template t = getVe().getTemplate(template);
45              File file = new File(targetFile);
46              FileWriter writer = new FileWriter(file);
47  
48              env.log("generating " + file);
49  
50              t.merge(velocityContext, writer);
51              writer.flush();
52              writer.close();
53          }
54          catch (Exception e)
55          {
56              e.printStackTrace();
57          }
58      }
59  
60      protected File[] getFiles(final GraphConfig config, final String extension)
61      {
62          File[] htmlFiles = config.getOutputDirectory().listFiles(new FilenameFilter()
63          {
64              public boolean accept(File dir, String name)
65              {
66                  if (name.toLowerCase().equals("index.html"))
67                  {
68                      return false;
69                  }
70                  else
71                  {
72                      if (name.toLowerCase().equals("gallery.html"))
73                      {
74                          return false;
75                      }
76                      else
77                      {
78                          return name.endsWith(extension);
79                      }
80                  }
81              }
82          });
83          return htmlFiles;
84      }
85  
86      public void init(RuntimeServices arg0) throws Exception
87      {
88          // TODO Auto-generated method stub
89  
90      }
91  
92      public void logVelocityMessage(int arg0, String arg1)
93      {
94          if (getEnv() != null)
95          {
96              getEnv().log(arg1);
97          }
98  
99      }
100 }