model.put("webappAttributes", webAppConfig.getWebXmlAttributes());
}
processTemplate(getWebXmlTemplateURL(), model);
}
catch (TemplateException e) {
throw new EnunciateException("Error processing web.xml template file.", e);
}
File mergedWebXml = webXML;
WebAppConfig webAppConfig = getWebAppConfig();
if ((webAppConfig != null) && (webAppConfig.getMergeWebXMLURL() != null || webAppConfig.getMergeWebXML() != null)) {
URL webXmlToMerge = webAppConfig.getMergeWebXMLURL();
if (webXmlToMerge == null) {
webXmlToMerge = enunciate.resolvePath(webAppConfig.getMergeWebXML()).toURL();
}
try {
Document source1Doc = loadMergeXml(webXmlToMerge.openStream());
NodeModel.simplify(source1Doc);
Document source2Doc = loadMergeXml(new FileInputStream(webXML));
NodeModel.simplify(source2Doc);
Map<String, String> mergedAttributes = new HashMap<String, String>();
NamedNodeMap source2Attributes = source2Doc.getDocumentElement().getAttributes();
for (int i = 0; i < source2Attributes.getLength(); i++) {
mergedAttributes.put(source2Attributes.item(i).getNodeName(), source2Attributes.item(i).getNodeValue());
}
NamedNodeMap source1Attributes = source1Doc.getDocumentElement().getAttributes();
for (int i = 0; i < source1Attributes.getLength(); i++) {
mergedAttributes.put(source1Attributes.item(i).getNodeName(), source1Attributes.item(i).getNodeValue());
}
model.put("source1", NodeModel.wrap(source1Doc.getDocumentElement()));
model.put("source2", NodeModel.wrap(source2Doc.getDocumentElement()));
model.put("mergedAttributes", mergedAttributes);
processTemplate(getMergeWebXmlTemplateURL(), model);
}
catch (TemplateException e) {
throw new EnunciateException("Error while merging web xml files.", e);
}
File mergeTarget = new File(getGenerateDir(), "merged-web.xml");
if (!mergeTarget.exists()) {
throw new EnunciateException("Error: " + mergeTarget + " doesn't exist.");
}
debug("Merged %s and %s into %s...", webXmlToMerge, webXML, mergeTarget);
mergedWebXml = mergeTarget;
}
if ((webAppConfig != null) && (webAppConfig.getWebXMLTransformURL() != null || webAppConfig.getWebXMLTransform() != null)) {
URL transformURL = webAppConfig.getWebXMLTransformURL();
if (transformURL == null) {
transformURL = enunciate.resolvePath(webAppConfig.getWebXMLTransform()).toURI().toURL();
}
debug("web.xml transform has been specified as %s.", transformURL);
try {
StreamSource source = new StreamSource(transformURL.openStream());
Transformer transformer = TransformerFactory.newInstance().newTransformer(source);
debug("Transforming %s to %s.", mergedWebXml, destWebXML);
transformer.transform(new StreamSource(new FileReader(mergedWebXml)), new StreamResult(destWebXML));
}
catch (TransformerException e) {
throw new EnunciateException("Error during transformation of the web.xml (stylesheet " + transformURL + ", file " + mergedWebXml + ")", e);
}
}
else {
enunciate.copyFile(mergedWebXml, destWebXML);
}