skips = new ArrayList<String>();
substitutions = new ArrayList<Substitute>();
}
public void execute() {
Build build = getBuild();
if (prototypeFile == null) {
getConsole().error("Please specify a source web.xml file!");
throw new RuntimeException();
}
if (destinationFile == null) {
getConsole().error("Please specify a destination file!");
throw new RuntimeException();
}
// read properties file
StringBuilder parameters = new StringBuilder();
if (propertiesFile != null) {
try {
BufferedReader propertiesReader = new BufferedReader(new FileReader(propertiesFile));
Vector<Setting> settings = new Vector<Setting>();
List<String> comments = new ArrayList<String>();
String line = null;
while ((line = propertiesReader.readLine()) != null) {
if (line.length() == 0) {
comments.clear();
} else {
if (line.charAt(0) == '#') {
if (line.length() > 1) {
comments.add(line.substring(1).trim());
}
} else {
String[] kvp = line.split("=", 2);
String key = kvp[0].trim();
if (!skipKey(key)) {
Setting s = new Setting(key, kvp[1].trim(), comments);
settings.add(s);
}
comments.clear();
}
}
}
propertiesReader.close();
for (Setting setting : settings) {
for (String comment : setting.comments) {
parameters.append(MessageFormat.format(COMMENT_PATTERN, comment));
}
parameters.append(MessageFormat.format(PARAM_PATTERN, setting.name,
StringUtils.escapeForHtml(setting.value, false)));
}
} catch (Throwable t) {
getConsole().error(t);
throw new BuildException(t);
}
}
titleClass();
getConsole().key("source", prototypeFile.getAbsolutePath());
if (propertiesFile != null) {
build.getConsole().key("properties", propertiesFile.getAbsolutePath());
}
getConsole().key("generated", destinationFile.getAbsolutePath());
try {
// Read the prototype web.xml file
char[] buffer = new char[(int) prototypeFile.length()];
FileReader webxmlReader = new FileReader(prototypeFile);
webxmlReader.read(buffer);
webxmlReader.close();
String webXmlContent = new String(buffer);
// Insert the properties into the prototype web.xml
for (String stripToken : STRIP_TOKENS) {
webXmlContent = webXmlContent.replace(stripToken, "");
}
int idx = webXmlContent.indexOf(PARAMS);
StringBuilder sb = new StringBuilder();
sb.append(webXmlContent.substring(0, idx));
sb.append(parameters.toString());
sb.append(webXmlContent.substring(idx + PARAMS.length()));
String content = sb.toString();
for (Substitute sub : substitutions) {
content = content.replace(sub.token, sub.value.toString());
}
// Save the merged web.xml to the destination file
FileOutputStream os = new FileOutputStream(destinationFile, false);
os.write(content.getBytes("UTF-8"));
os.close();
} catch (Throwable t) {
build.getConsole().error(t);
throw new BuildException(t);
}
}