blob: 50f437f15399d9afb539b846e87ad5090ea3534f [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
2 * $RCSfile$
3 * $Revision$
4 * $Date$
5 *
6 * Copyright 2003-2007 Jive Software.
7 *
8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21package org.jivesoftware.smackx.provider;
22
23import org.jivesoftware.smack.packet.PacketExtension;
24import org.jivesoftware.smack.provider.PacketExtensionProvider;
25import org.jivesoftware.smack.util.StringUtils;
26import org.jivesoftware.smackx.packet.XHTMLExtension;
27import org.xmlpull.v1.XmlPullParser;
28
29/**
30 * The XHTMLExtensionProvider parses XHTML packets.
31 *
32 * @author Gaston Dombiak
33 */
34public class XHTMLExtensionProvider implements PacketExtensionProvider {
35
36 /**
37 * Creates a new XHTMLExtensionProvider.
38 * ProviderManager requires that every PacketExtensionProvider has a public, no-argument constructor
39 */
40 public XHTMLExtensionProvider() {
41 }
42
43 /**
44 * Parses a XHTMLExtension packet (extension sub-packet).
45 *
46 * @param parser the XML parser, positioned at the starting element of the extension.
47 * @return a PacketExtension.
48 * @throws Exception if a parsing error occurs.
49 */
50 public PacketExtension parseExtension(XmlPullParser parser)
51 throws Exception {
52 XHTMLExtension xhtmlExtension = new XHTMLExtension();
53 boolean done = false;
54 StringBuilder buffer = new StringBuilder();
55 int startDepth = parser.getDepth();
56 int depth = parser.getDepth();
57 String lastTag = "";
58 while (!done) {
59 int eventType = parser.next();
60 if (eventType == XmlPullParser.START_TAG) {
61 if (parser.getName().equals("body")) {
62 buffer = new StringBuilder();
63 depth = parser.getDepth();
64 }
65 lastTag = parser.getText();
66 buffer.append(parser.getText());
67 } else if (eventType == XmlPullParser.TEXT) {
68 if (buffer != null) {
69 // We need to return valid XML so any inner text needs to be re-escaped
70 buffer.append(StringUtils.escapeForXML(parser.getText()));
71 }
72 } else if (eventType == XmlPullParser.END_TAG) {
73 if (parser.getName().equals("body") && parser.getDepth() <= depth) {
74 buffer.append(parser.getText());
75 xhtmlExtension.addBody(buffer.toString());
76 }
77 else if (parser.getName().equals(xhtmlExtension.getElementName())
78 && parser.getDepth() <= startDepth) {
79 done = true;
80 }
81 else {
82 // This is a check for tags that are both a start and end tag like <br/>
83 // So that they aren't doubled
84 if(lastTag == null || !lastTag.equals(parser.getText())) {
85 buffer.append(parser.getText());
86 }
87 }
88 }
89 }
90
91 return xhtmlExtension;
92 }
93
94}