View Javadoc

1   package org.apache.maven.vdoclet;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  import java.io.File;
21  import java.io.FileWriter;
22  import java.io.IOException;
23  
24  import org.apache.commons.jelly.JellyContext;
25  import org.apache.commons.jelly.tags.velocity.JellyContextAdapter;
26  import org.apache.velocity.context.Context;
27  
28  import com.thoughtworks.qdox.model.JavaSource;
29  
30  import vdoclet.Generator;
31  import vdoclet.docinfo.DocInfo;
32  import vdoclet.docinfo.QDoxBuilder;
33  
34  /**
35   * Command-line entry-point to vDoclet
36   */
37  public class VDocletBean
38  {
39      /** output directory */
40      File srcDir;
41      
42      /** output directory */
43      File destDir;
44  
45      /** output directory */
46      String outputFile;
47  
48      /** control template */
49      String template;
50  
51      /** encoding */
52      String encoding;
53  
54      /** verbose flag */
55      boolean verbose = false;
56      
57      /** docinfo */
58      DocInfo docInfo;
59  
60      /** The jelly context */
61      JellyContext jellyContext;
62  
63      /**
64       * Alias for srcPath
65       */
66      public void setSrcDir(File srcDir)
67      {
68          this.srcDir = srcDir;
69      }
70  
71      /**
72       * destDir attribute
73       */
74      public void setDestDir(File destDir)
75      {
76          this.destDir = destDir;
77      }
78  
79      /** 
80       * Set the Jelly context.
81       * 
82       * @param jellyContext the Jelly context
83       */
84      public void setJellyContext(JellyContext jellyContext)
85      {
86          this.jellyContext = jellyContext;
87      }
88  
89      /**
90       * destDir attribute
91       */
92      public void setOutputFile(String outputFile)
93      {
94          this.outputFile = outputFile;
95      }
96  
97      /**
98       * template attribute
99       */
100     public void setTemplate(String template)
101     {
102         this.template = template;
103     }
104     
105     /**
106      * encoding attribute
107      */
108     public void setEncoding(String encoding)
109     {
110         this.encoding = encoding;
111     }
112 
113     /**
114      * Description of the Method
115      */
116     public void execute()
117         throws Exception
118     {
119         parseSources();
120         generate();
121     }
122 
123     /**
124      * Description of the Method
125      */
126     void log(String msg)
127     {
128         if (verbose)
129         {
130             System.err.println(msg);
131         }
132     }
133 
134     /**
135      * Parse the source-files
136      */
137     void parseSources()
138         throws IOException
139     {
140         com.thoughtworks.qdox.JavaDocBuilder builder =
141             new com.thoughtworks.qdox.JavaDocBuilder();
142         
143         builder.addSourceTree(srcDir);
144         
145         JavaSource[] javaSources = builder.getSources();
146         log(javaSources.length + " source-files");
147         docInfo = QDoxBuilder.build(javaSources);
148         docInfo.setSourcePath(srcDir.getAbsolutePath());
149     }
150 
151     /**
152      * Kick-start generation
153      */
154     void generate()
155         throws Exception
156     {
157         if (!destDir.exists())
158         {
159             destDir.mkdirs();
160         }
161         
162         Generator generator = new Generator(destDir);
163         generator.setAttribute("docInfo", docInfo);
164 
165         if (jellyContext != null)
166         {
167             generator.setAttribute("jellyContext", getJellyContextAdapter());
168         }
169 
170         if (encoding != null)
171         {
172             generator.setAttribute("encoding", encoding);
173         }
174         generator.eval(template, new FileWriter(new File(destDir, outputFile)));
175     }
176 
177     private Context getJellyContextAdapter()
178     {
179         JellyContextAdapter adapter = new JellyContextAdapter(jellyContext);
180 
181         adapter.setReadOnly(true);
182 
183         return adapter;
184     }
185 }
186