KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > dumper > ProjectDocumentationDumper


1 package net.sf.invicta.dumper;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.TreeMap JavaDoc;
9 import java.util.TreeSet JavaDoc;
10
11 import net.sf.invicta.InvictaConstants;
12 import net.sf.invicta.InvictaException;
13 import net.sf.invicta.api.InvictaComponent;
14 import net.sf.invicta.type.BasicDefinedProperty;
15
16 import org.apache.commons.lang.StringUtils;
17
18 /**
19  * Created a Javadoc-like documentation of a project. Generated
20  * multiple HTML pages.<BR>
21  * TODO: Get rid of the hard-coded HTML parts (add abstraction that wraps
22  * the actual HTML creation.
23  */

24 public class ProjectDocumentationDumper extends InvictaBasicDumper {
25     public final static String JavaDoc DOC_FILE_EXTENSION = ".html";
26     public final static String JavaDoc LIST_FILE_NAME = "list.html";
27     public final static String JavaDoc PROJECT_FILE_NAME = "project.html";
28     public final static String JavaDoc FRAMES_FILE_NAME = "index.html";
29             
30     public final static String JavaDoc NAME_PRODUCTS = "products";
31     public final static String JavaDoc NAME_TARGETS = "targets";
32     public final static String JavaDoc NAME_DEPENDS = "depends";
33     public final static String JavaDoc NAME_GENERAL_PROPERTIES = "general_properties";
34     public final static String JavaDoc NAME_PROJECT_PROPERTIES = "project_properties";
35     public final static String JavaDoc NAME_COMPONENT_PROPERTIES = "component_properties";
36     
37     /**
38      * Constructor for ProjectDocumentationDumper.
39      */

40     public ProjectDocumentationDumper() {
41         super();
42     }
43
44     /**
45      *
46      */

47     public String JavaDoc getName() {
48         return "projectDocumentation";
49     }
50
51     /**
52      * Dumps multiple documentation files to the output directory.
53      *
54      */

55     public void dump() throws InvictaException {
56     
57         String JavaDoc docDir = getFileName();
58         
59         // Prepares a 'menu' file with a list of component names and
60
// links.
61
String JavaDoc componentListContent = "<HTML><BODY>";
62         componentListContent +=
63                         "<a target=\"component\" HREF=\"" + PROJECT_FILE_NAME + "\">" +
64                         getProject().getName() + "</a><BR>\n";
65         
66         // Go over all components and dump a documentation file for each
67
// component
68
TreeSet JavaDoc treeSet = new TreeSet JavaDoc(project.getComponents());
69         for (Iterator JavaDoc iter = treeSet.iterator(); iter.hasNext();) {
70             InvictaComponent component = (InvictaComponent) iter.next();
71             String JavaDoc shortFileName = component.getName() + DOC_FILE_EXTENSION;
72             String JavaDoc fullFileName = docDir + InvictaConstants.FILE_SEPARATOR + shortFileName;
73                                         
74             writeContentToFile(fullFileName, getComponentContent(component));
75             componentListContent +=
76                 "<a target=\"component\" HREF=\"" + shortFileName + "\">" +
77                 component.getName() + "</a><BR>\n";
78         }
79         
80         componentListContent += "</BODY></HTML>";
81         
82         // Write the 'menu' (list), frame and project files.
83
writeContentToFile(docDir + InvictaConstants.FILE_SEPARATOR + LIST_FILE_NAME, componentListContent);
84         writeContentToFile(docDir + InvictaConstants.FILE_SEPARATOR + FRAMES_FILE_NAME, getFramesFile());
85         writeContentToFile(docDir + InvictaConstants.FILE_SEPARATOR + PROJECT_FILE_NAME, getProjectFile());
86          
87         log("Documentation files (in '" + docDir + "') were created successfully !");
88     }
89     
90     /**
91      * Prepares a documentation page of the project.
92      * @return String
93      * @throws InvictaDumperException
94      */

95     protected String JavaDoc getProjectFile() throws InvictaDumperException {
96         String JavaDoc title = getProject().getName() + " - Invicta Project Definition Documentation";
97         String JavaDoc file = "<HTML>";
98         file += "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n";
99         file += "<BODY>";
100         file += "<H2><font color=\"#505050\">Project:</font> " +
101                     "<font color=\"darkblue\"><I>" +
102                     getProject().getName() +
103                     "<I></font></H2>";
104                     
105         file += getTable(getProject().getProjectProperties().iterator(),
106                         new String JavaDoc[] { "Name", "Value", "Description" },
107                         "Project Properties", NAME_PROJECT_PROPERTIES);
108         file += getTable(getProject().getGeneralProperties().iterator(),
109                         new String JavaDoc[] { "Name", "Value", "Description"},
110                         "General Properties", NAME_GENERAL_PROPERTIES);
111         file += "</HTML>";
112                         
113         return file;
114     }
115     
116     /**
117      * Return the content of the frames file.
118      *
119      * @return String
120      */

121     protected String JavaDoc getFramesFile() {
122         String JavaDoc title = getProject().getName() + " - Invicta Project Definition Documentation";
123         String JavaDoc frames = "<HTML>";
124         frames += "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n";
125                         
126         frames += "<FRAMESET cols=\"20%,80%\">\n";
127         frames += "<FRAME SRC=\"" + LIST_FILE_NAME + "\" name=\"list\">";
128         frames += "<FRAME SRC=\"" + PROJECT_FILE_NAME + "\" name=\"component\">";
129         frames += "</FRAMESET>";
130         frames += "</HTML>";
131
132         return frames;
133     }
134     
135     /**
136      * Returns the full content of a component documentation.
137      *
138      * @param component
139      * @return String
140      * @throws InvictaDumperException
141      */

142     protected String JavaDoc getComponentContent(InvictaComponent component) throws InvictaDumperException {
143         String JavaDoc content = "";
144         
145         content += getHeader(component);
146         content += getComponentProducts(component);
147         content += getComponentTargets(component);
148         content += getComponentDepends(component);
149         content += getComponentProperties(component);
150         content += getFooter(component);
151         
152         return content;
153     }
154     
155     /**
156      * Returns a header of a component documentation page
157      *
158      * @param component
159      * @return String
160      */

161     protected String JavaDoc getHeader(InvictaComponent component) {
162         String JavaDoc header = "<HTML>";
163         header += "<header>";
164         header += "<title>" + component.getName() + "</title>";
165         header += "</header>";
166         header += "<BODY>";
167         header += "<TABLE cellpadding=7><TR>" +
168             "<TD><B><font size=\"-1\"><A HREF=\"#" + NAME_PRODUCTS + "\">Products</A></B></TD>" +
169             "<TD><B><font size=\"-1\"><A HREF=\"#" + NAME_TARGETS + "\">Targets</A></B></TD>" +
170             "<TD><B><font size=\"-1\"><A HREF=\"#" + NAME_DEPENDS + "\">Dependencies</A></B></TD>" +
171             "<TD><B><font size=\"-1\"><A HREF=\"#" + NAME_COMPONENT_PROPERTIES + "\">Component Properties</A></B></TD>" +
172             "<TD><B><font size=\"-1\"><A HREF=\"#" + NAME_PROJECT_PROPERTIES + "\">Project Properties</A></B></TD>" +
173             "<TD><B><font size=\"-1\"><A HREF=\"#" + NAME_GENERAL_PROPERTIES + "\">General Properties</A></B></TD>" +
174             "</TR></TABLE>";
175         header += "<HR>";
176         header += "<H2><font color=\"#505050\">Component:</font> " +
177             "<font color=\"darkblue\"><I>" +
178             component.getName() +
179             "<I></font></H2>";
180         header += "<H3>Type: <I><font color=\"darkblue\">" + component.getTypeName() + "<FONT></I></H3>";
181         header += "<HR>";
182                 
183         return header;
184     }
185     
186     /**
187      * Returns a footer of a component documentation page
188      * @param component
189      * @return String
190      */

191     protected String JavaDoc getFooter(InvictaComponent component) {
192         String JavaDoc footer = "</BODY></HTML>";
193         return footer;
194     }
195     
196     
197     /**
198      * Returns the properties tables of a component.
199      *
200      * @param component
201      * @return String
202      * @throws InvictaDumperException
203      */

204     protected String JavaDoc getComponentProperties(InvictaComponent component) throws InvictaDumperException {
205         
206         List JavaDoc componentProperties = new ArrayList JavaDoc();
207         List JavaDoc projectProperties = new ArrayList JavaDoc();
208         List JavaDoc generalProperties = new ArrayList JavaDoc();
209     
210         // Go over all defined properties and divide them into
211
// groups of component, project and general properties.
212
TreeMap JavaDoc treeMap = new TreeMap JavaDoc(component.getDefinedPropertiesMap());
213         for (Iterator JavaDoc iter = treeMap.values().iterator(); iter.hasNext();) {
214             BasicDefinedProperty property = (BasicDefinedProperty) iter.next();
215             if (property.isComponentType())
216                 componentProperties.add(property);
217             else if (property.isProjectType())
218                 projectProperties.add(property);
219             else if (property.isGeneralType())
220                 generalProperties.add(property);
221         }
222                 
223         String JavaDoc properties = getTable(componentProperties.iterator(),
224                         new String JavaDoc[] { "Name", "ActualValue", "Description" },
225                         "Component Properties", NAME_COMPONENT_PROPERTIES);
226         
227         properties += getTable(projectProperties.iterator(),
228                         new String JavaDoc[] { "Name", "ActualValue", "Description" },
229                         "Project Properties", NAME_PROJECT_PROPERTIES);
230
231         properties += getTable(generalProperties.iterator(),
232                         new String JavaDoc[] { "Name", "ActualValue", "Description" },
233                         "General Properties", NAME_GENERAL_PROPERTIES);
234             
235         return properties;
236     }
237     
238     
239     /**
240      * Returns a header of a documentation table.
241      *
242      * @return String
243      */

244     protected String JavaDoc getTableHeader() {
245         String JavaDoc header = "<TABLE border=\"1\" CELLPADDING=\"3\" CELLSPACING=\"0\">";
246         return header;
247     }
248     
249     /**
250      * Returns a special separation bold line of a table.
251      *
252      * @param text
253      * @param cols
254      * @param name
255      * @return String
256      */

257     protected String JavaDoc getTableBoldLine(String JavaDoc text, int cols, String JavaDoc name) {
258         String JavaDoc line = "<TR BGCOLOR=\"#CCCCFF\">" +
259             "<TD COLSPAN=" + cols + "><FONT SIZE=\"+2\"><B>" +
260             "<A name=\"" + name + "\">" +
261             text + "</A></B></FONT></TD>" + "</TR>";
262         return line;
263     }
264
265     /**
266      * Returns the table of targets of a component.
267      *
268      * @param buildComponent
269      * @return String
270      * @throws InvictaDumperException
271      */

272     protected String JavaDoc getComponentTargets(InvictaComponent component) throws InvictaDumperException {
273         
274         TreeMap JavaDoc treeMap = new TreeMap JavaDoc(component.getTargetMap());
275         return getTable(treeMap.values().iterator(),
276                         new String JavaDoc[] { "Name", "DependsList", "Description" },
277                         "Targets", NAME_TARGETS);
278     }
279     
280     /**
281      * Returns the table of dependencies of a component.
282      *
283      * @param component
284      * @return String
285      * @throws InvictaDumperException
286      */

287     protected String JavaDoc getComponentDepends(InvictaComponent component) throws InvictaDumperException {
288         return getTable(component.getDependItems().iterator(),
289                         new String JavaDoc[] { "Name", "ProductsList", "Export" },
290                         "Dependencies", NAME_DEPENDS);
291     }
292     
293     /**
294      * Returns the table of products of a component.
295      * @param component
296      * @return String
297      * @throws InvictaDumperException
298      */

299     protected String JavaDoc getComponentProducts(InvictaComponent component) throws InvictaDumperException {
300                                         
301         return getTable(component.getSelfProducts().iterator(),
302                         new String JavaDoc[] {"Name", "File", "Type"},
303                         "Products", NAME_PRODUCTS);
304     }
305     
306     /**
307      * Returns a generic documentation table for a list (iterator)
308      * of items (using reflection).
309      * @param itemsIter
310      * @param fields
311      * @param title
312      * @param bookmark
313      * @return String
314      * @throws InvictaDumperException
315      */

316     protected String JavaDoc getTable(Iterator JavaDoc itemsIter, String JavaDoc[] fields, String JavaDoc title, String JavaDoc bookmark) throws InvictaDumperException {
317
318         if (!itemsIter.hasNext())
319             return "";
320
321         try {
322     
323             String JavaDoc table = getTableHeader();
324             table += getTableBoldLine(title, fields.length, bookmark);
325             
326             // Prepare the headers of the table by going over the given fields.
327
for (int i = 0; i < fields.length; i++) {
328                 table += "<TH>" + fields[i] + "</TH>";
329             }
330             
331             // Go over all given items
332
while (itemsIter.hasNext()) {
333                 table += "<TR>";
334                 Object JavaDoc item = (Object JavaDoc) itemsIter.next();
335                 // Go over all fields on the item and create a table line.
336
for (int i = 0; i < fields.length; i++) {
337                     
338                     Method JavaDoc method = item.getClass().getMethod("get" + fields[i], null);
339                     Object JavaDoc result = method.invoke(item, null);
340                     String JavaDoc resultStr;
341                     if (result instanceof List JavaDoc)
342                         resultStr = getListString((List JavaDoc)result);
343                     else
344                         resultStr = getString(result);
345                     String JavaDoc linkStart = "";
346                     String JavaDoc linkEnd = "";
347                                         
348                     if (getProject().getComponent(resultStr) != null) {
349                         String JavaDoc shortFileName = resultStr + DOC_FILE_EXTENSION;
350                         linkStart = "<a target=\"component\" HREF=\"" + shortFileName + "\">";
351                         linkEnd = "</a>";
352                     }
353                     
354                     table += "<TD>" + linkStart;
355                     if (i == 0)
356                         table += "<B>" + resultStr;
357                     else
358                         table += resultStr;
359                     table += linkEnd + "</TD>";
360                 }
361                 table += "</TR>";
362             }
363             table += "</TABLE><BR><BR>";
364             return table;
365         } catch (InvocationTargetException JavaDoc e) {
366             throw new InvictaDumperException(this, "Error in building " + title + " table: " + e.getTargetException());
367         } catch (Exception JavaDoc e) {
368             throw new InvictaDumperException(this, "Error in building " + title + " table: " + e);
369         }
370
371     }
372
373     protected String JavaDoc getDumpContent() throws InvictaException {
374         return null;
375     }
376
377     /**
378      * Returns a string of the given object. Returns a single space
379      * if the given object is null or an empty string.
380      * @param object
381      * @return String
382      */

383     protected String JavaDoc getString(Object JavaDoc object) {
384         
385         if ((object == null) || (object.toString().length() == 0))
386             return "&nbsp;";
387         return object.toString();
388     }
389             
390     /**
391      * Returns a string with a comma-separated list of the given list
392      * of objects.
393      * @param list
394      * @return String
395      */

396     protected String JavaDoc getListString (List JavaDoc list) {
397         if (list.size() ==0 )
398             return "&nbsp;";
399             
400         return StringUtils.join(list.iterator(), ", ");
401     }
402
403 }
404
Popular Tags