From 992872af8bbbc0c36106ba59938a3ca5c4aeddc9 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Sat, 25 Jun 2011 17:01:16 -0400
Subject: [PATCH] Documentation.

---
 src/com/gitblit/BuildWebXml.java |  115 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 90 insertions(+), 25 deletions(-)

diff --git a/src/com/gitblit/BuildWebXml.java b/src/com/gitblit/BuildWebXml.java
index 3b687f6..8e957df 100644
--- a/src/com/gitblit/BuildWebXml.java
+++ b/src/com/gitblit/BuildWebXml.java
@@ -15,51 +15,90 @@
  */
 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;
+
+import com.beust.jcommander.JCommander;
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.ParameterException;
+import com.beust.jcommander.Parameters;
+import com.gitblit.utils.StringUtils;
 
 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 {
+		Params params = new Params();
+		JCommander jc = new JCommander(params);
+		try {
+			jc.parse(args);
+		} catch (ParameterException t) {
+			System.err.println(t.getMessage());
+			jc.usage();
+		}
+		generateWebXml(params);
+	}
+
+	private static void generateWebXml(Params params) 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(
+				params.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();
+
+		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, StringUtils.escapeForHtml(setting.value, false)));
 		}
 
 		// 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();
+		File webxml = new File(params.sourceFile);
+		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);
@@ -69,7 +108,7 @@
 		sb.append(webXmlContent.substring(idx + PARAMS.length()));
 
 		// Save the merged web.xml to the war build folder
-		FileOutputStream os = new FileOutputStream(new File("war/WEB-INF/web.xml"), false);
+		FileOutputStream os = new FileOutputStream(new File(params.destinationFile), false);
 		os.write(sb.toString().getBytes());
 		os.close();
 	}
@@ -77,4 +116,30 @@
 	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);
+		}
+	}
+
+	@Parameters(separators = " ")
+	private static class Params {
+
+		@Parameter(names = { "--sourceFile" }, description = "Source web.xml file", required = true)
+		public String sourceFile;
+
+		@Parameter(names = { "--propertiesFile" }, description = "Properties settings file", required = true)
+		public String propertiesFile;
+
+		@Parameter(names = { "--destinationFile" }, description = "Destination web.xml file", required = true)
+		public String destinationFile;
+
+	}
 }

--
Gitblit v1.9.1