From: James Moger Date: Sat, 18 Jun 2011 13:02:00 +0000 (-0400) Subject: Improved web.xml builder. Now it includes the parameter comments. X-Git-Tag: v0.5.0~8 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=669686f6118544bf2122f1e7f3e39c5dbc9654f5;p=gitblit.git Improved web.xml builder. Now it includes the parameter comments. --- diff --git a/src/com/gitblit/BuildWebXml.java b/src/com/gitblit/BuildWebXml.java index 3b687f6d..557c6a85 100644 --- a/src/com/gitblit/BuildWebXml.java +++ b/src/com/gitblit/BuildWebXml.java @@ -15,51 +15,72 @@ */ package com.gitblit; +import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.text.MessageFormat; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.Properties; +import java.util.Vector; public class BuildWebXml { private static final String PARAMS = ""; - - private static final String [] STRIP_TOKENS = { "" }; + + private static final String[] STRIP_TOKENS = { "" }; + + private static final String COMMENT_PATTERN = "\n\t"; private static final String PARAM_PATTERN = "\n\t\n\t\t{0}\n\t\t{1}\n\t\n"; public static void main(String[] args) throws Exception { // Read the current Gitblit properties - // TODO extract the comments and inject them into web.xml too - FileInputStream fis = new FileInputStream(new File("distrib/gitblit.properties")); - Properties fileSettings = new Properties(); - fileSettings.load(fis); - fis.close(); - List keys = new ArrayList(fileSettings.stringPropertyNames()); - Collections.sort(keys); - + BufferedReader propertiesReader = new BufferedReader(new FileReader(new File( + "distrib/gitblit.properties"))); + + Vector settings = new Vector(); + List comments = new ArrayList(); + 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(); + StringBuilder parameters = new StringBuilder(); - for (String key : keys) { - if (!skipKey(key)) { - String value = fileSettings.getProperty(key); - parameters.append(MessageFormat.format(PARAM_PATTERN, key, value)); + + for (Setting setting : settings) { + for (String comment : setting.comments) { + parameters.append(MessageFormat.format(COMMENT_PATTERN, comment)); } + parameters.append(MessageFormat.format(PARAM_PATTERN, setting.name, setting.value)); } // Read the prototype web.xml file File webxml = new File("src/WEB-INF/web.xml"); - char [] buffer = new char[(int) webxml.length()]; - FileReader reader = new FileReader(webxml); - reader.read(buffer); - reader.close(); + char[] buffer = new char[(int) webxml.length()]; + FileReader webxmlReader = new FileReader(webxml); + webxmlReader.read(buffer); + webxmlReader.close(); String webXmlContent = new String(buffer); // Insert the Gitblit properties into the prototype web.xml - for (String stripToken:STRIP_TOKENS) { + for (String stripToken : STRIP_TOKENS) { webXmlContent = webXmlContent.replace(stripToken, ""); } int idx = webXmlContent.indexOf(PARAMS); @@ -77,4 +98,16 @@ public class BuildWebXml { private static boolean skipKey(String key) { return key.startsWith(Keys.server._ROOT); } + + private static class Setting { + final String name; + final String value; + final List comments; + + Setting(String name, String value, List comments) { + this.name = name; + this.value = value; + this.comments = new ArrayList(comments); + } + } }