Coverage Report - net.sf.json.JSONFunction
 
Classes in this File Line Coverage Branch Coverage Complexity
JSONFunction
0%
0/55
0%
0/36
3.75
 
 1  
 /*
 2  
  * Copyright 2002-2009 the original author or authors.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package net.sf.json;
 18  
 
 19  
 import java.io.Serializable;
 20  
 
 21  
 import net.sf.json.util.JSONUtils;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.apache.commons.lang.builder.EqualsBuilder;
 25  
 import org.apache.commons.lang.builder.HashCodeBuilder;
 26  
 
 27  
 /**
 28  
  * JSONFunction represents a javaScript function's text.
 29  
  *
 30  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 31  
  */
 32  
 public class JSONFunction implements Serializable {
 33  
    /** constant array for empty parameters */
 34  0
    private static final String[] EMPTY_PARAM_ARRAY = new String[0];
 35  
    
 36  
 
 37  
    /**
 38  
     * Constructs a JSONFunction from a text representation
 39  
     */
 40  
    public static JSONFunction parse( String str ) {
 41  0
       if( !JSONUtils.isFunction( str ) ) {
 42  0
          throw new JSONException( "String is not a function. " + str );
 43  
       } else {
 44  0
          String params = JSONUtils.getFunctionParams( str );
 45  0
          String text = JSONUtils.getFunctionBody( str );
 46  0
          return new JSONFunction( (params != null) ? StringUtils.split( params, "," ) : null, text != null ? text : "" );
 47  
       }
 48  
    }
 49  
 
 50  
    /** the parameters of this function */
 51  
    private String[] params;
 52  
 
 53  
    /** the text of this function */
 54  
    private String text;
 55  
 
 56  
    /**
 57  
     * Constructs a JSONFunction with no parameters.
 58  
     *
 59  
     * @param text The text of the function
 60  
     */
 61  
    public JSONFunction( String text ) {
 62  0
       this( null, text );
 63  0
    }
 64  
 
 65  
    /**
 66  
     * Constructs a JSONFunction with parameters.
 67  
     *
 68  
     * @param params The parameters of the function
 69  
     * @param text The text of the function
 70  
     */
 71  0
    public JSONFunction( String[] params, String text ) {
 72  0
       this.text = (text != null) ? text.trim() : "";
 73  0
       if( params != null ){
 74  0
          if( params.length == 1 && params[0].trim()
 75  
                .equals( "" ) ){
 76  0
             this.params = EMPTY_PARAM_ARRAY;
 77  
          }else{
 78  0
             this.params = new String[params.length];
 79  0
             System.arraycopy( params, 0, this.params, 0, params.length );
 80  
             // remove empty spaces
 81  0
             for( int i = 0; i < params.length; i++ ){
 82  0
                this.params[i] = this.params[i].trim();
 83  
             }
 84  
          }
 85  
       }else{
 86  0
          this.params = EMPTY_PARAM_ARRAY;
 87  
       }
 88  0
    }
 89  
 
 90  
    public boolean equals( Object obj ) {
 91  0
       if( this == obj ){
 92  0
          return true;
 93  
       }
 94  0
       if( obj == null ){
 95  0
          return false;
 96  
       }
 97  
 
 98  0
       if( obj instanceof String ){
 99  
          try{
 100  0
             JSONFunction other = parse( (String) obj );
 101  0
             return equals( other );
 102  0
          }catch( JSONException e ){
 103  0
             return false;
 104  
          }
 105  
       }
 106  
 
 107  0
       if( !(obj instanceof JSONFunction) ){
 108  0
          return false;
 109  
       }
 110  
 
 111  0
       JSONFunction other = (JSONFunction) obj;
 112  0
       if( params.length != other.params.length ){
 113  0
          return false;
 114  
       }
 115  0
       EqualsBuilder builder = new EqualsBuilder();
 116  0
       for( int i = 0; i < params.length; i++ ){
 117  0
          builder.append( params[i], other.params[i] );
 118  
       }
 119  0
       builder.append( text, other.text );
 120  0
       return builder.isEquals();
 121  
    }
 122  
 
 123  
    /**
 124  
     * Returns the parameters of this function.
 125  
     */
 126  
    public String[] getParams() {
 127  0
       return params;
 128  
    }
 129  
 
 130  
    /**
 131  
     * Reeturns the text of this function.
 132  
     */
 133  
    public String getText() {
 134  0
       return text;
 135  
    }
 136  
 
 137  
    public int hashCode() {
 138  0
       HashCodeBuilder builder = new HashCodeBuilder();
 139  0
       for( int i = 0; i < params.length; i++ ){
 140  0
          builder.append( params[i] );
 141  
       }
 142  0
       builder.append( text );
 143  0
       return builder.toHashCode();
 144  
    }
 145  
 
 146  
    /**
 147  
     * Returns the string representation of this function.
 148  
     */
 149  
    public String toString() {
 150  0
       StringBuffer b = new StringBuffer( "function(" );
 151  0
       if( params.length > 0 ){
 152  0
          for( int i = 0; i < params.length - 1; i++ ){
 153  0
             b.append( params[i] )
 154  
                   .append( ',' );
 155  
          }
 156  0
          b.append( params[params.length - 1] );
 157  
       }
 158  0
       b.append( "){" );
 159  0
       if( text.length() > 0 ){
 160  0
          b.append( ' ' )
 161  
                .append( text )
 162  
                .append( ' ' );
 163  
       }
 164  0
       b.append( '}' );
 165  0
       return b.toString();
 166  
    }
 167  
 }