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.FileUtils;
20 import org.apache.commons.io.FilenameUtils;
21 import org.apache.commons.io.IOUtils;
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.forgerock.doc.maven.AbstractDocbkxMojo;
24 import org.forgerock.doc.maven.utils.HtmlUtils;
25 import org.forgerock.doc.maven.utils.SyntaxHighlighterCopier;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.net.URL;
30 import java.util.HashMap;
31
32
33
34
35 public class Html {
36
37
38
39
40 private AbstractDocbkxMojo m;
41
42
43
44
45
46
47 public Html(final AbstractDocbkxMojo mojo) {
48 m = mojo;
49
50 outputDirectories = new String[2];
51 outputDirectories[0] = "";
52 outputDirectories[1] = File.separator + FilenameUtils.getBaseName(m.getDocumentSrcName());
53 }
54
55
56
57
58
59
60 public void execute() throws MojoExecutionException {
61
62 addScript();
63
64
65
66 final File htmlDir = new File(m.getDocbkxOutputDirectory(), "html");
67 final String chunkDirName = FilenameUtils.getBaseName(m.getDocumentSrcName());
68
69 String[] outputDirectories = new String[2 * m.getDocNames().size()];
70
71 int i = 0;
72 for (final String docName : m.getDocNames()) {
73
74 final File docDir = new File(htmlDir, docName);
75
76
77
78 outputDirectories[i] = docDir.getPath();
79 ++i;
80
81
82 outputDirectories[i] = new File(docDir, chunkDirName).getPath();
83 ++i;
84 }
85
86 SyntaxHighlighterCopier copier =
87 new SyntaxHighlighterCopier(outputDirectories);
88 try {
89 copier.copy();
90 } catch (IOException e) {
91 throw new MojoExecutionException(
92 "Failed to copy files: " + e.getMessage(), e);
93 }
94
95
96
97 editBuiltHtml(htmlDir.getPath());
98
99
100
101 if (m.doCopyResourceFiles() && m.getResourcesDirectory().exists()) {
102
103 final String baseName = FilenameUtils.getBaseName(m.getResourcesDirectory().getPath());
104
105 for (final String docName : m.getDocNames()) {
106
107 final File docDir = new File(htmlDir, docName);
108
109 try {
110 HtmlUtils.fixResourceLinks(new File(docDir, chunkDirName).getPath(), baseName);
111 } catch (IOException e) {
112 throw new MojoExecutionException("Failed to update resource links", e);
113 }
114 }
115 }
116 }
117
118
119
120
121 private String[] outputDirectories;
122
123
124
125
126
127
128
129
130 private void addScript() throws MojoExecutionException {
131
132 final URL scriptUrl = getClass().getResource("/js/" + m.getJavaScriptFileName());
133 String scriptString;
134 try {
135 scriptString = IOUtils.toString(scriptUrl);
136 } catch (IOException ie) {
137 throw new MojoExecutionException("Failed to read " + scriptUrl, ie);
138 }
139
140 if (scriptString != null) {
141 scriptString = scriptString.replace("PROJECT_NAME", m.getProjectName().toLowerCase());
142 scriptString = scriptString.replace("PROJECT_VERSION", m.getProjectVersion());
143 scriptString = scriptString.replace("LATEST_JSON", m.getLatestJson());
144 scriptString = scriptString.replace("DOCS_SITE", m.getDocsSite());
145 scriptString = scriptString.replace("EOSL_JSON", m.getEoslJson());
146 } else {
147 throw new MojoExecutionException(scriptUrl + " was empty");
148 }
149
150
151
152
153
154
155 for (final String outputDirectory : outputDirectories) {
156
157 for (final String docName : m.getDocNames()) {
158
159 final File parent = FileUtils.getFile(
160 m.getDocbkxOutputDirectory(), "html", docName + outputDirectory);
161 final File scriptFile = new File(parent, m.getJavaScriptFileName());
162
163 try {
164 FileUtils.writeStringToFile(scriptFile, scriptString, "UTF-8");
165 } catch (IOException ie) {
166 throw new MojoExecutionException(
167 "Failed to write to " + scriptFile.getPath(), ie);
168 }
169 }
170 }
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 final void editBuiltHtml(final String htmlDir) throws MojoExecutionException {
194 try {
195 HashMap<String, String> replacements = new HashMap<String, String>();
196
197 String doctype = IOUtils.toString(
198 getClass().getResourceAsStream("/starthtml-doctype.txt"), "UTF-8");
199 replacements.put("<html>", doctype);
200
201
202 String robots = "<head>" + System.getProperty("line.separator")
203 + IOUtils.toString(getClass().getResourceAsStream("/robots.txt"), "UTF-8");
204 replacements.put("<head>", robots);
205
206 String favicon = IOUtils.toString(
207 getClass().getResourceAsStream("/endhead-favicon.txt"), "UTF-8");
208 favicon = favicon.replace("FAVICON-LINK", m.getFaviconLink());
209 replacements.put("</head>", favicon);
210
211 String linkToJira = getLinkToJira();
212
213 String gascript = IOUtils.toString(
214 getClass().getResourceAsStream("/endbody-ga.txt"), "UTF-8");
215 gascript = gascript.replace("ANALYTICS-ID", m.getGoogleAnalyticsId());
216 replacements.put("</body>", linkToJira + "\n" + gascript);
217
218 HtmlUtils.updateHtml(htmlDir, replacements);
219 } catch (IOException e) {
220 throw new MojoExecutionException(
221 "Failed to update output HTML correctly: " + e.getMessage());
222 }
223 }
224
225
226
227
228
229
230
231 private String getLinkToJira() {
232 String link = "<p> </p><div id=\"footer\"><p>Something wrong on this page? "
233 + "<a href=\"JIRA-URL\">Log a documentation bug.</a></p></div>";
234
235
236 String jiraURL = "https://bugster.forgerock.org/jira/secure/CreateIssueDetails!init.jspa";
237
238 if (m.getProjectName().equalsIgnoreCase("OpenAM")) {
239 jiraURL += "?pid=10000&components=10007&issuetype=1";
240 }
241 if (m.getProjectName().equalsIgnoreCase("OpenDJ")) {
242 jiraURL += "?pid=10040&components=10132&issuetype=1";
243 }
244 if (m.getProjectName().equalsIgnoreCase("OpenICF")) {
245 jiraURL += "?pid=10041&components=10170&issuetype=1";
246 }
247 if (m.getProjectName().equalsIgnoreCase("OpenIDM")) {
248 jiraURL += "?pid=10020&components=10164&issuetype=1";
249 }
250 if (m.getProjectName().equalsIgnoreCase("OpenIG")) {
251 jiraURL += "?pid=10060&components=10220&issuetype=1";
252 }
253 if (m.getProjectName().equalsIgnoreCase("ForgeRock")) {
254 jiraURL += "?pid=10010&issuetype=1";
255 }
256
257 if (!jiraURL.contains("pid")) {
258 link = "";
259 } else {
260 link = link.replaceFirst("JIRA-URL", jiraURL);
261 }
262 return link;
263 }
264 }