~brian-thomason/+junk/groovy-1.7.2

« back to all changes in this revision

Viewing changes to src/main/org/codehaus/groovy/tools/groovydoc/GroovyDocWriter.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:31:12 UTC
  • Revision ID: brian.thomason@canonical.com-20111220173112-u53pbzhiy5y1hau0
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2003-2010 the original author or authors.
 
3
 *
 
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
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
package org.codehaus.groovy.tools.groovydoc;
 
17
 
 
18
import java.io.File;
 
19
import java.util.Iterator;
 
20
import java.util.Properties;
 
21
 
 
22
import org.codehaus.groovy.groovydoc.GroovyClassDoc;
 
23
import org.codehaus.groovy.groovydoc.GroovyPackageDoc;
 
24
import org.codehaus.groovy.groovydoc.GroovyRootDoc;
 
25
 
 
26
/**
 
27
 * Write GroovyDoc resources to destination.
 
28
 */
 
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;
 
35
 
 
36
    public GroovyDocWriter(GroovyDocTool tool, OutputTool output, GroovyDocTemplateEngine templateEngine, Properties properties) {
 
37
        this.tool = tool;
 
38
        this.output = output;
 
39
        this.templateEngine = templateEngine;
 
40
        this.properties = properties;
 
41
    }
 
42
 
 
43
    public void writeClasses(GroovyRootDoc rootDoc, String destdir) throws Exception {
 
44
        for (GroovyClassDoc classDoc : rootDoc.classes()) {
 
45
            writeClassToOutput(classDoc, destdir);
 
46
        }
 
47
    }
 
48
 
 
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);
 
56
        }
 
57
    }
 
58
 
 
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);
 
64
        }
 
65
        StringBuilder sb = new StringBuilder();
 
66
        for (GroovyPackageDoc packageDoc : rootDoc.specifiedPackages()) {
 
67
            sb.append(packageDoc.nameWithDots());
 
68
            sb.append("\n");
 
69
        }
 
70
        String destFileName = destdir + FS + "package-list";
 
71
        System.out.println("Generating " + destFileName);
 
72
        output.writeToOutput(destFileName, sb.toString());
 
73
    }
 
74
 
 
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);
 
83
        }
 
84
    }
 
85
 
 
86
    public void writeRoot(GroovyRootDoc rootDoc, String destdir) throws Exception {
 
87
        output.makeOutputArea(destdir);
 
88
        writeRootDocToOutput(rootDoc, destdir);
 
89
    }
 
90
 
 
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);
 
99
            } else {
 
100
                String renderedSrc = templateEngine.applyRootDocTemplate(template, rootDoc);
 
101
                output.writeToOutput(destFileName, renderedSrc);
 
102
            }
 
103
        }
 
104
    }
 
105
 
 
106
    private boolean hasBinaryExtension(String template) {
 
107
        return template.endsWith(".gif") || template.endsWith(".ico");
 
108
    }
 
109
 
 
110
}