Coverage Report - org.simpleframework.xml.stream.Builder
 
Classes in this File Line Coverage Branch Coverage Complexity
Builder
82%
19/23
75%
6/8
2.2
 
 1  
 /*
 2  
  * Builder.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.stream;
 20  
 
 21  
 import org.simpleframework.xml.util.Cache;
 22  
 import org.simpleframework.xml.util.ConcurrentCache;
 23  
 
 24  
 /**
 25  
  * The <code>Builder</code> class is used to represent an XML style
 26  
  * that can be applied to a serialized object. A style can be used to
 27  
  * modify the element and attribute names for the generated document.
 28  
  * Styles can be used to generate hyphenated or camel case XML.
 29  
  * <pre>
 30  
  * 
 31  
  *    &lt;example-element&gt;
 32  
  *        &lt;child-element example-attribute='example'&gt;
 33  
  *           &lt;inner-element&gt;example&lt;/inner-element&gt;
 34  
  *        &lt;/child-element&gt;
 35  
  *     &lt;/example-element&gt;
 36  
  *     
 37  
  * </pre>
 38  
  * Above the hyphenated XML elements and attributes can be generated
 39  
  * from a style implementation. Styles enable the same objects to be
 40  
  * serialized in different ways, generating different styles of XML
 41  
  * without having to modify the class schema for that object.    
 42  
  * 
 43  
  * @author Niall Gallagher
 44  
  */
 45  
 class Builder implements Style {
 46  
    
 47  
    /**
 48  
     * This is the cache for the constructed attribute values.
 49  
     */
 50  
    private final Cache<String> attributes;
 51  
    
 52  
    /**
 53  
     * This is the cache for the constructed element values. 
 54  
     */
 55  
    private final Cache<String> elements;
 56  
    
 57  
    /**
 58  
     * This is the style object used to create the values used.
 59  
     */
 60  
    private final Style style;
 61  
    
 62  
    /**
 63  
     * Constructor for the <code>Builder</code> object. This will cache
 64  
     * values constructed from the inner style object, which allows the
 65  
     * results from the style to retrieved quickly the second time.
 66  
     * 
 67  
     * @param style this is the internal style object to be used
 68  
     */
 69  474
    public Builder(Style style) {
 70  474
       this.attributes = new ConcurrentCache<String>();
 71  474
       this.elements = new ConcurrentCache<String>();
 72  474
       this.style = style;
 73  474
    }
 74  
 
 75  
    /**
 76  
     * This is used to generate the XML attribute representation of 
 77  
     * the specified name. Attribute names should ensure to keep the
 78  
     * uniqueness of the name such that two different names will
 79  
     * be styled in to two different strings.
 80  
     * 
 81  
     * @param name this is the attribute name that is to be styled
 82  
     * 
 83  
     * @return this returns the styled name of the XML attribute
 84  
     */
 85  
    public String getAttribute(String name) {
 86  7768
       String value = attributes.fetch(name);
 87  
       
 88  7768
       if(value != null) {
 89  7211
          return value;
 90  
       }
 91  557
       value = style.getAttribute(name);
 92  
       
 93  557
       if(value != null) {
 94  557
          attributes.cache(name, value);
 95  
       }
 96  557
       return value;
 97  
    }
 98  
 
 99  
    /**
 100  
     * This is used to generate the XML element representation of 
 101  
     * the specified name. Element names should ensure to keep the
 102  
     * uniqueness of the name such that two different names will
 103  
     * be styled in to two different strings.
 104  
     * 
 105  
     * @param name this is the element name that is to be styled
 106  
     * 
 107  
     * @return this returns the styled name of the XML element
 108  
     */
 109  
    public String getElement(String name) {
 110  46506
       String value = elements.fetch(name);
 111  
       
 112  46506
       if(value != null) {
 113  42883
          return value;
 114  
       }
 115  3623
       value = style.getElement(name);
 116  
       
 117  3623
       if(value != null) {
 118  3623
          elements.cache(name, value);
 119  
       }
 120  3623
       return value;
 121  
    }
 122  
 
 123  
    /**
 124  
     * This is used to set the attribute values within this builder.
 125  
     * Overriding the attribute values ensures that the default
 126  
     * algorithm does not need to determine each of the values. It
 127  
     * allows special behaviour that the user may require for XML.
 128  
     * 
 129  
     * @param name the name of the XML attribute to be overridden
 130  
     * @param value the value that is to be used for that attribute
 131  
     */
 132  
    public void setAttribute(String name, String value) {
 133  0
       attributes.cache(name, value);
 134  0
    }
 135  
 
 136  
    /**
 137  
     * This is used to set the element values within this builder.
 138  
     * Overriding the element values ensures that the default
 139  
     * algorithm does not need to determine each of the values. It
 140  
     * allows special behaviour that the user may require for XML.
 141  
     * 
 142  
     * @param name the name of the XML element to be overridden
 143  
     * @param value the value that is to be used for that element
 144  
     */
 145  
    public void setElement(String name, String value) {
 146  0
       elements.cache(name, value);   
 147  0
    }
 148  
 }