Made getValue and removeValue in MJsonObject throw an uncatched exception when key...
authorMarco Zanon <info@marcozanon.com>
Wed, 1 Feb 2012 15:16:36 +0000 (15:16 +0000)
committerMarco Zanon <info@marcozanon.com>
Wed, 1 Feb 2012 15:16:36 +0000 (15:16 +0000)
src/java/com/marcozanon/macaco/json/MJsonObject.java
src/java/com/marcozanon/macaco/json/MValueNotFoundJsonException.java [deleted file]
src/java/com/marcozanon/macaco/web/ui/MWebMessage.java

index 304759e7f4a4c723827fac9ef6aca622c10ec405..439e3203280c3e14e56d67287e65d23a73ca0f33 100644 (file)
@@ -80,13 +80,13 @@ public class MJsonObject extends MJsonValue {
         return this.values;
     }
 
-    public MJsonValue getValue(String key) throws MValueNotFoundJsonException {
+    public MJsonValue getValue(String key) {
         if ((null == key) || ("".equals(key))) {
             throw new IllegalArgumentException("Invalid 'key': null or empty.");
         }
         //
-        if (!this.getValuesReference().containsKey(key)) {
-            throw new MValueNotFoundJsonException(String.format("Invalid 'key': %s: not available.", key));
+        if (!this.containsKey(key)) {
+            throw new IllegalArgumentException(String.format("Invalid 'key': %s: not available.", key));
         }
         return this.getValuesReference().get(key).clone();
     }
@@ -95,13 +95,13 @@ public class MJsonObject extends MJsonValue {
         return this.getValuesReference().size();
     }
 
-    public void removeValue(String key) throws MValueNotFoundJsonException {
+    public void removeValue(String key) {
         if ((null == key) || ("".equals(key))) {
             throw new IllegalArgumentException("Invalid 'key': null or empty.");
         }
         //
-        if (!this.getValuesReference().containsKey(key)) {
-            throw new MValueNotFoundJsonException(String.format("Invalid 'key': %s: not available.", key));
+        if (!this.containsKey(key)) {
+            throw new IllegalArgumentException(String.format("Invalid 'key': %s: not available.", key));
         }
         this.getValuesReference().remove(key);
     }
@@ -110,6 +110,14 @@ public class MJsonObject extends MJsonValue {
         this.getValuesReference().clear();
     }
 
+    public boolean containsKey(String key) {
+        if ((null == key) || ("".equals(key))) {
+            throw new IllegalArgumentException("Invalid 'key': null or empty.");
+        }
+        //
+        return this.getValuesReference().containsKey(key);
+    }
+
     public LinkedHashSet<String> getKeys() {
         LinkedHashSet<String> keys = new LinkedHashSet<String>();
         for (String key: this.getValuesReference().keySet()) {
diff --git a/src/java/com/marcozanon/macaco/json/MValueNotFoundJsonException.java b/src/java/com/marcozanon/macaco/json/MValueNotFoundJsonException.java
deleted file mode 100644 (file)
index 51378f4..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon
- * 
- * Released under the MIT License:
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- * 
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-package com.marcozanon.macaco.json;
-
-public class MValueNotFoundJsonException extends MJsonException {
-
-    private static final long serialVersionUID = 0L;
-
-    /* */
-
-    public MValueNotFoundJsonException() {
-        super();
-    }
-
-    public MValueNotFoundJsonException(String message) {
-        super(message);
-    }
-
-    public MValueNotFoundJsonException(Throwable error) {
-        super(error);
-    }
-
-    public MValueNotFoundJsonException(String message, Throwable error) {
-        super(message, error);
-    }
-
-}
index 03f2ced5b326f92306d64ca201252bc64d98158e..7949d9a2e1b961835715ff426c5fecfd0a010245 100644 (file)
@@ -29,7 +29,6 @@ import com.marcozanon.macaco.MObject;
 import com.marcozanon.macaco.json.MInvalidValueJsonException;
 import com.marcozanon.macaco.json.MJsonObject;
 import com.marcozanon.macaco.json.MJsonString;
-import com.marcozanon.macaco.json.MValueNotFoundJsonException;
 import java.util.LinkedHashMap;
 
 public class MWebMessage extends MObject {
@@ -55,16 +54,19 @@ public class MWebMessage extends MObject {
             throw new MMessagingWebException("Invalid 'message': not a Json object."); // no need to propagate exception
         }
         // parse widget id component
+        if (!jsonMessage.containsKey("widgetId")) {
+            throw new MMessagingWebException("Invalid 'message': no widget id key.");
+        }
         try {
             this.widgetId = ((MJsonString)jsonMessage.getValue("widgetId")).getValue();
         }
         catch (ClassCastException exception) {
             throw new MMessagingWebException("Invalid 'message': invalid widget id."); // no need to propagate exception
         }
-        catch (MValueNotFoundJsonException exception) {
-            throw new MMessagingWebException("Invalid 'message': no widget id key."); // no need to propagate exception
-        }
         // parse event component
+        if (!jsonMessage.containsKey("event")) {
+            throw new MMessagingWebException("Invalid 'message': no event key.");
+        }
         try {
             String temporaryEvent = ((MJsonString)jsonMessage.getValue("event")).getValue();
             if ("".equals(temporaryEvent)) {
@@ -75,10 +77,10 @@ public class MWebMessage extends MObject {
         catch (ClassCastException exception) {
             throw new MMessagingWebException("Invalid 'message': invalid event."); // no need to propagate exception
         }
-        catch (MValueNotFoundJsonException exception) {
-            throw new MMessagingWebException("Invalid 'message': no event key."); // no need to propagate exception
-        }
         // parse parameters component
+        if (!jsonMessage.containsKey("parameters")) {
+            throw new MMessagingWebException("Invalid 'message': no parameters key.");
+        }
         this.parameters = new LinkedHashMap<String, String>();
         try {
             MJsonObject temporaryParameters = (MJsonObject)jsonMessage.getValue("parameters");
@@ -89,9 +91,6 @@ public class MWebMessage extends MObject {
         catch (ClassCastException exception) {
             throw new MMessagingWebException("Invalid 'message': invalid parameters."); // no need to propagate exception
         }
-        catch (MValueNotFoundJsonException exception) {
-            throw new MMessagingWebException("Invalid 'message': no parameters key."); // no need to propagate exception
-        }
     }
 
     public MWebMessage clone() {