1 /*
2 * jSDP: A Java implementation of SDP protocol Copyright (C) 2007 Claudio Di
3 * Vita
4 */
5 package net.sourceforge.jsdp;
6
7 /**
8 * A <tt>Version</tt> represents the <b>v=<i><field value></i></b>
9 * field contained in a SDP message. The version field gives the version of the
10 * Session Description Protocol: there is no minor version number and the only
11 * allowed version value is <tt>0</tt>.
12 *
13 * @since 0.1.0
14 *
15 * @version 1.0
16 *
17 * @author <a href="mailto:cdivita@users.sourceforge.net">Claudio Di Vita</a>
18 */
19 public class Version implements Field {
20
21 /** The class Stream Unique Identifier, SUID */
22 private static final long serialVersionUID = 5365815747331173108L;
23
24 /** The default SDP version */
25 public static final Version DEFAULT_VERSION = new Version();
26
27 /** The SDP protocol version */
28 protected int version;
29
30 /**
31 * Creates a new <tt>Version</tt>. The version number is 0.
32 */
33 protected Version() {
34
35 super();
36
37 this.version = 0;
38 }
39
40 /**
41 * Parse an input string and constructs the equivalent session name field.
42 *
43 * @param field the string to parse
44 *
45 * @return a new <tt>SessionName</tt> instance
46 *
47 * @throws SDPParseException if an error occurs while parsing
48 */
49 public static Version parse(final String field) throws SDPParseException {
50
51 if (!field.startsWith("v=")) {
52 throw new SDPParseException("The string \"" + field + "\" isn't a version field");
53 }
54
55 try {
56 /* Extract the protocol versio */
57 int version = Integer.parseInt(field.substring(2));
58
59 if (version != 0) {
60 throw new SDPParseException("Invalid SDP protocol version: the only allowed value is 0");
61 }
62 }
63 catch (NumberFormatException parseException) {
64 throw new SDPParseException("The string \"" + field + "\" isn't a valid version field", parseException);
65 }
66
67 return DEFAULT_VERSION;
68 }
69
70 /**
71 * Returns a clone of this field.
72 *
73 * @return a clone of this field
74 */
75 public Object clone() {
76
77 Version field = new Version();
78 field.version = this.version;
79
80 return field;
81 }
82
83 /**
84 * Returns the type character for the field.
85 *
86 * @return the field type character: <b>v</b>
87 */
88 public char getType() {
89
90 return Field.VERSION_FIELD;
91 }
92
93 /**
94 * Returns the SDP protocol version.
95 *
96 * @return the protocol version
97 */
98 public int getValue() {
99
100 return version;
101 }
102
103 /**
104 * Sets the SDP protocol version
105 *
106 * @param version the SDP protocol version
107 *
108 * @throws SDPException if the version specified is not valid. At this time
109 * the only version allowed is <tt>0</tt>
110 */
111 protected void setValue(final int version) throws SDPException {
112
113 if (version != 0) {
114 throw new SDPException("Invalid SDP protocol version: the only allowed value is 0");
115 }
116
117 this.version = version;
118 }
119
120 /**
121 * Returns a string representation of the field. The representation has the
122 * form: <b>v=<i><version></i></b>.
123 *
124 * @return the string representation of the field
125 */
126 public String toString() {
127
128 return getType() + "=" + version;
129 }
130 }