1 /*
2 * jSDP: A Java implementation of SDP protocol Copyright (C) 2007 Claudio Di
3 * Vita
4 */
5 package net.sourceforge.jsdp;
6
7 import java.util.regex.Pattern;
8
9 /**
10 * A <tt>SessionName</tt> represents a <b>s=<i><field value></i></b>
11 * field contained in a SDP message. This field identifies the name of the
12 * session.
13 *
14 * @since 0.1.0
15 *
16 * @version 1.0
17 *
18 * @author <a href="mailto:cdivita@users.sourceforge.net">Claudio Di Vita</a>
19 */
20 public class SessionName implements Field {
21
22 /** The class Stream Unique Identifier, SUID */
23 private static final long serialVersionUID = 6770714073389574787L;
24
25 /** The pattern used to validate the session name */
26 private static final Pattern namePattern = Pattern.compile("[^\\r\\n\0]+");
27
28 /** The session name */
29 private String value;
30
31 /**
32 * Creates a new <tt>SessionName</tt>. The name value is set to
33 * <tt>-</tt>.
34 */
35 public SessionName() {
36
37 super();
38 this.value = "-";
39 }
40
41 /**
42 * Creates a new <tt>SessionName</tt>.
43 *
44 * @param value the name of the session
45 *
46 * @throws SDPException if the characters of the session name are not
47 * allowed
48 */
49 public SessionName(final String value) throws SDPException {
50
51 super();
52 setValue(value);
53 }
54
55 /**
56 * Parse an input string and constructs the equivalent session name field.
57 *
58 * @param field the string to parse
59 *
60 * @return a new <tt>SessionName</tt> instance
61 *
62 * @throws SDPParseException if an error occurs while parsing
63 */
64 public static SessionName parse(final String field) throws SDPParseException {
65
66 if (!field.startsWith("s=")) {
67 throw new SDPParseException("The string \"" + field + "\" isn't a session name field");
68 }
69
70 SessionName s = null;
71
72 try {
73 /* Create the field */
74 s = new SessionName(field.substring(2));
75 }
76 catch (SDPException parseException) {
77 throw new SDPParseException("The string \"" + field + "\" isn't a valid session name field", parseException);
78 }
79
80 return s;
81 }
82
83 /**
84 * Returns a clone of this field.
85 *
86 * @return a clone of this field
87 */
88 public Object clone() {
89
90 SessionName session = new SessionName();
91 session.value = new String(this.value);
92
93 return session;
94 }
95
96 /**
97 * Returns the type character for the field.
98 *
99 * @return the field type character: <b>s</b>
100 */
101 public char getType() {
102
103 return Field.SESSION_NAME_FIELD;
104 }
105
106 /**
107 * Returns the session name.
108 *
109 * @return the session name
110 */
111 public String getValue() {
112
113 return value;
114 }
115
116 /**
117 * Sets the session name.
118 *
119 * @param value the session name
120 *
121 * @throws SDPException if the characters of the session name are not
122 * allowed
123 */
124 public void setValue(final String value) throws SDPException {
125
126 if (!namePattern.matcher(value).matches()) {
127 throw new SDPException("Invalid session name");
128 }
129
130 this.value = value;
131 }
132
133 /**
134 * Returns a string representation of the field. The representation has the
135 * form: <b>s=<i><name></i></b>.
136 *
137 * @return the string representation of the field
138 */
139 public String toString() {
140
141 return getType() + "=" + value;
142 }
143 }