001 // Copyright May 16, 2006 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014 package org.apache.tapestry.dojo;
015
016 import java.text.ParseException;
017
018 import org.apache.hivemind.ApplicationRuntimeException;
019 import org.apache.tapestry.IBinding;
020 import org.apache.tapestry.IComponent;
021 import org.apache.tapestry.json.JSONObject;
022
023 /**
024 * @author andyhot
025 * @since 4.1
026 */
027 public final class DojoUtils
028 {
029 /* defeat instantiation */
030 private DojoUtils() { }
031
032 /**
033 * Converts a parameter of an {@link IComponent} to an instance of {@link JSONObject}.
034 *
035 * @param component
036 * @param parameterName
037 * @return The parameter parsed into a json structure.
038 */
039 public static JSONObject parseJSONParameter(IComponent component, String parameterName)
040 {
041 IBinding binding = component.getBinding(parameterName);
042 if (binding == null || binding.getObject() == null)
043 return new JSONObject();
044
045 try
046 {
047 return new JSONObject((String) binding.getObject(String.class));
048 }
049 catch (ParseException ex)
050 {
051 throw new ApplicationRuntimeException( DojoMessages.mustUseValidJsonInParameter(parameterName),
052 binding.getLocation() , ex);
053 }
054 }
055 }