diff options
author | David Gageot <david@gageot.net> | 2012-09-25 15:58:42 +0200 |
---|---|---|
committer | David Gageot <david@gageot.net> | 2012-09-25 15:59:38 +0200 |
commit | dad576683bcc8aa250e4c018375e86c7dc6dbd61 (patch) | |
tree | 8b5913b5ee2e49c083e43f51ca87153bd4ed776a /sonar-plugin-api/src/main/java/org/sonar/api | |
parent | d86f29b63dc617efc57b81c9305f5420390e061c (diff) | |
download | sonarqube-dad576683bcc8aa250e4c018375e86c7dc6dbd61.tar.gz sonarqube-dad576683bcc8aa250e4c018375e86c7dc6dbd61.zip |
SONAR-3529 prepare for json parsing of property sets
Diffstat (limited to 'sonar-plugin-api/src/main/java/org/sonar/api')
4 files changed, 109 insertions, 11 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/Property.java b/sonar-plugin-api/src/main/java/org/sonar/api/Property.java index 64037142ac2..ca2c7270699 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/Property.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/Property.java @@ -104,5 +104,5 @@ public @interface Property { * * @since 3.3 */ - String property_set_name() default ""; + String propertySetName() default ""; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java index 5464b2f3cdd..5c20b5c56e5 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java @@ -67,7 +67,7 @@ public final class PropertyDefinition { private boolean onModule = false; private boolean isGlobal = true; private boolean multiValues; - private String property_set_name; + private String propertySetName; private PropertyDefinition(Property annotation) { this.key = annotation.key(); @@ -81,7 +81,7 @@ public final class PropertyDefinition { this.type = fixType(annotation.key(), annotation.type()); this.options = annotation.options(); this.multiValues = annotation.multiValues(); - this.property_set_name = annotation.property_set_name(); + this.propertySetName = annotation.propertySetName(); } private static PropertyType fixType(String key, PropertyType type) { @@ -186,7 +186,7 @@ public final class PropertyDefinition { /** * @since 3.3 */ - public String getProperty_set_name() { - return property_set_name; + public String getPropertySetName() { + return propertySetName; } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetValue.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetValue.java index aadfa06a969..8df3ca267cb 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetValue.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetValue.java @@ -19,8 +19,99 @@ */ package org.sonar.api.config; +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import org.apache.commons.lang.ArrayUtils; +import org.sonar.api.utils.DateUtils; + +import java.util.Date; +import java.util.List; +import java.util.Map; + /** * @since 3.3 */ -public class PropertySetValue { +public final class PropertySetValue { + private final Map<String, String> keyValues; + + private PropertySetValue(Map<String, String> keyValues) { + this.keyValues = ImmutableMap.copyOf(keyValues); + } + + public static PropertySetValue create(Map<String, String> keyValues) { + return new PropertySetValue(keyValues); + } + + /** + * @return the field as String. If the field does not exist, then an empty string is returned. + */ + public String getString(String fieldName) { + String value = keyValues.get(fieldName); + return (value == null) ? "" : value; + } + + /** + * @return the field as int. If the field does not exist, then <code>0</code> is returned. + */ + public int getInt(String fieldName) { + String value = keyValues.get(fieldName); + return (value == null) ? 0 : Integer.parseInt(value); + } + + /** + * @return the field as boolean. If the field does not exist, then <code>false</code> is returned. + */ + public boolean getBoolean(String fieldName) { + String value = keyValues.get(fieldName); + return (value == null) ? false : Boolean.parseBoolean(value); + } + + /** + * @return the field as float. If the field does not exist, then <code>0.0</code> is returned. + */ + public float getFloat(String fieldName) { + String value = keyValues.get(fieldName); + return (value == null) ? 0f : Float.parseFloat(value); + } + + /** + * @return the field as long. If the field does not exist, then <code>0L</code> is returned. + */ + public long getLong(String fieldName) { + String value = keyValues.get(fieldName); + return (value == null) ? 0L : Long.parseLong(value); + } + + /** + * @return the field as Date. If the field does not exist, then <code>null</code> is returned. + */ + public Date getDate(String fieldName) { + String value = keyValues.get(fieldName); + return (value == null) ? null : DateUtils.parseDate(value); + } + + /** + * @return the field as Date with time. If the field does not exist, then <code>null</code> is returned. + */ + public Date getDateTime(String fieldName) { + String value = keyValues.get(fieldName); + return (value == null) ? null : DateUtils.parseDateTime(value); + } + + /** + * @return the field as an array of String. If the field does not exist, then an empty array is returned. + */ + public String[] getStringArray(String fieldName) { + String value = keyValues.get(fieldName); + if (value == null) { + return ArrayUtils.EMPTY_STRING_ARRAY; + } + + List<String> values = Lists.newArrayList(); + for (String v : Splitter.on(",").trimResults().split(value)) { + values.add(v.replace("%2C", ",")); + } + return values.toArray(new String[values.size()]); + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java index d9b55af1109..0735c91073b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java @@ -19,6 +19,8 @@ */ package org.sonar.api.config; +import com.google.common.collect.Iterables; + import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.base.Strings; @@ -33,6 +35,8 @@ import org.sonar.api.utils.DateUtils; import javax.annotation.Nullable; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Project Settings on batch side, Global Settings on server side. This component does not access to database, so @@ -186,14 +190,17 @@ public class Settings implements BatchComponent, ServerComponent { throw new IllegalArgumentException("Property " + key + " is not of type PROPERTY_SET"); } - String propertySetValueName = getString(key); - - // read json for given key - // search value for given propertySetValueName + String propertySetName = property.getPropertySetName(); + String valueName = getString(key); + String propertySetJson = getString("sonar.property_set." + propertySetName); - return null; + return PropertySetValue.create(lowTechJsonParsing(valueName, propertySetJson)); } + private static Map<String, String> lowTechJsonParsing(String valueName, String json) { + return Maps.newHashMap(); + } + /** * Value is split by carriage returns. * |