2
* Copyright 2003-2010 the original author or authors.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
package org.codehaus.groovy.tools.groovydoc;
19
import java.util.Iterator;
20
import java.util.Properties;
22
import org.codehaus.groovy.groovydoc.GroovyClassDoc;
23
import org.codehaus.groovy.groovydoc.GroovyPackageDoc;
24
import org.codehaus.groovy.groovydoc.GroovyRootDoc;
27
* Write GroovyDoc resources to destination.
29
public class GroovyDocWriter {
30
private GroovyDocTool tool;
31
private OutputTool output;
32
private GroovyDocTemplateEngine templateEngine;
33
private static final String FS = "/";
34
private Properties properties;
36
public GroovyDocWriter(GroovyDocTool tool, OutputTool output, GroovyDocTemplateEngine templateEngine, Properties properties) {
39
this.templateEngine = templateEngine;
40
this.properties = properties;
43
public void writeClasses(GroovyRootDoc rootDoc, String destdir) throws Exception {
44
for (GroovyClassDoc classDoc : rootDoc.classes()) {
45
writeClassToOutput(classDoc, destdir);
49
public void writeClassToOutput(GroovyClassDoc classDoc, String destdir) throws Exception {
50
if (classDoc.isPublic() || classDoc.isProtected() && "true".equals(properties.getProperty("protectedScope")) ||
51
classDoc.isPackagePrivate() && "true".equals(properties.getProperty("packageScope")) || "true".equals(properties.getProperty("privateScope"))) {
52
String destFileName = destdir + FS + classDoc.getFullPathName() + ".html";
53
System.out.println("Generating " + destFileName);
54
String renderedSrc = templateEngine.applyClassTemplates(classDoc);
55
output.writeToOutput(destFileName, renderedSrc);
59
public void writePackages(GroovyRootDoc rootDoc, String destdir) throws Exception {
60
for (GroovyPackageDoc packageDoc : rootDoc.specifiedPackages()) {
61
if (new File(packageDoc.name()).isAbsolute()) continue;
62
output.makeOutputArea(destdir + FS + packageDoc.name());
63
writePackageToOutput(packageDoc, destdir);
65
StringBuilder sb = new StringBuilder();
66
for (GroovyPackageDoc packageDoc : rootDoc.specifiedPackages()) {
67
sb.append(packageDoc.nameWithDots());
70
String destFileName = destdir + FS + "package-list";
71
System.out.println("Generating " + destFileName);
72
output.writeToOutput(destFileName, sb.toString());
75
public void writePackageToOutput(GroovyPackageDoc packageDoc, String destdir) throws Exception {
76
Iterator<String> templates = templateEngine.packageTemplatesIterator();
77
while (templates.hasNext()) {
78
String template = templates.next();
79
String renderedSrc = templateEngine.applyPackageTemplate(template, packageDoc);
80
String destFileName = destdir + FS + packageDoc.name() + FS + tool.getFile(template);
81
System.out.println("Generating " + destFileName);
82
output.writeToOutput(destFileName, renderedSrc);
86
public void writeRoot(GroovyRootDoc rootDoc, String destdir) throws Exception {
87
output.makeOutputArea(destdir);
88
writeRootDocToOutput(rootDoc, destdir);
91
public void writeRootDocToOutput(GroovyRootDoc rootDoc, String destdir) throws Exception {
92
Iterator<String> templates = templateEngine.docTemplatesIterator();
93
while (templates.hasNext()) {
94
String template = templates.next();
95
String destFileName = destdir + FS + tool.getFile(template);
96
System.out.println("Generating " + destFileName);
97
if (hasBinaryExtension(template)) {
98
templateEngine.copyBinaryResource(template, destFileName);
100
String renderedSrc = templateEngine.applyRootDocTemplate(template, rootDoc);
101
output.writeToOutput(destFileName, renderedSrc);
106
private boolean hasBinaryExtension(String template) {
107
return template.endsWith(".gif") || template.endsWith(".ico");