Shuyi Chen | d7955ce | 2013-05-22 14:51:55 -0700 | [diff] [blame] | 1 | /**
|
| 2 | * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
| 3 | * you may not use this file except in compliance with the License.
|
| 4 | * You may obtain a copy of the License at
|
| 5 | *
|
| 6 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 7 | *
|
| 8 | * Unless required by applicable law or agreed to in writing, software
|
| 9 | * distributed under the License is distributed on an "AS IS" BASIS,
|
| 10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 11 | * See the License for the specific language governing permissions and
|
| 12 | * limitations under the License.
|
| 13 | */
|
| 14 | package org.jivesoftware.smackx.pubsub.provider;
|
| 15 |
|
| 16 | import org.jivesoftware.smack.packet.PacketExtension;
|
| 17 | import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
| 18 | import org.jivesoftware.smack.provider.ProviderManager;
|
| 19 | import org.jivesoftware.smack.util.PacketParserUtils;
|
| 20 | import org.jivesoftware.smackx.pubsub.Item;
|
| 21 | import org.jivesoftware.smackx.pubsub.PayloadItem;
|
| 22 | import org.jivesoftware.smackx.pubsub.SimplePayload;
|
| 23 | import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
|
| 24 | import org.xmlpull.v1.XmlPullParser;
|
| 25 |
|
| 26 | /**
|
| 27 | * Parses an <b>item</b> element as is defined in both the {@link PubSubNamespace#BASIC} and {@link PubSubNamespace#EVENT}
|
| 28 | * namespaces. To parse the item contents, it will use whatever {@link PacketExtensionProvider} is registered in
|
| 29 | * <b>smack.providers</b> for its element name and namespace. If no provider is registered, it will return a {@link SimplePayload}.
|
| 30 | *
|
| 31 | * @author Robin Collier
|
| 32 | */
|
| 33 | public class ItemProvider implements PacketExtensionProvider {
|
| 34 | public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
|
| 35 | String id = parser.getAttributeValue(null, "id");
|
| 36 | String node = parser.getAttributeValue(null, "node");
|
| 37 | String elem = parser.getName();
|
| 38 |
|
| 39 | int tag = parser.next();
|
| 40 |
|
| 41 | if (tag == XmlPullParser.END_TAG) {
|
| 42 | return new Item(id, node);
|
| 43 | } else {
|
| 44 | String payloadElemName = parser.getName();
|
| 45 | String payloadNS = parser.getNamespace();
|
| 46 |
|
| 47 | if (ProviderManager.getInstance().getExtensionProvider(payloadElemName, payloadNS) == null) {
|
| 48 | StringBuilder payloadText = new StringBuilder();
|
| 49 | boolean done = false;
|
| 50 | boolean isEmptyElement = false;
|
| 51 |
|
| 52 | // Parse custom payload
|
| 53 | while (!done) {
|
| 54 | if (tag == XmlPullParser.END_TAG && parser.getName().equals(elem)) {
|
| 55 | done = true;
|
| 56 | } else if (parser.getEventType() == XmlPullParser.START_TAG) {
|
| 57 | payloadText.append("<").append(parser.getName());
|
| 58 | if (parser.getName().equals(payloadElemName) && (!"".equals(payloadNS))) {
|
| 59 | payloadText.append(" xmlns=\"").append(payloadNS).append("\"");
|
| 60 | }
|
| 61 | int n = parser.getAttributeCount();
|
| 62 | for (int i = 0; i < n; i++) {
|
| 63 | payloadText.append(" ").append(parser.getAttributeName(i)).append("=\"")
|
| 64 | .append(parser.getAttributeValue(i)).append("\"");
|
| 65 | }
|
| 66 | if (parser.isEmptyElementTag()) {
|
| 67 | payloadText.append("/>");
|
| 68 | isEmptyElement = true;
|
| 69 | } else {
|
| 70 | payloadText.append(">");
|
| 71 | }
|
| 72 | } else if (parser.getEventType() == XmlPullParser.END_TAG) {
|
| 73 | if (isEmptyElement) {
|
| 74 | isEmptyElement = false;
|
| 75 | } else {
|
| 76 | payloadText.append("</").append(parser.getName()).append(">");
|
| 77 | }
|
| 78 | } else if (parser.getEventType() == XmlPullParser.TEXT) {
|
| 79 | payloadText.append(parser.getText());
|
| 80 | }
|
| 81 |
|
| 82 | tag = parser.next();
|
| 83 | }
|
| 84 | return new PayloadItem<SimplePayload>(id, node, new SimplePayload(payloadElemName, payloadNS,
|
| 85 | payloadText.toString()));
|
| 86 | } else {
|
| 87 | return new PayloadItem<PacketExtension>(id, node, PacketParserUtils.parsePacketExtension(
|
| 88 | payloadElemName, payloadNS, parser));
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 | }
|