如何在Java中从应用程序中提取所有字符串
在Android应用程序中,所有字符串值都是硬编码的(标签、对话框标题、按钮等)。我的任务是将所有这些字符串提取到一个资源文件中。 不需要手动检查代码和大量的c/p,有没有一种方法可以从应用程序中提取所有字符串?使用正则表达式?我在考虑写一个模式,比如“*” 或者通过某种方式解析代码 编辑:我知道Eclipse的外部化字符串,但它会创建.properties文件,我需要的是一个.xml文件。因此,将其转换为.xml文件需要再次付出一些努力 我正在考虑编写一个简单的程序,它将提取所有字符串以及它们所在的类的名称 Eclipse提供了一个向导。对于特定于Android的解决方案:如何在Java中从应用程序中提取所有字符串,java,regex,string,Java,Regex,String,在Android应用程序中,所有字符串值都是硬编码的(标签、对话框标题、按钮等)。我的任务是将所有这些字符串提取到一个资源文件中。 不需要手动检查代码和大量的c/p,有没有一种方法可以从应用程序中提取所有字符串?使用正则表达式?我在考虑写一个模式,比如“*” 或者通过某种方式解析代码 编辑:我知道Eclipse的外部化字符串,但它会创建.properties文件,我需要的是一个.xml文件。因此,将其转换为.xml文件需要再次付出一些努力 我正在考虑编写一个简单的程序,它将提取所有字符串以及它们
希望有帮助。我写了一个简短的程序来帮助我实现这一目标。我有一个包含超过100个字符串的文件,所以在每一行中按Ctrl+1-->Enter键会非常麻烦 这个小程序将.java文件的文件位置作为输入,并将信息放在控制台中供C&p使用。人们可以对其进行图像改进,以便在文件系统中爬行并对所有.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;
}
package de.panschk.androidutil;
导入java.io.BufferedInputStream;
导入java.io.ByteArrayOutputStream;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.UnsupportedEncodingException;
导入java.util.HashSet;
导入java.util.Set;
公共类提取器帮助器{
公共静态void main(字符串[]args)引发IOException{
String fileName=“.JAVA文件的位置”;
FileInputStream fis=新的FileInputStream(文件名);
StreamToStringReplace实体(FI);
fis.close();
}
私有静态集varNames=newhashset();
公共静态void streamtostringresplaceentities(InputStream为)
抛出IOException{
BufferedInputStream bis=新的BufferedInputStream(is);
ByteArrayOutputStream codeOut=新建ByteArrayOutputStream();
StringBuffer xmlOut=新的StringBuffer();
布尔inLineComment=false;
布尔inmultilecomment=false;
布尔inQuotes=false;
char lastChar='';
ByteArrayOutputStream stringContent=新建ByteArrayOutputStream();
int result=bis.read();
而(结果!=-1){
布尔不相容=inLineComment | | inMultilineComment;
字节b=(字节)结果;
//读取下一个字节
结果=bis.read();
布尔值copyCharToBuffer=true;
如果(!inQuotes&&!inComment&&b=='''”){
stringContent=newbytearrayoutputstream();
inQuotes=true;
copyCharToBuffer=false;
}else if(引号和&b==''“')){
字符串内容=stringContent.toString(“UTF-8”);
字符串varName=makeVariableName(内容);
inQuotes=false;
addXMLContent(varName,content,xmlOut);
addCodeContent(varName,codeOut);
copyCharToBuffer=false;
}else if(引号中){
copyCharToBuffer=false;
编写(b);
}如果(!inComment&&!inQuotes&&lastChar='/'&&b='/'),则为else{
inLineComment=true;
}如果(!inComment&&!inQuotes&&lastChar='/'&&b='*'),则为else{
InMultilecomment=true;
}else if(inLineComment&&b=='\n'){
inLineComment=false;
}else if(InMultilecomment&&lastChar='*'&&b=='/')){
InMultilecomment=false;
}
如果(复制图表缓冲){
编码输出。写入(b);
}
lastChar=(char)b;
}
System.out.println(codeOut.toString(“UTF-8”);
System.out.println(xmlOut.toString());
}
私有静态void addCodeContent(字符串varName,
ByteArrayOutputStream代码输出)引发IOException{
String contentToAdd=“getResources().getString(R.String.+varName+”);
字节[]字节;
试一试{
字节=contentToAdd.getBytes(“UTF-8”);
codeOut.write(字节);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
}
私有静态void addXMLContent(String varName、String content、,
StringBuffer(XML输出){
如果(!varNames.contains(varName)){
content=content.replace(“'”,“\\”);
xmlOut.append(“”).append(content.append(“\n”);
添加(varName);
}
}
静态字符串makeVariableName(字符串s){
s=s。替换(“”,“”);
s=s.replaceAll(“[^A-Za-z0-9!],”).toLowerCase();
返回s;
}
}我想你解决了自己的问题:p.c/p是复制/粘贴:)Thanx,我知道这一点。但它创建了.properties文件,这并不是我所需要的。Android使用xml文件作为字符串资源。因此,我需要将.properties文件转换为.xml文件,这也是一个问题。我在Eclipse中找不到它,但在注释中它说我需要升级adt版本。谢谢,你是最棒的:)