From: Marco Zanon Date: Wed, 1 Feb 2012 15:16:36 +0000 (+0000) Subject: Made getValue and removeValue in MJsonObject throw an uncatched exception when key... X-Git-Tag: SVN-to-Git~162 X-Git-Url: https://gitweb.marcozanon.com/?a=commitdiff_plain;h=c13232b57d343034914fd44fdfd12c5c676377db;p=Macaco Made getValue and removeValue in MJsonObject throw an uncatched exception when key is not available. --- diff --git a/src/java/com/marcozanon/macaco/json/MJsonObject.java b/src/java/com/marcozanon/macaco/json/MJsonObject.java index 304759e..439e320 100644 --- a/src/java/com/marcozanon/macaco/json/MJsonObject.java +++ b/src/java/com/marcozanon/macaco/json/MJsonObject.java @@ -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 getKeys() { LinkedHashSet keys = new LinkedHashSet(); 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 index 51378f4..0000000 --- a/src/java/com/marcozanon/macaco/json/MValueNotFoundJsonException.java +++ /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); - } - -} diff --git a/src/java/com/marcozanon/macaco/web/ui/MWebMessage.java b/src/java/com/marcozanon/macaco/web/ui/MWebMessage.java index 03f2ced..7949d9a 100644 --- a/src/java/com/marcozanon/macaco/web/ui/MWebMessage.java +++ b/src/java/com/marcozanon/macaco/web/ui/MWebMessage.java @@ -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(); 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() {