<commit_before>//
// $Id$

package com.threerings.gwt.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import com.google.common.collect.Lists;

import com.samskivert.util.FileUtil;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;

/**
 * Does something extraordinary.
 */
public class I18nSyncTask extends Task
{
    public void addFileset (FileSet set)
    {
        _filesets.add(set);
    }

    public void setSrcdir (File srcdir)
    {
        _sourcePath = srcdir.getAbsolutePath();
    }

    @Override
    public void execute () throws BuildException
    {
        if (_sourcePath == null) {
            throw new BuildException("Missing required attribute 'srcdir'");
        }

        for (FileSet fs : _filesets) {
            DirectoryScanner ds = fs.getDirectoryScanner(getProject());
            File fromDir = fs.getDir(getProject());
            for (String srcFile : ds.getIncludedFiles()) {
                try {
                    processFile(new File(fromDir, srcFile));
                } catch (IOException ioe) {
                    throw new BuildException("Failure converting " + srcFile, ioe);
                }
            }
        }
    }

    protected void processFile (File propsFile)
        throws IOException
    {
        if (!propsFile.getName().endsWith(".properties")) {
            System.err.println("Ignoring non-properties file: " + propsFile);
            return;
        }

        File javaFile = new File(FileUtil.resuffix(propsFile, ".properties", ".java"));
        if (propsFile.lastModified() <= javaFile.lastModified()) {
            return;
        }

        String javaPath = javaFile.getAbsolutePath();
        if (!javaPath.startsWith(_sourcePath)) {
            System.err.println("Ignoring properties file outside 'srcdir': " + propsFile);
            return;
        }

        String pkg = javaPath.substring(_sourcePath.length());
        if (pkg.startsWith(File.separator)) {
            pkg = pkg.substring(1);
        }
        pkg = pkg.substring(0, pkg.indexOf(javaFile.getName()));
        if (pkg.endsWith(File.separator)) {
            pkg = pkg.substring(0, pkg.length()-1);
        }
        pkg = pkg.replace(File.separatorChar, '.');

        String clazz = javaFile.getName().substring(0, javaFile.getName().indexOf(".java"));

        System.out.println("Generating " + pkg + "." + clazz + "...");

        StringBuilder buf = new StringBuilder();
        buf.append("//\n");
        buf.append("// Generated by I18nSyncTask on ").append(new Date()).append("\n\n");
        buf.append("package ").append(pkg).append(";\n\n");
        buf.append("public interface ").append(clazz);
        buf.append(" extends com.google.gwt.i18n.client.Messages\n");
        buf.append("{\n");

        Properties props = new Properties();
        props.load(new FileInputStream(propsFile));
        Enumeration<?> names = props.propertyNames();
        for (int method = 0; names.hasMoreElements(); method++) {
            if (method > 0) {
                buf.append("\n");
            }
            String key = String.valueOf(names.nextElement());
            buf.append("    @Key(\"").append(key).append("\")\n");
            buf.append("    String ").append(keyToMethod(key)).append(" (");
            String value = props.getProperty(key);
            int idx = 0;
            while (value.indexOf("{" + idx) != -1) {
                if (idx > 0) {
                    buf.append(", ");
                }
                // TODO: make {N:integer} into an int argument, {N:percent" into a float, etc.
                buf.append("String arg").append(idx);
                idx++;
            }
            buf.append(");\n");
        }
        buf.append("}\n");

        PrintWriter out = new PrintWriter(javaFile, "UTF-8");
        out.print(buf.toString());
        out.close();
    }

    protected static String keyToMethod (String key)
    {
        return key.replace('.', '_');
    }

    /** The absolute path to the root of our source tree. */
    protected String _sourcePath;

    /** A list of filesets that contain tile images. */
    protected List<FileSet> _filesets = Lists.newArrayList();
}
<commit_msg>Fixed comment.<commit_after>//
// $Id$

package com.threerings.gwt.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import com.google.common.collect.Lists;

import com.samskivert.util.FileUtil;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;

/**
 * Does something extraordinary.
 */
public class I18nSyncTask extends Task
{
    public void addFileset (FileSet set)
    {
        _filesets.add(set);
    }

    public void setSrcdir (File srcdir)
    {
        _sourcePath = srcdir.getAbsolutePath();
    }

    @Override
    public void execute () throws BuildException
    {
        if (_sourcePath == null) {
            throw new BuildException("Missing required attribute 'srcdir'");
        }

        for (FileSet fs : _filesets) {
            DirectoryScanner ds = fs.getDirectoryScanner(getProject());
            File fromDir = fs.getDir(getProject());
            for (String srcFile : ds.getIncludedFiles()) {
                try {
                    processFile(new File(fromDir, srcFile));
                } catch (IOException ioe) {
                    throw new BuildException("Failure converting " + srcFile, ioe);
                }
            }
        }
    }

    protected void processFile (File propsFile)
        throws IOException
    {
        if (!propsFile.getName().endsWith(".properties")) {
            System.err.println("Ignoring non-properties file: " + propsFile);
            return;
        }

        File javaFile = new File(FileUtil.resuffix(propsFile, ".properties", ".java"));
        if (propsFile.lastModified() <= javaFile.lastModified()) {
            return;
        }

        String javaPath = javaFile.getAbsolutePath();
        if (!javaPath.startsWith(_sourcePath)) {
            System.err.println("Ignoring properties file outside 'srcdir': " + propsFile);
            return;
        }

        String pkg = javaPath.substring(_sourcePath.length());
        if (pkg.startsWith(File.separator)) {
            pkg = pkg.substring(1);
        }
        pkg = pkg.substring(0, pkg.indexOf(javaFile.getName()));
        if (pkg.endsWith(File.separator)) {
            pkg = pkg.substring(0, pkg.length()-1);
        }
        pkg = pkg.replace(File.separatorChar, '.');

        String clazz = javaFile.getName().substring(0, javaFile.getName().indexOf(".java"));

        System.out.println("Generating " + pkg + "." + clazz + "...");

        StringBuilder buf = new StringBuilder();
        buf.append("//\n");
        buf.append("// Generated by I18nSyncTask on ").append(new Date()).append("\n\n");
        buf.append("package ").append(pkg).append(";\n\n");
        buf.append("public interface ").append(clazz);
        buf.append(" extends com.google.gwt.i18n.client.Messages\n");
        buf.append("{\n");

        Properties props = new Properties();
        props.load(new FileInputStream(propsFile));
        Enumeration<?> names = props.propertyNames();
        for (int method = 0; names.hasMoreElements(); method++) {
            if (method > 0) {
                buf.append("\n");
            }
            String key = String.valueOf(names.nextElement());
            buf.append("    @Key(\"").append(key).append("\")\n");
            buf.append("    String ").append(keyToMethod(key)).append(" (");
            String value = props.getProperty(key);
            int idx = 0;
            while (value.indexOf("{" + idx) != -1) {
                if (idx > 0) {
                    buf.append(", ");
                }
                // TODO: make {N:integer} into an int argument, {N:percent} into a float, etc.
                buf.append("String arg").append(idx);
                idx++;
            }
            buf.append(");\n");
        }
        buf.append("}\n");

        PrintWriter out = new PrintWriter(javaFile, "UTF-8");
        out.print(buf.toString());
        out.close();
    }

    protected static String keyToMethod (String key)
    {
        return key.replace('.', '_');
    }

    /** The absolute path to the root of our source tree. */
    protected String _sourcePath;

    /** A list of filesets that contain tile images. */
    protected List<FileSet> _filesets = Lists.newArrayList();
}

{'fraction_non_alphanumeric': 0.14119879450831566, 'fraction_numerical': 0.003795066413662239, 'mean_word_length': 2.107873742629206, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2557173', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2489, 'n_words': 549}