当前位置: 首页 > 工具软件 > BeanGenerator > 使用案例 >

BeanGenerator_3

贲俊才
2023-12-01
	/**
	 * Step 1:根据beanExcelPath路径,导入所有Bean到beans
	 */
	private void importBeans() {
		Bean bean = null;
		//获取页签个数,从第二个页签开始,每个页签为一个bean
		int sum = this.hssfWorkbook.getNumberOfSheets();
		for (int excelBeanIndex = 1; excelBeanIndex < sum; excelBeanIndex++) {
			//将页签内容解析生成bean
			bean = importBean(excelBeanIndex);
			beans.add(bean);
		}
	}

	private Bean importBean(int excelBeanIndex) {
		Bean bean = null;
		String beanName = null;
		String beanNote = null;
		String since = null;
		String version = null;
		String author = null;
		List<Parameter> parameters = new ArrayList<Parameter>();
		Parameter parameter = null;
		String parameterName = null;
		String parameterNote = null;
		String parameterClazz = null;
		int beginParameter = 1;

		hssfSheet=hssfWorkbook.getSheetAt(excelBeanIndex);
		beanName = hssfWorkbook.getSheetName(excelBeanIndex);
		beanNote = cellValue(0, 1);
		since = Util.date2String(new Date(), "yyyy/MM/dd");
		version = cellValue(0, 3);
		author = cellValue(0, 4);

		while (true) {
			parameterName = cellValue(beginParameter+1, 0);
			if (Util.isNullOrEmpty(parameterName))
				break;
			parameterNote = cellValue(beginParameter+1, 4);
			// TODO 数据库字段类型转换成java基本类型
			parameterClazz = cellValue(beginParameter+1, 1);
			parameter = new Parameter(parameterName, parameterNote,
					parameterClazz);
			parameters.add(parameter);
			beginParameter++;
		}

		bean = new Bean(beanName, beanNote, since, version, author, parameters);
		return bean;
	}

	/**
	 * Step 2:根据模板地址,返回模板的字符串
	 * @param templatePath 某一个模板地址
	 * @return 读出模板的所有字符
	 */
	private String importTemplate(String templatePath, Bean bean) {
		String s, temp = new String(), result = new String();
		try {
			bufferedReader = new BufferedReader(new FileReader(templatePath));
			while ((s = bufferedReader.readLine()) != null) {
				if (s.indexOf(PARAMETER_FOR_BEGIN) > 0) {
					while (true) {
						s = bufferedReader.readLine();
						if (s == null || s.indexOf(PARAMETER_FOR_END) > 0)
							break;
						temp += s + "\n";
					}
					result += convertBean(temp, bean);
					temp = "";
				} else {
					result += convert(s, bean) + "\n";
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	private String convertBean(String s, Bean bean) {
		String result = "";
		String temp;
		List<Parameter> parameters = bean.getParameters();
		for (Parameter parameter : parameters) {
			temp = s;
			result += converParameter(temp, parameter);
		}
		return result;
	}

 

 类似资料:

相关阅读

相关文章

相关问答