0

我想使用 ANT build 将.properties文件转换为文本文件(代码)。

输入文件my.properties

FOO=foo bar
BAR=bar bar foo

所需的输出文件Constants.as

package foo.bar {
    public final class Constants {
        public static const FOO:String = "FOO";
        public static const BAR:String = "BAR";
    }
}

注意:ActionScript 不能使用像 Java 这样的属性文件,所以我需要将此翻译用作预构建任务。我的属性文件是本地化的,我希望将键作为常量。

4

2 回答 2

1

您可以通过在concat任务中包含过滤器链来做到这一点。

        <concat destfile="Constants.as">

            <header filtering="false">
package foo.bar {
    public final class Constants {
</header>
            <footer filtering="false">
    }
}
</footer>

            <fileset file="my.properties"/>
            <filterchain>
                <tokenfilter>
                    <ignoreblank/>
                    <trim/>
                    <replaceregex pattern="(.*)=(.*)" replace="        public static const \1:String = &#34;\2&#34;;"/>
                </tokenfilter>
            </filterchain>

        </concat>
于 2012-06-07T08:41:27.403 回答
0

我已经设法scriptdefJavaScript中破解了它。

任何想法如何做得更好?

<scriptdef name="localization-regen" language="javascript">
    <attribute name="input.file.path"/>
    <attribute name="output.file.category"/>
    <attribute name="output.file.package"/>
    <attribute name="output.file.dir"/>
    <attribute name="output.property"/>
    <![CDATA[

        importPackage(java.io);

        inputFilePath = attributes.get("input.file.path");
        outputFileCategory = attributes.get("output.file.category");
        outputFilePackage = attributes.get("output.file.package");
        outputFileDir = attributes.get("output.file.dir");

        outputFileClass = "LocalizationKeys" + outputFileCategory.substring(0, 1).toUpperCase() + outputFileCategory.substring(1, outputFileCategory.length());


        inputFile = new File(inputFilePath);

        inputStream = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));

        outputLines = "";

        for(line = inputStream.readLine(); line != null; line = inputStream.readLine())
        {
            vars = line.split("=");
            c = vars[0];
            if(c.substring(0, 1) == "#") continue;
            outputLines += "\t\tpublic static const " + c + ":String = \"" + c + "\";\n";
        }

        output = "";
        output  = "package " + outputFilePackage + "\n"
                + "{\n"
                + "\tpublic final class " + outputFileClass + "\n"
                + "\t{\n"
                + outputLines
                + "\t}\n"
                + "}";


        outputFilePath = outputFileDir + "/" + outputFileClass+ ".as";

        out = new BufferedWriter(new FileWriter(outputFilePath));
        out.write(output, 0, output.length);
        out.flush();
        out.close();
    ]]>
</scriptdef>
于 2012-06-06T15:53:02.980 回答