001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2012-2014 ForgeRock AS 015 */ 016 017package org.forgerock.doc.maven.utils; 018 019import org.apache.commons.io.FileUtils; 020import org.apache.commons.io.filefilter.SuffixFileFilter; 021import org.forgerock.doc.maven.AbstractDocbkxMojo; 022 023import java.io.File; 024import java.io.FileFilter; 025import java.io.IOException; 026import java.util.Set; 027 028/** 029 * Copy images from source to destination. 030 */ 031public final class ImageCopier { 032 033 /** 034 * Support a subset of formats described in the documentation for the <a 035 * href="http://www.docbook.org/tdg/en/html/imagedata.html">ImageData</a> 036 * element. 037 */ 038 private static final String[] IMAGE_FILE_SUFFIXES = 039 {".bmp", ".eps", ".gif", ".jpeg", ".jpg", ".png", ".svg", ".tiff"}; 040 041 /** 042 * Copy images from source to destination. 043 * 044 * 045 * @param docType Type of output document, 046 * such as {@code epub} or {@code html} 047 * @param baseName Directory name to add, such as {@code index}. 048 * Leave null or empty when not adding a directory name. 049 * @param mojo Mojo holding configuration information. 050 * 051 * @throws IOException Something went wrong copying images. 052 */ 053 public static void copyImages(final String docType, 054 final String baseName, 055 final AbstractDocbkxMojo mojo) 056 throws IOException { 057 copyImages( 058 docType, 059 baseName, 060 mojo.getDocumentSrcName(), 061 mojo.getDocbkxModifiableSourcesDirectory(), 062 mojo.getDocbkxOutputDirectory()); 063 } 064 065 /** 066 * Copy images from source to destination. 067 * 068 * <p> 069 * 070 * DocBook XSL does not copy the images, 071 * because XSL does not have a facility for copying files. 072 * Unfortunately, neither does docbkx-tools. 073 * 074 * @param docType Type of output document, 075 * such as {@code epub} or {@code html} 076 * @param baseName Directory name to add, such as {@code index}. 077 * Leave null or empty when not adding a directory name. 078 * @param documentSrcName Top-level DocBook XML document source name, 079 * such as {@code index.xml}. 080 * @param sourceDirectory Base directory for DocBook XML sources. 081 * @param outputDirectory Base directory where the output is found. 082 * 083 * @throws IOException Something went wrong copying images. 084 */ 085 public static void copyImages(final String docType, 086 final String baseName, 087 final String documentSrcName, 088 final File sourceDirectory, 089 final File outputDirectory) 090 throws IOException { 091 092 if (docType == null) { 093 throw new IllegalArgumentException("Type of output document must not be null."); 094 } 095 096 097 if (documentSrcName == null) { 098 throw new IllegalArgumentException( 099 "Top-level DocBook XML document source name must not be null."); 100 } 101 102 Set<String> docNames = NameUtils.getDocumentNames( 103 sourceDirectory, documentSrcName); 104 if (docNames.isEmpty()) { 105 throw new IOException("No document names found."); 106 } 107 108 String extra = ""; 109 if (!(baseName == null) && !baseName.equalsIgnoreCase("")) { 110 extra = File.separator + baseName; 111 } 112 113 FileFilter onlyImages = new SuffixFileFilter(IMAGE_FILE_SUFFIXES); 114 115 for (String docName : docNames) { 116 117 // Copy images specific to the document. 118 File srcDir = FileUtils.getFile(sourceDirectory, docName, "images"); 119 File destDir = FileUtils.getFile(outputDirectory, docType, docName + extra, "images"); 120 if (srcDir.exists()) { 121 FileUtils.copyDirectory(srcDir, destDir, onlyImages); 122 } 123 124 125 // Copy any shared images. 126 String shared = "shared" + File.separator + "images"; 127 srcDir = new File(sourceDirectory, shared); 128 destDir = FileUtils.getFile(outputDirectory, docType, docName + extra, shared); 129 if (srcDir.exists()) { 130 FileUtils.copyDirectory(srcDir, destDir, onlyImages); 131 } 132 } 133 } 134 135 private ImageCopier() { 136 // Not used. 137 } 138}