Coverage Report - net.sf.json.util.CycleDetectionStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
CycleDetectionStrategy
0%
0/7
N/A
1.25
CycleDetectionStrategy$1
N/A
N/A
1.25
CycleDetectionStrategy$LenientCycleDetectionStrategy
0%
0/3
N/A
1.25
CycleDetectionStrategy$LenientNoRefCycleDetectionStrategy
0%
0/3
N/A
1.25
CycleDetectionStrategy$StrictCycleDetectionStrategy
0%
0/3
N/A
1.25
 
 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.util;
 18  
 
 19  
 import net.sf.json.JSONArray;
 20  
 import net.sf.json.JSONException;
 21  
 import net.sf.json.JSONObject;
 22  
 
 23  
 /**
 24  
  * Base class for cycle detection in a hierarchy.<br>
 25  
  * The JSON spec forbides cycles in a hierarchy and most parsers will raise and
 26  
  * error when a cycle is detected. This class defines a contract for handling
 27  
  * those cycles and two base implementations:
 28  
  * <ul>
 29  
  * <li>STRICT - will throw a JSONException if a cycle is found.</li>
 30  
  * <li>LENIENT - will return an empty array or null object if a cycle is found.</li>
 31  
  * </ul>
 32  
  *
 33  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 34  
  */
 35  0
 public abstract class CycleDetectionStrategy {
 36  0
    public static final JSONArray IGNORE_PROPERTY_ARR = new JSONArray();
 37  0
    public static final JSONObject IGNORE_PROPERTY_OBJ = new JSONObject();
 38  
 
 39  
    /** Returns empty array and null object */
 40  0
    public static final CycleDetectionStrategy LENIENT = new LenientCycleDetectionStrategy();
 41  
    /**
 42  
     * Returns a special object (IGNORE_PROPERTY_OBJ) that indicates the entire
 43  
     * property should be ignored
 44  
     */
 45  0
    public static final CycleDetectionStrategy NOPROP = new LenientNoRefCycleDetectionStrategy();
 46  
    /** Throws a JSONException */
 47  0
    public static final CycleDetectionStrategy STRICT = new StrictCycleDetectionStrategy();
 48  
 
 49  
    /**
 50  
     * Handle a repeated reference<br>
 51  
     * Must return a valid JSONArray or null.
 52  
     *
 53  
     * @param reference the repeated reference.
 54  
     */
 55  
    public abstract JSONArray handleRepeatedReferenceAsArray( Object reference );
 56  
 
 57  
    /**
 58  
     * Handle a repeated reference<br>
 59  
     * Must return a valid JSONObject or null.
 60  
     *
 61  
     * @param reference the repeated reference.
 62  
     */
 63  
    public abstract JSONObject handleRepeatedReferenceAsObject( Object reference );
 64  
 
 65  0
    private static final class LenientCycleDetectionStrategy extends CycleDetectionStrategy {
 66  
       public JSONArray handleRepeatedReferenceAsArray( Object reference ) {
 67  0
          return new JSONArray();
 68  
       }
 69  
 
 70  
       public JSONObject handleRepeatedReferenceAsObject( Object reference ) {
 71  0
          return new JSONObject( true );
 72  
       }
 73  
    }
 74  
 
 75  
    /**
 76  
     * A cycle detection strategy that prevents any mention of the possible
 77  
     * conflict from appearing.
 78  
     *
 79  
     * @author small
 80  
     */
 81  0
    private static final class LenientNoRefCycleDetectionStrategy extends CycleDetectionStrategy {
 82  
       public JSONArray handleRepeatedReferenceAsArray( Object reference ) {
 83  0
          return IGNORE_PROPERTY_ARR;
 84  
       }
 85  
 
 86  
       public JSONObject handleRepeatedReferenceAsObject( Object reference ) {
 87  0
          return IGNORE_PROPERTY_OBJ;
 88  
       }
 89  
    }
 90  
 
 91  0
    private static final class StrictCycleDetectionStrategy extends CycleDetectionStrategy {
 92  
       public JSONArray handleRepeatedReferenceAsArray( Object reference ) {
 93  0
          throw new JSONException( "There is a cycle in the hierarchy!" );
 94  
       }
 95  
 
 96  
       public JSONObject handleRepeatedReferenceAsObject( Object reference ) {
 97  0
          throw new JSONException( "There is a cycle in the hierarchy!" );
 98  
       }
 99  
    }
 100  
 }