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 2014-2015 ForgeRock AS. 015 */ 016 017package org.forgerock.doc.maven.build; 018 019import org.apache.maven.plugin.MojoExecutionException; 020import org.forgerock.doc.maven.AbstractDocbkxMojo; 021import org.forgerock.doc.maven.utils.ImageCopier; 022import org.forgerock.doc.maven.utils.OLinkUtils; 023import org.twdata.maven.mojoexecutor.MojoExecutor; 024 025import java.io.File; 026import java.io.IOException; 027import java.util.ArrayList; 028 029/** 030 * Build XHTML output. 031 */ 032public class HtmlForBootstrap { 033 034 /** 035 * The Mojo that holds configuration and related methods. 036 */ 037 private AbstractDocbkxMojo m; 038 039 /** 040 * The Executor to run docbkx-tools. 041 */ 042 private final Executor executor; 043 044 /** 045 * Constructor setting the Mojo that holds the configuration. 046 * 047 * @param mojo The Mojo that holds the configuration. 048 */ 049 public HtmlForBootstrap(final AbstractDocbkxMojo mojo) { 050 m = mojo; 051 this.executor = new Executor(); 052 } 053 054 /** 055 * Build documents from DocBook XML sources. 056 * 057 * @throws MojoExecutionException Failed to build output. 058 */ 059 public void execute() throws MojoExecutionException { 060 executor.prepareOlinkDB(); 061 executor.build(); 062 } 063 064 /** 065 * Get absolute path to an Olink target database XML document 066 * that points to the individual generated Olink DB files, 067 * for Bootstrap HTML. 068 * 069 * @return Absolute path to the file. 070 * @throws MojoExecutionException Could not write target DB file. 071 */ 072 final String getTargetDB() throws MojoExecutionException { 073 File targetDB = new File(m.getBuildDirectory(), "olinkdb-bootstrap.xml"); 074 075 try { 076 OLinkUtils.createTargetDatabase(targetDB, "bootstrap", m); 077 } catch (Exception e) { 078 throw new MojoExecutionException( 079 "Failed to write link target database: " + targetDB.getPath(), e); 080 } 081 082 return targetDB.getPath(); 083 } 084 085 /** 086 * Enclose methods to run plugins. 087 */ 088 class Executor extends MojoExecutor { 089 090 /** 091 * Prepare olink target database from DocBook XML sources. 092 * 093 * @throws MojoExecutionException Failed to build target database. 094 */ 095 void prepareOlinkDB() throws MojoExecutionException { 096 097 for (String docName : m.getDocNames()) { 098 ArrayList<Element> cfg = new ArrayList<Element>(); 099 cfg.add(element(name("chunkedOutput"), "false")); 100 cfg.add(element(name("collectXrefTargets"), "only")); 101 cfg.add(element(name("includes"), docName + "/" + m.getDocumentSrcName())); 102 cfg.add(element(name("sourceDirectory"), m.path(m.getDocbkxModifiableSourcesDirectory()))); 103 cfg.add(element(name("htmlCustomization"), m.path(m.getBootstrapCustomization()))); 104 cfg.add(element(name("xincludeSupported"), m.isXincludeSupported())); 105 cfg.add(element(name("targetDirectory"), m.path(m.getDocbkxOutputDirectory()) + "/bootstrap")); 106 cfg.add(element(name("targetsFilename"), m.getDocumentSrcName() + ".html.target.db")); 107 108 executeMojo( 109 plugin( 110 groupId("com.agilejava.docbkx"), 111 artifactId("docbkx-maven-plugin"), 112 version(m.getDocbkxVersion())), 113 goal("generate-html"), 114 configuration(cfg.toArray(new Element[cfg.size()])), 115 executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager())); 116 } 117 } 118 119 /** 120 * Build documents from DocBook XML sources. 121 * 122 * @throws MojoExecutionException Failed to build the output. 123 */ 124 void build() throws MojoExecutionException { 125 126 try { 127 ImageCopier.copyImages("bootstrap", "", m); 128 } catch (IOException e) { 129 throw new MojoExecutionException("Failed to copy images", e); 130 } 131 132 ArrayList<Element> cfg = new ArrayList<Element>(); 133 cfg.add(element(name("chunkedOutput"), "false")); 134 cfg.add(element(name("highlightSource"), m.useSyntaxHighlighting())); 135 cfg.add(element(name("includes"), "*/" + m.getDocumentSrcName())); 136 cfg.add(element(name("sectionAutolabel"), m.areSectionsAutolabeled())); 137 cfg.add(element(name("sectionLabelIncludesComponentLabel"), m.doesSectionLabelIncludeComponentLabel())); 138 cfg.add(element(name("sourceDirectory"), m.path(m.getDocbkxModifiableSourcesDirectory()))); 139 cfg.add(element(name("targetDatabaseDocument"), getTargetDB())); 140 cfg.add(element(name("targetDirectory"), m.path(m.getDocbkxOutputDirectory()) + "/bootstrap")); 141 cfg.add(element(name("htmlCustomization"), m.path(m.getBootstrapCustomization()))); 142 cfg.add(element(name("xincludeSupported"), m.isXincludeSupported())); 143 144 executeMojo( 145 plugin( 146 groupId("com.agilejava.docbkx"), 147 artifactId("docbkx-maven-plugin"), 148 version(m.getDocbkxVersion())), 149 goal("generate-html"), 150 configuration(cfg.toArray(new Element[cfg.size()])), 151 executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager())); 152 } 153 } 154}