Coverage Report - net.sf.json.AbstractJSON
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractJSON
0%
0/127
0%
0/70
5.143
AbstractJSON$1
N/A
N/A
5.143
AbstractJSON$CycleSet
0%
0/7
0%
0/2
5.143
 
 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.lang.ref.SoftReference;
 20  
 import java.util.HashSet;
 21  
 import java.util.Iterator;
 22  
 import java.util.Set;
 23  
 
 24  
 import net.sf.json.util.JSONTokener;
 25  
 import net.sf.json.util.JSONUtils;
 26  
 import net.sf.json.util.JsonEventListener;
 27  
 
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 
 31  
 /**
 32  
  * Base class for JSONObject and JSONArray.
 33  
  *
 34  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 35  
  */
 36  0
 abstract class AbstractJSON {
 37  0
    private static class CycleSet extends ThreadLocal {
 38  
       protected Object initialValue() {
 39  0
          return new SoftReference(new HashSet());
 40  
       }
 41  
 
 42  
       public Set getSet() {
 43  0
          Set set = (Set) ((SoftReference)get()).get();
 44  0
          if( set == null ) {
 45  0
              set = new HashSet();
 46  0
              set(new SoftReference(set));
 47  
          }
 48  0
          return set;
 49  
       }
 50  
    }
 51  
    
 52  0
    private static CycleSet cycleSet = new CycleSet();
 53  
 
 54  0
    private static final Log log = LogFactory.getLog( AbstractJSON.class );
 55  
 
 56  
    /**
 57  
     * Adds a reference for cycle detection check.
 58  
     *
 59  
     * @param instance the reference to add
 60  
     * @return true if the instance has not been added previously, false
 61  
     *        otherwise.
 62  
     */
 63  
    protected static boolean addInstance( Object instance ) {
 64  0
       return getCycleSet().add( instance );
 65  
    }
 66  
 
 67  
    /**
 68  
     * Fires an end of array event.
 69  
     */
 70  
    protected static void fireArrayEndEvent( JsonConfig jsonConfig ) {
 71  0
       if( jsonConfig.isEventTriggeringEnabled() ){
 72  0
          for( Iterator listeners = jsonConfig.getJsonEventListeners()
 73  0
                .iterator(); listeners.hasNext(); ){
 74  0
             JsonEventListener listener = (JsonEventListener) listeners.next();
 75  
             try{
 76  0
                listener.onArrayEnd();
 77  0
             }catch( RuntimeException e ){
 78  0
                log.warn( e );
 79  0
             }
 80  0
          }
 81  
       }
 82  0
    }
 83  
 
 84  
    /**
 85  
     * Fires a start of array event.
 86  
     */
 87  
    protected static void fireArrayStartEvent( JsonConfig jsonConfig ) {
 88  0
       if( jsonConfig.isEventTriggeringEnabled() ){
 89  0
          for( Iterator listeners = jsonConfig.getJsonEventListeners()
 90  0
                .iterator(); listeners.hasNext(); ){
 91  0
             JsonEventListener listener = (JsonEventListener) listeners.next();
 92  
             try{
 93  0
                listener.onArrayStart();
 94  0
             }catch( RuntimeException e ){
 95  0
                log.warn( e );
 96  0
             }
 97  0
          }
 98  
       }
 99  0
    }
 100  
 
 101  
    /**
 102  
     * Fires an element added event.
 103  
     *
 104  
     * @param index the index where the element was added
 105  
     * @param element the added element
 106  
     */
 107  
    protected static void fireElementAddedEvent( int index, Object element, JsonConfig jsonConfig ) {
 108  0
       if( jsonConfig.isEventTriggeringEnabled() ){
 109  0
          for( Iterator listeners = jsonConfig.getJsonEventListeners()
 110  0
                .iterator(); listeners.hasNext(); ){
 111  0
             JsonEventListener listener = (JsonEventListener) listeners.next();
 112  
             try{
 113  0
                listener.onElementAdded( index, element );
 114  0
             }catch( RuntimeException e ){
 115  0
                log.warn( e );
 116  0
             }
 117  0
          }
 118  
       }
 119  0
    }
 120  
 
 121  
    /**
 122  
     * Fires an error event.
 123  
     *
 124  
     * @param jsone the thrown exception
 125  
     */
 126  
    protected static void fireErrorEvent( JSONException jsone, JsonConfig jsonConfig ) {
 127  0
       if( jsonConfig.isEventTriggeringEnabled() ){
 128  0
          for( Iterator listeners = jsonConfig.getJsonEventListeners()
 129  0
                .iterator(); listeners.hasNext(); ){
 130  0
             JsonEventListener listener = (JsonEventListener) listeners.next();
 131  
             try{
 132  0
                listener.onError( jsone );
 133  0
             }catch( RuntimeException e ){
 134  0
                log.warn( e );
 135  0
             }
 136  0
          }
 137  
       }
 138  0
    }
 139  
 
 140  
    /**
 141  
     * Fires an end of object event.
 142  
     */
 143  
    protected static void fireObjectEndEvent( JsonConfig jsonConfig ) {
 144  0
       if( jsonConfig.isEventTriggeringEnabled() ){
 145  0
          for( Iterator listeners = jsonConfig.getJsonEventListeners()
 146  0
                .iterator(); listeners.hasNext(); ){
 147  0
             JsonEventListener listener = (JsonEventListener) listeners.next();
 148  
             try{
 149  0
                listener.onObjectEnd();
 150  0
             }catch( RuntimeException e ){
 151  0
                log.warn( e );
 152  0
             }
 153  0
          }
 154  
       }
 155  0
    }
 156  
 
 157  
    /**
 158  
     * Fires a start of object event.
 159  
     */
 160  
    protected static void fireObjectStartEvent( JsonConfig jsonConfig ) {
 161  0
       if( jsonConfig.isEventTriggeringEnabled() ){
 162  0
          for( Iterator listeners = jsonConfig.getJsonEventListeners()
 163  0
                .iterator(); listeners.hasNext(); ){
 164  0
             JsonEventListener listener = (JsonEventListener) listeners.next();
 165  
             try{
 166  0
                listener.onObjectStart();
 167  0
             }catch( RuntimeException e ){
 168  0
                log.warn( e );
 169  0
             }
 170  0
          }
 171  
       }
 172  0
    }
 173  
 
 174  
    /**
 175  
     * Fires a property set event.
 176  
     *
 177  
     * @param key the name of the property
 178  
     * @param value the value of the property
 179  
     * @param accumulated if the value has been accumulated over 'key'
 180  
     */
 181  
    protected static void firePropertySetEvent( String key, Object value, boolean accumulated,
 182  
          JsonConfig jsonConfig ) {
 183  0
       if( jsonConfig.isEventTriggeringEnabled() ){
 184  0
          for( Iterator listeners = jsonConfig.getJsonEventListeners()
 185  0
                .iterator(); listeners.hasNext(); ){
 186  0
             JsonEventListener listener = (JsonEventListener) listeners.next();
 187  
             try{
 188  0
                listener.onPropertySet( key, value, accumulated );
 189  0
             }catch( RuntimeException e ){
 190  0
                log.warn( e );
 191  0
             }
 192  0
          }
 193  
       }
 194  0
    }
 195  
 
 196  
    /**
 197  
     * Fires a warning event.
 198  
     *
 199  
     * @param warning the warning message
 200  
     */
 201  
    protected static void fireWarnEvent( String warning, JsonConfig jsonConfig ) {
 202  0
       if( jsonConfig.isEventTriggeringEnabled() ){
 203  0
          for( Iterator listeners = jsonConfig.getJsonEventListeners()
 204  0
                .iterator(); listeners.hasNext(); ){
 205  0
             JsonEventListener listener = (JsonEventListener) listeners.next();
 206  
             try{
 207  0
                listener.onWarning( warning );
 208  0
             }catch( RuntimeException e ){
 209  0
                log.warn( e );
 210  0
             }
 211  0
          }
 212  
       }
 213  0
    }
 214  
 
 215  
    /**
 216  
     * Removes a reference for cycle detection check.
 217  
     */
 218  
    protected static void removeInstance( Object instance ) {
 219  0
       getCycleSet().remove( instance );
 220  0
    }
 221  
 
 222  
    protected Object _processValue( Object value, JsonConfig jsonConfig ) {
 223  0
       if( JSONNull.getInstance().equals( value ) ) {
 224  0
          return JSONNull.getInstance();
 225  0
       } else if( Class.class.isAssignableFrom( value.getClass() ) || value instanceof Class ) {
 226  0
          return ((Class) value).getName();
 227  0
       } else if( JSONUtils.isFunction( value ) ) {
 228  0
          if( value instanceof String ) {
 229  0
             value = JSONFunction.parse( (String) value );
 230  
          }
 231  0
          return value;
 232  0
       } else if( value instanceof JSONString ) {
 233  0
          return JSONSerializer.toJSON( (JSONString) value, jsonConfig );
 234  0
       } else if( value instanceof JSON ) {
 235  0
          return JSONSerializer.toJSON( value, jsonConfig );
 236  0
       } else if( JSONUtils.isArray( value ) ) {
 237  0
          return JSONArray.fromObject( value, jsonConfig );
 238  0
       } else if( JSONUtils.isString( value ) ) {
 239  0
          String str = String.valueOf( value );
 240  0
          if( JSONUtils.hasQuotes( str ) ){
 241  0
             str = JSONUtils.stripQuotes( str );
 242  0
             if( JSONUtils.isFunction( str )){
 243  0
                return JSONUtils.DOUBLE_QUOTE + str + JSONUtils.DOUBLE_QUOTE;
 244  
             }
 245  0
             return str;
 246  0
          } else if( JSONUtils.isJsonKeyword( str, jsonConfig ) ) {
 247  0
             if( jsonConfig.isJavascriptCompliant() && "undefined".equals( str )){
 248  0
                return JSONNull.getInstance();
 249  
             }
 250  0
             return str;
 251  0
          } else if( JSONUtils.mayBeJSON( str ) ) {
 252  
             try {
 253  0
                return JSONSerializer.toJSON( str, jsonConfig );
 254  0
             } catch( JSONException jsone ) {
 255  0
                return str;
 256  
             }
 257  
          }
 258  0
          return str;
 259  0
       } else if( JSONUtils.isNumber( value ) ) {
 260  0
          JSONUtils.testValidity( value );
 261  0
          return JSONUtils.transformNumber( (Number) value );
 262  0
       } else if( JSONUtils.isBoolean( value ) ) {
 263  0
          return value;
 264  
       } else {
 265  0
          JSONObject jsonObject = JSONObject.fromObject( value, jsonConfig );
 266  0
          if( jsonObject.isNullObject() ) {
 267  0
             return JSONNull.getInstance();
 268  
          } else {
 269  0
             return jsonObject;
 270  
          }
 271  
       }
 272  
    }
 273  
    
 274  
    private static Set getCycleSet() {
 275  0
       return cycleSet.getSet();
 276  
    }
 277  
 }