Based on the description of the include task, it works very much like an entity includes with two additional features:
- target overriding
- special properties
For the purposes of viewing the "effective build" I don't think the special properties processing is required (though it could be added by iterating the inserted targets). So the processing to achieve this becomes.
- Parse the build.xml to a DOM
- For each top-level include tag found (only top-level are allowed), find the referenced source file.
- Parse the referenced build.xml
- insert any content from the referenced build.xml that don't collide with those in the current file.
- Repeat step 2 for referenced build.xml file(s) until no more found
- Output the resultant DOM
You can define a custom ant task so that this processing can be defined in a task to be run from within your build. See this tutorial for more details.
Here's a basic implementation that recurses through imports and inserts the DOM elements from the referenced files. There's almost certainly a few bugs in it as I threw it together, but it should do largely what you're after:
/**
* Reads the build.xml and outputs the resolved build to stdout
*/
public static void main(String[] args) {
try {
Element root = new EffectiveBuild().parse(new File(args[0]));
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(root, System.out);
} catch (Exception e) {
// TODO handle errors
e.printStackTrace();
}
}
/**
* Get the DOM for the passed file and iterate all imports, replacing with
* non-duplicate referenced content
*/
private Element parse(File buildFile) throws JDOMException, IOException {
Element root = getRootElement(buildFile);
List<Element> imports = root.getChildren("import");
for (int i = 0; i < imports.size(); i++) {
Element element = imports.get(i);
List<Content> importContent = parseImport(element, root, buildFile);
int replaceIndex = root.indexOf(element);
root.addContent(replaceIndex, importContent);
root.removeContent(element);
}
root.removeChildren("import");
return root;
}
/**
* Get the imported file and merge it into the parent.
*/
private List<Content> parseImport(Element element, Element currentRoot,
File buildFile) throws JDOMException, IOException {
String importFileName = element.getAttributeValue("file");
File importFile = new File(buildFile.getParentFile(), importFileName)
.getAbsoluteFile();
if (importFileName != null) {
Element importRoot = getRootElement(importFile);
return getImportContent(element, currentRoot, importRoot,
importFile);
}
return Collections.emptyList();
}
/**
* Replace the passed element with the content of the importRoot
* (not the project tag)
*/
private List<Content> getImportContent(Element element,
Element currentRoot, Element importRoot, File buildFile)
throws JDOMException, IOException {
if (currentRoot != null) {
// copy all the reference import elements to the parent if needed
List<Content> childNodes = importRoot.cloneContent();
List<Content> importContent = new ArrayList<Content>();
for (Content content : childNodes) {
if (content instanceof Element
&& ((Element) content).getName().equals("import")) {
importContent.addAll(parseImport((Element) content,
currentRoot, buildFile));
}
if (!existsInParent(currentRoot, content)) {
importContent.add(content);
} else {
// TODO note the element was skipped
}
}
return importContent;
}
return Collections.emptyList();
}
/**
* Return true if the content already defined in the parent
*/
private boolean existsInParent(Element parent, Content content) {
if (content instanceof Text) {
if (((Text) content).getText().trim().length() == 0) {
// let the pretty printer deal with the whitespace
return false;
}
return true;
}
if (content instanceof Element) {
String id = ((Element) content).getAttributeValue("name");
String name = ((Element) content).getName();
List<Content> parentContent = parent.getChildren();
if (id != null) {
for (Content content2 : parentContent) {
if (content2 instanceof Element
&& ((Element) content2).getName().equals(name)) {
String parentId = ((Element) content2)
.getAttributeValue("name");
if (parentId != null && parentId.equals(id)) {
return true;
}
}
}
}
}
return false;
}
/**
* Parse the passed file.
*/
private Element getRootElement(File buildFile) throws JDOMException,
IOException {
SAXBuilder builder = new SAXBuilder();
builder.setValidation(false);
builder.setIgnoringElementContentWhitespace(true);
Document doc = builder.build(buildFile);
Element root = doc.getRootElement();
return root;
}