protected ProjectTemplate parse(Resource resource) throws Exception {
Document xmlDoc = xmlParser.parse(resource);
Element projectEle = xmlDoc.getDocumentElement();
//
ProjectTemplate projectTemplate = new ProjectTemplate();
Element descriptionEle = DomUtils.getChildElementByTagName(projectEle, "description");
projectTemplate.setDescription(DomUtils.getTextValue(descriptionEle));
// project features
Element featuresEle = DomUtils.getChildElementByTagName(projectEle, "features");
List<Element> featureEles = DomUtils.getChildElementsByTagName(featuresEle, "feature");
for(Element featureEle : featureEles) {
ProjectFeature projectFeature = new ProjectFeature();
projectFeature.setCode(featureEle.getAttribute("code"));
projectFeature.setLabel(featureEle.getAttribute("label"));
projectFeature.setEnabled(true);
String level = featureEle.getAttribute("level");
if(StringUtils.hasText(level)) {
projectFeature.setLevel(Integer.parseInt(level));
}
projectTemplate.addProjectFeature(projectFeature);
}
// project attributes
Element attributesEle = DomUtils.getChildElementByTagName(projectEle, "attributes");
List<Element> attributeEles = DomUtils.getChildElementsByTagName(featuresEle, "attribute");
for(Element attributeEle : attributeEles) {
ProjectAttribute projectAttribute = new ProjectAttribute();
projectAttribute.setName(attributeEle.getAttribute("name"));
projectAttribute.setValue(attributeEle.getAttribute("value"));
projectAttribute.setType(attributeEle.getAttribute("type"));
String level = attributeEle.getAttribute("level");
if(StringUtils.hasText(level)) {
projectAttribute.setLevel(Integer.parseInt(level));
}
projectTemplate.addProjectAttribute(projectAttribute);
}
// project roles
Element rolesEle = DomUtils.getChildElementByTagName(projectEle, "roles");
List<Element> roleEles = DomUtils.getChildElementsByTagName(rolesEle, "role");
for(Element roleEle : roleEles) {
// project role
ProjectRole projectRole = new ProjectRole();
projectRole.setLabel(roleEle.getAttribute("label"));
String label = roleEle.getAttribute("level");
if(StringUtils.hasText(label)) {
projectRole.setLevel(Integer.parseInt(label));
}
// project authorities
List<ProjectAuthority> projectAuthorities = new ArrayList<ProjectAuthority>();
List<Element> _authorityEles = DomUtils.getChildElementsByTagName(roleEle, "authority");
for(Element authorityEle : _authorityEles) {
// project authority
ProjectAuthority projectAuthority = new ProjectAuthority();
projectAuthority.setCode(authorityEle.getAttribute("code"));
projectAuthority.setEnabled("true".equals(authorityEle.getAttribute("enabled")));
projectAuthorities.add(projectAuthority);
}
//
projectTemplate.addProjectRoleAuthorities(projectRole, projectAuthorities);
}
//
return projectTemplate;
}