001/* 002 * HyphenStyle.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>HyphenStyle</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 hyphenated XML. 026 * <pre> 027 * 028 * <example-element> 029 * <child-element example-attribute='example'> 030 * <inner-element>example</inner-element> 031 * </child-element> 032 * </example-element> 033 * 034 * </pre> 035 * Above the hyphenated 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 HyphenStyle 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>HyphenStyle</code> object. This is 056 * used to create a style that will hyphenate XML attributes 057 * and elements allowing a consistent format for generated XML. 058 */ 059 public HyphenStyle() { 060 this.style = new HyphenBuilder(); 061 this.builder = new Builder(style); 062 } 063 064 /** 065 * This is used to generate the XML attribute representation of 066 * the specified name. Attribute names should ensure to keep the 067 * uniqueness of the name such that two different names will 068 * be styled in to two different strings. 069 * 070 * @param name this is the attribute name that is to be styled 071 * 072 * @return this returns the styled name of the XML attribute 073 */ 074 public String getAttribute(String name) { 075 return builder.getAttribute(name); 076 } 077 078 /** 079 * This is used to set the attribute values within this builder. 080 * Overriding the attribute values ensures that the default 081 * algorithm does not need to determine each of the values. It 082 * allows special behaviour that the user may require for XML. 083 * 084 * @param name the name of the XML attribute to be overridden 085 * @param value the value that is to be used for that attribute 086 */ 087 public void setAttribute(String name, String value) { 088 builder.setAttribute(name, value); 089 } 090 091 /** 092 * This is used to generate the XML element representation of 093 * the specified name. Element names should ensure to keep the 094 * uniqueness of the name such that two different names will 095 * be styled in to two different strings. 096 * 097 * @param name this is the element name that is to be styled 098 * 099 * @return this returns the styled name of the XML element 100 */ 101 public String getElement(String name) { 102 return builder.getElement(name); 103 } 104 105 /** 106 * This is used to set the element values within this builder. 107 * Overriding the element values ensures that the default 108 * algorithm does not need to determine each of the values. It 109 * allows special behaviour that the user may require for XML. 110 * 111 * @param name the name of the XML element to be overridden 112 * @param value the value that is to be used for that element 113 */ 114 public void setElement(String name, String value) { 115 builder.setElement(name, value); 116 } 117}