001/* 002 * CamelCaseStyle.java July 2008 003 * 004 * Copyright (C) 2008, Niall Gallagher <niallg@users.sf.net> 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 015 * implied. See the License for the specific language governing 016 * permissions and limitations under the License. 017 */ 018 019package org.simpleframework.xml.stream; 020 021/** 022 * The <code>CamelCaseStyle</code> is used to represent an XML style 023 * that can be applied to a serialized object. A style can be used to 024 * modify the element and attribute names for the generated document. 025 * This styles can be used to generate camel case XML. 026 * <pre> 027 * 028 * <ExampleElement> 029 * <ChildElement exampleAttribute='example'> 030 * <InnerElement>example</InnerElement> 031 * </ChildElement> 032 * </ExampleElement> 033 * 034 * </pre> 035 * Above the camel case XML elements and attributes can be generated 036 * from a style implementation. Styles enable the same objects to be 037 * serialized in different ways, generating different styles of XML 038 * without having to modify the class schema for that object. 039 * 040 * @author Niall Gallagher 041 */ 042public class CamelCaseStyle implements Style { 043 044 /** 045 * This is used to perform the actual building of tokens. 046 */ 047 private final Builder builder; 048 049 /** 050 * This is the strategy used to generate the style tokens. 051 */ 052 private final Style style; 053 054 /** 055 * Constructor for the <code>CamelCaseStyle</code> object. This 056 * is used to create a style that will create camel case XML 057 * attributes and elements allowing a consistent format for 058 * generated XML. By default the elements have an upper case 059 * initial character and a lower case attribute. 060 */ 061 public CamelCaseStyle() { 062 this(true, false); 063 } 064 065 /** 066 * Constructor for the <code>CamelCaseStyle</code> object. This 067 * is used to create a style that will create camel case XML 068 * attributes and elements allowing a consistent format for 069 * generated XML. By default the attributes have a lower case 070 * initial character and an configurable element. 071 * 072 * @param element if true the element will start as upper case 073 */ 074 public CamelCaseStyle(boolean element) { 075 this(element, false); 076 } 077 078 /** 079 * Constructor for the <code>CamelCaseStyle</code> object. This 080 * is used to create a style that will create camel case XML 081 * attributes and elements allowing a consistent format for 082 * generated XML. Both the attribute an elements are configurable. 083 * 084 * @param element if true the element will start as upper case 085 * @param attribute if true the attribute starts as upper case 086 */ 087 public CamelCaseStyle(boolean element, boolean attribute) { 088 this.style = new CamelCaseBuilder(element, attribute); 089 this.builder = new Builder(style); 090 } 091 092 /** 093 * This is used to generate the XML attribute representation of 094 * the specified name. Attribute names should ensure to keep the 095 * uniqueness of the name such that two different names will 096 * be styled in to two different strings. 097 * 098 * @param name this is the attribute name that is to be styled 099 * 100 * @return this returns the styled name of the XML attribute 101 */ 102 public String getAttribute(String name) { 103 return builder.getAttribute(name); 104 } 105 106 /** 107 * This is used to set the attribute values within this builder. 108 * Overriding the attribute values ensures that the default 109 * algorithm does not need to determine each of the values. It 110 * allows special behaviour that the user may require for XML. 111 * 112 * @param name the name of the XML attribute to be overridden 113 * @param value the value that is to be used for that attribute 114 */ 115 public void setAttribute(String name, String value) { 116 builder.setAttribute(name, value); 117 } 118 119 /** 120 * This is used to generate the XML element representation of 121 * the specified name. Element names should ensure to keep the 122 * uniqueness of the name such that two different names will 123 * be styled in to two different strings. 124 * 125 * @param name this is the element name that is to be styled 126 * 127 * @return this returns the styled name of the XML element 128 */ 129 public String getElement(String name) { 130 return builder.getElement(name); 131 } 132 133 /** 134 * This is used to set the element values within this builder. 135 * Overriding the element values ensures that the default 136 * algorithm does not need to determine each of the values. It 137 * allows special behaviour that the user may require for XML. 138 * 139 * @param name the name of the XML element to be overridden 140 * @param value the value that is to be used for that element 141 */ 142 public void setElement(String name, String value) { 143 builder.setElement(name, value); 144 } 145}