001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: */
027: package gov.nist.javax.sdp.fields;
028:
029: import gov.nist.core.*;
030: import gov.nist.javax.sdp.*;
031:
032: /**
033: * Attribute Field.
034: *
035: *
036: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
037: *
038: */
039: public class AttributeField extends SDPField {
040: /** Current attribute. */
041: protected NameValue attribute;
042:
043: /**
044: * Copies the current instance.
045: * @return the copy of this object
046: */
047: public Object clone() {
048: AttributeField retval = new AttributeField();
049: if (attribute != null)
050: retval.attribute = (NameValue) this .attribute.clone();
051: return retval;
052: }
053:
054: /**
055: * Gets the current attribute.
056: * @return the current attribute.
057: */
058: public NameValue getAttribute() {
059: return attribute;
060: }
061:
062: /**
063: * Default constructor.
064: */
065: public AttributeField() {
066: super (ATTRIBUTE_FIELD);
067: }
068:
069: /**
070: * Sets the attribute member.
071: * @param a the new attribute value
072: */
073: public void setAttribute(NameValue a) {
074: attribute = a;
075: attribute.setSeparator(Separators.COLON);
076: }
077:
078: /**
079: * Gets the string encoded version of this object.
080: * @return encoding string of attribute contents
081: * @since v1.0
082: */
083: public String encode() {
084: String encoded_string = ATTRIBUTE_FIELD;
085: if (attribute != null)
086: encoded_string += attribute.encode();
087: return encoded_string + Separators.NEWLINE;
088: }
089:
090: /**
091: * Returns the encoded string of the attribute field contents.
092: * @return encoded string of attribute contents.
093: */
094: public String toString() {
095: return this .encode();
096: }
097:
098: /**
099: * Returns the name of this attribute
100: * @throws SdpParseException if the name is not well formatted.
101: * @return a String identity or null.
102: */
103: public String getName() throws SdpParseException {
104: NameValue nameValue = getAttribute();
105: if (nameValue == null)
106: return null;
107: else {
108: String name = nameValue.getName();
109: if (name == null)
110: return null;
111: else
112: return name;
113: }
114: }
115:
116: /**
117: * Sets the id of this attribute.
118: * @param name the string name/id of the attribute.
119: * @throws SdpException if the name is null
120: */
121: public void setName(String name) throws SdpException {
122: if (name == null)
123: throw new SdpException("The name is null");
124: else {
125: NameValue nameValue = getAttribute();
126: if (nameValue == null)
127: nameValue = new NameValue();
128: nameValue.setName(name);
129: setAttribute(nameValue);
130: }
131: }
132:
133: /**
134: * Determines if this attribute has an associated value.
135: * @throws SdpParseException if the value is not well formatted.
136: * @return true if the attribute has a value.
137: */
138: public boolean hasValue() throws SdpParseException {
139: NameValue nameValue = getAttribute();
140: if (nameValue == null)
141: return false;
142: else {
143: Object value = nameValue.getValue();
144: if (value == null)
145: return false;
146: else
147: return true;
148: }
149: }
150:
151: /**
152: * Returns the value of this attribute.
153: * @throws SdpParseException if the value is not well formatted.
154: * @return the value; null if the attribute has no associated value.
155: */
156: public String getValue() throws SdpParseException {
157: NameValue nameValue = getAttribute();
158: if (nameValue == null)
159: return null;
160: else {
161: Object value = nameValue.getValue();
162: if (value == null)
163: return null;
164: else if (value instanceof String)
165: return (String) value;
166: else
167: return value.toString();
168: }
169: }
170:
171: /**
172: * Sets the value of this attribute.
173: * @param value the - attribute value
174: * @throws SdpException if the value is null.
175: */
176: public void setValue(String value) throws SdpException {
177: if (value == null)
178: throw new SdpException("The value is null");
179: else {
180: NameValue nameValue = getAttribute();
181: if (nameValue == null)
182: nameValue = new NameValue();
183: nameValue.setValue(value);
184: setAttribute(nameValue);
185: }
186: }
187: }
|