Coverage Report - org.simpleframework.xml.core.Model
 
Classes in this File Line Coverage Branch Coverage Complexity
Model
N/A
N/A
1
 
 1  
 /*
 2  
  * Model.java November 2010
 3  
  *
 4  
  * Copyright (C) 2010, 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>Model</code> interface represents the core data structure
 23  
  * used for representing an XML schema. This is effectively a tree
 24  
  * like structure in that it can contain other models as well as XML
 25  
  * attributes and elements. Each model represents a context within an
 26  
  * XML document, each context is navigated to with an XPath expression.
 27  
  * <p>
 28  
  * The model is responsible for building the element and attribute
 29  
  * labels used to read and write and also to ensure the correct order
 30  
  * of the XML elements and attributes is enforced. Once the model has
 31  
  * been completed it can then be validated to ensure its contents 
 32  
  * represent a valid XML structure.
 33  
  * 
 34  
  * @author Niall Gallagher
 35  
  * 
 36  
  * @see org.simpleframework.xml.core.Section
 37  
  */
 38  
 interface Model extends Iterable<String> {
 39  
    
 40  
    /**
 41  
     * Used to determine if a model is empty. A model is considered
 42  
     * empty if that model does not contain any registered elements
 43  
     * or attributes. However, if the model contains other models
 44  
     * that have registered elements or attributes it is not empty.
 45  
     * 
 46  
     * @return true if the model does not contain registrations
 47  
     */
 48  
    boolean isEmpty();
 49  
    
 50  
    /**
 51  
     * This is used to determine if the provided name represents
 52  
     * a model. This is useful when validating the model as it
 53  
     * allows determination of a named model, which is an element. 
 54  
     * 
 55  
     * @param name this is the name of the section to determine
 56  
     * 
 57  
     * @return this returns true if the model is registered
 58  
     */
 59  
    boolean isModel(String name);
 60  
    
 61  
    /**
 62  
     * This is used to determine if the provided name represents
 63  
     * an element. This is useful when validating the model as 
 64  
     * it allows determination of a named XML element. 
 65  
     * 
 66  
     * @param name this is the name of the section to determine
 67  
     * 
 68  
     * @return this returns true if the element is registered
 69  
     */
 70  
    boolean isElement(String name);
 71  
    
 72  
    /**
 73  
     * This is used to determine if the provided name represents
 74  
     * an attribute. This is useful when validating the model as 
 75  
     * it allows determination of a named XML attribute
 76  
     * 
 77  
     * @param name this is the name of the attribute to determine
 78  
     * 
 79  
     * @return this returns true if the attribute is registered
 80  
     */
 81  
    boolean isAttribute(String name);
 82  
    
 83  
    /**
 84  
     * This is used to perform a recursive search of the models that
 85  
     * have been registered, if a model has elements or attributes
 86  
     * then this returns true. If however no other model contains 
 87  
     * any attributes or elements then this will return false.
 88  
     * 
 89  
     * @return true if any model has elements or attributes
 90  
     */
 91  
    boolean isComposite();
 92  
    
 93  
    /**
 94  
     * This is used to validate the model to ensure all elements and
 95  
     * attributes are valid. Validation also ensures that any order
 96  
     * specified by an annotated class did not contain invalid XPath
 97  
     * values, or redundant elements and attributes.
 98  
     * 
 99  
     * @param type this is the object type representing the schema
 100  
     * 
 101  
     * @throws Exception if text and element annotations are present
 102  
     */
 103  
    void validate(Class type) throws Exception;
 104  
    
 105  
    /**
 106  
     * This is used to register an XML entity within the model. The
 107  
     * registration process has the affect of telling the model that
 108  
     * it will contain a specific, named, XML entity. It also has 
 109  
     * the affect of ordering them within the model, such that the
 110  
     * first registered entity is the first iterated over.
 111  
     * 
 112  
     * @param label this is the label to register with the model
 113  
     */
 114  
    void register(Label label) throws Exception;
 115  
    
 116  
    /**
 117  
     * This is used to register an XML entity within the model. The
 118  
     * registration process has the affect of telling the model that
 119  
     * it will contain a specific, named, XML entity. It also has 
 120  
     * the affect of ordering them within the model, such that the
 121  
     * first registered entity is the first iterated over.
 122  
     * 
 123  
     * @param label this is the label to register with the model
 124  
     */
 125  
    void registerText(Label label) throws Exception;
 126  
    
 127  
    /**
 128  
     * This is used to register an XML entity within the model. The
 129  
     * registration process has the affect of telling the model that
 130  
     * it will contain a specific, named, XML entity. It also has 
 131  
     * the affect of ordering them within the model, such that the
 132  
     * first registered entity is the first iterated over.
 133  
     * 
 134  
     * @param label this is the label to register with the model
 135  
     */
 136  
    void registerElement(Label label) throws Exception;
 137  
    
 138  
    /**
 139  
     * This is used to register an XML entity within the model. The
 140  
     * registration process has the affect of telling the model that
 141  
     * it will contain a specific, named, XML entity. It also has 
 142  
     * the affect of ordering them within the model, such that the
 143  
     * first registered entity is the first iterated over.
 144  
     * 
 145  
     * @param label this is the label to register with the model
 146  
     */
 147  
    void registerAttribute(Label label) throws Exception;
 148  
    
 149  
    /**
 150  
     * This is used to register an XML entity within the model. The
 151  
     * registration process has the affect of telling the model that
 152  
     * it will contain a specific, named, XML entity. It also has 
 153  
     * the affect of ordering them within the model, such that the
 154  
     * first registered entity is the first iterated over.
 155  
     * 
 156  
     * @param name this is the name of the element to register
 157  
     */
 158  
    void registerElement(String name) throws Exception;
 159  
    
 160  
    /**
 161  
     * This is used to register an XML entity within the model. The
 162  
     * registration process has the affect of telling the model that
 163  
     * it will contain a specific, named, XML entity. It also has 
 164  
     * the affect of ordering them within the model, such that the
 165  
     * first registered entity is the first iterated over.
 166  
     * 
 167  
     * @param name this is the name of the element to register
 168  
     */
 169  
    void registerAttribute(String name) throws Exception;
 170  
    
 171  
    /**
 172  
     * This is used to register a <code>Model</code> within this
 173  
     * model. Registration of a model creates a tree of models that
 174  
     * can be used to represent an XML structure. Each model can
 175  
     * contain elements and attributes associated with a type.
 176  
     * 
 177  
     * @param name this is the name of the model to be registered
 178  
     * @param prefix this is the prefix used for this model
 179  
     * @param index this is the index used to order the model
 180  
     * 
 181  
     * @return this returns the model that was registered
 182  
     */
 183  
    Model register(String name, String prefix, int index) throws Exception;   
 184  
    
 185  
    /**
 186  
     * This method is used to look for a <code>Model</code> that
 187  
     * matches the specified element name. If no such model exists
 188  
     * then this will return null. This is used as an alternative
 189  
     * to providing an XPath expression to navigate the tree.
 190  
     * 
 191  
     * @param name this is the name of the model to be acquired
 192  
     * @param index this is the index used to order the model
 193  
     * 
 194  
     * @return this returns the model located by the expression
 195  
     */
 196  
    Model lookup(String name, int index);
 197  
    
 198  
    /**
 199  
     * This method is used to look for a <code>Model</code> that
 200  
     * matches the specified expression. If no such model exists
 201  
     * then this will return null. Using an XPath expression allows
 202  
     * a tree like structure to be navigated with ease.
 203  
     * 
 204  
     * @param path an XPath expression used to locate a model
 205  
     * 
 206  
     * @return this returns the model located by the expression
 207  
     */
 208  
    Model lookup(Expression path);
 209  
    
 210  
    /**
 211  
     * This is used to build a map from a <code>Context</code> object.
 212  
     * Building a map in this way ensures that any style specified by
 213  
     * the context can be used to create the XML element and attribute
 214  
     * names in the styled format. It also ensures that the model
 215  
     * remains immutable as it only provides copies of its data.
 216  
     * 
 217  
     * @return this returns a map built from the specified context
 218  
     */
 219  
    LabelMap getElements() throws Exception;
 220  
    
 221  
    /**
 222  
     * This is used to build a map from a <code>Context</code> object.
 223  
     * Building a map in this way ensures that any style specified by
 224  
     * the context can be used to create the XML element and attribute
 225  
     * names in the styled format. It also ensures that the model
 226  
     * remains immutable as it only provides copies of its data.
 227  
     * 
 228  
     * @return this returns a map built from the specified context
 229  
     */
 230  
    LabelMap getAttributes() throws Exception;
 231  
    
 232  
    /**
 233  
     * This is used to build a map from a <code>Context</code> object.
 234  
     * Building a map in this way ensures that any style specified by
 235  
     * the context can be used to create the XML element and attribute
 236  
     * names in the styled format. It also ensures that the model
 237  
     * remains immutable as it only provides copies of its data.
 238  
     * 
 239  
     * @return this returns a map built from the specified context
 240  
     */
 241  
    ModelMap getModels() throws Exception;
 242  
    
 243  
    /**
 244  
     * This returns a text label if one is associated with the model.
 245  
     * If the model does not contain a text label then this method
 246  
     * will return null. Any model with a text label should not be
 247  
     * composite and should not contain any elements.
 248  
     * 
 249  
     * @return this is the optional text label for this model
 250  
     */
 251  
    Label getText();
 252  
    
 253  
    /**
 254  
     * This returns an <code>Expression</code> representing the path
 255  
     * this model exists at within the class schema. This should 
 256  
     * never be null for any model that is not empty.
 257  
     * 
 258  
     * @return this returns the expression associated with this
 259  
     */
 260  
    Expression getExpression();
 261  
    
 262  
    /**
 263  
     * This is used to acquire the path prefix for the model. The
 264  
     * path prefix is used when the model is transformed in to an
 265  
     * XML structure. This ensures that the XML element created to
 266  
     * represent the model contains the optional prefix.
 267  
     * 
 268  
     * @return this returns the prefix for this model
 269  
     */
 270  
    String getPrefix();
 271  
    
 272  
    /**
 273  
     * This is used to return the name of the model. The name is 
 274  
     * must be a valid XML element name. It is used when a style
 275  
     * is applied to a section as the model name must be styled.
 276  
     * 
 277  
     * @return this returns the name of this model instance
 278  
     */
 279  
    String getName();   
 280  
 
 281  
    /**
 282  
     * This method is used to return the index of the model. The
 283  
     * index is the order that this model appears within the XML
 284  
     * document. Having an index allows multiple models of the
 285  
     * same name to be inserted in to a sorted collection.
 286  
     * 
 287  
     * @return this is the index of this model instance
 288  
     */
 289  
    int getIndex();
 290  
 }