Open Source Repository

Home /json/json-lib-2.4-jdk13 | Repository Home



net/sf/json/JSONFunction.java
/*
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sf.json;

import java.io.Serializable;

import net.sf.json.util.JSONUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * JSONFunction represents a javaScript function's text.
 *
 @author Andres Almiray <[email protected]>
 */
public class JSONFunction implements Serializable {
   /** constant array for empty parameters */
   private static final String[] EMPTY_PARAM_ARRAY = new String[0];
   

   /**
    * Constructs a JSONFunction from a text representation
    */
   public static JSONFunction parseString str ) {
      if!JSONUtils.isFunctionstr ) ) {
         throw new JSONException"String is not a function. " + str );
      else {
         String params = JSONUtils.getFunctionParamsstr );
         String text = JSONUtils.getFunctionBodystr );
         return new JSONFunction( (params != null? StringUtils.splitparams, "," : null, text != null ? text : "" );
      }
   }

   /** the parameters of this function */
   private String[] params;

   /** the text of this function */
   private String text;

   /**
    * Constructs a JSONFunction with no parameters.
    *
    @param text The text of the function
    */
   public JSONFunctionString text ) {
      thisnull, text );
   }

   /**
    * Constructs a JSONFunction with parameters.
    *
    @param params The parameters of the function
    @param text The text of the function
    */
   public JSONFunctionString[] params, String text ) {
      this.text = (text != null? text.trim() "";
      ifparams != null ){
         ifparams.length == && params[0].trim()
               .equals"" ) ){
            this.params = EMPTY_PARAM_ARRAY;
         }else{
            this.params = new String[params.length];
            System.arraycopyparams, 0this.params, 0, params.length );
            // remove empty spaces
            forint i = 0; i < params.length; i++ ){
               this.params[ithis.params[i].trim();
            }
         }
      }else{
         this.params = EMPTY_PARAM_ARRAY;
      }
   }

   public boolean equalsObject obj ) {
      ifthis == obj ){
         return true;
      }
      ifobj == null ){
         return false;
      }

      ifobj instanceof String ){
         try{
            JSONFunction other = parse( (Stringobj );
            return equalsother );
         }catchJSONException e ){
            return false;
         }
      }

      if!(obj instanceof JSONFunction) ){
         return false;
      }

      JSONFunction other = (JSONFunctionobj;
      ifparams.length != other.params.length ){
         return false;
      }
      EqualsBuilder builder = new EqualsBuilder();
      forint i = 0; i < params.length; i++ ){
         builder.appendparams[i], other.params[i] );
      }
      builder.appendtext, other.text );
      return builder.isEquals();
   }

   /**
    * Returns the parameters of this function.
    */
   public String[] getParams() {
      return params;
   }

   /**
    * Reeturns the text of this function.
    */
   public String getText() {
      return text;
   }

   public int hashCode() {
      HashCodeBuilder builder = new HashCodeBuilder();
      forint i = 0; i < params.length; i++ ){
         builder.appendparams[i] );
      }
      builder.appendtext );
      return builder.toHashCode();
   }

   /**
    * Returns the string representation of this function.
    */
   public String toString() {
      StringBuffer b = new StringBuffer"function(" );
      ifparams.length > ){
         forint i = 0; i < params.length - 1; i++ ){
            b.appendparams[i] )
                  .append',' );
         }
         b.appendparams[params.length - 1] );
      }
      b.append"){" );
      iftext.length() ){
         b.append' ' )
               .appendtext )
               .append' ' );
      }
      b.append'}' );
      return b.toString();
   }
}