Created
October 18, 2013 01:56
-
-
Save ae6rt/7035322 to your computer and use it in GitHub Desktop.
How to parse a Spring beans file using JDOM2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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