001package org.asciidoctor.asciidoclet; 002 003import com.google.common.io.Resources; 004import com.sun.javadoc.DocErrorReporter; 005 006import java.io.File; 007import java.io.FileOutputStream; 008import java.io.IOException; 009 010/** 011 * Responsible for copying the appropriate stylesheet to the javadoc 012 * output directory. 013 */ 014public class Stylesheets { 015 static String JAVA8_STYLESHEET = "stylesheet8.css"; 016 static String JAVA6_STYLESHEET = "stylesheet6.css"; 017 static String CODERAY_STYLESHEET = "coderay-asciidoctor.css"; 018 static String OUTPUT_STYLESHEET = "stylesheet.css"; 019 020 private final DocletOptions docletOptions; 021 private final DocErrorReporter errorReporter; 022 023 public Stylesheets(DocletOptions options, DocErrorReporter errorReporter) { 024 this.docletOptions = options; 025 this.errorReporter = errorReporter; 026 } 027 028 public boolean copy() { 029 if (!docletOptions.destDir().isPresent()) { 030 // standard doclet must have checked this by the time we are called 031 errorReporter.printError("Destination directory not specified, cannot copy stylesheet"); 032 return false; 033 } 034 String stylesheet = selectStylesheet(System.getProperty("java.version")); 035 File destDir = docletOptions.destDir().get(); 036 File destFile = new File(docletOptions.destDir().get(), OUTPUT_STYLESHEET); 037 try { 038 Resources.copy(Resources.getResource(stylesheet), new FileOutputStream(new File(destDir, OUTPUT_STYLESHEET))); 039 Resources.copy(Resources.getResource(CODERAY_STYLESHEET), new FileOutputStream(new File(destDir, CODERAY_STYLESHEET))); 040 return true; 041 } catch (IOException e) { 042 errorReporter.printError(e.getLocalizedMessage()); 043 return false; 044 } 045 } 046 047 String selectStylesheet(String javaVersion) { 048 if (javaVersion.matches("^1\\.[56]\\D.*")) return JAVA6_STYLESHEET; 049 if (javaVersion.matches("^1\\.[78]\\D.*")) return JAVA8_STYLESHEET; 050 errorReporter.printWarning("Unrecognized Java version " + javaVersion + ", using Java 7/8 stylesheet"); 051 // TODO: review this when Java 9 becomes available! 052 return JAVA8_STYLESHEET; 053 } 054}