1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.doc.maven.post;
18
19 import org.apache.commons.io.IOUtils;
20
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.forgerock.doc.maven.AbstractDocbkxMojo;
23 import org.forgerock.doc.maven.utils.HtmlUtils;
24 import org.forgerock.doc.maven.utils.BootstrapCopier;
25 import org.forgerock.doc.maven.utils.NameUtils;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.HashMap;
30
31
32
33
34 public class Bootstrap {
35
36
37
38
39 private AbstractDocbkxMojo m;
40
41
42
43
44
45
46 public Bootstrap(final AbstractDocbkxMojo mojo) {
47 m = mojo;
48
49 outputDirectories = new String[1];
50 outputDirectories[0] = "";
51 }
52
53
54
55
56
57
58 public void execute() throws MojoExecutionException {
59
60
61 final File htmlDir = new File(m.getDocbkxOutputDirectory(),
62 "bootstrap");
63
64 String[] outputDirectories = new String[m.getDocNames().size()];
65
66 int i = 0;
67 for (final String docName : m.getDocNames()) {
68
69 final File docDir = new File(htmlDir, docName);
70
71
72 if (m.getFormats().contains(AbstractDocbkxMojo.Format.pdf)) {
73 addPDFLink(docDir.getPath(), docName);
74 }
75
76
77
78 outputDirectories[i] = docDir.getPath();
79 ++i;
80
81 }
82 editBuiltHtml(htmlDir.getPath());
83
84 if (m.isDraftMode().equals("yes")) {
85 addDraftAlert(htmlDir.getPath());
86 }
87
88 BootstrapCopier copier =
89 new BootstrapCopier(outputDirectories);
90 try {
91 copier.copy();
92 } catch (IOException e) {
93 throw new MojoExecutionException(
94 "Failed to copy files: " + e.getMessage(), e);
95 }
96
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110 final void editBuiltHtml(final String htmlDir) throws
111 MojoExecutionException {
112 try {
113 HashMap<String, String> replacements = new HashMap<String, String>();
114
115 String gascript = IOUtils.toString(
116 getClass().getResourceAsStream("/endhead-ga.txt"), "UTF-8");
117 gascript = gascript.replace("ANALYTICS-ID", m.getGoogleAnalyticsId());
118 replacements.put("</head>", gascript);
119
120 HtmlUtils.updateHtml(htmlDir, replacements);
121 } catch (IOException e) {
122 throw new MojoExecutionException(
123 "Failed to update output HTML correctly: " + e.getMessage());
124 }
125 }
126
127
128
129 final void addDraftAlert(final String htmlDir) throws
130 MojoExecutionException {
131 try {
132 HashMap<String, String> replacements = new HashMap<String, String>();
133 String draftAlert = IOUtils.toString(
134 getClass().getResourceAsStream("/endbody-draftalert.txt"), "UTF-8");
135 replacements.put("</body>", draftAlert);
136 HtmlUtils.updateHtml(htmlDir, replacements);
137 } catch (IOException e) {
138 throw new MojoExecutionException(
139 "Failed to update output HTML correctly: " + e.getMessage());
140 }
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 final void addPDFLink(final String htmlDir, final String docName) throws
157 MojoExecutionException {
158 try {
159 HashMap<String, String> replacements = new HashMap<String, String>();
160
161 String linkToPdf = getLinkToPdf(docName);
162 replacements.put("<ul id=\"pdf-link\">", linkToPdf);
163
164 HtmlUtils.updateHtml(htmlDir, replacements);
165 } catch (IOException e) {
166 throw new MojoExecutionException(
167 "Failed to inject PDF link HTML correctly: " + e.getMessage());
168 }
169 }
170
171 private String getLinkToPdf(final String docName) {
172
173 String link = "<ul id=\"pdf-link\" "
174 + "class=\"nav navbar-nav navbar-right hidden-xs\">"
175 + "<li><a href=\"PDF-URL\" target=\"_blank\">"
176 + "<span class=\"glyphicon glyphicon-save\"></span> "
177 + "Download PDF Version</a></li>";
178
179 String pdfUrl = "../../" + NameUtils.renameDoc(m.getProjectName(),
180 docName, "pdf");
181 link = link.replaceFirst("PDF-URL", pdfUrl);
182
183 return link;
184 }
185
186
187
188
189 private String[] outputDirectories;
190 }