Coverage Report - org.simpleframework.xml.core.Caller
 
Classes in this File Line Coverage Branch Coverage Complexity
Caller
100%
27/27
100%
12/12
2.143
 
 1  
 /*
 2  
  * Caller.java June 2007
 3  
  *
 4  
  * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.core;
 20  
 
 21  
 /**
 22  
  * The <code>Caller</code> acts as a means for the schema to invoke
 23  
  * the callback methods on an object. This ensures that the correct
 24  
  * method is invoked within the schema class. If the annotated method
 25  
  * accepts a map then this will provide that map to the method. This
 26  
  * also ensures that if specific annotation is not present in the 
 27  
  * class that no action is taken on a persister callback. 
 28  
  * 
 29  
  * @author Niall Gallagher
 30  
  */
 31  
 class Caller {
 32  
    
 33  
    /**
 34  
     * This is the pointer to the schema class commit function.
 35  
     */
 36  
    private final Function commit;
 37  
 
 38  
    /**
 39  
     * This is the pointer to the schema class validation function.
 40  
     */
 41  
    private final Function validate;
 42  
 
 43  
    /**
 44  
     * This is the pointer to the schema class persist function.
 45  
     */
 46  
    private final Function persist;
 47  
 
 48  
    /**
 49  
     * This is the pointer to the schema class complete function.
 50  
     */
 51  
    private final Function complete;
 52  
    
 53  
    /**
 54  
     * This is the pointer to the schema class replace function.
 55  
     */
 56  
    private final Function replace;
 57  
    
 58  
    /**
 59  
     * This is the pointer to the schema class resolve function.
 60  
     */
 61  
    private final Function resolve;
 62  
    
 63  
    /**
 64  
     * This is the context that is used to invoke the functions.
 65  
     */
 66  
    private final Context context;
 67  
    
 68  
    /**
 69  
     * Constructor for the <code>Caller</code> object. This is used 
 70  
     * to wrap the schema class such that callbacks from the persister
 71  
     * can be dealt with in a seamless manner. This ensures that the
 72  
     * correct function and arguments are provided to the functions.
 73  
     * element and attribute XML annotations scanned from
 74  
     * 
 75  
     * @param schema this is the scanner that contains the functions
 76  
     * @param context this is the context used to acquire the session
 77  
     */
 78  1352429
    public Caller(Scanner schema, Context context) {     
 79  1352429
       this.validate = schema.getValidate();      
 80  1352429
       this.complete = schema.getComplete();
 81  1352429
       this.replace = schema.getReplace();
 82  1352429
       this.resolve = schema.getResolve();
 83  1352429
       this.persist = schema.getPersist();  
 84  1352429
       this.commit = schema.getCommit();  
 85  1352429
       this.context = context;
 86  1352429
    }
 87  
    
 88  
    /**
 89  
     * This is used to replace the deserialized object with another
 90  
     * instance, perhaps of a different type. This is useful when an
 91  
     * XML schema class acts as a reference to another XML document
 92  
     * which needs to be loaded externally to create an object of
 93  
     * a different type.
 94  
     * 
 95  
     * @param source the source object to invoke the function on
 96  
     * 
 97  
     * @return this returns the object that acts as the replacement
 98  
     * 
 99  
     * @throws Exception if the replacement function cannot complete
 100  
     */
 101  
    public Object replace(Object source) throws Exception {
 102  420111
       if(replace != null) {        
 103  13
          return replace.call(context, source);
 104  
       }
 105  420098
       return source;
 106  
    }
 107  
    
 108  
    /**
 109  
     * This is used to replace the deserialized object with another
 110  
     * instance, perhaps of a different type. This is useful when an
 111  
     * XML schema class acts as a reference to another XML document
 112  
     * which needs to be loaded externally to create an object of
 113  
     * a different type.
 114  
     * 
 115  
     * @param source the source object to invoke the function on 
 116  
     * 
 117  
     * @return this returns the object that acts as the replacement
 118  
     * 
 119  
     * @throws Exception if the replacement function cannot complete
 120  
     */
 121  
    public Object resolve(Object source) throws Exception {
 122  712954
       if(resolve != null) {
 123  2
          return resolve.call(context, source);
 124  
       }
 125  712952
       return source;
 126  
    }
 127  
    
 128  
    /**
 129  
     * This method is used to invoke the provided objects commit function
 130  
     * during the deserialization process. The commit function must be
 131  
     * marked with the <code>Commit</code> annotation so that when the
 132  
     * object is deserialized the persister has a chance to invoke the
 133  
     * function so that the object can build further data structures.
 134  
     * 
 135  
     * @param source this is the object that has just been deserialized
 136  
     * 
 137  
     * @throws Exception thrown if the commit process cannot complete
 138  
     */
 139  
    public void commit(Object source) throws Exception {
 140  712954
       if(commit != null) {
 141  13
          commit.call(context, source);
 142  
       }
 143  712954
    }
 144  
 
 145  
    /**
 146  
     * This method is used to invoke the provided objects validation
 147  
     * function during the deserialization process. The validation function
 148  
     * must be marked with the <code>Validate</code> annotation so that
 149  
     * when the object is deserialized the persister has a chance to 
 150  
     * invoke that function so that object can validate its field values.
 151  
     * 
 152  
     * @param source this is the object that has just been deserialized
 153  
     * 
 154  
     * @throws Exception thrown if the validation process failed
 155  
     */
 156  
    public void validate(Object source) throws Exception {
 157  712954
       if(validate != null) {
 158  13
          validate.call(context, source);
 159  
       }
 160  712954
    }
 161  
    
 162  
    /**
 163  
     * This method is used to invoke the provided objects persistence
 164  
     * function. This is invoked during the serialization process to
 165  
     * get the object a chance to perform an necessary preparation
 166  
     * before the serialization of the object proceeds. The persist
 167  
     * function must be marked with the <code>Persist</code> annotation.
 168  
     * 
 169  
     * @param source the object that is about to be serialized
 170  
     * 
 171  
     * @throws Exception thrown if the object cannot be persisted
 172  
     */
 173  
    public void persist(Object source) throws Exception {
 174  217262
       if(persist != null) {
 175  1
          persist.call(context, source);
 176  
       }
 177  217262
    }
 178  
    
 179  
    /**
 180  
     * This method is used to invoke the provided objects completion
 181  
     * function. This is invoked after the serialization process has
 182  
     * completed and gives the object a chance to restore its state
 183  
     * if the persist function required some alteration or locking.
 184  
     * This is marked with the <code>Complete</code> annotation.
 185  
     * 
 186  
     * @param source this is the object that has been serialized
 187  
     * 
 188  
     * @throws Exception thrown if the object cannot complete
 189  
     */
 190  
    public void complete(Object source) throws Exception {
 191  217406
       if(complete != null) {
 192  1
          complete.call(context, source);
 193  
       }
 194  217406
    }
 195  
 }