]> source.dussan.org Git - gitblit.git/commitdiff
Improved web.xml builder. Now it includes the parameter comments.
authorJames Moger <james.moger@gitblit.com>
Sat, 18 Jun 2011 13:02:00 +0000 (09:02 -0400)
committerJames Moger <james.moger@gitblit.com>
Sat, 18 Jun 2011 13:02:00 +0000 (09:02 -0400)
src/com/gitblit/BuildWebXml.java

index 3b687f6dd19fde9855612e32fea524a3ed0e8a11..557c6a85dda23170b139b7a644feeaf071dd4b63 100644 (file)
  */\r
 package com.gitblit;\r
 \r
+import java.io.BufferedReader;\r
 import java.io.File;\r
-import java.io.FileInputStream;\r
 import java.io.FileOutputStream;\r
 import java.io.FileReader;\r
 import java.text.MessageFormat;\r
 import java.util.ArrayList;\r
-import java.util.Collections;\r
 import java.util.List;\r
-import java.util.Properties;\r
+import java.util.Vector;\r
 \r
 public class BuildWebXml {\r
        private static final String PARAMS = "<!-- PARAMS -->";\r
-       \r
-       private static final String [] STRIP_TOKENS = { "<!-- STRIP", "STRIP -->" };\r
+\r
+       private static final String[] STRIP_TOKENS = { "<!-- STRIP", "STRIP -->" };\r
+\r
+       private static final String COMMENT_PATTERN = "\n\t<!-- {0} -->";\r
 \r
        private static final String PARAM_PATTERN = "\n\t<context-param>\n\t\t<param-name>{0}</param-name>\n\t\t<param-value>{1}</param-value>\n\t</context-param>\n";\r
 \r
        public static void main(String[] args) throws Exception {\r
                // Read the current Gitblit properties\r
-               // TODO extract the comments and inject them into web.xml too\r
-               FileInputStream fis = new FileInputStream(new File("distrib/gitblit.properties"));\r
-               Properties fileSettings = new Properties();             \r
-               fileSettings.load(fis);\r
-               fis.close();\r
-               List<String> keys = new ArrayList<String>(fileSettings.stringPropertyNames());\r
-               Collections.sort(keys);\r
-               \r
+               BufferedReader propertiesReader = new BufferedReader(new FileReader(new File(\r
+                               "distrib/gitblit.properties")));\r
+\r
+               Vector<Setting> settings = new Vector<Setting>();\r
+               List<String> comments = new ArrayList<String>();\r
+               String line = null;\r
+               while ((line = propertiesReader.readLine()) != null) {\r
+                       if (line.length() == 0) {\r
+                               comments.clear();\r
+                       } else {\r
+                               if (line.charAt(0) == '#') {\r
+                                       if (line.length() > 1) {\r
+                                               comments.add(line.substring(1).trim());\r
+                                       }\r
+                               } else {\r
+                                       String[] kvp = line.split("=", 2);\r
+                                       String key = kvp[0].trim();\r
+                                       if (!skipKey(key)) {\r
+                                               Setting s = new Setting(key, kvp[1].trim(), comments);\r
+                                               settings.add(s);\r
+                                       }\r
+                                       comments.clear();\r
+                               }\r
+                       }\r
+               }\r
+               propertiesReader.close();\r
+\r
                StringBuilder parameters = new StringBuilder();\r
-               for (String key : keys) {\r
-                       if (!skipKey(key)) {\r
-                               String value = fileSettings.getProperty(key);\r
-                               parameters.append(MessageFormat.format(PARAM_PATTERN, key, value));\r
+\r
+               for (Setting setting : settings) {\r
+                       for (String comment : setting.comments) {\r
+                               parameters.append(MessageFormat.format(COMMENT_PATTERN, comment));\r
                        }\r
+                       parameters.append(MessageFormat.format(PARAM_PATTERN, setting.name, setting.value));\r
                }\r
 \r
                // Read the prototype web.xml file\r
                File webxml = new File("src/WEB-INF/web.xml");\r
-               char [] buffer = new char[(int) webxml.length()];\r
-               FileReader reader = new FileReader(webxml);\r
-               reader.read(buffer);\r
-               reader.close();\r
+               char[] buffer = new char[(int) webxml.length()];\r
+               FileReader webxmlReader = new FileReader(webxml);\r
+               webxmlReader.read(buffer);\r
+               webxmlReader.close();\r
                String webXmlContent = new String(buffer);\r
 \r
                // Insert the Gitblit properties into the prototype web.xml\r
-               for (String stripToken:STRIP_TOKENS) {\r
+               for (String stripToken : STRIP_TOKENS) {\r
                        webXmlContent = webXmlContent.replace(stripToken, "");\r
                }\r
                int idx = webXmlContent.indexOf(PARAMS);\r
@@ -77,4 +98,16 @@ public class BuildWebXml {
        private static boolean skipKey(String key) {\r
                return key.startsWith(Keys.server._ROOT);\r
        }\r
+\r
+       private static class Setting {\r
+               final String name;\r
+               final String value;\r
+               final List<String> comments;\r
+\r
+               Setting(String name, String value, List<String> comments) {\r
+                       this.name = name;\r
+                       this.value = value;\r
+                       this.comments = new ArrayList<String>(comments);\r
+               }\r
+       }\r
 }\r