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 JAVA8_STYLESHEET = "stylesheet8.css"; 031 static final String JAVA6_STYLESHEET = "stylesheet6.css"; 032 static final String CODERAY_STYLESHEET = "coderay-asciidoctor.css"; 033 static final String OUTPUT_STYLESHEET = "stylesheet.css"; 034 035 private final DocletOptions docletOptions; 036 private final DocErrorReporter errorReporter; 037 038 public Stylesheets(DocletOptions options, DocErrorReporter errorReporter) { 039 this.docletOptions = options; 040 this.errorReporter = errorReporter; 041 } 042 043 public boolean copy() { 044 if (!docletOptions.destDir().isPresent()) { 045 // standard doclet must have checked this by the time we are called 046 errorReporter.printError("Destination directory not specified, cannot copy stylesheet"); 047 return false; 048 } 049 String stylesheet = selectStylesheet(System.getProperty("java.version")); 050 File destDir = docletOptions.destDir().get(); 051 try { 052 Resources.copy(Resources.getResource(stylesheet), new FileOutputStream(new File(destDir, OUTPUT_STYLESHEET))); 053 Resources.copy(Resources.getResource(CODERAY_STYLESHEET), new FileOutputStream(new File(destDir, CODERAY_STYLESHEET))); 054 return true; 055 } catch (IOException e) { 056 errorReporter.printError(e.getLocalizedMessage()); 057 return false; 058 } 059 } 060 061 String selectStylesheet(String javaVersion) { 062 if (javaVersion.matches("^1\\.[56]\\D.*")) { 063 return JAVA6_STYLESHEET; 064 } 065 if (javaVersion.matches("^1\\.[78]\\D.*")) { 066 return JAVA8_STYLESHEET; 067 } 068 errorReporter.printWarning("Unrecognized Java version " + javaVersion + ", using Java 7/8 stylesheet"); 069 // TODO: review this when Java 9 becomes available! 070 return JAVA8_STYLESHEET; 071 } 072}