2013년 12월 2일 월요일

중첩 loop YAML을 Bean에 넣어보자.


/**
 * yaml gluScripts 파싱내역 가져오기
 *
 * @param userConfig
 * @param RepositoryId
 * @return DeployConfig
 */
public List<GluScript> getYamlParserGluScripts() {

String yamlApiUrlContent = "";
try {
yamlApiUrlContent =
FileUtils.readFileToString(FileUtils.toFile(this.getClass().getResource(
YamlConstants.SystemConfigYaml.FILE_PATH)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

List<GluScript> gluScripts = new ArrayList<GluScript>();
if (yamlApiUrlContent != null && !yamlApiUrlContent.equals(""))
gluScripts = parseGluScripts(yamlApiUrlContent); // yaml 파싱

return gluScripts;
}

/**
 * GluScripts yaml 내용을 파싱한다.
 *
 * @param yamlContent
 * @return DeployConfig
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private List<GluScript> parseGluScripts(String yamlContent) {

if (yamlContent == null || yamlContent.equals("")) {
return null;
}

// yaml data를 Map에 넣는다.
Yaml yaml = new Yaml();

Map<String, List<Map>> data = null; // 최상단 data
Map<String, List<Map>> data2 = null; // gluScripts data
List<Map> rows = null; // 최상단 yaml list
List<Map> rows2 = null; // gluScripts yaml list
GluScript gluScript = new GluScript(); // gluScripts 설정
Skeleton skeleton = new Skeleton(); // skeletons 설정

List<GluScript> gluScripts = new ArrayList<GluScript>();
List<Skeleton> skeletons = new ArrayList<Skeleton>();

data = (Map<String, List<Map>>) yaml.load(yamlContent);

try {

for (Map.Entry<String, List<Map>> ent : data.entrySet()) {
// gluScripts loop 파싱
if (ent.getKey().equals("gluScripts")) {
rows = ent.getValue();

for (Map values : rows) {
data2 = (Map<String, List<Map>>) values;

gluScript = new GluScript();
skeletons = new ArrayList<Skeleton>();
// mapping to gluScripts
BeanUtils.populate(gluScript, values);

for (Map.Entry<String, List<Map>> ent2 : data2.entrySet()) {

// skeletons loop 파싱
if (ent2.getKey().equals("skeletons")) {
rows2 = ent2.getValue();

for (Map values2 : rows2) {
skeleton = new Skeleton();
// mapping to skeletons
BeanUtils.populate(skeleton, values2);
skeletons.add(skeleton);
}
}
}
gluScript.setSkeletons(skeletons);
gluScripts.add(gluScript);
}
}
}

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

return gluScripts;

}

댓글 없음:

댓글 쓰기