| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.taskdefs; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileInputStream; |
| 37 | | | import java.io.IOException; |
| 38 | | | import java.util.ArrayList; |
| 39 | | | import java.util.Collections; |
| 40 | | | import java.util.Iterator; |
| 41 | | | import java.util.List; |
| 42 | | | |
| 43 | | | import javax.xml.parsers.SAXParser; |
| 44 | | | import javax.xml.parsers.SAXParserFactory; |
| 45 | | | |
| 46 | | | import org.apache.tools.ant.BuildException; |
| 47 | | | import org.apache.tools.ant.Project; |
| 48 | | | import org.apache.tools.ant.Task; |
| 49 | | | import org.apache.tools.ant.taskdefs.Javadoc; |
| 50 | | | import org.apache.tools.ant.taskdefs.Javadoc.DocletInfo; |
| 51 | | | import org.apache.tools.ant.types.FileSet; |
| 52 | | | import org.apache.tools.ant.types.Path; |
| 53 | | | import org.apache.tools.ant.types.PatternSet.NameEntry; |
| 54 | | | import org.jcoderz.commons.util.IoUtil; |
| 55 | | | import org.xml.sax.Attributes; |
| 56 | | | import org.xml.sax.InputSource; |
| 57 | | | import org.xml.sax.helpers.DefaultHandler; |
| 58 | | | |
| 59 | | | |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | @author |
| 64 | | | |
| 65 | 0 | | public class ApiDocTask |
| 66 | | | extends Task |
| 67 | | | { |
| 68 | | | |
| 69 | | | public static final String NAME = "apidoc"; |
| 70 | | | |
| 71 | | | |
| 72 | | | private static final String JAVA_EXTENSION = ".java"; |
| 73 | | | |
| 74 | | | |
| 75 | | | private File mOutDir; |
| 76 | | | |
| 77 | | | |
| 78 | | | private File mInFile; |
| 79 | | | |
| 80 | | | |
| 81 | | | private boolean mFailOnError; |
| 82 | | | |
| 83 | | | |
| 84 | | | private Path mDocletPath; |
| 85 | | | |
| 86 | | | |
| 87 | 0 | | private final List mSources = new ArrayList(); |
| 88 | | | |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | | | @param |
| 93 | | | |
| 94 | | | public void setIn (File f) |
| 95 | | | { |
| 96 | 0 | | mInFile = f; |
| 97 | 0 | | } |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | |
| 102 | | | |
| 103 | | | @param |
| 104 | | | |
| 105 | | | public void setOut (File dir) |
| 106 | | | { |
| 107 | 0 | | mOutDir = dir; |
| 108 | 0 | | } |
| 109 | | | |
| 110 | | | |
| 111 | | | |
| 112 | | | |
| 113 | | | @param |
| 114 | | | |
| 115 | | | public void setFailonerror (boolean b) |
| 116 | | | { |
| 117 | 0 | | mFailOnError = b; |
| 118 | 0 | | } |
| 119 | | | |
| 120 | | | |
| 121 | | | |
| 122 | | | |
| 123 | | | @param |
| 124 | | | |
| 125 | | | |
| 126 | | | public void addSrc (SourceDirectory src) |
| 127 | | | { |
| 128 | 0 | | mSources.add(src); |
| 129 | 0 | | } |
| 130 | | | |
| 131 | | | |
| 132 | | | |
| 133 | | | |
| 134 | | | @param |
| 135 | | | |
| 136 | | | |
| 137 | | | public void setDocletPath (Path path) |
| 138 | | | { |
| 139 | 0 | | if (mDocletPath == null) |
| 140 | | | { |
| 141 | 0 | | mDocletPath = path; |
| 142 | | | } |
| 143 | | | else |
| 144 | | | { |
| 145 | 0 | | mDocletPath.add(path); |
| 146 | | | } |
| 147 | 0 | | } |
| 148 | | | |
| 149 | | | |
| 150 | | | |
| 151 | | | |
| 152 | | | @throws |
| 153 | | | |
| 154 | | | public void execute () |
| 155 | | | throws BuildException |
| 156 | | | { |
| 157 | | | try |
| 158 | | | { |
| 159 | 0 | | checkAttributes(); |
| 160 | 0 | | final ApiDocSaxHandler handler = parse(); |
| 161 | 0 | | log("APIs: " + handler.apiDocs().toString(), Project.MSG_DEBUG); |
| 162 | 0 | | final Iterator iterator = handler.apiDocs().iterator(); |
| 163 | 0 | | while (iterator.hasNext()) |
| 164 | | | { |
| 165 | 0 | | final ApiDocType apiDoc = (ApiDocType) iterator.next(); |
| 166 | 0 | | final File xmlFile = runXmlDoclet(apiDoc); |
| 167 | 0 | | log("Generated xmlFile " + xmlFile); |
| 168 | 0 | | } |
| 169 | | | } |
| 170 | 0 | | catch (BuildException e) |
| 171 | | | { |
| 172 | 0 | | if (mFailOnError) |
| 173 | | | { |
| 174 | 0 | | throw e; |
| 175 | | | } |
| 176 | 0 | | log(e.getMessage(), Project.MSG_ERR); |
| 177 | 0 | | } |
| 178 | 0 | | } |
| 179 | | | |
| 180 | | | private File runXmlDoclet (final ApiDocType apiDoc) |
| 181 | | | { |
| 182 | 0 | | final Javadoc javadocTask = new Javadoc(); |
| 183 | 0 | | javadocTask.setProject(getProject()); |
| 184 | 0 | | javadocTask.setFailonerror(mFailOnError); |
| 185 | 0 | | javadocTask.setTaskName("xml-doclet"); |
| 186 | 0 | | javadocTask.setPackage(true); |
| 187 | 0 | | javadocTask.setClasspath(mDocletPath); |
| 188 | 0 | | javadocTask.setClasspath(Path.systemClasspath); |
| 189 | 0 | | javadocTask.setDestdir(mOutDir); |
| 190 | 0 | | javadocTask.setAdditionalparam("-quiet"); |
| 191 | 0 | | for (final Iterator i = mSources.iterator(); i.hasNext();) |
| 192 | | | { |
| 193 | 0 | | final SourceDirectory fs = (SourceDirectory) i.next(); |
| 194 | 0 | | javadocTask.addFileset(addClasses(apiDoc, fs.getDir())); |
| 195 | 0 | | } |
| 196 | 0 | | final DocletInfo info = javadocTask.createDoclet(); |
| 197 | 0 | | info.setProject(getProject()); |
| 198 | 0 | | info.setName("org.jcoderz.commons.doclet.XmlDoclet"); |
| 199 | 0 | | info.setPath(mDocletPath); |
| 200 | 0 | | final File tmpFile = new File(mOutDir, "/javadoc.xml"); |
| 201 | 0 | | javadocTask.execute(); |
| 202 | 0 | | final File outFile = new File(mOutDir, apiDoc.getName() + ".xml"); |
| 203 | 0 | | if (!tmpFile.renameTo(outFile)) |
| 204 | | | { |
| 205 | | | try |
| 206 | | | { |
| 207 | | | |
| 208 | 0 | | IoUtil.copy(tmpFile, outFile); |
| 209 | 0 | | if (!tmpFile.delete()) |
| 210 | | | { |
| 211 | 0 | | throw new BuildException("Cannot delete file " + tmpFile); |
| 212 | | | } |
| 213 | | | } |
| 214 | 0 | | catch (IOException e) |
| 215 | | | { |
| 216 | 0 | | throw new BuildException("Cannot move file " + tmpFile |
| 217 | | | + " to " + outFile); |
| 218 | 0 | | } |
| 219 | | | } |
| 220 | | | try |
| 221 | | | { |
| 222 | 0 | | IoUtil.copy(outFile, new File(outFile.getParent(), outFile |
| 223 | | | .getName() |
| 224 | | | + ".in")); |
| 225 | | | } |
| 226 | 0 | | catch (IOException e) |
| 227 | | | { |
| 228 | 0 | | throw new BuildException("Failed to copy file: " + outFile, e); |
| 229 | 0 | | } |
| 230 | 0 | | return outFile; |
| 231 | | | } |
| 232 | | | |
| 233 | | | private FileSet addClasses (final ApiDocType diagram, File path) |
| 234 | | | { |
| 235 | 0 | | final FileSet filez = new FileSet(); |
| 236 | 0 | | filez.setDir(path); |
| 237 | 0 | | filez.setProject(getProject()); |
| 238 | 0 | | final Iterator i = diagram.classList().iterator(); |
| 239 | 0 | | while (i.hasNext()) |
| 240 | | | { |
| 241 | 0 | | final String name = (String) i.next(); |
| 242 | 0 | | final NameEntry entry = filez.createInclude(); |
| 243 | 0 | | final String pathName = name.replaceAll("\\.", "/") |
| 244 | | | + JAVA_EXTENSION; |
| 245 | 0 | | log("Adding Source file " + pathName, Project.MSG_VERBOSE); |
| 246 | 0 | | entry.setName(pathName); |
| 247 | 0 | | } |
| 248 | 0 | | log("Source files: " + filez, Project.MSG_VERBOSE); |
| 249 | 0 | | return filez; |
| 250 | | | } |
| 251 | | | |
| 252 | | | private ApiDocSaxHandler parse () |
| 253 | | | { |
| 254 | 0 | (1) | final ApiDocSaxHandler handler = new ApiDocSaxHandler(); |
| 255 | | | try |
| 256 | | | { |
| 257 | | | |
| 258 | 0 | | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 259 | 0 | | factory.setNamespaceAware(true); |
| 260 | 0 | | factory.setValidating(true); |
| 261 | 0 | | final SAXParser parser = factory.newSAXParser(); |
| 262 | | | |
| 263 | | | |
| 264 | | | |
| 265 | | | |
| 266 | | | |
| 267 | 0 | | parser |
| 268 | | | .parse(new InputSource(new FileInputStream(mInFile)), |
| 269 | | | handler); |
| 270 | 0 | | log(mInFile + " parsed successfully.", Project.MSG_INFO); |
| 271 | | | } |
| 272 | 0 | | catch (Exception e) |
| 273 | | | { |
| 274 | 0 | | throw new BuildException( |
| 275 | | | "Failed to parse " + mInFile + ": " + e, e); |
| 276 | 0 | | } |
| 277 | 0 | | return handler; |
| 278 | | | } |
| 279 | | | |
| 280 | | | |
| 281 | | | |
| 282 | | | |
| 283 | | | @throws |
| 284 | | | |
| 285 | | | private void checkAttributes () |
| 286 | | | throws BuildException |
| 287 | | | { |
| 288 | 0 | | checkAttributeInFile(); |
| 289 | 0 | | } |
| 290 | | | |
| 291 | | | private void checkAttributeInFile () |
| 292 | | | { |
| 293 | 0 | | if (mInFile == null) |
| 294 | | | { |
| 295 | 0 | | throw new BuildException("Missing mandatory attribute 'in'.", |
| 296 | | | getLocation()); |
| 297 | | | } |
| 298 | 0 | | if (!mInFile.exists()) |
| 299 | | | { |
| 300 | 0 | | throw new BuildException("Input file '" + mInFile + "' not found.", |
| 301 | | | getLocation()); |
| 302 | | | } |
| 303 | 0 | | } |
| 304 | | | |
| 305 | 0 | | private static class ApiDocSaxHandler |
| 306 | | | extends DefaultHandler |
| 307 | | | { |
| 308 | 0 | | private final StringBuffer mBuffer = new StringBuffer(); |
| 309 | | | |
| 310 | 0 | | private boolean mCaptureCharacters = false; |
| 311 | | | |
| 312 | 0 | | private final List mApiDocElementList = new ArrayList(); |
| 313 | | | |
| 314 | 0 | | private ApiDocType mCurrentApiDocElement = null; |
| 315 | | | |
| 316 | | | {@inheritDoc} |
| 317 | | | public void startElement (String uri, String localName, String qName, |
| 318 | | | Attributes attributes) |
| 319 | | | { |
| 320 | 0 | | if ("apidoc".equals(localName)) |
| 321 | | | { |
| 322 | 0 | | mCurrentApiDocElement = new ApiDocType(attributes |
| 323 | | | .getValue("name")); |
| 324 | 0 | | mApiDocElementList.add(mCurrentApiDocElement); |
| 325 | | | } |
| 326 | 0 | | else if ("class".equals(localName) && mCurrentApiDocElement != null) |
| 327 | | | { |
| 328 | 0 | | mCurrentApiDocElement.add(attributes.getValue("name")); |
| 329 | | | } |
| 330 | 0 | | else if ("description".equals(localName) |
| 331 | | | && mCurrentApiDocElement != null) |
| 332 | | | { |
| 333 | 0 | | captureCharacters(); |
| 334 | | | } |
| 335 | 0 | | } |
| 336 | | | |
| 337 | | | {@inheritDoc} |
| 338 | | | public void endElement (String uri, String localName, String qName) |
| 339 | | | { |
| 340 | 0 | | if ("apidoc".equals(localName)) |
| 341 | | | { |
| 342 | 0 | | mCurrentApiDocElement = null; |
| 343 | | | } |
| 344 | 0 | | else if ("description".equals(localName) |
| 345 | | | && mCurrentApiDocElement != null) |
| 346 | | | { |
| 347 | 0 | | mCurrentApiDocElement.setDescription(characters().trim()); |
| 348 | | | } |
| 349 | 0 | | } |
| 350 | | | |
| 351 | | | {@inheritDoc} |
| 352 | | | public void characters (char[] ch, int start, int length) |
| 353 | | | { |
| 354 | 0 | | if (mCaptureCharacters) |
| 355 | | | { |
| 356 | 0 | | mBuffer.append(ch, start, length); |
| 357 | | | } |
| 358 | 0 | | } |
| 359 | | | |
| 360 | | | void captureCharacters () |
| 361 | | | { |
| 362 | 0 | | mCaptureCharacters = true; |
| 363 | 0 | | } |
| 364 | | | |
| 365 | | | |
| 366 | | | <b></b> |
| 367 | | | |
| 368 | | | |
| 369 | | | @return |
| 370 | | | |
| 371 | | | String characters () |
| 372 | | | { |
| 373 | 0 | | final String result = mBuffer.toString(); |
| 374 | 0 | | mBuffer.setLength(0); |
| 375 | 0 | | mCaptureCharacters = false; |
| 376 | 0 | | return result; |
| 377 | | | } |
| 378 | | | |
| 379 | | | |
| 380 | | | {@link } |
| 381 | | | |
| 382 | | | @return{@link } |
| 383 | | | |
| 384 | | | public List apiDocs () |
| 385 | | | { |
| 386 | 0 | | return Collections.unmodifiableList(mApiDocElementList); |
| 387 | | | } |
| 388 | | | } |
| 389 | | | |
| 390 | 0 | | private static class ApiDocType |
| 391 | | | { |
| 392 | | | private final String mName; |
| 393 | | | |
| 394 | 0 | | private final List mClasses = new ArrayList(); |
| 395 | | | |
| 396 | 0 | | private String mDescription = ""; |
| 397 | | | |
| 398 | | | ApiDocType (String name) |
| 399 | 0 | | { |
| 400 | 0 | | mName = name; |
| 401 | 0 | | } |
| 402 | | | |
| 403 | | | void add (String clazz) |
| 404 | | | { |
| 405 | 0 | | mClasses.add(clazz); |
| 406 | 0 | | } |
| 407 | | | |
| 408 | | | List classList () |
| 409 | | | { |
| 410 | 0 | | return Collections.unmodifiableList(mClasses); |
| 411 | | | } |
| 412 | | | |
| 413 | | | String getName () |
| 414 | | | { |
| 415 | 0 | | return mName; |
| 416 | | | } |
| 417 | | | |
| 418 | | | {@inheritDoc} |
| 419 | | | public String toString () |
| 420 | | | { |
| 421 | 0 | (2) | final StringBuffer sb = new StringBuffer(); |
| 422 | 0 | | sb.append("clazzes "); |
| 423 | 0 | | sb.append(mName); |
| 424 | 0 | | sb.append(" = "); |
| 425 | 0 | | sb.append(mClasses); |
| 426 | 0 | | sb.append(" description: '"); |
| 427 | 0 | | sb.append(mDescription); |
| 428 | 0 | | sb.append('\''); |
| 429 | 0 | | return sb.toString(); |
| 430 | | | } |
| 431 | | | |
| 432 | | | String getDescription () |
| 433 | | | { |
| 434 | 0 | | return mDescription; |
| 435 | | | } |
| 436 | | | |
| 437 | | | void setDescription (String description) |
| 438 | | | { |
| 439 | 0 | | mDescription = description; |
| 440 | 0 | | } |
| 441 | | | } |
| 442 | | | } |