Coverage Report - org.simpleframework.xml.core.NamespaceDecorator
 
Classes in this File Line Coverage Branch Coverage Complexity
NamespaceDecorator
100%
27/27
87%
7/8
1.571
 
 1  
 /*
 2  
  * NamespaceDecorator.java July 2008
 3  
  *
 4  
  * Copyright (C) 2008, 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 java.util.ArrayList;
 22  
 import java.util.List;
 23  
 
 24  
 import org.simpleframework.xml.Namespace;
 25  
 import org.simpleframework.xml.stream.NamespaceMap;
 26  
 import org.simpleframework.xml.stream.OutputNode;
 27  
 
 28  
 /**
 29  
  * The <code>NamespaceDecorator</code> object is used to decorate
 30  
  * any output node with namespaces. All namespaces added to this are
 31  
  * applied to nodes that require decoration. This can add namespaces
 32  
  * to the node as well as setting the primary namespace reference
 33  
  * for the node. This results in qualification for the node.
 34  
  * 
 35  
  * @author Niall Gallagher
 36  
  * 
 37  
  * @see org.simpleframework.xml.core.Qualifier
 38  
  */
 39  
 class NamespaceDecorator implements Decorator {
 40  
    
 41  
    /**
 42  
     * This is used to contain the namespaces used for scoping.
 43  
     */
 44  
    private List<Namespace> scope;
 45  
    
 46  
    /**
 47  
     * This is used to set the primary namespace reference used.
 48  
     */
 49  
    private Namespace primary;
 50  
    
 51  
    /**
 52  
     * Constructor for the <code>NamespaceDecorator</code> object. A
 53  
     * namespace decorator can be used for applying namespaces to a
 54  
     * specified node. It can add namespaces to set the scope of the
 55  
     * namespace reference to the node and it can also be used to set
 56  
     * the primary namespace reference used for the node.
 57  
     */
 58  8297
    public NamespaceDecorator() {
 59  8297
       this.scope = new ArrayList<Namespace>();
 60  8297
    }
 61  
    
 62  
    /**
 63  
     * This is used to set the primary namespace for nodes that will
 64  
     * be decorated by the namespace decorator. If no namespace is set
 65  
     * using this method then this decorator will leave the namespace
 66  
     * reference unchanged and only add namespaces for scoping.
 67  
     * 
 68  
     * @param namespace this is the primary namespace to be set
 69  
     */
 70  
    public void set(Namespace namespace) {
 71  349
       if(namespace != null) {
 72  349
          add(namespace);
 73  
       }
 74  349
       primary = namespace;
 75  349
    }
 76  
    
 77  
    /**
 78  
     * This is used to add a namespace to the decorator so that it can
 79  
     * be added to decorated nodes. Namespaces that are added will be
 80  
     * set on the element so that child elements can reference the
 81  
     * namespace and will thus inherit the prefix from that elment.
 82  
     * 
 83  
     * @param namespace this is the namespace to be added for scoping
 84  
     */
 85  
    public void add(Namespace namespace) {
 86  729
       scope.add(namespace);
 87  729
    }
 88  
    
 89  
    /**
 90  
     * This method is used to decorate the provided node. This node 
 91  
     * can be either an XML element or an attribute. Decorations that
 92  
     * can be applied to the node by invoking this method include
 93  
     * things like comments and namespaces.
 94  
     * 
 95  
     * @param node this is the node that is to be decorated by this
 96  
     */
 97  
    public void decorate(OutputNode node) {
 98  451035
       decorate(node, null);
 99  451035
    }
 100  
    
 101  
    /**
 102  
     * This method is used to decorate the provided node. This node 
 103  
     * can be either an XML element or an attribute. Decorations that
 104  
     * can be applied to the node by invoking this method include
 105  
     * things like namespaces and namespace lists. This can also be 
 106  
     * given another <code>Decorator</code> which is applied before 
 107  
     * this decorator, any common data can then be overwritten.
 108  
     * 
 109  
     * @param node this is the node that is to be decorated by this
 110  
     * @param decorator this is a secondary decorator to be applied
 111  
     */
 112  
    public void decorate(OutputNode node, Decorator decorator) {
 113  870734
       if(decorator != null) {
 114  103386
          decorator.decorate(node);
 115  
       }
 116  870734
       scope(node);
 117  870734
       namespace(node);
 118  870734
    }
 119  
    
 120  
    /**
 121  
     * This is use to apply for <code>NamespaceList</code> annotations 
 122  
     * on the node. If there is no namespace list then this will return 
 123  
     * and the node will be left unchanged. If however the namespace 
 124  
     * list is not empty the the namespaces are added.
 125  
     * 
 126  
     * @param node this is the node to apply the namespace list to
 127  
     */
 128  
    private void scope(OutputNode node) {
 129  870734
       NamespaceMap map = node.getNamespaces();
 130  
       
 131  870734
       for(Namespace next : scope) {
 132  579
          String reference = next.reference();
 133  579
          String prefix = next.prefix();
 134  
             
 135  579
          map.setReference(reference, prefix);
 136  579
       }
 137  870734
    }
 138  
    
 139  
    /**
 140  
     * This is use to apply the <code>Namespace</code> annotations on
 141  
     * the node. If there is no namespace then this will return and
 142  
     * the node will be left unchanged. If however the namespace is 
 143  
     * not null then the reference is applied to the specified node.
 144  
     * 
 145  
     * @param node this is the node to apply the namespace to
 146  
     */
 147  
    private void namespace(OutputNode node) {
 148  870734
       if(primary != null) {
 149  263
          String reference = primary.reference();
 150  
          
 151  263
          node.setReference(reference);
 152  
       }
 153  870734
    }
 154  
 }