use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.
the class STUNResolver method loadSTUNServers.
/**
* Load the STUN configuration from a stream.
*
* @param stunConfigStream An InputStream with the configuration file.
* @return A list of loaded servers
*/
public ArrayList<STUNService> loadSTUNServers(java.io.InputStream stunConfigStream) {
ArrayList<STUNService> serversList = new ArrayList<>();
String serverName;
int serverPort;
try {
XmlPullParser parser = PacketParserUtils.getParserFor(stunConfigStream);
XmlPullParser.Event eventType = parser.getEventType();
do {
if (eventType == XmlPullParser.Event.START_ELEMENT) {
// Parse a STUN server definition
if (parser.getName().equals("stunServer")) {
serverName = null;
serverPort = -1;
// Parse the hostname
parser.next();
parser.next();
serverName = parser.nextText();
// Parse the port
parser.next();
parser.next();
serverPort = Integer.parseInt(parser.nextText());
// it to the list.
if (serverName != null && serverPort != -1) {
STUNService service = new STUNService(serverName, serverPort);
serversList.add(service);
}
}
}
eventType = parser.next();
} while (eventType != XmlPullParser.Event.END_DOCUMENT);
} catch (XmlPullParserException e) {
LOGGER.log(Level.SEVERE, "Exception", e);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Exception", e);
}
currentServer = bestSTUNServer(serversList);
return serversList;
}
use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.
the class JingleProvider method parse.
/**
* Parse a iq/jingle element.
* @throws XmlPullParserException if an error in the XML parser occurred.
* @throws IOException if an I/O error occurred.
* @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
*/
@Override
public Jingle parse(XmlPullParser parser, int intialDepth, XmlEnvironment xmlEnvironment) throws IOException, XmlPullParserException, SmackParsingException {
Jingle jingle = new Jingle();
String sid;
JingleActionEnum action;
Jid initiator, responder;
boolean done = false;
JingleContent currentContent = null;
// Sub-elements providers
JingleContentProvider jcp = new JingleContentProvider();
JingleDescriptionProvider jdpAudio = new JingleDescriptionProvider.Audio();
JingleTransportProvider jtpRawUdp = new JingleTransportProvider.RawUdp();
JingleTransportProvider jtpIce = new JingleTransportProvider.Ice();
ExtensionElementProvider<?> jmipAudio = new JingleContentInfoProvider.Audio();
XmlPullParser.Event eventType;
String elementName;
String namespace;
// Get some attributes for the <jingle> element
sid = parser.getAttributeValue("", "sid");
action = JingleActionEnum.getAction(parser.getAttributeValue("", "action"));
initiator = ParserUtils.getJidAttribute(parser, "initiator");
responder = ParserUtils.getJidAttribute(parser, "responder");
jingle.setSid(sid);
jingle.setAction(action);
jingle.setInitiator(initiator);
jingle.setResponder(responder);
// Start processing sub-elements
while (!done) {
eventType = parser.next();
if (eventType == XmlPullParser.Event.START_ELEMENT) {
elementName = parser.getName();
namespace = parser.getNamespace();
if (elementName.equals(JingleContent.NODENAME)) {
// Add a new <content> element to the jingle
currentContent = jcp.parse(parser);
jingle.addContent(currentContent);
} else if (elementName.equals(JingleDescription.NODENAME) && namespace.equals(JingleDescription.Audio.NAMESPACE)) {
// Set the <description> element of the <content>
currentContent.setDescription(jdpAudio.parse(parser));
} else if (elementName.equals(JingleTransport.NODENAME)) {
// Parse the possible transport namespaces
if (namespace.equals(JingleTransport.RawUdp.NAMESPACE)) {
currentContent.addJingleTransport(jtpRawUdp.parse(parser));
} else if (namespace.equals(JingleTransport.Ice.NAMESPACE)) {
currentContent.addJingleTransport(jtpIce.parse(parser));
} else {
// TODO: Should be SmackParseException.
throw new IOException("Unknown transport namespace \"" + namespace + "\" in Jingle packet.");
}
} else if (namespace.equals(JingleContentInfo.Audio.NAMESPACE)) {
jingle.setContentInfo((JingleContentInfo) jmipAudio.parse(parser));
} else {
// TODO: Should be SmackParseException.
throw new IOException("Unknown combination of namespace \"" + namespace + "\" and element name \"" + elementName + "\" in Jingle packet.");
}
} else if (eventType == XmlPullParser.Event.END_ELEMENT) {
if (parser.getName().equals(Jingle.NODENAME)) {
done = true;
}
}
}
return jingle;
}
use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.
the class TranscriptsProvider method parseSummary.
private static Transcripts.TranscriptSummary parseSummary(XmlPullParser parser) throws IOException, XmlPullParserException, ParseException {
String sessionID = parser.getAttributeValue("", "sessionID");
Date joinTime = null;
Date leftTime = null;
List<Transcripts.AgentDetail> agents = new ArrayList<>();
boolean done = false;
while (!done) {
XmlPullParser.Event eventType = parser.next();
if (eventType == XmlPullParser.Event.START_ELEMENT) {
if (parser.getName().equals("joinTime")) {
synchronized (UTC_FORMAT) {
joinTime = UTC_FORMAT.parse(parser.nextText());
}
} else if (parser.getName().equals("leftTime")) {
synchronized (UTC_FORMAT) {
leftTime = UTC_FORMAT.parse(parser.nextText());
}
} else if (parser.getName().equals("agents")) {
agents = parseAgents(parser);
}
} else if (eventType == XmlPullParser.Event.END_ELEMENT) {
if (parser.getName().equals("transcript")) {
done = true;
}
}
}
return new Transcripts.TranscriptSummary(sessionID, joinTime, leftTime, agents);
}
use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.
the class TranscriptsProvider method parse.
@Override
public Transcripts parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, TextParseException, ParseException {
Jid userID = ParserUtils.getJidAttribute(parser, "userID");
List<Transcripts.TranscriptSummary> summaries = new ArrayList<>();
boolean done = false;
while (!done) {
XmlPullParser.Event eventType = parser.next();
if (eventType == XmlPullParser.Event.START_ELEMENT) {
if (parser.getName().equals("transcript")) {
Transcripts.TranscriptSummary summary = parseSummary(parser);
summaries.add(summary);
}
} else if (eventType == XmlPullParser.Event.END_ELEMENT) {
if (parser.getName().equals("transcripts")) {
done = true;
}
}
}
return new Transcripts(userID, summaries);
}
use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.
the class DiscoverItemsProvider method parse.
@Override
public DiscoverItems parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
DiscoverItems discoverItems = new DiscoverItems();
boolean done = false;
DiscoverItems.Item item;
Jid jid = null;
String name = "";
String action = "";
String node = "";
discoverItems.setNode(parser.getAttributeValue("", "node"));
while (!done) {
XmlPullParser.Event eventType = parser.next();
if (eventType == XmlPullParser.Event.START_ELEMENT && "item".equals(parser.getName())) {
// Initialize the variables from the parsed XML
jid = ParserUtils.getJidAttribute(parser);
name = parser.getAttributeValue("", "name");
node = parser.getAttributeValue("", "node");
action = parser.getAttributeValue("", "action");
} else if (eventType == XmlPullParser.Event.END_ELEMENT && "item".equals(parser.getName())) {
// Create a new Item and add it to DiscoverItems.
item = new DiscoverItems.Item(jid);
item.setName(name);
item.setNode(node);
item.setAction(action);
discoverItems.addItem(item);
} else if (eventType == XmlPullParser.Event.END_ELEMENT && "query".equals(parser.getName())) {
done = true;
}
}
return discoverItems;
}
Aggregations