为flex的as文件生成po类的set,get方法。貌似工具只支持一次产生一个,所以就写了一个小程序,支持一次生成多个set/get方法。
遇到一个问题,就是怎么在一个字符串前面,得到第一个非空格字符的位置?也就是说,想要知道第一个非空格字符前有多少个空格?这个怎么得到?注意:两个空tab,和4个空格,实现的效果是一样的,但怎么分他们呢?
package com.lee.genPO.genFlexAsPO;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
* @author painarthur
* @since 2013.11
* @version 1.0
* @share http://blog.csdn.net/paincupid
*/
public class GenFlexAsPO {
public static void genPO(String fileName) throws Exception {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
String str = tempString.trim();
if(!str.contains("var")||!str.contains(";")||str.contains("return")||str.contains("function")){
continue;
}
if(str==null||str.length()<2){
throw new Exception("不可为空,或没有冒号或数据类型");
}
int num = str.indexOf(";");
str = str.substring(0, num);
str = str.replace("private", "").replace("var", "").trim();
int num_Colon = str.indexOf(":");
String poName = str.substring(0, num_Colon);
if(poName==null||poName.equals("")
||!(poName.subSequence(0, 1).equals("_"))||poName.length()<2){
throw new Exception("不可为空,或属性前不是下划线");
}
String type = str.substring(num_Colon+1, str.length());
StringBuilder getMethod = new StringBuilder();
getMethod.append(genSpace(4)).append("public function get ").append(poName.substring(1, poName.length()))
.append("():").append(type).append("{").append("\r\n");
getMethod.append(genSpace(6)).append("return ")
.append(poName).append(";").append("\r\n").append(genSpace(4)).append("}").append("\r\n");
StringBuilder postMethod = new StringBuilder();
postMethod.append(genSpace(4)).append("public function set ").append(poName.substring(1, poName.length()))
.append("(value:").append(type).append("):void").append("{").append("\r\n")
.append(genSpace(6)).append(poName).append("=")
.append("value;").append("\r\n").append(genSpace(4)).append("}").append("\r\n");
System.out.println(getMethod);
System.out.println(postMethod);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
public static String genSpace(int num){
StringBuilder s = new StringBuilder();
if(num<=0) return "";
for(int i= 0;i<num;i++){
s.append("\u0020");
}
return s.toString();
}
public static void main(String[] args) throws Exception {
String path = "G:\\asPOgen.txt";
GenFlexAsPO g = new GenFlexAsPO();
genPO(path);
}
}
文章来自于(转载时请注明): http://blog.csdn.net/paincupid/article/details/16898211