From 669686f6118544bf2122f1e7f3e39c5dbc9654f5 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Sat, 18 Jun 2011 09:02:00 -0400
Subject: [PATCH] Improved web.xml builder. Now it includes the parameter comments.

---
 src/com/gitblit/BuildWebXml.java |   79 ++++++++++++++++++++++++++++-----------
 1 files changed, 56 insertions(+), 23 deletions(-)

diff --git a/src/com/gitblit/BuildWebXml.java b/src/com/gitblit/BuildWebXml.java
index 3b687f6..557c6a8 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 = "<!-- PARAMS -->";
-	
-	private static final String [] STRIP_TOKENS = { "<!-- STRIP", "STRIP -->" };
+
+	private static final String[] STRIP_TOKENS = { "<!-- STRIP", "STRIP -->" };
+
+	private static final String COMMENT_PATTERN = "\n\t<!-- {0} -->";
 
 	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";
 
 	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<String> keys = new ArrayList<String>(fileSettings.stringPropertyNames());
-		Collections.sort(keys);
-		
-		StringBuilder parameters = new StringBuilder();
-		for (String key : keys) {
-			if (!skipKey(key)) {
-				String value = fileSettings.getProperty(key);
-				parameters.append(MessageFormat.format(PARAM_PATTERN, key, value));
+		BufferedReader propertiesReader = new BufferedReader(new FileReader(new File(
+				"distrib/gitblit.properties")));
+
+		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();
+
+		StringBuilder parameters = new StringBuilder();
+
+		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 @@
 	private static boolean skipKey(String key) {
 		return key.startsWith(Keys.server._ROOT);
 	}
+
+	private static class Setting {
+		final String name;
+		final String value;
+		final List<String> comments;
+
+		Setting(String name, String value, List<String> comments) {
+			this.name = name;
+			this.value = value;
+			this.comments = new ArrayList<String>(comments);
+		}
+	}
 }

--
Gitblit v1.9.1