View Javadoc

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     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        if( !JSONUtils.isFunction( str ) ) {
42           throw new JSONException( "String is not a function. " + str );
43        } else {
44           String params = JSONUtils.getFunctionParams( str );
45           String text = JSONUtils.getFunctionBody( str );
46           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        this( null, text );
63     }
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     public JSONFunction( String[] params, String text ) {
72        this.text = (text != null) ? text.trim() : "";
73        if( params != null ){
74           if( params.length == 1 && params[0].trim()
75                 .equals( "" ) ){
76              this.params = EMPTY_PARAM_ARRAY;
77           }else{
78              this.params = new String[params.length];
79              System.arraycopy( params, 0, this.params, 0, params.length );
80              // remove empty spaces
81              for( int i = 0; i < params.length; i++ ){
82                 this.params[i] = this.params[i].trim();
83              }
84           }
85        }else{
86           this.params = EMPTY_PARAM_ARRAY;
87        }
88     }
89  
90     public boolean equals( Object obj ) {
91        if( this == obj ){
92           return true;
93        }
94        if( obj == null ){
95           return false;
96        }
97  
98        if( obj instanceof String ){
99           try{
100             JSONFunction other = parse( (String) obj );
101             return equals( other );
102          }catch( JSONException e ){
103             return false;
104          }
105       }
106 
107       if( !(obj instanceof JSONFunction) ){
108          return false;
109       }
110 
111       JSONFunction other = (JSONFunction) obj;
112       if( params.length != other.params.length ){
113          return false;
114       }
115       EqualsBuilder builder = new EqualsBuilder();
116       for( int i = 0; i < params.length; i++ ){
117          builder.append( params[i], other.params[i] );
118       }
119       builder.append( text, other.text );
120       return builder.isEquals();
121    }
122 
123    /**
124     * Returns the parameters of this function.
125     */
126    public String[] getParams() {
127       return params;
128    }
129 
130    /**
131     * Reeturns the text of this function.
132     */
133    public String getText() {
134       return text;
135    }
136 
137    public int hashCode() {
138       HashCodeBuilder builder = new HashCodeBuilder();
139       for( int i = 0; i < params.length; i++ ){
140          builder.append( params[i] );
141       }
142       builder.append( text );
143       return builder.toHashCode();
144    }
145 
146    /**
147     * Returns the string representation of this function.
148     */
149    public String toString() {
150       StringBuffer b = new StringBuffer( "function(" );
151       if( params.length > 0 ){
152          for( int i = 0; i < params.length - 1; i++ ){
153             b.append( params[i] )
154                   .append( ',' );
155          }
156          b.append( params[params.length - 1] );
157       }
158       b.append( "){" );
159       if( text.length() > 0 ){
160          b.append( ' ' )
161                .append( text )
162                .append( ' ' );
163       }
164       b.append( '}' );
165       return b.toString();
166    }
167 }