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