Coverage Report - org.simpleframework.xml.stream.CamelCaseBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
CamelCaseBuilder
80%
8/10
50%
2/4
2
CamelCaseBuilder$1
N/A
N/A
2
CamelCaseBuilder$Attribute
100%
10/10
75%
3/4
2
CamelCaseBuilder$Element
100%
8/8
25%
1/4
2
 
 1  
 /*
 2  
  * CamelCaseBuilder.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  
 /**
 22  
  * The <code>CamelCaseBuilder</code> is used to represent an XML style
 23  
  * that can be applied to a serialized object. A style can be used to
 24  
  * modify the element and attribute names for the generated document.
 25  
  * This styles can be used to generate camel case XML.
 26  
  * <pre>
 27  
  * 
 28  
  *    &lt;ExampleElement&gt;
 29  
  *        &lt;ChildElement exampleAttribute='example'&gt;
 30  
  *           &lt;InnerElement&gt;example&lt;/InnerElement&gt;
 31  
  *        &lt;/ChildElement&gt;
 32  
  *     &lt;/ExampleElement&gt;
 33  
  *     
 34  
  * </pre>
 35  
  * Above the camel case XML elements and attributes can be generated
 36  
  * from a style implementation. Styles enable the same objects to be
 37  
  * serialized in different ways, generating different styles of XML
 38  
  * without having to modify the class schema for that object.    
 39  
  * 
 40  
  * @author Niall Gallagher
 41  
  */
 42  
 class CamelCaseBuilder implements Style {
 43  
    
 44  
    /**
 45  
     * If true then the attribute will start with upper case.
 46  
     */
 47  
    protected final boolean attribute;
 48  
    
 49  
    /**
 50  
     * If true then the element will start with upper case.
 51  
     */
 52  
    protected final boolean element;
 53  
    
 54  
    /**
 55  
     * Constructor for the <code>CamelCaseBuilder</code> object. This 
 56  
     * is used to create a style that will create camel case XML 
 57  
     * attributes and elements allowing a consistent format for 
 58  
     * generated XML. Both the attribute an elements are configurable.
 59  
     * 
 60  
     * @param element if true the element will start as upper case
 61  
     * @param attribute if true the attribute starts as upper case
 62  
     */
 63  243
    public CamelCaseBuilder(boolean element, boolean attribute) {
 64  243
       this.attribute = attribute;
 65  243
       this.element = element;
 66  243
    }
 67  
 
 68  
    /**
 69  
     * This is used to generate the XML attribute representation of 
 70  
     * the specified name. Attribute names should ensure to keep the
 71  
     * uniqueness of the name such that two different names will
 72  
     * be styled in to two different strings.
 73  
     * 
 74  
     * @param name this is the attribute name that is to be styled
 75  
     * 
 76  
     * @return this returns the styled name of the XML attribute
 77  
     */
 78  
    public String getAttribute(String name) {
 79  280
       if(name != null) {
 80  280
          return new Attribute(name).process();
 81  
       }
 82  0
       return null;
 83  
    }
 84  
 
 85  
    /**
 86  
     * This is used to generate the XML element representation of 
 87  
     * the specified name. Element names should ensure to keep the
 88  
     * uniqueness of the name such that two different names will
 89  
     * be styled in to two different strings.
 90  
     * 
 91  
     * @param name this is the element name that is to be styled
 92  
     * 
 93  
     * @return this returns the styled name of the XML element
 94  
     */
 95  
    public String getElement(String name) {
 96  2168
       if(name != null) {
 97  2168
          return new Element(name).process();
 98  
       }
 99  0
       return null;
 100  
    }
 101  
    
 102  
    /**
 103  
     * This is used to parse the style for this builder. This takes 
 104  
     * all of the words split from the original string and builds all
 105  
     * of the processed tokens for the styles elements and attributes.
 106  
     * 
 107  
     * @author Niall Gallagher
 108  
     */
 109  2448
    private class Attribute extends Splitter {
 110  
       
 111  
       /**
 112  
        * This determines whether to capitalise a split token
 113  
        */
 114  
       private boolean capital;
 115  
       
 116  
       /**
 117  
        * Constructor for the <code>Attribute</code> object. This will
 118  
        * take the original string and parse it such that all of the
 119  
        * words are emitted and used to build the styled token.
 120  
        * 
 121  
        * @param source this is the original string to be parsed
 122  
        */
 123  2448
       private Attribute(String source) {
 124  2448
          super(source);
 125  2448
       }
 126  
       
 127  
       /**
 128  
        * This is used to parse the provided text in to the style that
 129  
        * is required. Manipulation of the text before committing it
 130  
        * ensures that the text adheres to the required style.
 131  
        * 
 132  
        * @param text this is the text buffer to acquire the token from
 133  
        * @param off this is the offset in the buffer token starts at
 134  
        * @param len this is the length of the token to be parsed
 135  
        */
 136  
       @Override
 137  
       protected void parse(char[] text, int off, int len) {
 138  303
          if(attribute || capital) {
 139  26
             text[off] = toUpper(text[off]);
 140  
          }
 141  303
          capital = true;
 142  303
       }
 143  
       
 144  
       /**
 145  
        * This is used to commit the provided text in to the style that
 146  
        * is required. Committing the text to the buffer assembles the
 147  
        * tokens resulting in a complete token.
 148  
        * 
 149  
        * @param text this is the text buffer to acquire the token from
 150  
        * @param off this is the offset in the buffer token starts at
 151  
        * @param len this is the length of the token to be committed
 152  
        */
 153  
       @Override
 154  
       protected void commit(char[] text, int off, int len) {
 155  3098
          builder.append(text, off, len);
 156  3098
       }
 157  
    }
 158  
    
 159  
    /**
 160  
     * This is used to parse the style for this builder. This takes 
 161  
     * all of the words split from the original string and builds all
 162  
     * of the processed tokens for the styles elements and attributes.
 163  
     * 
 164  
     * @author Niall Gallagher
 165  
     */
 166  2168
    private class Element extends Attribute {
 167  
       
 168  
       /**
 169  
        * This determines whether to capitalise a split token
 170  
        */
 171  
       private boolean capital;
 172  
       
 173  
       /**
 174  
        * Constructor for the <code>Element</code> object. This will
 175  
        * take the original string and parse it such that all of the
 176  
        * words are emitted and used to build the styled token.
 177  
        * 
 178  
        * @param source this is the original string to be parsed
 179  
        */
 180  2168
       private Element(String source) {
 181  2168
          super(source);
 182  2168
       }
 183  
       
 184  
       /**
 185  
        * This is used to parse the provided text in to the style that
 186  
        * is required. Manipulation of the text before committing it
 187  
        * ensures that the text adheres to the required style.
 188  
        * 
 189  
        * @param text this is the text buffer to acquire the token from
 190  
        * @param off this is the offset in the buffer token starts at
 191  
        * @param len this is the length of the token to be parsed
 192  
        */
 193  
       @Override
 194  
       protected void parse(char[] text, int off, int len) {
 195  2740
          if(element || capital) {
 196  2740
             text[off] = toUpper(text[off]);
 197  
          }
 198  2740
          capital = true;
 199  2740
       }
 200  
    }
 201  
 
 202  
 }