Coverage Report - org.simpleframework.xml.stream.HyphenBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
HyphenBuilder
75%
6/8
50%
2/4
2
HyphenBuilder$1
N/A
N/A
2
HyphenBuilder$Parser
100%
10/10
100%
2/2
2
 
 1  
 /*
 2  
  * HyphenBuilder.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>HyphenBuilder</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 hyphenated XML.
 26  
  * <pre>
 27  
  * 
 28  
  *    &lt;example-element&gt;
 29  
  *        &lt;child-element example-attribute='example'&gt;
 30  
  *           &lt;inner-element&gt;example&lt;/inner-element&gt;
 31  
  *        &lt;/child-element&gt;
 32  
  *     &lt;/example-element&gt;
 33  
  *     
 34  
  * </pre>
 35  
  * Above the hyphenated 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  231
 class HyphenBuilder implements Style {
 43  
 
 44  
    /**
 45  
     * This is used to generate the XML attribute representation of 
 46  
     * the specified name. Attribute names should ensure to keep the
 47  
     * uniqueness of the name such that two different names will
 48  
     * be styled in to two different strings.
 49  
     * 
 50  
     * @param name this is the attribute name that is to be styled
 51  
     * 
 52  
     * @return this returns the styled name of the XML attribute
 53  
     */
 54  
    public String getAttribute(String name) {
 55  277
       if(name != null) {
 56  277
          return new Parser(name).process();
 57  
       }
 58  0
       return null;
 59  
    }
 60  
 
 61  
    /**
 62  
     * This is used to generate the XML element representation of 
 63  
     * the specified name. Element names should ensure to keep the
 64  
     * uniqueness of the name such that two different names will
 65  
     * be styled in to two different strings.
 66  
     * 
 67  
     * @param name this is the element name that is to be styled
 68  
     * 
 69  
     * @return this returns the styled name of the XML element
 70  
     */
 71  
    public String getElement(String name) {
 72  1455
       if(name != null) {
 73  1455
          return new Parser(name).process();
 74  
       }
 75  0
       return null;
 76  
    }
 77  
    
 78  
    /**
 79  
     * This is used to parse the style for this builder. This takes 
 80  
     * all of the words split from the original string and builds all
 81  
     * of the processed tokens for the styles elements and attributes.
 82  
     * 
 83  
     * @author Niall Gallagher
 84  
     */
 85  231
    private class Parser extends Splitter {
 86  
       
 87  
       /**
 88  
        * Constructor for the <code>Parser</code> object. This will
 89  
        * take the original string and parse it such that all of the
 90  
        * words are emitted and used to build the styled token.
 91  
        * 
 92  
        * @param source this is the original string to be parsed
 93  
        */
 94  1732
       private Parser(String source) {
 95  1732
          super(source);
 96  1732
       }
 97  
       
 98  
       /**
 99  
        * This is used to parse the provided text in to the style that
 100  
        * is required. Manipulation of the text before committing it
 101  
        * ensures that the text adheres to the required style.
 102  
        * 
 103  
        * @param text this is the text buffer to acquire the token from
 104  
        * @param off this is the offset in the buffer token starts at
 105  
        * @param len this is the length of the token to be parsed
 106  
        */
 107  
       @Override
 108  
       protected void parse(char[] text, int off, int len) {
 109  2302
          text[off] = toLower(text[off]);
 110  2302
       }
 111  
       
 112  
       /**
 113  
        * This is used to commit the provided text in to the style that
 114  
        * is required. Committing the text to the buffer assembles the
 115  
        * tokens resulting in a complete token.
 116  
        * 
 117  
        * @param text this is the text buffer to acquire the token from
 118  
        * @param off this is the offset in the buffer token starts at
 119  
        * @param len this is the length of the token to be committed
 120  
        */
 121  
       @Override
 122  
       protected void commit(char[] text, int off, int len) {
 123  2351
          builder.append(text, off, len);
 124  
          
 125  2351
          if(off + len < count) {
 126  619
             builder.append('-');
 127  
          }
 128  2351
       }
 129  
    }
 130  
 
 131  
 }