Open Source Repository

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



net/sf/json/AbstractJSON.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.lang.ref.SoftReference;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import net.sf.json.util.JSONUtils;
import net.sf.json.util.JsonEventListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Base class for JSONObject and JSONArray.
 *
 @author Andres Almiray <[email protected]>
 */
abstract class AbstractJSON {
   private static class CycleSet extends ThreadLocal {
      protected Object initialValue() {
         return new SoftReference(new HashSet());
      }

      public Set getSet() {
         Set set = (Set) ((SoftReference)get()).get();
         ifset == null ) {
             set = new HashSet();
             set(new SoftReference(set));
         }
         return set;
      }
   }
   
   private static CycleSet cycleSet = new CycleSet();

   private static final Log log = LogFactory.getLogAbstractJSON.class );

   /**
    * Adds a reference for cycle detection check.
    *
    @param instance the reference to add
    @return true if the instance has not been added previously, false
    *        otherwise.
    */
   protected static boolean addInstanceObject instance ) {
      return getCycleSet().addinstance );
   }

   /**
    * Fires an end of array event.
    */
   protected static void fireArrayEndEventJsonConfig jsonConfig ) {
      ifjsonConfig.isEventTriggeringEnabled() ){
         forIterator listeners = jsonConfig.getJsonEventListeners()
               .iterator(); listeners.hasNext()){
            JsonEventListener listener = (JsonEventListenerlisteners.next();
            try{
               listener.onArrayEnd();
            }catchRuntimeException e ){
               log.warn);
            }
         }
      }
   }

   /**
    * Fires a start of array event.
    */
   protected static void fireArrayStartEventJsonConfig jsonConfig ) {
      ifjsonConfig.isEventTriggeringEnabled() ){
         forIterator listeners = jsonConfig.getJsonEventListeners()
               .iterator(); listeners.hasNext()){
            JsonEventListener listener = (JsonEventListenerlisteners.next();
            try{
               listener.onArrayStart();
            }catchRuntimeException e ){
               log.warn);
            }
         }
      }
   }

   /**
    * Fires an element added event.
    *
    @param index the index where the element was added
    @param element the added element
    */
   protected static void fireElementAddedEventint index, Object element, JsonConfig jsonConfig ) {
      ifjsonConfig.isEventTriggeringEnabled() ){
         forIterator listeners = jsonConfig.getJsonEventListeners()
               .iterator(); listeners.hasNext()){
            JsonEventListener listener = (JsonEventListenerlisteners.next();
            try{
               listener.onElementAddedindex, element );
            }catchRuntimeException e ){
               log.warn);
            }
         }
      }
   }

   /**
    * Fires an error event.
    *
    @param jsone the thrown exception
    */
   protected static void fireErrorEventJSONException jsone, JsonConfig jsonConfig ) {
      ifjsonConfig.isEventTriggeringEnabled() ){
         forIterator listeners = jsonConfig.getJsonEventListeners()
               .iterator(); listeners.hasNext()){
            JsonEventListener listener = (JsonEventListenerlisteners.next();
            try{
               listener.onErrorjsone );
            }catchRuntimeException e ){
               log.warn);
            }
         }
      }
   }

   /**
    * Fires an end of object event.
    */
   protected static void fireObjectEndEventJsonConfig jsonConfig ) {
      ifjsonConfig.isEventTriggeringEnabled() ){
         forIterator listeners = jsonConfig.getJsonEventListeners()
               .iterator(); listeners.hasNext()){
            JsonEventListener listener = (JsonEventListenerlisteners.next();
            try{
               listener.onObjectEnd();
            }catchRuntimeException e ){
               log.warn);
            }
         }
      }
   }

   /**
    * Fires a start of object event.
    */
   protected static void fireObjectStartEventJsonConfig jsonConfig ) {
      ifjsonConfig.isEventTriggeringEnabled() ){
         forIterator listeners = jsonConfig.getJsonEventListeners()
               .iterator(); listeners.hasNext()){
            JsonEventListener listener = (JsonEventListenerlisteners.next();
            try{
               listener.onObjectStart();
            }catchRuntimeException e ){
               log.warn);
            }
         }
      }
   }

   /**
    * Fires a property set event.
    *
    @param key the name of the property
    @param value the value of the property
    @param accumulated if the value has been accumulated over 'key'
    */
   protected static void firePropertySetEventString key, Object value, boolean accumulated,
         JsonConfig jsonConfig ) {
      ifjsonConfig.isEventTriggeringEnabled() ){
         forIterator listeners = jsonConfig.getJsonEventListeners()
               .iterator(); listeners.hasNext()){
            JsonEventListener listener = (JsonEventListenerlisteners.next();
            try{
               listener.onPropertySetkey, value, accumulated );
            }catchRuntimeException e ){
               log.warn);
            }
         }
      }
   }

   /**
    * Fires a warning event.
    *
    @param warning the warning message
    */
   protected static void fireWarnEventString warning, JsonConfig jsonConfig ) {
      ifjsonConfig.isEventTriggeringEnabled() ){
         forIterator listeners = jsonConfig.getJsonEventListeners()
               .iterator(); listeners.hasNext()){
            JsonEventListener listener = (JsonEventListenerlisteners.next();
            try{
               listener.onWarningwarning );
            }catchRuntimeException e ){
               log.warn);
            }
         }
      }
   }

   /**
    * Removes a reference for cycle detection check.
    */
   protected static void removeInstanceObject instance ) {
      Set set = getCycleSet();
      set.removeinstance );
      if(set.size() == 0) {
          cycleSet.remove();
      }
   }

   protected Object _processValueObject value, JsonConfig jsonConfig ) {
      ifJSONNull.getInstance().equalsvalue ) ) {
         return JSONNull.getInstance();
      else ifClass.class.isAssignableFromvalue.getClass() ) || value instanceof Class ) {
         return ((Classvalue).getName();
      else ifJSONUtils.isFunctionvalue ) ) {
         ifvalue instanceof String ) {
            value = JSONFunction.parse( (Stringvalue );
         }
         return value;
      else ifvalue instanceof JSONString ) {
         return JSONSerializer.toJSON( (JSONStringvalue, jsonConfig );
      else ifvalue instanceof JSON ) {
         return JSONSerializer.toJSONvalue, jsonConfig );
      else ifJSONUtils.isArrayvalue ) ) {
         return JSONArray.fromObjectvalue, jsonConfig );
      else ifJSONUtils.isStringvalue ) ) {
         String str = String.valueOfvalue );
         ifJSONUtils.hasQuotesstr ) ){
            String stripped = JSONUtils.stripQuotesstr );
            ifJSONUtils.isFunctionstripped )){
               return JSONUtils.DOUBLE_QUOTE + stripped + JSONUtils.DOUBLE_QUOTE;
            }
            if(stripped.startsWith("["&& stripped.endsWith("]")) {
               return stripped;
            }
            if(stripped.startsWith("{"&& stripped.endsWith("}")) {
               return stripped;
            }
            return str;
         else ifJSONUtils.isJsonKeywordstr, jsonConfig ) ) {
            ifjsonConfig.isJavascriptCompliant() && "undefined".equalsstr )){
               return JSONNull.getInstance();
            }
            return str;
         else ifJSONUtils.mayBeJSONstr ) ) {
            try {
               return JSONSerializer.toJSONstr, jsonConfig );
            catchJSONException jsone ) {
               return str;
            }
         }
         return str;
      else ifJSONUtils.isNumbervalue ) ) {
         JSONUtils.testValidityvalue );
         return JSONUtils.transformNumber( (Numbervalue );
      else ifJSONUtils.isBooleanvalue ) ) {
         return value;
      else {
         JSONObject jsonObject = JSONObject.fromObjectvalue, jsonConfig );
         ifjsonObject.isNullObject() ) {
            return JSONNull.getInstance();
         else {
            return jsonObject;
         }
      }
   }
   
   private static Set getCycleSet() {
      return cycleSet.getSet();
   }
}