Coverage Report - org.simpleframework.xml.core.ClassSchema
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassSchema
90%
18/20
N/A
1
 
 1  
 /*
 2  
  * Schema.java July 2006
 3  
  *
 4  
  * Copyright (C) 2006, 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  
 import org.simpleframework.xml.Version;
 22  
 
 23  
 /**
 24  
  * The <code>Schema</code> object is used to track which fields within
 25  
  * an object have been visited by a converter. This object is necessary
 26  
  * for processing <code>Composite</code> objects. In particular it is
 27  
  * necessary to keep track of which required nodes have been visited 
 28  
  * and which have not, if a required not has not been visited then the
 29  
  * XML source does not match the XML class schema and serialization
 30  
  * must fail before processing any further. 
 31  
  * 
 32  
  * @author Niall Gallagher
 33  
  */ 
 34  
 class ClassSchema implements Schema {
 35  
    
 36  
    /**
 37  
     * This is the instantiator used to create all object instances.
 38  
     */
 39  
    private final Instantiator factory;
 40  
    
 41  
    /**
 42  
     * This is the decorator associated with this schema object.
 43  
     */
 44  
    private final Decorator decorator;
 45  
 
 46  
    /**
 47  
     * This represents the XML section defined for the class schema.
 48  
     */
 49  
    private final Section section;
 50  
    
 51  
    /**
 52  
     * This is the version annotation for the XML class schema.
 53  
     */
 54  
    private final Version revision;
 55  
    
 56  
    /**
 57  
     * This is the pointer to the schema class replace method.
 58  
     */
 59  
    private final Caller caller;
 60  
    
 61  
    /**
 62  
     * This is the version label used to read the version attribute.
 63  
     */
 64  
    private final Label version;
 65  
    
 66  
    /**
 67  
     * This is used to represent a text value within the schema.
 68  
     */
 69  
    private final Label text;
 70  
    
 71  
    /**
 72  
     * This is the type that this class schema is representing.
 73  
     */
 74  
    private final Class type;
 75  
    
 76  
    /**
 77  
     * This is used to specify whether the type is a primitive class.
 78  
     */
 79  
    private final boolean primitive;
 80  
 
 81  
    /**
 82  
     * Constructor for the <code>Schema</code> object. This is used 
 83  
     * to wrap the element and attribute XML annotations scanned from
 84  
     * a class schema. The schema tracks all fields visited so that
 85  
     * a converter can determine if all fields have been serialized.
 86  
     * 
 87  
     * @param schema this contains all labels scanned from the class
 88  
     * @param context this is the context object for serialization
 89  
     */
 90  932318
    public ClassSchema(Scanner schema, Context context) throws Exception {  
 91  932318
       this.caller = schema.getCaller(context);
 92  932318
       this.factory = schema.getInstantiator();
 93  932318
       this.revision = schema.getRevision();
 94  932318
       this.decorator = schema.getDecorator();
 95  932318
       this.primitive = schema.isPrimitive();
 96  932318
       this.version = schema.getVersion();
 97  932318
       this.section = schema.getSection();
 98  932318
       this.text = schema.getText();
 99  932318
       this.type = schema.getType();
 100  932318
    }
 101  
    
 102  
    /**
 103  
     * This is used to determine whether the scanned class represents
 104  
     * a primitive type. A primitive type is a type that contains no
 105  
     * XML annotations and so cannot be serialized with an XML form.
 106  
     * Instead primitives a serialized using transformations.
 107  
     * 
 108  
     * @return this returns true if no XML annotations were found
 109  
     */
 110  
    public boolean isPrimitive() {
 111  217406
       return primitive;
 112  
    }
 113  
    
 114  
    /**
 115  
     * This is used to acquire the instantiator for the type. This is
 116  
     * used to create object instances based on the constructors that
 117  
     * have been annotated. If no constructors have been annotated
 118  
     * then this can be used to create default no argument instances.
 119  
     * 
 120  
     * @return this instantiator responsible for creating instances
 121  
     */
 122  
    public Instantiator getInstantiator() {
 123  713257
       return factory;
 124  
    }
 125  
    
 126  
    /**
 127  
     * This returns the <code>Label</code> that represents the version
 128  
     * annotation for the scanned class. Only a single version can
 129  
     * exist within the class if more than one exists an exception is
 130  
     * thrown. This will read only floating point types such as double.
 131  
     * 
 132  
     * @return this returns the label used for reading the version
 133  
     */
 134  
    public Label getVersion() {
 135  930245
       return version;
 136  
    }
 137  
    
 138  
    /**
 139  
     * This is the <code>Version</code> for the scanned class. It 
 140  
     * allows the deserialization process to be configured such that
 141  
     * if the version is different from the schema class none of
 142  
     * the fields and methods are required and unmatched elements
 143  
     * and attributes will be ignored.
 144  
     * 
 145  
     * @return this returns the version of the class that is scanned
 146  
     */
 147  
    public Version getRevision() {
 148  217262
       return revision;
 149  
    }
 150  
    
 151  
    /**
 152  
     * This is used to acquire the <code>Decorator</code> for this.
 153  
     * A decorator is an object that adds various details to the
 154  
     * node without changing the overall structure of the node. For
 155  
     * example comments and namespaces can be added to the node with
 156  
     * a decorator as they do not affect the deserialization.
 157  
     * 
 158  
     * @return this returns the decorator associated with this
 159  
     */
 160  
    public Decorator getDecorator() {
 161  0
       return decorator;
 162  
    }
 163  
    
 164  
    /**
 165  
     * This is used to acquire the <code>Caller</code> object. This
 166  
     * is used to call the callback methods within the object. If the
 167  
     * object contains no callback methods then this will return an
 168  
     * object that does not invoke any methods that are invoked. 
 169  
     * 
 170  
     * @return this returns the caller for the specified type
 171  
     */
 172  
    public Caller getCaller() {
 173  930389
       return caller;
 174  
    }
 175  
    
 176  
    /**
 177  
     * This is used to acquire the <code>Section</code> that defines
 178  
     * the XML structure for this class schema. A section, is the 
 179  
     * section of XML that the class is represented within. A
 180  
     * section contains all the elements and attributes defined for
 181  
     * the class in a tree like structure.
 182  
     * 
 183  
     * @return this returns the section defined for the schama
 184  
     */
 185  
    public Section getSection() {
 186  932174
       return section;
 187  
    }
 188  
    
 189  
    /**
 190  
     * This returns the <code>Label</code> that represents the text
 191  
     * annotation for the scanned class. Only a single text annotation
 192  
     * can be used per class, so this returns only a single label
 193  
     * rather than a <code>LabelMap</code> object. Also if this is
 194  
     * not null then the elements label map will be empty.
 195  
     * 
 196  
     * @return this returns the text label for the scanned class
 197  
     */
 198  
    public Label getText() {
 199  1929
       return text;
 200  
    }
 201  
    
 202  
    /**
 203  
     * This is used to acquire a description of the schema. This is
 204  
     * useful when debugging an issue as it allows a representation
 205  
     * of the instance to be viewed with the class it represents.
 206  
     * 
 207  
     * @return this returns a visible description of the schema
 208  
     */
 209  
    public String toString() {
 210  0
       return String.format("schema for %s", type);
 211  
    }
 212  
 }