001/** 002 * Copyright 2013-2015 John Ericksen 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.asciidoctor.asciidoclet; 017 018import com.google.common.io.Resources; 019import com.sun.javadoc.DocErrorReporter; 020 021import java.io.File; 022import java.io.FileOutputStream; 023import java.io.IOException; 024 025/** 026 * Responsible for copying the appropriate stylesheet to the javadoc 027 * output directory. 028 */ 029public class Stylesheets { 030 static final String JAVA9_STYLESHEET = "stylesheet9.css"; 031 static final String JAVA8_STYLESHEET = "stylesheet8.css"; 032 static final String JAVA6_STYLESHEET = "stylesheet6.css"; 033 static final String CODERAY_STYLESHEET = "coderay-asciidoctor.css"; 034 static final String OUTPUT_STYLESHEET = "stylesheet.css"; 035 036 private final DocletOptions docletOptions; 037 private final DocErrorReporter errorReporter; 038 039 public Stylesheets(DocletOptions options, DocErrorReporter errorReporter) { 040 this.docletOptions = options; 041 this.errorReporter = errorReporter; 042 } 043 044 public boolean copy() { 045 if (!docletOptions.destDir().isPresent()) { 046 // standard doclet must have checked this by the time we are called 047 errorReporter.printError("Destination directory not specified, cannot copy stylesheet"); 048 return false; 049 } 050 String stylesheet = selectStylesheet(System.getProperty("java.version")); 051 File destDir = docletOptions.destDir().get(); 052 try { 053 Resources.copy(Resources.getResource(stylesheet), new FileOutputStream(new File(destDir, OUTPUT_STYLESHEET))); 054 Resources.copy(Resources.getResource(CODERAY_STYLESHEET), new FileOutputStream(new File(destDir, CODERAY_STYLESHEET))); 055 return true; 056 } catch (IOException e) { 057 errorReporter.printError(e.getLocalizedMessage()); 058 return false; 059 } 060 } 061 062 String selectStylesheet(String javaVersion) { 063 if (javaVersion.matches("^1\\.[56]\\D.*")) { 064 return JAVA6_STYLESHEET; 065 } 066 if (javaVersion.matches("^1\\.[78]\\D.*")) { 067 return JAVA8_STYLESHEET; 068 } 069 if (javaVersion.matches("^(9|10)(\\.)?.*")) { 070 return JAVA9_STYLESHEET; 071 } 072 errorReporter.printWarning("Unrecognized Java version " + javaVersion + ", using Java 9 stylesheet"); 073 // TODO: review this when Java 11 becomes available and/or make more configurable! 074 return JAVA9_STYLESHEET; 075 } 076}