summaryrefslogtreecommitdiff
path: root/sources/meta/util/AbstractFileExpander.java
blob: 580fa08bd8c7b01a52da3c17ddfbb0edf186eacc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package meta.util;

import java.io.File;

/** A base class for file expanders. */
public abstract class AbstractFileExpander {

    //########################################################################
    // Private Constants

    /** The meta package */
    private static final String meta = "meta";

    /** The meta name prefix */
    private static final String Meta = "Meta";

    //########################################################################
    // Public Methods

    /** Returns the TextWriter in which this expander writes. */
    public abstract TextWriter getTextWriter();

    /**
     * Returns the package associated with this expander or null if
     * there no such package. The default implementation returns the
     * package of this instance's class. If the outermost package is
     * named "meta", this package is omitted.
     */
    public String getPackage() {
        String fullname = getClass().getName();
        int end = fullname.lastIndexOf('.');
        if (end < 0) return null;
        int start = fullname.startsWith(meta + ".") ? meta.length() + 1 : 0;
        return fullname.substring(start, end);
    }

    /**
     * Returns the name associated with this expander. The default
     * implementation returns the name of this instance's class. If
     * that name starts with "Meta", this prefix is omitted.
     */
    public String getName() {
        String fullname = getClass().getName();
        int index = fullname.lastIndexOf('.');
        String name = index < 0 ? fullname : fullname.substring(index + 1);
        return name.startsWith(Meta) ? name.substring(Meta.length()) : name;
    }

    /**
     * Returns the directory of the target file. The default
     * implementation returns the directory corresponding to the
     * this instance's associated package.
     */
    public String getTargetDirectory() {
        String peckage = getPackage();
        return peckage == null ? "." : peckage.replace('.',File.separatorChar);
    }

    /**
     * Returns the base name of the target file. The default
     * implementation returns this instance's associated name.
     */
    public String getTargetBaseName() {
        return getName();
    }

    /**
     * Returns the suffix of the target file or null if it has no
     * suffix. The default implementation returns null.
     */
    public String getTargetSuffix() {
        return null;
    }

    /** Returns the target file. */
    public File getTargetFile(File root) {
        String suffix = getTargetSuffix();
        String name = getTargetBaseName();
        if (suffix != null) name = name + "." + suffix;
        return new File(new File(root, getTargetDirectory()), name);
    }

    /**
     * Returns the directory of the source file. The default
     * implementation returns the directory of the target file.
     */
    public String getSourceDirectory() {
        return getTargetDirectory();
    }

    /**
     * Returns the base name of the source file. The default
     * implementation returns the target base name.
     */
    public String getSourceBaseName() {
        return getTargetBaseName();
    }

    /**
     * Returns the suffix of the source file or null if it has no
     * suffix. The default implementation returns the target suffix
     * suffixed with ".tmpl".
     */
    public String getSourceSuffix() {
        String suffix = getTargetSuffix();
        return (suffix == null ? "" : suffix + ".") + "tmpl";
    }

    /** Returns the source file. */
    public File getSourceFile(File root) {
        String suffix = getSourceSuffix();
        String name = getSourceBaseName();
        if (suffix != null) name = name + "." + suffix;
        return new File(new File(root, getSourceDirectory()), name);
    }

    //########################################################################
}