Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
GenerateDocBookMojo |
|
| 3.75;3.75 |
1 | /** |
|
2 | * |
|
3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
4 | * contributor license agreements. See the NOTICE file distributed with |
|
5 | * this work for additional information regarding copyright ownership. |
|
6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
7 | * (the "License"); you may not use this file except in compliance with |
|
8 | * the License. You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | package org.apache.camel.maven; |
|
19 | ||
20 | import org.apache.maven.plugin.AbstractMojo; |
|
21 | import org.apache.maven.plugin.MojoExecutionException; |
|
22 | ||
23 | import java.io.File; |
|
24 | import java.io.FileOutputStream; |
|
25 | import java.io.IOException; |
|
26 | import java.io.BufferedInputStream; |
|
27 | import java.io.BufferedOutputStream; |
|
28 | import java.io.FileWriter; |
|
29 | import java.io.ByteArrayOutputStream; |
|
30 | import java.io.InputStream; |
|
31 | import java.io.PrintWriter; |
|
32 | import java.util.ArrayList; |
|
33 | import java.util.Iterator; |
|
34 | import java.util.List; |
|
35 | import java.util.StringTokenizer; |
|
36 | import java.net.URL; |
|
37 | import java.net.URLConnection; |
|
38 | ||
39 | import javax.xml.transform.Transformer; |
|
40 | import javax.xml.transform.TransformerFactory; |
|
41 | import javax.xml.transform.dom.DOMSource; |
|
42 | import javax.xml.transform.stream.StreamResult; |
|
43 | import javax.xml.transform.stream.StreamSource; |
|
44 | ||
45 | import org.w3c.dom.Element; |
|
46 | import org.w3c.dom.NodeList; |
|
47 | import org.w3c.dom.Document; |
|
48 | import org.w3c.dom.Node; |
|
49 | import org.w3c.dom.NamedNodeMap; |
|
50 | ||
51 | import org.w3c.tidy.DOMElementImpl; |
|
52 | import org.w3c.tidy.Tidy; |
|
53 | ||
54 | /** |
|
55 | * Goal which extracts the content of a wiki page and converts it to docbook |
|
56 | * format |
|
57 | * |
|
58 | * @goal htmlToDocbook |
|
59 | * @phase process-sources |
|
60 | */ |
|
61 | 0 | public class GenerateDocBookMojo extends AbstractMojo { |
62 | ||
63 | /** |
|
64 | * Base URL. |
|
65 | * |
|
66 | * @parameter expression="${baseURL}" |
|
67 | * default-value="http://activemq.apache.org/camel/" |
|
68 | * @required |
|
69 | */ |
|
70 | private String baseURL; |
|
71 | ||
72 | /** |
|
73 | * List of resources |
|
74 | * |
|
75 | * @parameter |
|
76 | */ |
|
77 | private String[] resources; |
|
78 | ||
79 | /** |
|
80 | * List of author's fullname |
|
81 | * |
|
82 | * @parameter |
|
83 | */ |
|
84 | private String[] authors; |
|
85 | ||
86 | /** |
|
87 | * Location of the xsl file. |
|
88 | * |
|
89 | * @parameter expression="${configDirectory}" |
|
90 | * default-value="${basedir}/src/styles/docbook.xsl" |
|
91 | * @required |
|
92 | */ |
|
93 | private String xslFile; |
|
94 | ||
95 | /** |
|
96 | * Location of the output directory. |
|
97 | * |
|
98 | * @parameter expression="${project.build.directory}/docbkx/docbkx-source" |
|
99 | */ |
|
100 | private String outputPath; |
|
101 | ||
102 | /** |
|
103 | * Location of the output directory for wiki source. |
|
104 | * |
|
105 | * @parameter expression="${project.build.directory}/docbkx/wiki-source" |
|
106 | */ |
|
107 | private String wikiOutputPath; |
|
108 | ||
109 | /** |
|
110 | * @parameter expression="${title}" |
|
111 | * @required |
|
112 | */ |
|
113 | private String title; |
|
114 | ||
115 | /** |
|
116 | * @parameter expression="${subtitle}" |
|
117 | */ |
|
118 | private String subtitle; |
|
119 | ||
120 | /** |
|
121 | * @parameter expression="${mainFilename}" default-value="manual" |
|
122 | * @required |
|
123 | */ |
|
124 | private String mainFilename; |
|
125 | ||
126 | /** |
|
127 | * @parameter expression="${version}" default-value="${project.version}" |
|
128 | */ |
|
129 | private String version; |
|
130 | ||
131 | /** |
|
132 | * @parameter expression="${legalNotice}" |
|
133 | */ |
|
134 | private String legalNotice; |
|
135 | ||
136 | /** |
|
137 | * Location of image files. |
|
138 | * |
|
139 | * @parameter expression="${project.build.directory}/site/book/images" |
|
140 | * |
|
141 | */ |
|
142 | private String imageLocation; |
|
143 | ||
144 | private String chapterId; |
|
145 | ||
146 | ||
147 | public void execute() throws MojoExecutionException { |
|
148 | 0 | File outputDir = new File(outputPath); |
149 | 0 | File wikiOutputDir = new File(wikiOutputPath); |
150 | 0 | File imageDir = new File(imageLocation); |
151 | 0 | if (!outputDir.exists()) { |
152 | 0 | outputDir.mkdirs(); |
153 | 0 | imageDir.mkdirs(); |
154 | 0 | wikiOutputDir.mkdirs(); |
155 | } |
|
156 | 0 | this.createMainXML(); |
157 | ||
158 | 0 | for (int i = 0; i < resources.length; ++i) { |
159 | 0 | this.setChapterId( removeExtension(resources[i])); |
160 | ||
161 | 0 | process(resources[i]); |
162 | } |
|
163 | ||
164 | 0 | } |
165 | ||
166 | ||
167 | /** |
|
168 | * Extract the wiki content and tranform it into docbook format |
|
169 | * |
|
170 | * @param resource |
|
171 | */ |
|
172 | public void process(String resource) { |
|
173 | ||
174 | 0 | Tidy tidy = new Tidy(); |
175 | 0 | ByteArrayOutputStream out = null; |
176 | 0 | BufferedOutputStream output = null; |
177 | 0 | BufferedOutputStream wikiOutput = null; |
178 | ||
179 | 0 | tidy.setXmlOut(true); |
180 | try { |
|
181 | 0 | out = new ByteArrayOutputStream(); |
182 | 0 | URL u = new URL(baseURL + resource); |
183 | 0 | Document doc = tidy.parseDOM( |
184 | new BufferedInputStream(u.openStream()), out); |
|
185 | 0 | out.close(); |
186 | // let's extract the div element with class="wiki-content |
|
187 | // maincontent" |
|
188 | 0 | NodeList nodeList = doc.getElementsByTagName("div"); |
189 | 0 | for (int i = 0; i < nodeList.getLength(); ++i) { |
190 | 0 | Node node = nodeList.item(i); |
191 | ||
192 | 0 | NamedNodeMap nm = node.getAttributes(); |
193 | 0 | Node attr = nm.getNamedItem("class"); |
194 | ||
195 | 0 | if (attr != null |
196 | && attr.getNodeValue().equalsIgnoreCase( |
|
197 | "wiki-content maincontent")) { |
|
198 | 0 | downloadImages(node); |
199 | // These attributes will be used by xsl to |
|
200 | 0 | Element element = (Element) node; |
201 | 0 | element.setAttribute("chapterId", chapterId); |
202 | 0 | element.setAttribute("baseURL", baseURL); |
203 | 0 | element.setAttribute("imageLocation", "../images/"); |
204 | ||
205 | 0 | DOMSource source = new DOMSource(node); |
206 | ||
207 | ||
208 | 0 | output = new BufferedOutputStream( |
209 | new FileOutputStream(outputPath + File.separator |
|
210 | + removeExtension(resource) + ".xml")); |
|
211 | 0 | StreamResult result = new StreamResult(output); |
212 | 0 | TransformerFactory tFactory = TransformerFactory |
213 | .newInstance(); |
|
214 | 0 | Transformer transformer = tFactory |
215 | .newTransformer(new StreamSource(xslFile)); |
|
216 | 0 | transformer.transform(source, result); |
217 | ||
218 | // generate the wiki source for debugging |
|
219 | 0 | wikiOutput = new BufferedOutputStream( |
220 | new FileOutputStream(wikiOutputPath + File.separator |
|
221 | + removeExtension(resource) + ".html")); |
|
222 | 0 | result = new StreamResult(wikiOutput); |
223 | 0 | transformer = tFactory.newTransformer(); |
224 | 0 | transformer.transform(source, result); |
225 | ||
226 | 0 | break; |
227 | } |
|
228 | ||
229 | } |
|
230 | ||
231 | 0 | } catch (Exception e) { |
232 | 0 | e.printStackTrace(); |
233 | }finally { |
|
234 | 0 | try { |
235 | 0 | if(output != null) |
236 | 0 | output.close(); |
237 | 0 | } catch (IOException e) { |
238 | // TODO Auto-generated catch block |
|
239 | 0 | e.printStackTrace(); |
240 | 0 | } |
241 | 0 | } |
242 | 0 | } |
243 | ||
244 | ||
245 | /* |
|
246 | * create the main docbook xml file |
|
247 | */ |
|
248 | public void createMainXML() { |
|
249 | try { |
|
250 | ||
251 | 0 | PrintWriter out = new PrintWriter(new FileWriter(outputPath |
252 | + File.separator + mainFilename + ".xml")); |
|
253 | ||
254 | 0 | out |
255 | .println("<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.4//EN\" \"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd\" "); |
|
256 | 0 | out.println("["); |
257 | ||
258 | 0 | for (int i = 0; i < resources.length; ++i) { |
259 | 0 | out.println("<!ENTITY " + removeExtension(resources[i]) + " SYSTEM \"" + removeExtension(resources[i]) |
260 | + ".xml\">"); |
|
261 | } |
|
262 | ||
263 | 0 | out.println("]>"); |
264 | 0 | out.println("<book>"); |
265 | 0 | out.println("<bookinfo>"); |
266 | 0 | out.println("<title>" + title + "</title>"); |
267 | 0 | out.println("<subtitle>" + subtitle + "</subtitle>"); |
268 | 0 | out.println("<releaseinfo>" + version + "</releaseinfo>"); |
269 | 0 | out.println(" <authorgroup>"); |
270 | 0 | if (authors != null) { |
271 | 0 | for (int i = 0; i < authors.length; ++i) { |
272 | 0 | StringTokenizer name = new StringTokenizer(authors[i]); |
273 | 0 | String fname = name.nextToken(); |
274 | 0 | String lname = ""; |
275 | 0 | if (name.hasMoreTokens()) { |
276 | 0 | lname = name.nextToken(); |
277 | } |
|
278 | 0 | out.println("<author>"); |
279 | 0 | out.println("<firstname>" + fname + "</firstname>"); |
280 | 0 | out.println("<surname>" + lname + "</surname>"); |
281 | 0 | out.println("</author>"); |
282 | ||
283 | } |
|
284 | } |
|
285 | ||
286 | 0 | out.println("</authorgroup>"); |
287 | 0 | out.println("<legalnotice>"); |
288 | 0 | if (legalNotice != null && legalNotice.length() > 0) { |
289 | 0 | out.println("<para>"); |
290 | 0 | out.println(legalNotice); |
291 | 0 | out.println("</para>"); |
292 | 0 | } else { |
293 | 0 | out |
294 | .println("<para>Licensed to the Apache Software Foundation (ASF) under one or more"); |
|
295 | 0 | out |
296 | .println("contributor license agreements. See the NOTICE file distributed with"); |
|
297 | 0 | out |
298 | .println("this work for additional information regarding copyright ownership. The"); |
|
299 | 0 | out |
300 | .println("ASF licenses this file to You under the Apache License, Version 2.0 (the"); |
|
301 | 0 | out |
302 | .println("\"License\"); you may not use this file except in compliance with the"); |
|
303 | 0 | out |
304 | .println("License. You may obtain a copy of the License at</para>"); |
|
305 | 0 | out |
306 | .println("<para>http://www.apache.org/licenses/LICENSE-2.0</para>"); |
|
307 | 0 | out |
308 | .println("<para>Unless required by applicable law or agreed to in writing,"); |
|
309 | 0 | out |
310 | .println(" software distributed under the License is distributed on an \"AS IS\""); |
|
311 | 0 | out |
312 | .println("BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or"); |
|
313 | 0 | out |
314 | .println("implied. See the License for the specific language governing permissions"); |
|
315 | 0 | out.println("and limitations under the License.</para>"); |
316 | } |
|
317 | ||
318 | 0 | out.println("</legalnotice>"); |
319 | 0 | out.println("</bookinfo>"); |
320 | 0 | out.println("<toc></toc>"); |
321 | ||
322 | 0 | for (int i = 0; i < resources.length; ++i) { |
323 | 0 | out.println("&" + removeExtension(resources[i]) + ";"); |
324 | } |
|
325 | ||
326 | 0 | out.println("</book>"); |
327 | 0 | out.flush(); |
328 | 0 | out.close(); |
329 | ||
330 | 0 | } catch (IOException e) { |
331 | // TODO Auto-generated catch block |
|
332 | 0 | e.printStackTrace(); |
333 | 0 | } |
334 | 0 | } |
335 | ||
336 | ||
337 | public void downloadImages(Node node) { |
|
338 | 0 | List imageList = getImageUrls(node); |
339 | 0 | Iterator iter = imageList.iterator(); |
340 | 0 | while(iter.hasNext()) { |
341 | 0 | String imageUrl = (String)iter.next(); |
342 | 0 | String imageFile = "imageFile"; |
343 | ||
344 | //check if url path is relative |
|
345 | 0 | if (imageUrl.indexOf("http://") < 0) { |
346 | 0 | imageUrl = baseURL + imageUrl; |
347 | } |
|
348 | try { |
|
349 | ||
350 | 0 | URL url = new URL(imageUrl); |
351 | 0 | StringTokenizer st=new StringTokenizer(url.getFile(), "/"); |
352 | 0 | while (st.hasMoreTokens()) { |
353 | 0 | imageFile=st.nextToken(); |
354 | 0 | } |
355 | ||
356 | 0 | URLConnection connection = url.openConnection(); |
357 | 0 | InputStream stream = connection.getInputStream(); |
358 | 0 | BufferedInputStream in = new BufferedInputStream(stream); |
359 | 0 | FileOutputStream file = new FileOutputStream(imageLocation + File.separator + imageFile); |
360 | 0 | BufferedOutputStream out = new BufferedOutputStream(file); |
361 | int i; |
|
362 | 0 | while ((i = in.read()) != -1) { |
363 | 0 | out.write(i); |
364 | 0 | } |
365 | 0 | out.flush(); |
366 | 0 | }catch(Exception e) { |
367 | 0 | e.printStackTrace(); |
368 | 0 | } |
369 | ||
370 | ||
371 | 0 | } |
372 | 0 | } |
373 | ||
374 | ||
375 | public List getImageUrls(Node node) { |
|
376 | 0 | ArrayList list = new ArrayList(); |
377 | 0 | DOMElementImpl doc = (DOMElementImpl)node; |
378 | 0 | NodeList imageList = doc.getElementsByTagName("img"); |
379 | ||
380 | 0 | if (imageList != null) { |
381 | 0 | for (int i=0; i<imageList.getLength(); ++i) { |
382 | 0 | Node imageNode = imageList.item(i); |
383 | ||
384 | 0 | NamedNodeMap nm = imageNode.getAttributes(); |
385 | 0 | Node attr = nm.getNamedItem("src"); |
386 | 0 | if(attr != null) { |
387 | 0 | list.add(attr.getNodeValue()); |
388 | } |
|
389 | ||
390 | } |
|
391 | } |
|
392 | 0 | return list; |
393 | } |
|
394 | ||
395 | public String getChapterId() { |
|
396 | 0 | return chapterId; |
397 | } |
|
398 | ||
399 | public void setChapterId(String chapterId) { |
|
400 | 0 | this.chapterId = chapterId; |
401 | 0 | } |
402 | ||
403 | ||
404 | public String removeExtension(String resource) { |
|
405 | 0 | int index = resource.indexOf('.'); |
406 | 0 | return resource.substring(0, index); |
407 | } |
|
408 | } |