1 /*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "talk/xmllite/xmlelement.h"
29 #include "talk/base/common.h"
30 #include "talk/xmpp/xmppstanzaparser.h"
31 #include "talk/xmpp/constants.h"
32 #ifdef EXPAT_RELATIVE_PATH
33 #include "lib/expat.h"
34 #else
35 #include "third_party/expat/v2_0_1/Source/lib/expat.h"
36 #endif
37
38 namespace buzz {
39
XmppStanzaParser(XmppStanzaParseHandler * psph)40 XmppStanzaParser::XmppStanzaParser(XmppStanzaParseHandler *psph) :
41 psph_(psph),
42 innerHandler_(this),
43 parser_(&innerHandler_),
44 depth_(0),
45 builder_() {
46 }
47
48 void
Reset()49 XmppStanzaParser::Reset() {
50 parser_.Reset();
51 depth_ = 0;
52 builder_.Reset();
53 }
54
55 void
IncomingStartElement(XmlParseContext * pctx,const char * name,const char ** atts)56 XmppStanzaParser::IncomingStartElement(
57 XmlParseContext * pctx, const char * name, const char ** atts) {
58 if (depth_++ == 0) {
59 XmlElement * pelStream = XmlBuilder::BuildElement(pctx, name, atts);
60 if (pelStream == NULL) {
61 pctx->RaiseError(XML_ERROR_SYNTAX);
62 return;
63 }
64 psph_->StartStream(pelStream);
65 delete pelStream;
66 return;
67 }
68
69 builder_.StartElement(pctx, name, atts);
70 }
71
72 void
IncomingCharacterData(XmlParseContext * pctx,const char * text,int len)73 XmppStanzaParser::IncomingCharacterData(
74 XmlParseContext * pctx, const char * text, int len) {
75 if (depth_ > 1) {
76 builder_.CharacterData(pctx, text, len);
77 }
78 }
79
80 void
IncomingEndElement(XmlParseContext * pctx,const char * name)81 XmppStanzaParser::IncomingEndElement(
82 XmlParseContext * pctx, const char * name) {
83 if (--depth_ == 0) {
84 psph_->EndStream();
85 return;
86 }
87
88 builder_.EndElement(pctx, name);
89
90 if (depth_ == 1) {
91 XmlElement *element = builder_.CreateElement();
92 psph_->Stanza(element);
93 delete element;
94 }
95 }
96
97 void
IncomingError(XmlParseContext * pctx,XML_Error errCode)98 XmppStanzaParser::IncomingError(
99 XmlParseContext * pctx, XML_Error errCode) {
100 UNUSED(pctx);
101 UNUSED(errCode);
102 psph_->XmlError();
103 }
104
105 }
106