| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
public class JSONFunction implements Serializable { |
| 33 | |
|
| 34 | 0 | private static final String[] EMPTY_PARAM_ARRAY = new String[0]; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 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 | |
|
| 51 | |
private String[] params; |
| 52 | |
|
| 53 | |
|
| 54 | |
private String text; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public JSONFunction( String text ) { |
| 62 | 0 | this( null, text ); |
| 63 | 0 | } |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 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 | |
|
| 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 | |
|
| 125 | |
|
| 126 | |
public String[] getParams() { |
| 127 | 0 | return params; |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 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 | |
|
| 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 | |
} |