flex生成po类get,set方法

为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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值