Stardrad Yin
2013-01-23 aefb59ef98522ef8a10ab64b25840f11e622e1c7
src/com/gitblit/build/BuildWebXml.java
@@ -29,9 +29,15 @@
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
import com.gitblit.Keys;
import com.gitblit.Keys.server;
import com.gitblit.utils.StringUtils;
/**
 * Builds the Gitblit WAR web.xml file by merging the Gitblit GO web.xml file
 * with the gitblit.properties comments, settings, and values.
 *
 * @author James Moger
 *
 */
public class BuildWebXml {
   private static final String PARAMS = "<!-- PARAMS -->";
@@ -54,43 +60,44 @@
   }
   private static void generateWebXml(Params params) throws Exception {
      StringBuilder parameters = new StringBuilder();
      // Read the current Gitblit properties
      BufferedReader propertiesReader = new BufferedReader(new FileReader(new File(
            params.propertiesFile)));
      if (params.propertiesFile != null) {
         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);
               }
         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();
         propertiesReader.close();
      StringBuilder parameters = new StringBuilder();
      for (Setting setting : settings) {
         for (String comment : setting.comments) {
            parameters.append(MessageFormat.format(COMMENT_PATTERN, comment));
         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)));
         }
         parameters.append(MessageFormat.format(PARAM_PATTERN, setting.name, StringUtils.escapeForHtml(setting.value, false)));
      }
      // Read the prototype web.xml file
      File webxml = new File(params.sourceFile);
      char[] buffer = new char[(int) webxml.length()];
@@ -119,6 +126,9 @@
      return key.startsWith(Keys.server._ROOT);
   }
   /**
    * Setting represents a setting and its comments from the properties file.
    */
   private static class Setting {
      final String name;
      final String value;
@@ -131,17 +141,20 @@
      }
   }
   /**
    * JCommander Parameters class for BuildWebXml.
    */
   @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)
      @Parameter(names = { "--propertiesFile" }, description = "Properties settings file")
      public String propertiesFile;
      @Parameter(names = { "--destinationFile" }, description = "Destination web.xml file", required = true)
      public String destinationFile;
   }
}