如何在Java中从应用程序提取所有字符串

4
在Android应用程序中,所有字符串值都是硬编码的(标签、对话框标题、按钮等)。我的任务是将所有这些字符串提取到资源文件中。是否有一种方法可以从应用程序中提取所有字符串,而无需手动浏览代码并进行大量复制和粘贴?可以使用正则表达式吗?我正在考虑编写一个类似于“.*”的模式。或通过代码解析的方式实现?
编辑:我知道在Eclipse中可外部化字符串,但它会创建.properties文件,而我需要一个.xml文件。所以,再次转换为.xml文件需要一些努力。
我正在考虑编写一个简单的程序,以提取所有字符串及其所在类的名称。

看来你自己解决了问题 :P。 - Mike Lentini
c/p is for copy/paste :) - Maggie
2个回答

3

谢谢,我知道这个。但它创建的是.properties文件,不太符合我的需求。Android使用xml文件作为字符串资源。所以,我需要将.properties文件转换为.xml文件,这也是一个麻烦。 - Maggie
我在Eclipse中找不到它,但在注释中写道我需要升级我的adt版本。谢谢,你是最棒的 :) - Maggie

1
我写了一个简短的程序来帮助我实现这个目标。我有一个包含一百多个字符串的文件,所以在每一行中按下Ctrl+1 --> Enter会太麻烦了。
这个小程序将你的.java文件的文件位置作为输入,并将信息放到控制台上进行复制和粘贴。可以想象将其改进为遍历文件系统并对所有.java文件执行此操作,但对于我的目的来说已经足够了...
package de.panschk.androidutil;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Set;

public class ExtractStringHelper {

public static void main (String[] args) throws IOException {
    String fileName = "LOCATION OF .JAVA FILE";
    FileInputStream fis = new FileInputStream(fileName);
    streamToStringReplaceEntities(fis);
    fis.close();

}

private static Set<String> varNames= new HashSet<String>();
public static void streamToStringReplaceEntities(InputStream is)
        throws IOException {
    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayOutputStream codeOut = new ByteArrayOutputStream();
    StringBuffer xmlOut = new StringBuffer();
    boolean inLineComment = false;
    boolean inMultilineComment = false;
    boolean inQuotes = false;
    char lastChar = ' ';
    ByteArrayOutputStream stringContent = new ByteArrayOutputStream();
    int result = bis.read();
    while (result != -1) {
        boolean inComment = inLineComment || inMultilineComment;
        byte b = (byte) result;
        // read next byte
        result = bis.read();
        boolean copyCharToBuffer = true;
        if (!inQuotes && !inComment && b == '"') {
            stringContent = new ByteArrayOutputStream();
            inQuotes = true;
            copyCharToBuffer = false;
        } else if (inQuotes && b == '"') {
            String content = stringContent.toString("UTF-8");
            String varName = makeVariableName(content);
            inQuotes = false;
            addXMLContent(varName, content, xmlOut);
            addCodeContent(varName, codeOut);
            copyCharToBuffer = false;

        } else if (inQuotes) {
            copyCharToBuffer = false;
            stringContent.write(b);

        } else if (!inComment && !inQuotes && lastChar == '/' && b == '/') {
            inLineComment = true;
        } else if (!inComment && !inQuotes && lastChar == '/' && b == '*') {
            inMultilineComment = true;
        } else if (inLineComment && b == '\n') {
            inLineComment = false;
        } else if (inMultilineComment && lastChar == '*' && b == '/') {
            inMultilineComment = false;
        }

        if (copyCharToBuffer) {
            codeOut.write(b);
        }
        lastChar = (char) b;
    }

    System.out.println(codeOut.toString("UTF-8"));
    System.out.println(xmlOut.toString());

}

private static void addCodeContent(String varName,
        ByteArrayOutputStream codeOut) throws IOException {
    String contentToAdd = "getResources().getString(R.string."+varName+")";
    byte[] bytes;
    try {
        bytes = contentToAdd.getBytes("UTF-8");
        codeOut.write(bytes);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

}

private static void addXMLContent(String varName, String content,
        StringBuffer xmlOut) {
    if (!varNames.contains(varName)) {
        content = content.replace("'", "\\'");
        xmlOut.append("    <string name=\"").append(varName).append("\">").append(content).append("</string>\n");
        varNames.add(varName);
    }

}

static String makeVariableName(String s) {
    s = s.replace(' ', '_');
    s = s.replaceAll("[^A-Za-z0-9_]", "").toLowerCase();
    return s;
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接