1 package org.apache.maven.tools.plugin.generator; 2 3 18 19 import org.apache.maven.plugin.descriptor.MojoDescriptor; 20 import org.apache.maven.plugin.descriptor.Parameter; 21 import org.apache.maven.plugin.descriptor.PluginDescriptor; 22 import org.codehaus.plexus.util.IOUtil; 23 import org.codehaus.plexus.util.StringUtils; 24 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; 25 import org.codehaus.plexus.util.xml.XMLWriter; 26 27 import java.io.File ; 28 import java.io.FileWriter ; 29 import java.io.IOException ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 36 public class PluginXdocGenerator 37 implements Generator 38 { 39 public void execute( File destinationDirectory, PluginDescriptor pluginDescriptor ) 40 throws IOException 41 { 42 writeOverview( destinationDirectory, pluginDescriptor ); 43 44 for ( Iterator it = pluginDescriptor.getMojos().iterator(); it.hasNext(); ) 45 { 46 MojoDescriptor descriptor = (MojoDescriptor) it.next(); 47 processMojoDescriptor( descriptor, destinationDirectory ); 48 } 49 } 50 51 protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, File destinationDirectory ) 52 throws IOException 53 { 54 FileWriter writer = null; 55 try 56 { 57 writer = new FileWriter ( new File ( destinationDirectory, getMojoFilename( mojoDescriptor, "xml" ) ) ); 58 59 writeBody( writer, mojoDescriptor ); 60 61 writer.flush(); 62 } 63 finally 64 { 65 IOUtil.close( writer ); 66 } 67 } 68 69 private String getMojoFilename( MojoDescriptor mojo, String ext ) 70 { 71 return mojo.getGoal() + "-mojo." + ext; 72 } 73 74 private void writeOverview( File destinationDirectory, PluginDescriptor pluginDescriptor ) 75 throws IOException 76 { 77 FileWriter writer = null; 78 try 79 { 80 writer = new FileWriter ( new File ( destinationDirectory, "index.xml" ) ); 81 82 writeOverview( writer, pluginDescriptor ); 83 84 writer.flush(); 85 } 86 finally 87 { 88 IOUtil.close( writer ); 89 } 90 } 91 92 private void writeOverview( FileWriter writer, PluginDescriptor pluginDescriptor ) 93 { 94 XMLWriter w = new PrettyPrintXMLWriter( writer ); 95 96 w.startElement( "document" ); 97 98 102 w.startElement( "properties" ); 103 104 w.startElement( "title" ); 105 106 w.writeText( pluginDescriptor.getArtifactId() + " - Overview" ); 108 109 w.endElement(); 110 111 w.endElement(); 112 113 117 w.startElement( "body" ); 118 119 w.startElement( "section" ); 120 121 w.addAttribute( "name", pluginDescriptor.getArtifactId() ); 123 124 126 w.startElement( "p" ); 127 128 w.writeText( "Goals available: " ); 129 130 w.endElement(); 131 132 writeGoalTable( pluginDescriptor, w ); 133 134 w.endElement(); 135 136 w.endElement(); 137 } 138 139 private void writeGoalTable( PluginDescriptor pluginDescriptor, XMLWriter w ) 140 { 141 w.startElement( "table" ); 142 143 w.startElement( "tr" ); 144 145 w.startElement( "th" ); 146 147 w.writeText( "Goal" ); 148 149 w.endElement(); 150 151 w.startElement( "th" ); 152 153 w.writeText( "Description" ); 154 155 w.endElement(); 156 157 w.endElement(); 158 159 List mojos = pluginDescriptor.getMojos(); 160 161 if ( mojos != null ) 162 { 163 for ( Iterator i = mojos.iterator(); i.hasNext(); ) 164 { 165 MojoDescriptor mojo = (MojoDescriptor) i.next(); 166 167 w.startElement( "tr" ); 168 169 173 w.startElement( "td" ); 174 175 String paramName = mojo.getFullGoalName(); 176 177 w.startElement( "a" ); 178 179 w.addAttribute( "href", getMojoFilename( mojo, "html" ) ); 180 181 w.startElement( "code" ); 182 183 w.writeText( paramName ); 184 185 w.endElement(); 186 187 w.endElement(); 188 189 w.endElement(); 190 191 195 w.startElement( "td" ); 196 197 if ( StringUtils.isNotEmpty( mojo.getDescription() ) ) 198 { 199 w.writeMarkup( mojo.getDescription() ); 200 } 201 else 202 { 203 w.writeText( "No description." ); 204 } 205 206 String deprecationWarning = mojo.getDeprecated(); 207 if ( deprecationWarning != null ) 208 { 209 w.writeMarkup( "<br/><b>Deprecated:</b> " ); 210 w.writeMarkup( deprecationWarning ); 211 if ( deprecationWarning.length() == 0 ) 212 { 213 w.writeText( "No reason given." ); 214 } 215 216 w.endElement(); 217 } 218 219 w.endElement(); 220 221 w.endElement(); 222 } 223 } 224 225 w.endElement(); 226 227 w.endElement(); 228 } 229 230 private void writeBody( FileWriter writer, MojoDescriptor mojoDescriptor ) 231 { 232 XMLWriter w = new PrettyPrintXMLWriter( writer ); 233 234 w.startElement( "document" ); 235 236 240 w.startElement( "properties" ); 241 242 w.startElement( "title" ); 243 244 w.writeText( mojoDescriptor.getPluginDescriptor().getArtifactId() + " - " + mojoDescriptor.getFullGoalName() ); 246 247 w.endElement(); 249 w.endElement(); 251 255 w.startElement( "body" ); 256 257 w.startElement( "section" ); 258 259 w.addAttribute( "name", mojoDescriptor.getFullGoalName() ); 260 261 w.startElement( "p" ); 262 263 if ( mojoDescriptor.getDescription() != null ) 264 { 265 w.writeMarkup( mojoDescriptor.getDescription() ); 266 } 267 else 268 { 269 w.writeText( "No description." ); 270 } 271 272 w.endElement(); 274 w.startElement( "p" ); 275 276 w.writeText( "Parameters for the goal: " ); 277 278 w.endElement(); 280 writeGoalParameterTable( mojoDescriptor, w ); 281 282 w.endElement(); 284 w.endElement(); 286 w.endElement(); } 288 289 private void writeGoalParameterTable( MojoDescriptor mojoDescriptor, XMLWriter w ) 290 { 291 w.startElement( "table" ); 292 293 w.startElement( "tr" ); 294 295 w.startElement( "th" ); 296 297 w.writeText( "Parameter" ); 298 299 w.endElement(); 301 w.startElement( "th" ); 302 303 w.writeText( "Type" ); 304 305 w.endElement(); 307 w.startElement( "th" ); 308 309 w.writeText( "Expression" ); 310 311 w.endElement(); 313 w.startElement( "th" ); 314 315 w.writeText( "Default Value" ); 316 317 w.endElement(); 319 w.startElement( "th" ); 320 321 w.writeText( "Description" ); 322 323 w.endElement(); 325 w.endElement(); 327 List parameters = mojoDescriptor.getParameters(); 328 329 if ( parameters != null ) 330 { 331 for ( int i = 0; i < parameters.size(); i++ ) 332 { 333 Parameter parameter = (Parameter) parameters.get( i ); 334 335 w.startElement( "tr" ); 336 337 341 w.startElement( "td" ); 342 343 String paramName = parameter.getAlias(); 344 345 if ( StringUtils.isEmpty( paramName ) ) 346 { 347 paramName = parameter.getName(); 348 } 349 350 w.startElement( "code" ); 351 352 w.writeText( paramName ); 353 354 w.endElement(); 356 if ( !parameter.isRequired() ) 357 { 358 w.writeMarkup( " <i>(Optional)</i>" ); 359 } 360 361 if ( parameter.getExpression() != null && parameter.getExpression().startsWith( "${component." ) ) 362 { 363 w.writeMarkup( " <i>(Discovered)</i>" ); 364 } 365 else if ( parameter.getRequirement() != null ) 366 { 367 w.writeMarkup( " <i>(Discovered)</i>" ); 368 } 369 370 w.endElement(); 372 376 w.startElement( "td" ); 377 378 w.startElement( "code" ); 379 380 w.addAttribute( "title", parameter.getType() ); 381 382 int index = parameter.getType().lastIndexOf( "." ); 383 if ( index >= 0 ) 384 { 385 w.writeText( parameter.getType().substring( index + 1 ) ); 386 } 387 else 388 { 389 w.writeText( parameter.getType() ); 390 } 391 392 w.endElement(); 394 w.endElement(); 396 400 w.startElement( "td" ); 401 402 w.startElement( "code" ); 403 404 if ( StringUtils.isNotEmpty( parameter.getExpression() ) && 405 !parameter.getExpression().startsWith( "${component." ) ) 406 { 407 w.writeText( parameter.getExpression() ); 408 } 409 else 410 { 411 w.writeText( "-" ); 412 } 413 414 w.endElement(); 416 w.endElement(); 418 422 w.startElement( "td" ); 423 424 w.startElement( "code" ); 425 426 if ( StringUtils.isNotEmpty( parameter.getDefaultValue() ) ) 427 { 428 w.writeText( parameter.getDefaultValue() ); 429 } 430 else 431 { 432 w.writeText( "-" ); 433 } 434 435 w.endElement(); 437 w.endElement(); 439 443 w.startElement( "td" ); 444 445 if ( StringUtils.isNotEmpty( parameter.getDescription() ) ) 446 { 447 w.writeMarkup( parameter.getDescription() ); 448 } 449 else 450 { 451 w.writeText( "No description." ); 452 } 453 454 String deprecationWarning = parameter.getDeprecated(); 455 if ( deprecationWarning != null ) 456 { 457 w.writeMarkup( "<br/><b>Deprecated:</b> " ); 458 w.writeMarkup( deprecationWarning ); 459 if ( deprecationWarning.length() == 0 ) 460 { 461 w.writeText( "No reason given." ); 462 } 463 } 464 465 w.endElement(); 467 w.endElement(); } 469 } 470 471 w.endElement(); } 473 } | Popular Tags |