Skip to content

Instantly share code, notes, and snippets.

@ae6rt
Created October 18, 2013 01:56
Show Gist options
  • Save ae6rt/7035322 to your computer and use it in GitHub Desktop.
Save ae6rt/7035322 to your computer and use it in GitHub Desktop.
How to parse a Spring beans file using JDOM2
private void run(File srcDir) throws IOException, JDOMException {
List<String> mapperClassNames = new ArrayList<String>();
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new FileReader(new File(srcDir, "src/main/resources/dataAccessContext.xml")));
Element rootElement = document.getRootElement();
Namespace namespace = Namespace.getNamespace("http://www.springframework.org/schema/beans");
List<Element> beans = rootElement.getChildren("bean", namespace);
for (Element e : beans) {
List<Element> property = e.getChildren("property", namespace);
for (Element p : property) {
Attribute name = p.getAttribute("name");
if (name != null) {
if (name.getValue().equals("mapperInterface")) {
mapperClassNames.add(p.getAttribute("value").getValue());
}
}
}
}
File daoProjectDir = new File(srcDir.getParentFile(), "dao");
for (String mapperClassName : mapperClassNames) {
String relativePath = String.format("src/main/java/%s.java", mapperClassName.replaceAll("\\.", "/"));
File existingMapperFile = new File(srcDir, relativePath);
assert existingMapperFile.exists();
File destinationMapperFile = new File(daoProjectDir, relativePath);
destinationMapperFile.getParentFile().mkdirs();
System.out.println(destinationMapperFile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment