001/* 002 * Attribute.java July 2006 003 * 004 * Copyright (C) 2006, 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; 020 021import java.lang.annotation.RetentionPolicy; 022import java.lang.annotation.Retention; 023 024/** 025 * The <code>Attribute</code> annotation represents a serializable XML 026 * attribute within an XML element. An object annotated with this is 027 * typically a primitive or enumerated type. Conversion from the 028 * attribute to primitive type is done with a <code>Transform</code> 029 * object. If a suitable transform can be found then this will convert 030 * the attribute string value to an object instance, which can be 031 * assigned to the annotated field, or passed to the annotated method. 032 * 033 * @author Niall Gallagher 034 * 035 * @see org.simpleframework.xml.transform.Transformer 036 */ 037@Retention(RetentionPolicy.RUNTIME) 038public @interface Attribute { 039 040 /** 041 * This represents the name of the XML attribute. Annotated fields 042 * or methods can optionally provide the name of the XML attribute 043 * they represent. If a name is not provided then the field or 044 * method name is used in its place. A name can be specified if 045 * the field or method name is not suitable for the XML attribute. 046 * 047 * @return the name of the XML attribute this represents 048 */ 049 String name() default ""; 050 051 /** 052 * This is used to provide a default value for the attribute if 053 * the annotated field or method is null. This ensures the the 054 * serialization process writes the attribute with a value even 055 * if the value is null, and allows deserialization to determine 056 * whether the value within the object was null or not. 057 * 058 * @return this returns the default attribute value to use 059 */ 060 String empty() default ""; 061 062 /** 063 * Determines whether the attribute is required within an XML 064 * element. Any field marked as not required will not have its 065 * value set when the object is deserialized. If an object is to 066 * be serialized only a null attribute will not appear as XML. 067 * 068 * @return true if the attribute is required, false otherwise 069 */ 070 boolean required() default true; 071}