Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.acceptance.config; 16 : 17 : import com.google.auto.value.AutoAnnotation; 18 : import com.google.common.base.Splitter; 19 : import com.google.common.base.Strings; 20 : import com.google.common.collect.Lists; 21 : import com.google.gerrit.common.Nullable; 22 : import java.util.ArrayList; 23 : import java.util.Arrays; 24 : import java.util.HashMap; 25 : import java.util.Map; 26 : import org.eclipse.jgit.lib.Config; 27 : 28 0 : public class ConfigAnnotationParser { 29 52 : private static Splitter splitter = Splitter.on(".").trimResults(); 30 : 31 : @Nullable 32 : public static Config parse(Config base, GerritConfigs annotation) { 33 15 : if (annotation == null) { 34 0 : return null; 35 : } 36 : 37 15 : Config cfg = new Config(base); 38 15 : for (GerritConfig c : annotation.value()) { 39 15 : parseAnnotation(cfg, c); 40 : } 41 15 : return cfg; 42 : } 43 : 44 : public static Config parse(Config base, GerritConfig annotation) { 45 49 : Config cfg = new Config(base); 46 49 : parseAnnotation(cfg, annotation); 47 49 : return cfg; 48 : } 49 : 50 : @Nullable 51 : public static Map<String, Config> parse(GlobalPluginConfigs annotation) { 52 1 : if (annotation == null || annotation.value().length < 1) { 53 0 : return null; 54 : } 55 : 56 1 : HashMap<String, Config> result = new HashMap<>(); 57 : 58 1 : for (GlobalPluginConfig c : annotation.value()) { 59 1 : String pluginName = c.pluginName(); 60 : Config config; 61 1 : if (result.containsKey(pluginName)) { 62 1 : config = result.get(pluginName); 63 : } else { 64 1 : config = new Config(); 65 1 : result.put(pluginName, config); 66 : } 67 1 : parseAnnotation(config, toGerritConfig(c)); 68 : } 69 : 70 1 : return result; 71 : } 72 : 73 : @Nullable 74 : public static Map<String, Config> parse(GlobalPluginConfig annotation) { 75 1 : if (annotation == null) { 76 0 : return null; 77 : } 78 1 : Map<String, Config> result = new HashMap<>(); 79 1 : Config cfg = new Config(); 80 1 : parseAnnotation(cfg, toGerritConfig(annotation)); 81 1 : result.put(annotation.pluginName(), cfg); 82 1 : return result; 83 : } 84 : 85 : private static GerritConfig toGerritConfig(GlobalPluginConfig annotation) { 86 1 : return newGerritConfig(annotation.name(), annotation.value(), annotation.values()); 87 : } 88 : 89 : @AutoAnnotation 90 : private static GerritConfig newGerritConfig(String name, String value, String[] values) { 91 1 : return new AutoAnnotation_ConfigAnnotationParser_newGerritConfig(name, value, values); 92 : } 93 : 94 : private static void parseAnnotation(Config cfg, GerritConfig c) { 95 52 : ArrayList<String> l = Lists.newArrayList(splitter.split(c.name())); 96 52 : if (l.size() == 2) { 97 50 : if (!Strings.isNullOrEmpty(c.value())) { 98 49 : cfg.setString(l.get(0), null, l.get(1), c.value()); 99 : } else { 100 7 : String[] values = c.values(); 101 7 : cfg.setStringList(l.get(0), null, l.get(1), Arrays.asList(values)); 102 7 : } 103 10 : } else if (l.size() == 3) { 104 10 : if (!Strings.isNullOrEmpty(c.value())) { 105 10 : cfg.setString(l.get(0), l.get(1), l.get(2), c.value()); 106 : } else { 107 2 : cfg.setStringList(l.get(0), l.get(1), l.get(2), Arrays.asList(c.values())); 108 : } 109 : } else { 110 0 : throw new IllegalArgumentException( 111 : "GerritConfig.name must be of the format section.subsection.name or section.name"); 112 : } 113 52 : } 114 : }