forked from igniterealtime/Smack
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Bind and Roster IQ parsing code into providers
- Loading branch information
Showing
5 changed files
with
155 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
smack-core/src/main/java/org/jivesoftware/smack/provider/BindIQProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* | ||
* Copyright © 2003-2007 Jive Software, 2014 Florian Schmaus | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jivesoftware.smack.provider; | ||
|
||
import java.io.IOException; | ||
|
||
import org.jivesoftware.smack.SmackException; | ||
import org.jivesoftware.smack.packet.Bind; | ||
import org.xmlpull.v1.XmlPullParser; | ||
import org.xmlpull.v1.XmlPullParserException; | ||
|
||
public class BindIQProvider extends IQProvider<Bind> { | ||
|
||
@Override | ||
public Bind parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, | ||
SmackException { | ||
String name; | ||
Bind bind = null; | ||
outerloop: while (true) { | ||
int eventType = parser.next(); | ||
switch (eventType) { | ||
case XmlPullParser.START_TAG: | ||
name = parser.getName(); | ||
switch (name) { | ||
case "resource": | ||
bind = Bind.newSet(parser.nextText()); | ||
break; | ||
case "jid": | ||
bind = Bind.newResult(parser.nextText()); | ||
break; | ||
} | ||
break; | ||
case XmlPullParser.END_TAG: | ||
if (parser.getDepth() == initialDepth) { | ||
break outerloop; | ||
} | ||
break; | ||
} | ||
} | ||
return bind; | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
smack-core/src/main/java/org/jivesoftware/smack/provider/RosterPacketProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* | ||
* Copyright © 2003-2007 Jive Software, 2014 Florian Schmaus | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jivesoftware.smack.provider; | ||
|
||
import java.io.IOException; | ||
|
||
import org.jivesoftware.smack.SmackException; | ||
import org.jivesoftware.smack.packet.RosterPacket; | ||
import org.xmlpull.v1.XmlPullParser; | ||
import org.xmlpull.v1.XmlPullParserException; | ||
|
||
public class RosterPacketProvider extends IQProvider<RosterPacket> { | ||
|
||
public static final RosterPacketProvider INSTANCE = new RosterPacketProvider(); | ||
|
||
private RosterPacketProvider() { | ||
} | ||
|
||
@Override | ||
public RosterPacket parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, | ||
SmackException { | ||
RosterPacket roster = new RosterPacket(); | ||
RosterPacket.Item item = null; | ||
|
||
String version = parser.getAttributeValue("", "ver"); | ||
roster.setVersion(version); | ||
|
||
outerloop: while (true) { | ||
int eventType = parser.next(); | ||
switch(eventType) { | ||
case XmlPullParser.START_TAG: | ||
String startTag = parser.getName(); | ||
switch (startTag) { | ||
case "item": | ||
String jid = parser.getAttributeValue("", "jid"); | ||
String name = parser.getAttributeValue("", "name"); | ||
// Create packet. | ||
item = new RosterPacket.Item(jid, name); | ||
// Set status. | ||
String ask = parser.getAttributeValue("", "ask"); | ||
RosterPacket.ItemStatus status = RosterPacket.ItemStatus.fromString(ask); | ||
item.setItemStatus(status); | ||
// Set type. | ||
String subscription = parser.getAttributeValue("", "subscription"); | ||
RosterPacket.ItemType type = RosterPacket.ItemType.valueOf(subscription != null ? subscription : "none"); | ||
item.setItemType(type); | ||
break; | ||
case "group": | ||
// TODO item!= null | ||
final String groupName = parser.nextText(); | ||
if (groupName != null && groupName.trim().length() > 0) { | ||
item.addGroupName(groupName); | ||
} | ||
break; | ||
} | ||
break; | ||
case XmlPullParser.END_TAG: | ||
String endTag = parser.getName(); | ||
switch(endTag) { | ||
case "item": | ||
roster.addRosterItem(item); | ||
break; | ||
case "query": | ||
break outerloop; | ||
} | ||
} | ||
} | ||
return roster; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters