View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2014-2015 ForgeRock AS
15   */
16  
17  package org.forgerock.doc.maven.utils;
18  
19  import org.apache.commons.io.FileUtils;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.net.URL;
24  
25  /**
26   * Copy Bootstrap files under the specified directories.
27   */
28  public final class BootstrapCopier {
29  
30      /**
31       * Bootstrap logo files.
32       */
33      private final String[] bootstrapLogoResources = {
34          "/logos/hero-bg-01.png",
35          "/logos/left-shape-4.png",
36          "/logos/Icon_External_Link.png",
37          "/logos/forgerock-header-logo.png",
38          "/logos/FR_logo_horiz_FC_rev.png"
39      };
40  
41      /**
42       * Bootstrap SWF files.
43       */
44      private final String[] bootstrapSwfResources = {
45          "/swf/ZeroClipboard.swf"
46      };
47  
48      /**
49       * Directories where HtmlForBootstrap scripts, SWF and CSS are to be added.
50       */
51      private String[] outputDirectories;
52  
53      /**
54       * Construct a BootstrapCopier, specifying output directories.
55       *
56       * @param outputDirectories Full path to directories under which to copy files.
57       */
58      public BootstrapCopier(final String[] outputDirectories) {
59          this.outputDirectories = outputDirectories;
60      }
61  
62      /**
63       * For each outputDirectory, copy HtmlForBootstrap files under outputDirectory/sh.
64       *
65       * @throws java.io.IOException Failed to copy files.
66       */
67      public void copy() throws IOException {
68          addBootstrapLogos();
69          addBootstrapSwf();
70      }
71  
72      /**
73       * Add HtmlForBootstrap logo files in each output directory.
74       *
75       * @throws java.io.IOException Failed to add scripts.
76       */
77      private void addBootstrapLogos() throws IOException {
78          addBootstrapResources(bootstrapLogoResources);
79      }
80  
81      private void addBootstrapSwf() throws IOException {
82          addBootstrapResources(bootstrapSwfResources);
83      }
84  
85      /**
86       * Add Bootstrap resource files in each output directory.
87       *
88       * @param resources List of resource files to copy.
89       * @throws java.io.IOException Failed to files.
90       */
91      private void addBootstrapResources(final String[] resources) throws IOException {
92  
93          for (String resource : resources) {
94              URL resourceUrl = getClass().getResource(resource);
95  
96              // The html.stylesheet parameter should probably take URLs.
97              // When local files are referenced,
98              // the DocBook XSL stylesheets do not copy the files.
99              // Instead the files must be copied to the output directories.
100 
101             if (resourceUrl != null) {
102                 for (final String outputDirectory : outputDirectories) {
103                     final File styleSheetFile = FileUtils.getFile(outputDirectory, "includes", resource);
104                     FileUtils.copyURLToFile(resourceUrl, styleSheetFile);
105 
106                 }
107             } else {
108                 System.err.println("WARNING: Resource " + resource + " "
109                     + "cannot be " + "found!");
110             }
111         }
112     }
113 }