extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/plugins/cmd/JsonSection.java

Code
Comments
Other
Rev Date Author Line
6200 08 Apr 21 nicklas 1 package net.sf.basedb.reggie.plugins.cmd;
6200 08 Apr 21 nicklas 2
6200 08 Apr 21 nicklas 3 import org.json.simple.JSONObject;
6200 08 Apr 21 nicklas 4
6200 08 Apr 21 nicklas 5
6200 08 Apr 21 nicklas 6 /**
6200 08 Apr 21 nicklas 7   A section in a JsonFile. A wrapper around a JSONObject which
6200 08 Apr 21 nicklas 8   is expected to mostly contain key=value entries.
6200 08 Apr 21 nicklas 9   @since 4.32
6200 08 Apr 21 nicklas 10 */
6200 08 Apr 21 nicklas 11 public class JsonSection
6200 08 Apr 21 nicklas 12 {
6200 08 Apr 21 nicklas 13   
6200 08 Apr 21 nicklas 14   private final JsonFile file;
6200 08 Apr 21 nicklas 15   private final String section;
6200 08 Apr 21 nicklas 16   private final JSONObject json;
6200 08 Apr 21 nicklas 17   
6200 08 Apr 21 nicklas 18   private boolean hasError;
6200 08 Apr 21 nicklas 19   private boolean hasWarning;
6200 08 Apr 21 nicklas 20   
6200 08 Apr 21 nicklas 21   /**
6200 08 Apr 21 nicklas 22     Create a new section.
6200 08 Apr 21 nicklas 23     @param file The file
6200 08 Apr 21 nicklas 24     @param section Name of the section
6200 08 Apr 21 nicklas 25     @param json The JSONObject with data in the section.
6200 08 Apr 21 nicklas 26   */
6200 08 Apr 21 nicklas 27   public JsonSection(JsonFile file, String section, JSONObject json)
6200 08 Apr 21 nicklas 28   {
6200 08 Apr 21 nicklas 29     this.file = file;
6200 08 Apr 21 nicklas 30     this.section = section;
6200 08 Apr 21 nicklas 31     this.json = json;
6200 08 Apr 21 nicklas 32   }
6200 08 Apr 21 nicklas 33   
6200 08 Apr 21 nicklas 34   /**
6200 08 Apr 21 nicklas 35     Get the JsonFile that this section is part of.
6200 08 Apr 21 nicklas 36   */
6200 08 Apr 21 nicklas 37   public JsonFile getFile()
6200 08 Apr 21 nicklas 38   {
6200 08 Apr 21 nicklas 39     return file;
6200 08 Apr 21 nicklas 40   }
6200 08 Apr 21 nicklas 41   
6200 08 Apr 21 nicklas 42   /**
6200 08 Apr 21 nicklas 43     Get the name of this section.
6200 08 Apr 21 nicklas 44   */
6200 08 Apr 21 nicklas 45   public String getName()
6200 08 Apr 21 nicklas 46   {
6200 08 Apr 21 nicklas 47     return section;
6200 08 Apr 21 nicklas 48   }
6200 08 Apr 21 nicklas 49   
6200 08 Apr 21 nicklas 50   /**
6200 08 Apr 21 nicklas 51     Get the JSONObjec that contains the data for this section.
6200 08 Apr 21 nicklas 52   */
6200 08 Apr 21 nicklas 53   public JSONObject getJSON()
6200 08 Apr 21 nicklas 54   {
6200 08 Apr 21 nicklas 55     return json;
6200 08 Apr 21 nicklas 56   }
6200 08 Apr 21 nicklas 57   
6200 08 Apr 21 nicklas 58   /**
6200 08 Apr 21 nicklas 59     Add an error message that is related to this section.
6200 08 Apr 21 nicklas 60     The message is forwarded to the file, but we keep a
6200 08 Apr 21 nicklas 61     flag here that can be checked by hasError().
6200 08 Apr 21 nicklas 62   */
6200 08 Apr 21 nicklas 63   public void addErrorMessage(String msg)
6200 08 Apr 21 nicklas 64   {
6200 08 Apr 21 nicklas 65     file.addErrorMessage(msg);
6200 08 Apr 21 nicklas 66     hasError = true;
6200 08 Apr 21 nicklas 67   }
6200 08 Apr 21 nicklas 68   public boolean hasError()
6200 08 Apr 21 nicklas 69   {
6200 08 Apr 21 nicklas 70     return hasError;
6200 08 Apr 21 nicklas 71   }
6200 08 Apr 21 nicklas 72   
6200 08 Apr 21 nicklas 73   /**
6200 08 Apr 21 nicklas 74     Add an warning message that is related to this section.
6200 08 Apr 21 nicklas 75     The message is forwarded to the file, but we keep a
6200 08 Apr 21 nicklas 76     flag here that can be checked by hasWarning().
6200 08 Apr 21 nicklas 77   */
6200 08 Apr 21 nicklas 78   public void addWarningMessage(String msg)
6200 08 Apr 21 nicklas 79   {
6200 08 Apr 21 nicklas 80     file.addWarningMessage(msg);
6200 08 Apr 21 nicklas 81     hasWarning = true;
6200 08 Apr 21 nicklas 82   }
6200 08 Apr 21 nicklas 83   public boolean hasWarning()
6200 08 Apr 21 nicklas 84   {
6200 08 Apr 21 nicklas 85     return hasWarning;
6200 08 Apr 21 nicklas 86   }
6200 08 Apr 21 nicklas 87
6200 08 Apr 21 nicklas 88   /**
6212 14 Apr 21 nicklas 89     Get an optional entry from the JSON file. A validator is
6212 14 Apr 21 nicklas 90     optional, but if a validator is given it will be called
6510 03 Dec 21 nicklas 91     also for null values or if the entry is missing.
6200 08 Apr 21 nicklas 92   */
6200 08 Apr 21 nicklas 93   @SuppressWarnings("unchecked")
6212 14 Apr 21 nicklas 94   public <T, F> T getOptionalEntry(String key, ValueValidator<F, T> validator)
6200 08 Apr 21 nicklas 95   {
6212 14 Apr 21 nicklas 96     T result = null;
6510 03 Dec 21 nicklas 97     Object val = json.get(key);
6510 03 Dec 21 nicklas 98     if (validator != null)
6200 08 Apr 21 nicklas 99     {
6955 12 Dec 22 nicklas 100       val = validator.preProcess(file.dc(), val, this, section+"."+key);
6510 03 Dec 21 nicklas 101       if (val != null && !validator.getExpectedClass().isInstance(val))
6212 14 Apr 21 nicklas 102       {
6510 03 Dec 21 nicklas 103         addErrorMessage("Invalid entry in JSON: "+section+"."+key+"="+val+
6510 03 Dec 21 nicklas 104           " (expected "+validator.getExpectedClass().getSimpleName()+", got " + val.getClass().getSimpleName()+")");
6510 03 Dec 21 nicklas 105       }
6510 03 Dec 21 nicklas 106       else
6510 03 Dec 21 nicklas 107       {
6510 03 Dec 21 nicklas 108         try
6212 14 Apr 21 nicklas 109         {
6510 03 Dec 21 nicklas 110           result = validator.isValid(file.dc(), (F)val, this, section+"."+key);
6212 14 Apr 21 nicklas 111         }
6510 03 Dec 21 nicklas 112         catch (Exception ex)
6212 14 Apr 21 nicklas 113         {
6510 03 Dec 21 nicklas 114           addErrorMessage("Invalid entry in JSON: "+section+"."+key+"="+val+" ("+ex.getMessage()+")");
6212 14 Apr 21 nicklas 115         }
6212 14 Apr 21 nicklas 116       }
6200 08 Apr 21 nicklas 117     }
6510 03 Dec 21 nicklas 118     else
6510 03 Dec 21 nicklas 119     {
6510 03 Dec 21 nicklas 120       result = (T)val;
6510 03 Dec 21 nicklas 121     }
6212 14 Apr 21 nicklas 122     return result;
6200 08 Apr 21 nicklas 123   }
6200 08 Apr 21 nicklas 124   
6200 08 Apr 21 nicklas 125   /**
6200 08 Apr 21 nicklas 126     Get a required entry from the JSON file that is validated
6200 08 Apr 21 nicklas 127     by a validator.
6200 08 Apr 21 nicklas 128   */
6200 08 Apr 21 nicklas 129   @SuppressWarnings("unchecked")
6200 08 Apr 21 nicklas 130   public <T, F> T getRequiredEntry(String key, ValueValidator<F, T> validator)
6200 08 Apr 21 nicklas 131   {
6955 12 Dec 22 nicklas 132     Object val = validator.preProcess(file.dc(), json.get(key), this, section+"."+key);
6200 08 Apr 21 nicklas 133     T result = null;
6200 08 Apr 21 nicklas 134     if (val == null)
6200 08 Apr 21 nicklas 135     {
6217 19 Apr 21 nicklas 136       if (json.containsKey(key))
6217 19 Apr 21 nicklas 137       {
6217 19 Apr 21 nicklas 138         addErrorMessage("Missing value in JSON: "+section+"."+key+" = null");
6217 19 Apr 21 nicklas 139       }
6217 19 Apr 21 nicklas 140       else
6217 19 Apr 21 nicklas 141       {
6217 19 Apr 21 nicklas 142         addErrorMessage("Missing entry in JSON: "+section+"."+key);
6217 19 Apr 21 nicklas 143       }
6200 08 Apr 21 nicklas 144     }
6200 08 Apr 21 nicklas 145     else if (!validator.getExpectedClass().isInstance(val))
6200 08 Apr 21 nicklas 146     {
6200 08 Apr 21 nicklas 147       addErrorMessage("Invalid entry in JSON: "+section+"."+key+"="+val+
6200 08 Apr 21 nicklas 148         " (expected "+validator.getExpectedClass().getSimpleName()+", got " + val.getClass().getSimpleName()+")");
6200 08 Apr 21 nicklas 149     }
6200 08 Apr 21 nicklas 150     else
6200 08 Apr 21 nicklas 151     {
6200 08 Apr 21 nicklas 152       try
6200 08 Apr 21 nicklas 153       {
6201 09 Apr 21 nicklas 154         result = validator.isValid(file.dc(), (F)val, this, section+"."+key);
6200 08 Apr 21 nicklas 155       }
6200 08 Apr 21 nicklas 156       catch (Exception ex)
6200 08 Apr 21 nicklas 157       {
6200 08 Apr 21 nicklas 158         addErrorMessage("Invalid entry in JSON: "+section+"."+key+"="+val+" ("+ex.getMessage()+")");
6200 08 Apr 21 nicklas 159       }
6200 08 Apr 21 nicklas 160     }
6200 08 Apr 21 nicklas 161     return result;
6200 08 Apr 21 nicklas 162   }
6200 08 Apr 21 nicklas 163
6200 08 Apr 21 nicklas 164 }