FileDocCategorySizeDatePackage
SdpFactory.javaAPI DocphoneME MR2 API (J2ME)22371Wed May 02 18:00:42 BST 2007gov.nist.javax.sdp

SdpFactory

public class SdpFactory extends Object
The SdpFactory enables applications to encode and decode SDP messages. The SdpFactory can be used to construct a SessionDescription object programmatically. The SdpFactory can also be used to construct a SessionDescription based on the contents of a String. Please refer to IETF RFC 2327 for a description of SDP. This code is in the public domain.
version
1.0

Fields Summary
protected static Vector
sessionDescriptionsList
Session description.
Constructors Summary
public SdpFactory()
Creates new SdpFactory

	sessionDescriptionsList = new Vector();
    
Methods Summary
public AttributeFieldcreateAttribute(java.lang.String name, java.lang.String value)
Returns Attribute object with the specified values.

param
name the name of the attribute
param
value the value of the attribute
return
the Attribute

	AttributeField attributeImpl = new AttributeField();
	try {
 
	    attributeImpl.setName(name);
	    attributeImpl.setValue(value);
 
	}
	catch (SdpException s) {
	    s.printStackTrace();
	}
	return attributeImpl;
    
public BandwidthFieldcreateBandwidth(java.lang.String modifier, int value)
Returns Bandwidth object with the specified values.

param
modifier the bandwidth type
param
value the bandwidth value measured in kilobits per second
return
bandwidth

	BandwidthField bandWidthImpl = new BandwidthField();
	try {
 
	    bandWidthImpl.setType(modifier);
	    bandWidthImpl.setValue(value);
 
	}
	catch (SdpException s) {
	    s.printStackTrace();
	}
	return bandWidthImpl;
    
public ConnectionFieldcreateConnection(java.lang.String netType, java.lang.String addrType, java.lang.String addr, int ttl, int numAddrs)
Returns a Connection object with the specified properties a

param
netType network type, eg "IN" for "Internet"
param
addrType address type, eg "IP4" for IPv4 type addresses
param
addr connection address
param
ttl time to live (TTL) for multicast addresses
param
numAddrs number of addresses used by the connection
return
Connection

	ConnectionField connectionImpl = new ConnectionField(); 
 
	connectionImpl.setNetworkType(netType);
	connectionImpl.setAddressType(addrType);
	connectionImpl.setAddress(addr);
 
	return connectionImpl;
    
public ConnectionFieldcreateConnection(java.lang.String netType, java.lang.String addrType, java.lang.String addr)
Returns a Connection object with the specified properties and no TTL and a default number of addresses (1).

param
netType network type, eg "IN" for "Internet"
param
addrType address type, eg "IP4" for IPv4 type addresses
param
addr connection address
throws
SdpException if the parameters are null
return
Connection

	ConnectionField connectionImpl = new ConnectionField();
 
	connectionImpl.setNetworkType(netType);
	connectionImpl.setAddressType(addrType);
	connectionImpl.setAddress(addr);
 
	return connectionImpl;
    
public ConnectionFieldcreateConnection(java.lang.String addr, int ttl, int numAddrs)
Returns a Connection object with the specified properties and a network and address type of "IN" and "IP4" respectively.

param
addr connection address
param
ttl time to live (TTL) for multicast addresses
param
numAddrs number of addresses used by the connection
return
Connection

	ConnectionField connectionImpl = new ConnectionField(); 
 
	connectionImpl.setAddress(addr);
 
	return connectionImpl;
    
public ConnectionFieldcreateConnection(java.lang.String addr)
Returns a Connection object with the specified address. This is equivalent to createConnection("IN", "IP4", addr);

param
addr connection address
throws
SdpException if the parameter is null
return
Connection

 
	return createConnection("IN", " IPV4", addr);
 
    
public EmailFieldcreateEMail(java.lang.String value)
Returns EMail object with the specified value.

param
value the string containing the description.
return
EMail

	EmailField emailImpl = new EmailField();
	try {
 
	    emailImpl.setValue(value);
 
	}
	catch (SdpException s) {
	    s.printStackTrace();
	}
	return emailImpl;
    
public InformationFieldcreateInfo(java.lang.String value)
Returns Info object with the specified value.

param
value the string containing the description.
return
Info

	InformationField infoImpl = new InformationField();
	try {
 
	    infoImpl.setValue(value);
 
	}
	catch (SdpException s) {
	    s.printStackTrace();
	}
	return infoImpl;
    
public KeyFieldcreateKey(java.lang.String method, java.lang.String key)
Returns Key object with the specified value.

param
method the string containing the method type.
param
key the key to set
return
Key

	KeyField keyImpl = new KeyField();
	try {
 
	    keyImpl.setMethod(method);
	    keyImpl.setKey(key);
 
	}
	catch (SdpException s) {
	    s.printStackTrace();
	    return null;
	}
	return keyImpl;
    
public MediaFieldcreateMedia(java.lang.String media, int port, int numPorts, java.lang.String transport, java.util.Vector staticRtpAvpTypes)
Returns Media object with the specified properties.

param
media the media type, eg "audio"
param
port port number on which to receive media
param
numPorts number of ports used for this media stream
param
transport transport type, eg "RTP/AVP"
param
staticRtpAvpTypes vector to set
throws
SdpException
return
Media

	MediaField mediaImpl = new MediaField();
	mediaImpl.setMediaType(media);
	mediaImpl.setMediaPort(port);
	mediaImpl.setPortCount(numPorts);
	mediaImpl.setProtocol(transport);
	mediaImpl.setMediaFormats(staticRtpAvpTypes);
	return mediaImpl;
    
public MediaDescriptionImplcreateMediaDescription(java.lang.String media, int port, int numPorts, java.lang.String transport, int[] staticRtpAvpTypes)
Returns MediaDescription object with the specified properties. The returned object will respond to Media.getMediaFormats(boolean) with a Vector of media formats.

param
media media -
param
port port number on which to receive media
param
numPorts number of ports used for this media stream
param
transport transport type, eg "RTP/AVP"
param
staticRtpAvpTypes list of static RTP/AVP media payload types which should be specified by the returned MediaDescription throws IllegalArgumentException if passed an invalid RTP/AVP payload type
throws
IllegalArgumentException
throws
SdpException
return
MediaDescription

	MediaDescriptionImpl 
	    mediaDescriptionImpl = new MediaDescriptionImpl();
	MediaField mediaImpl = new MediaField();
	mediaImpl.setMediaType(media);
	mediaImpl.setMediaPort(port);
	mediaImpl.setPortCount(numPorts);
	mediaImpl.setProtocol(transport);
	mediaDescriptionImpl.setMedia(mediaImpl);
	return mediaDescriptionImpl; 
    
public MediaDescriptionImplcreateMediaDescription(java.lang.String media, int port, int numPorts, java.lang.String transport, java.lang.String[] formats)
Returns MediaDescription object with the specified properties. The returned object will respond to Media.getMediaFormats(boolean) with a Vector of String objects specified by the 'formats argument.

param
media the media type, eg "audio"
param
port port number on which to receive media
param
numPorts number of ports used for this media stream
param
transport transport type, eg "RTP/AVP"
param
formats list of formats which should be specified by the returned MediaDescription
return
MediaDescription

	MediaDescriptionImpl mediaDescriptionImpl = new MediaDescriptionImpl();
	try {
 
	    MediaField mediaImpl = new MediaField();
	    mediaImpl.setMediaType(media);
	    mediaImpl.setMediaPort(port);
	    mediaImpl.setPortCount(numPorts);
	    mediaImpl.setProtocol(transport);
 
	    Vector formatsV = new Vector(formats.length);
	    for (int i = 0; i < formats.length; i++)
		formatsV.addElement(formats[i]);
	    mediaImpl.setMediaFormats(formatsV);
	    mediaDescriptionImpl.setMedia(mediaImpl);
	} catch (SdpException s) {
	    s.printStackTrace();
	}
	return mediaDescriptionImpl; 
    
public OriginFieldcreateOrigin(java.lang.String userName, java.lang.String address)
Returns Origin object with the specified properties.

param
userName the user name.
param
address the IP4 encoded address.
throws
SdpException if the parameters are null
return
Origin

	OriginField originImpl = new OriginField();
	originImpl.setUsername(userName);
	originImpl.setAddress(address);
	// need revisit - originImpl.setNetworkType(SDPKeywords.IN);
	originImpl.setAddressType(SDPKeywords.IPV4);
	return originImpl;
    
public OriginFieldcreateOrigin(java.lang.String userName, long sessionId, long sessionVersion, java.lang.String networkType, java.lang.String addrType, java.lang.String address)
Returns Origin object with the specified properties.

param
userName String containing the user that created the string.
param
sessionId long containing the session identifier.
param
sessionVersion long containing the session version.
param
networkType String network type for the origin (usually "IN").
param
addrType String address type (usually "IP4").
param
address String IP address usually the address of the host.
throws
SdpException if the parameters are null
return
Origin object with the specified properties.

	OriginField originImpl = new OriginField();
	originImpl.setUsername(userName);
	originImpl.setAddress(address);
	originImpl.setSessionId(sessionId);
	originImpl.setSessionVersion(sessionVersion);
	originImpl.setAddressType(addrType);
	originImpl.setNetworkType(networkType);
	return originImpl; 
    
public PhoneFieldcreatePhone(java.lang.String value)
Returns Phone object with the specified value.

param
value the string containing the description.
return
Phone

	PhoneField phoneImpl = new PhoneField();
	try {
 
	    phoneImpl.setValue(value);
 
	}
	catch (SdpException s) {
	    s.printStackTrace();
	}
	return phoneImpl;
    
public RepeatFieldcreateRepeatTime(int repeatInterval, int activeDuration, int[] offsets)
Returns a RepeatTime object with the specified interval, duration, and time offsets.

param
repeatInterval the "repeat interval" in seconds
param
activeDuration the "active duration" in seconds
param
offsets the list of offsets relative to the start time of the Time object with which the returned RepeatTime will be associated
return
RepeatTime

	RepeatField repeatTimeField = new RepeatField();
	try {
 
	    repeatTimeField.setRepeatInterval(repeatInterval);
	    repeatTimeField.setActiveDuration(activeDuration);
	    repeatTimeField.setOffsetArray(offsets);
 
	}
	catch (SdpException s) {
	    s.printStackTrace();
	} 
	return repeatTimeField;
    
public SessionDescriptionImplcreateSessionDescription()
Creates a new, empty SessionDescription. The session is set as follows:
v=0

o=this.createOrigin ("user",
InetAddress.getLocalHost().toString());

s=-

t=0 0

throws
SdpException if there is a problem constructing the SessionDescription.
return
a new, empty SessionDescription.

	SessionDescriptionImpl 
	    sessionDescriptionImpl = new SessionDescriptionImpl();
 
	ProtoVersionField ProtoVersionField = new ProtoVersionField();
	ProtoVersionField.setVersion(0);
	sessionDescriptionImpl.setVersion(ProtoVersionField);
 
	OriginField originImpl = null;
	originImpl = (OriginField)this.createOrigin("user",
						    "127.0.0.1");
	sessionDescriptionImpl.setOrigin(originImpl);
 
	SessionNameField sessionNameImpl = new SessionNameField();
	sessionNameImpl.setValue("-");
	sessionDescriptionImpl.setSessionName(sessionNameImpl);
 
	TimeDescriptionImpl timeDescriptionImpl = new TimeDescriptionImpl();
	TimeField timeImpl = new TimeField();
	timeImpl.setZero();
	timeDescriptionImpl.setTime(timeImpl);
	Vector times = new Vector();
	times.addElement(timeDescriptionImpl);
	sessionDescriptionImpl.setTimeDescriptions(times);
 
	sessionDescriptionsList.addElement(sessionDescriptionImpl);
	return sessionDescriptionImpl;
    
public SessionDescriptionImplcreateSessionDescription(java.lang.String s)
Creates a SessionDescription populated with the information contained within the string parameter.

Note: unknown field types should not cause exceptions.

param
s s - the sdp message that is to be parsed.
throws
SdpParseException if there is a problem parsing the String.
return
a populated SessionDescription object.

	try {
 
	    SDPAnnounceParser sdpParser = new SDPAnnounceParser(s);
	    return sdpParser.parse();
	} catch (ParseException e) {
	    e.printStackTrace();
	    throw new SdpParseException(0, 0, "Could not parse message");
	}
    
public SessionNameFieldcreateSessionName(java.lang.String name)
Returns SessionName object with the specified name.

param
name the string containing the name of the session.
return
SessionName

	SessionNameField sessionNameImpl = new SessionNameField();
	try {
 
	    sessionNameImpl.setValue(name);
 
	}
	catch (SdpException s) {
	    s.printStackTrace();
	}
	return sessionNameImpl;
    
public TimeFieldcreateTime(java.util.Date start, java.util.Date stop)
Returns a Time specification with the specified start and stop times.

param
start start time
param
stop stop time
throws
SdpException if the parameters are null
return
a Time specification with the specified start and stop times.

	TimeField timeImpl = new TimeField();
	timeImpl.setStart(start);
	timeImpl.setStop(stop);
	return timeImpl;
    
public TimeFieldcreateTime()
Returns an unbounded Time specification (i.e., "t=0 0").

throws
SdpException
return
an unbounded Time specification (i.e., "t=0 0").

	TimeField timeImpl = new TimeField();
	timeImpl.setZero();
	return timeImpl;
    
public TimeDescriptionImplcreateTimeDescription(TimeField t)
Returns TimeDescription object with the specified properties.

param
t the Time that the time description applies to. Returns TimeDescription object with the specified properties.
throws
SdpException
return
TimeDescription

	TimeDescriptionImpl timeDescriptionImpl = new TimeDescriptionImpl();
	timeDescriptionImpl.setTime(t);
	return timeDescriptionImpl; 
    
public TimeDescriptionImplcreateTimeDescription()
Returns TimeDescription unbounded (i.e. "t=0 0");

throws
SdpException
return
TimeDescription unbounded (i.e. "t=0 0");

	TimeDescriptionImpl timeDescriptionImpl = new TimeDescriptionImpl();
	TimeField timeImpl = new TimeField();
	timeImpl.setZero();
	timeDescriptionImpl.setTime(timeImpl);
	return timeDescriptionImpl; 
    
public TimeDescriptionImplcreateTimeDescription(java.util.Date start, java.util.Date stop)
Returns TimeDescription object with the specified properties.

param
start start time.
param
stop stop time.
throws
SdpException if the parameters are null
return
TimeDescription

	TimeDescriptionImpl timeDescriptionImpl = new TimeDescriptionImpl();
	TimeField timeImpl = new TimeField();
	timeImpl.setStart(start);
	timeImpl.setStop(stop);
	timeDescriptionImpl.setTime(timeImpl);
	return timeDescriptionImpl; 
    
public ZoneFieldcreateTimeZoneAdjustment(java.util.Date d, int offset)
Constructs a timezone adjustment record.

param
d the Date at which the adjustment is going to take place.
param
offset the adjustment in number of seconds relative to the start time of the SessionDescription with which this object is associated.
return
TimeZoneAdjustment

	ZoneField timeZoneAdjustmentImpl = new ZoneField();
	try {
 
	    Hashtable map = new Hashtable();
	    map.put(d, new Integer(offset));
	    timeZoneAdjustmentImpl.setZoneAdjustments(map);
	}
	catch (SdpException s) {
	    s.printStackTrace();
	} 
	return timeZoneAdjustmentImpl;
    
public URIFieldcreateURI(java.lang.String value)
Returns URI object with the specified value.

param
value the URL containing the description.
throws
SdpException
return
URI

	URIField uriImpl = new URIField();
	uriImpl.setURI(value);
	return uriImpl;
 
    
public ProtoVersionFieldcreateVersion(int value)
Returns Version object with the specified values.

param
value the version number.
return
Version

	ProtoVersionField protoVersionField = new ProtoVersionField();
	try {
 
	    protoVersionField.setVersion(value);
 
	}
	catch (SdpException s) {
	    s.printStackTrace();
	    return null;
	}
	return protoVersionField;
    
public static java.util.VectorfindSessions(java.lang.String source)
Returns a collection of Strings containing session description.

param
source String containing session descriptions.
return
a collection of Strings containing session descriptions.

	return sessionDescriptionsList;
    
public java.lang.StringformatMulticastAddress(java.lang.String addr, int ttl, int numAddrs)
Returns a String containing the computed form for a multi-connection address.

param
addr connection address
param
ttl time to live (TTL) for multicast addresses
param
numAddrs number of addresses used by the connection
return
computed form for a multi-connection address.

	String res = addr + "/" + ttl + "/"+numAddrs; 
	return res;
    
public static java.util.DategetDateFromNtp(long ntpTime)
Converts a NTP to regular date value.

param
ntpTime long to set
return
Returns a Date object for a given NTP date value.

	return new Date((ntpTime - SdpConstants.NTP_CONST) * 1000);
    
public static gov.nist.javax.sdp.SdpFactorygetInstance()
Obtains an instance of an SdpFactory.

This static method returns a factory instance.

Once an application has obtained a reference to an SdpFactory it can use the factory to configure and obtain parser instances and to create SDP objects.

throws
SdpException if a factory can not be accessed
return
a factory instance

	return new SdpFactory();
    
public static longgetNtpTime(java.util.Date d)
Returns a long containing the NTP value for a given Java Date.

param
d Date to set
return
long

	if (d == null)
	    return -1;
	return ((d.getTime()/1000) + SdpConstants.NTP_CONST);