1 package net.sf.invicta.dumper; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.lang.reflect.Method ; 5 import java.util.ArrayList ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.TreeMap ; 9 import java.util.TreeSet ; 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 24 public class ProjectDocumentationDumper extends InvictaBasicDumper { 25 public final static String DOC_FILE_EXTENSION = ".html"; 26 public final static String LIST_FILE_NAME = "list.html"; 27 public final static String PROJECT_FILE_NAME = "project.html"; 28 public final static String FRAMES_FILE_NAME = "index.html"; 29 30 public final static String NAME_PRODUCTS = "products"; 31 public final static String NAME_TARGETS = "targets"; 32 public final static String NAME_DEPENDS = "depends"; 33 public final static String NAME_GENERAL_PROPERTIES = "general_properties"; 34 public final static String NAME_PROJECT_PROPERTIES = "project_properties"; 35 public final static String NAME_COMPONENT_PROPERTIES = "component_properties"; 36 37 40 public ProjectDocumentationDumper() { 41 super(); 42 } 43 44 47 public String getName() { 48 return "projectDocumentation"; 49 } 50 51 55 public void dump() throws InvictaException { 56 57 String docDir = getFileName(); 58 59 String componentListContent = "<HTML><BODY>"; 62 componentListContent += 63 "<a target=\"component\" HREF=\"" + PROJECT_FILE_NAME + "\">" + 64 getProject().getName() + "</a><BR>\n"; 65 66 TreeSet treeSet = new TreeSet (project.getComponents()); 69 for (Iterator iter = treeSet.iterator(); iter.hasNext();) { 70 InvictaComponent component = (InvictaComponent) iter.next(); 71 String shortFileName = component.getName() + DOC_FILE_EXTENSION; 72 String 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 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 95 protected String getProjectFile() throws InvictaDumperException { 96 String title = getProject().getName() + " - Invicta Project Definition Documentation"; 97 String 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 [] { "Name", "Value", "Description" }, 107 "Project Properties", NAME_PROJECT_PROPERTIES); 108 file += getTable(getProject().getGeneralProperties().iterator(), 109 new String [] { "Name", "Value", "Description"}, 110 "General Properties", NAME_GENERAL_PROPERTIES); 111 file += "</HTML>"; 112 113 return file; 114 } 115 116 121 protected String getFramesFile() { 122 String title = getProject().getName() + " - Invicta Project Definition Documentation"; 123 String 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 142 protected String getComponentContent(InvictaComponent component) throws InvictaDumperException { 143 String 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 161 protected String getHeader(InvictaComponent component) { 162 String 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 191 protected String getFooter(InvictaComponent component) { 192 String footer = "</BODY></HTML>"; 193 return footer; 194 } 195 196 197 204 protected String getComponentProperties(InvictaComponent component) throws InvictaDumperException { 205 206 List componentProperties = new ArrayList (); 207 List projectProperties = new ArrayList (); 208 List generalProperties = new ArrayList (); 209 210 TreeMap treeMap = new TreeMap (component.getDefinedPropertiesMap()); 213 for (Iterator 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 properties = getTable(componentProperties.iterator(), 224 new String [] { "Name", "ActualValue", "Description" }, 225 "Component Properties", NAME_COMPONENT_PROPERTIES); 226 227 properties += getTable(projectProperties.iterator(), 228 new String [] { "Name", "ActualValue", "Description" }, 229 "Project Properties", NAME_PROJECT_PROPERTIES); 230 231 properties += getTable(generalProperties.iterator(), 232 new String [] { "Name", "ActualValue", "Description" }, 233 "General Properties", NAME_GENERAL_PROPERTIES); 234 235 return properties; 236 } 237 238 239 244 protected String getTableHeader() { 245 String header = "<TABLE border=\"1\" CELLPADDING=\"3\" CELLSPACING=\"0\">"; 246 return header; 247 } 248 249 257 protected String getTableBoldLine(String text, int cols, String name) { 258 String 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 272 protected String getComponentTargets(InvictaComponent component) throws InvictaDumperException { 273 274 TreeMap treeMap = new TreeMap (component.getTargetMap()); 275 return getTable(treeMap.values().iterator(), 276 new String [] { "Name", "DependsList", "Description" }, 277 "Targets", NAME_TARGETS); 278 } 279 280 287 protected String getComponentDepends(InvictaComponent component) throws InvictaDumperException { 288 return getTable(component.getDependItems().iterator(), 289 new String [] { "Name", "ProductsList", "Export" }, 290 "Dependencies", NAME_DEPENDS); 291 } 292 293 299 protected String getComponentProducts(InvictaComponent component) throws InvictaDumperException { 300 301 return getTable(component.getSelfProducts().iterator(), 302 new String [] {"Name", "File", "Type"}, 303 "Products", NAME_PRODUCTS); 304 } 305 306 316 protected String getTable(Iterator itemsIter, String [] fields, String title, String bookmark) throws InvictaDumperException { 317 318 if (!itemsIter.hasNext()) 319 return ""; 320 321 try { 322 323 String table = getTableHeader(); 324 table += getTableBoldLine(title, fields.length, bookmark); 325 326 for (int i = 0; i < fields.length; i++) { 328 table += "<TH>" + fields[i] + "</TH>"; 329 } 330 331 while (itemsIter.hasNext()) { 333 table += "<TR>"; 334 Object item = (Object ) itemsIter.next(); 335 for (int i = 0; i < fields.length; i++) { 337 338 Method method = item.getClass().getMethod("get" + fields[i], null); 339 Object result = method.invoke(item, null); 340 String resultStr; 341 if (result instanceof List ) 342 resultStr = getListString((List )result); 343 else 344 resultStr = getString(result); 345 String linkStart = ""; 346 String linkEnd = ""; 347 348 if (getProject().getComponent(resultStr) != null) { 349 String 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 e) { 366 throw new InvictaDumperException(this, "Error in building " + title + " table: " + e.getTargetException()); 367 } catch (Exception e) { 368 throw new InvictaDumperException(this, "Error in building " + title + " table: " + e); 369 } 370 371 } 372 373 protected String getDumpContent() throws InvictaException { 374 return null; 375 } 376 377 383 protected String getString(Object object) { 384 385 if ((object == null) || (object.toString().length() == 0)) 386 return " "; 387 return object.toString(); 388 } 389 390 396 protected String getListString (List list) { 397 if (list.size() ==0 ) 398 return " "; 399 400 return StringUtils.join(list.iterator(), ", "); 401 } 402 403 } 404 | Popular Tags |