You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by su...@apache.org on 2004/11/08 12:24:46 UTC
cvs commit: ws-axis/c/src/xml/tspp Array.hpp AxisInputStream.cpp AxisInputStream.hpp Buffer.cpp Buffer.hpp InputStream.hpp ParserLoader.cpp XMLParserAxis.cpp XMLParserAxis.hpp XmlPullParser.cpp XmlPullParser.hpp XmlPullParserException.cpp XmlPullParserException.hpp
susantha 2004/11/08 03:24:45
Added: c/src/xml/tspp Array.hpp AxisInputStream.cpp
AxisInputStream.hpp Buffer.cpp Buffer.hpp
InputStream.hpp ParserLoader.cpp XMLParserAxis.cpp
XMLParserAxis.hpp XmlPullParser.cpp
XmlPullParser.hpp XmlPullParserException.cpp
XmlPullParserException.hpp
Log:
Adding initial source files for tspp. Yet contains lot of bugs and issues.
Revision Changes Path
1.1 ws-axis/c/src/xml/tspp/Array.hpp
Index: Array.hpp
===================================================================
/***************************************************************************
*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*
***************************************************************************/
#if !defined(_ARRAY_HPP__INCLUDED_)
#define _ARRAY_HPP__INCLUDED_
#include "Buffer.hpp"
#include "XmlPullParserException.hpp"
template <class T>
class Array: public Buffer<T>
{
int last;
public:
Array(int size): Buffer<T>(size)
{
last= -1;
}
T& operator[](int i)
{
if (i > last)
throw new XmlPullParserException();
return buffer[i];
}
int count()
{
return last+1;
}
T& getLast()
{
if (last < 0)
throw new XmlPullParserException();
return buffer[last];
}
T& append()
{
if (++last == size)
grow();
return buffer[last];
}
int trunc(int size)
{
if ((size > 0 && last < size) || size < 0)
throw new XmlPullParserException();
else
return last= size-1;
}
int chip()
{
if (last < 0)
throw new XmlPullParserException();
else
return last--; // included return as there was an error occuring
}
};
#endif
1.1 ws-axis/c/src/xml/tspp/AxisInputStream.cpp
Index: AxisInputStream.cpp
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*/
#include "AxisInputStream.hpp"
AxisInputStream::AxisInputStream(AxisIOStream* pInputStream)
{
m_pInputStream = pInputStream;
}
AxisInputStream::~AxisInputStream()
{
}
int AxisInputStream::read(char* buffer, int offset, int length)
{
int nSize = length;
m_pInputStream-> getBytes(buffer+offset, &nSize);
return nSize;
}
1.1 ws-axis/c/src/xml/tspp/AxisInputStream.hpp
Index: AxisInputStream.hpp
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*/
#ifndef AXISINPUTSTREAM_HPP
#define AXISINPUTSTREAM_HPP
#include "InputStream.hpp"
#include "SOAPTransport.h"
AXIS_CPP_NAMESPACE_USE
/**
InputStream class that wraps the AxisIOStream object
@author Susantha Kumara
*/
class AxisInputStream : public InputStream
{
public:
AxisInputStream(AxisIOStream* pInputStream);
~AxisInputStream();
int read(char* buffer, int offset, int length);
private:
AxisIOStream* m_pInputStream;
};
#endif
1.1 ws-axis/c/src/xml/tspp/Buffer.cpp
Index: Buffer.cpp
===================================================================
/***************************************************************************
*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*
***************************************************************************/
#include "Buffer.hpp"
1.1 ws-axis/c/src/xml/tspp/Buffer.hpp
Index: Buffer.hpp
===================================================================
/***************************************************************************
*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*
***************************************************************************/
#if !defined(_BUFFER_HPP__INCLUDED_)
#define _BUFFER_HPP__INCLUDED_
#include "XmlPullParserException.hpp"
template <class T>
class Buffer
{
protected:
T *buffer;
int size;
public:
Buffer(int size);
~Buffer();
void grow();
T *get()
{
return buffer;
}
int getSize()
{
return size;
}
};
template <class T>
Buffer<T>::Buffer(int size)
{
this->size= size;
buffer= (T *)malloc(sizeof(T)*size);
if (!buffer)
throw new XmlPullParserException();
}
template <class T>
Buffer<T>::~Buffer()
{
free(buffer);
}
template <class T>
void Buffer<T>::grow()
{
size <<= 1;
T *p= (T *)realloc(buffer, sizeof(T)*size);
if (p)
buffer= p;
else
throw new XmlPullParserException();
}
#endif
1.1 ws-axis/c/src/xml/tspp/InputStream.hpp
Index: InputStream.hpp
===================================================================
/***************************************************************************
*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*
***************************************************************************/
#if !defined(_INPUTSTREAM_HPP__INCLUDED_)
#define _INPUTSTREAM_HPP__INCLUDED_
class InputStream
{
public:
virtual int read(char *buffer, int offset, int length)= 0;
};
#endif
1.1 ws-axis/c/src/xml/tspp/ParserLoader.cpp
Index: ParserLoader.cpp
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*/
/*
* @author Susantha Kumara (skumara@virtusa.com)
*
*/
#include "XMLParserAxis.hpp"
extern "C" {
STORAGE_CLASS_INFO
int CreateInstance(XMLParser **inst)
{
*inst = new XMLParserAxis();
if (*inst)
{
return AXIS_SUCCESS;
}
return AXIS_FAIL;
}
STORAGE_CLASS_INFO
int DestroyInstance(XMLParser *inst)
{
if (inst)
{
delete inst;
return AXIS_SUCCESS;
}
return AXIS_FAIL;
}
}
1.1 ws-axis/c/src/xml/tspp/XMLParserAxis.cpp
Index: XMLParserAxis.cpp
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*/
/*
* @author Susantha Kumara (susantha@opensource.lk)
*/
#ifdef WIN32
#pragma warning (disable : 4786)
#pragma warning (disable : 4101)
#endif
#include "XMLParserAxis.hpp"
#include "AxisInputStream.hpp"
XMLParserAxis::XMLParserAxis()
{
m_bEndElementFollows = false;
m_pParser = NULL;
m_pInputStream = NULL;
}
XMLParserAxis::~XMLParserAxis()
{
if (m_pParser)
delete m_pParser;
if (m_pInputStream)
delete m_pParser;
}
int XMLParserAxis::setInputStream(AxisIOStream* pInputStream)
{
m_bEndElementFollows = false;
if (m_pInputStream)
delete m_pInputStream;
m_pInputStream = new AxisInputStream(pInputStream);
//TODO : Reusing the same parser object should be possible. Improve the
// parser and then remove the following.
if (m_pParser)
delete m_pParser;
m_pParser = new XmlPullParser(m_pInputStream);
return AXIS_SUCCESS;
}
const XML_Ch* XMLParserAxis::getNS4Prefix(const XML_Ch* prefix)
{
//TODO : implement this correctly
return NULL;
}
int XMLParserAxis::getStatus()
{
//TODO : implement this correctly
return AXIS_SUCCESS;
}
void XMLParserAxis::freeElement()
{
if (m_bEndElementFollows) // free only attributes list if available.
{
m_Element.m_type = END_ELEMENT;
}
else // free all inner strings
{
if (m_Element.m_pchNameOrValue)
{
free (const_cast<void*>((const void*)m_Element.m_pchNameOrValue));
m_Element.m_pchNameOrValue = 0;
}
if (m_Element.m_pchNamespace)
{
free (const_cast<void*>((const void*)m_Element.m_pchNamespace));
m_Element.m_pchNamespace = 0;
}
}
freeAttributes();
}
void XMLParserAxis::freeAttributes()
{
for (int ix=0; m_Element.m_pchAttributes[ix]; ix+=3)
{
if (m_Element.m_pchAttributes[ix])
{
free (const_cast<void*>((const void*)m_Element.m_pchAttributes[ix]));
if (m_Element.m_pchAttributes[ix+1])
free (const_cast<void*>((const void*)m_Element.m_pchAttributes[ix+1]));
if (m_Element.m_pchAttributes[ix+2])
free (const_cast<void*>((const void*)m_Element.m_pchAttributes[ix+2]));
}
}
m_Element.m_pchAttributes[0] = 0;
}
void XMLParserAxis::setAttributes()
{
int iAttribIx=0;
int iAttrib = m_pParser->getAttributeCount();
m_Element.m_pchAttributes[iAttrib*3]=0;
for (int ix = 0; ix < iAttrib; ix++)
{
iAttribIx=ix*3;
m_Element.m_pchAttributes[iAttribIx] = m_pParser->getAttributeName(ix);
m_Element.m_pchAttributes[iAttribIx+1] =
m_pParser->getAttributeNamespaceUri(ix);
m_Element.m_pchAttributes[iAttribIx+2] = m_pParser->getAttributeValue(ix);
}
}
const AnyElement* XMLParserAxis::next(bool isCharData)
{
int nType;
freeElement();
if (m_bEndElementFollows)
{
m_bEndElementFollows = false;
return &m_Element;
}
while (true)
{
if ((nType= m_pParser->next()) != -1)
{
if (!isCharData && (XmlPullParser::Content == nType))
{ // ignorable white space
continue;
}
switch (nType)
{
case XmlPullParser::STag:
m_Element.m_type = START_ELEMENT;
m_Element.m_pchNameOrValue = m_pParser->getName();
m_Element.m_pchNamespace = m_pParser->getNamespaceUri();
setAttributes();
break;
case XmlPullParser::EmptyElemTag:
m_Element.m_type = START_ELEMENT;
m_Element.m_pchNameOrValue = m_pParser->getName();
m_Element.m_pchNamespace = m_pParser->getNamespaceUri();
setAttributes();
m_bEndElementFollows = true;
break;
case XmlPullParser::ETag:
m_Element.m_type = END_ELEMENT;
m_Element.m_pchNameOrValue = m_pParser->getName();
m_Element.m_pchNamespace = m_pParser->getNamespaceUri();
break;
case XmlPullParser::Content:
m_Element.m_type = CHARACTER_ELEMENT;
m_Element.m_pchNameOrValue = m_pParser->getCharData();
break;
default:;
}
return &m_Element;
}
else
{
return NULL;
}
}
}
const AnyElement* XMLParserAxis::anyNext()
{
//TODO : not implemented. I dont think that this ever need to be implemented
//because we are not using this any more in Axis.
return NULL;
}
const XML_Ch* XMLParserAxis::getPrefix4NS(const XML_Ch* pcNS)
{
//TODO : implement this correctly
return NULL;
}
1.1 ws-axis/c/src/xml/tspp/XMLParserAxis.hpp
Index: XMLParserAxis.hpp
===================================================================
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*/
/*
* @author Susantha Kumara (susantha@opensource.lk)
*/
#ifdef WIN32
#pragma warning (disable : 4786)
#endif
#if !defined(__XMLPARSERAXIS_H_OF_AXIS_INCLUDED__)
#define __XMLPARSERAXIS_H_OF_AXIS_INCLUDED__
// #include <axis/server/Packet.hpp>
// #include "../QName.h"
// #include "../Event.h"
#include "XMLParser.h"
// #include "../AxisParseException.h"
#include "XmlPullParser.hpp"
class XMLParserAxis: public XMLParser
{
public:
XMLParserAxis();
~XMLParserAxis();
int setInputStream(AxisIOStream* pInputStream);
const XML_Ch* getNS4Prefix(const XML_Ch* pcPrefix);
int getStatus();
const AnyElement* next(bool bIsCharData=false);
const AnyElement* anyNext();
const XML_Ch* getPrefix4NS(const XML_Ch* pcNS);
private:
XmlPullParser* m_pParser;
AnyElement m_Element;
InputStream* m_pInputStream;
bool m_bEndElementFollows;
void freeElement();
void freeAttributes();
void setAttributes();
};
#endif
1.1 ws-axis/c/src/xml/tspp/XmlPullParser.cpp
Index: XmlPullParser.cpp
===================================================================
/***************************************************************************
*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*
***************************************************************************/
#include "XmlPullParser.hpp"
void XmlPullParser::relocateTokens(int offset)
{
int size= token->count();
for (int ii= 0; ii < size; ii++)
(*token)[ii].relocate(offset);
}
void XmlPullParser::shift()
{
memmove(buffer->get(), buffer->get()+offset, _next-offset);
_next -= offset;
last -= offset;
offset= 0;
}
int XmlPullParser::read()
{
if (_next == buffer->getSize()) {
if (offset > 0) {
relocateTokens(offset);
shift();
}
else {
char *p= buffer->get();
buffer->grow();
relocateTokens(p - buffer->get());
}
}
int c= s->read(buffer->get(), _next, buffer->getSize()-_next);
last += c;
return !c;
}
int XmlPullParser::nextCh(int eof)
{
if (_next > last && read())
if (eof)
return -1;
else
throw new XmlPullParserException();
//Update next
// FIXME:
//printf("%c", buffer->get()[_next]);
return buffer->get()[_next++];
//update next depending on the encoding type
}
char *XmlPullParser::lastCh()
{
return buffer->get()+_next-1;
}
void XmlPullParser::reset()
{
offset= _next;
token->trunc(0);
attr->trunc(0);
if (event == ETag || event == EmptyElemTag) {
DEPTH &l= depth->getLast();
if (l.count) {
NAMESPACE *p= ns->get()+l.first;
for (int ii= l.first; ii < l.total; ii++, p++) {
free(p->prefix);
free(p->uri);
}
ns->trunc(l.first);
}
depth->chip();
// printf("\ndepth=%d\n", depth->count());
}
name= -1;
_ns= -1;
}
void XmlPullParser::startToken()
{
TOKEN &t= token->append();
t.type= TOKEN::Unknown;
t.start= lastCh();
}
void XmlPullParser::endToken(int type)
{
TOKEN &t= token->getLast();
t.type= type;
t.end= lastCh()-1;
}
XmlPullParser::XmlPullParser(InputStream *s)
{
this->s= s;
state= S_1;
offset= 0;
last= -1;
_next= 0;
buffer= new Buffer<char>(1024);
token= new Array<TOKEN>(64);
ns= new Array<NAMESPACE>(64);
depth= new Array<DEPTH>(16);
attr= new Array<ATTR>(64);
}
XmlPullParser::~XmlPullParser()
{
delete buffer;
delete token;
delete ns;
delete depth;
delete attr;
}
int XmlPullParser::isS(int c)
{
return 0x9 == c || 0xA == c || 0xD == c || 0x20 == c;
}
int XmlPullParser::parseS(int c)
{
while (isS(c))
c= nextCh();
return c;
}
int XmlPullParser::parseEq(int c)
{
if ('=' == parseS(c))
return parseS(nextCh());
else
throw new XmlPullParserException();
}
int XmlPullParser::parseNameToken(int c)
{
startToken();
while (!(isS(c) || '/' == c || '?' == c || '=' == c || '>' == c)) {
if (':' == c) {
endToken(TOKEN::Prefix);
c= nextCh();
startToken();
}
else
c= nextCh();
}
endToken(TOKEN::Name);
return c;
}
int XmlPullParser::parseAttValueToken(int quote)
{
if ('\'' == quote || '"' == quote) {
int c= nextCh();
startToken();
while ('&' != c && '<' != c) {
if (c == quote) {
endToken(TOKEN::AttValue);
return nextCh();
}
c= nextCh();
}
}
throw new XmlPullParserException();
}
int XmlPullParser::parseAttribute(int c)
{
c= parseEq(parseNameToken(c));
c= parseAttValueToken(c);
return parseS(c);
}
void XmlPullParser::parseXMLDecl()
{
if (nextCh() == 'x' &&
nextCh() == 'm' &&
nextCh() == 'l') {
int c= parseAttribute(parseS(nextCh()));
if (c == 'e')
c= parseAttribute(c);
if (c == 's')
c= parseAttribute(c);
if (c == '?' && nextCh() == '>') {
event= XMLDecl;
return;
}
}
throw new XmlPullParserException();
}
void XmlPullParser::parseSTagOrEmptyElemTag(int c)
{
c= parseS(parseNameToken(c));
event= STag;
for (;;) {
if (c == '/') {
event= EmptyElemTag;
if (nextCh() == '>')
return;
else
throw new XmlPullParserException();
}
else
if (c == '>')
return;
else
c= parseAttribute(c);
}
}
void XmlPullParser::parseETag()
{
if (parseS(parseNameToken(nextCh())) == '>')
event= ETag;
else
throw new XmlPullParserException();
}
int XmlPullParser::parseCharData()
{
int c;
event= Content;
startToken();
do {
c= nextCh(-1);
if (-1 == c) {
endToken(TOKEN::CharData);
return 0;
}
} while ('<' != c);
endToken(TOKEN::CharData);
return c;
}
int XmlPullParser::parse()
{
do {
int c= nextCh(-1);
if (-1 == c)
return -1;
switch (state) {
case S_1:
if ('<' == c)
state= S_2;
else
state= S_0;
break;
case S_2:
if ('?' == c)
parseXMLDecl();
else
parseSTagOrEmptyElemTag(c);
state= S_3;
break;
case S_3:
if ('<' == c)
state= S_4;
else {
c= parseCharData();
if ('<' == c) {
state= S_4;
return event;
}
else if ('\0' == c)
state= S_3;
else
state= S_0;
}
break;
case S_4:
if ('/' == c) {
parseETag();
state= S_3;
}
else if ('!' == c)
state= S_0;
else if ('?' == c)
state= S_0;
else {
parseSTagOrEmptyElemTag(c);
state= S_3;
}
}
if (state == S_0)
throw new XmlPullParserException();
} while (state != S_3);
return event;
}
int XmlPullParser::next()
{
reset();
if (parse() != -1) {
if (STag == event || EmptyElemTag == event || ETag == event) {
TOKEN *p= token->get();
int ii= 0;
if (p->type == TOKEN::Prefix) {
_ns= 0xffff;
name= 1;
p += 2;
ii += 2;
}
else {
_ns = -1;
name= 0;
p++;
ii++;
}
if (ETag == event)
return event;
int count= token->count();
while (ii < count) {
if (p->type == TOKEN::Prefix) {
if (!p->compare("xmlns", 5)) {
NAMESPACE &n= ns->append();
p++;
n.prefix= p->toString();
p++;
n.uri= p->toString();
p++;
ii += 3;
}
else {
ATTR &a= attr->append();
a.prefix= ii++;
a.name= ii++;
a.value= ii++;
a.ns= -1;
p += 3;
}
}
else {
if (!p->compare("xmlns", 5)) {
NAMESPACE &n= ns->append();
n.prefix= NULL;
p++;
n.uri= p->toString();
p++;
ii += 2;
}
else {
ATTR &a= attr->append();
a.prefix= -1;
a.name= ii++;
a.value= ii++;
a.ns= -1;
p += 2;
}
}
}
if (depth->count()) {
DEPTH &l= depth->getLast();
DEPTH &m= depth->append();
m.first= l.first+l.count;
m.total= ns->count();
m.count= m.total-l.total;
}
else {
DEPTH &m= depth->append();
m.first= 0;
m.total= ns->count();
m.count= m.total;
}
_ns= resolve(_ns == -1 ? NULL : token->get());
}
return event;
}
return -1;
}
int XmlPullParser::getAttributeCount()
{
if (STag != event && EmptyElemTag != event)
throw new XmlPullParserException();
return attr->count();
}
char *XmlPullParser::getAttributePrefix(int ii)
{
if (STag != event && EmptyElemTag != event)
throw new XmlPullParserException();
ii= (*attr)[ii].prefix;
if (ii == -1)
return NULL;
return (*token)[ii].toString();
}
char *XmlPullParser::getAttributeName(int ii)
{
if (STag != event && EmptyElemTag != event)
throw new XmlPullParserException();
ii= (*attr)[ii].name;
if (ii == -1)
return NULL;
return (*token)[ii].toString();
}
char *XmlPullParser::getAttributeValue(int ii)
{
if (STag != event && EmptyElemTag != event)
throw new XmlPullParserException();
ii= (*attr)[ii].value;
if (ii == -1)
return NULL;
return (*token)[ii].toString();
}
char *XmlPullParser::getAttributeNamespaceUri(int ii)
{
if (STag != event && EmptyElemTag != event)
throw new XmlPullParserException();
ATTR &t= (*attr)[ii];
if (t.ns == -1)
t.ns= resolve(t.prefix == -1 ? NULL : token->get()+t.prefix);
return strdup((*ns)[t.ns].uri);
}
int XmlPullParser::resolve(TOKEN *prefix)
{
int ii= ns->count()-1;
NAMESPACE *p= &ns->getLast();
while (ii >= 0) {
if (prefix == NULL) {
if (p->prefix == NULL)
return ii;
}
else
if (p->prefix && !prefix->compare(p->prefix, strlen(p->prefix)))
return ii;
ii--;
p--;
}
throw new XmlPullParserException();
}
int XmlPullParser::getDepth()
{
return depth->count();
}
int XmlPullParser::getNamespaceCount(int ii)
{
if (ii > getDepth())
throw new XmlPullParserException();
else
return (*depth)[ii-1].total;
}
char *XmlPullParser::getNamespacePrefix(int ii)
{
if (ii > ns->count())
throw new XmlPullParserException();
else {
char *p= (*ns)[ii].prefix;
return p == NULL ? NULL : strdup(p);
}
}
char *XmlPullParser::getNamespaceUri(int ii)
{
if (ii > ns->count())
throw new XmlPullParserException();
else {
char *p= (*ns)[ii].uri;
return p == NULL ? NULL : strdup(p);
}
}
char *XmlPullParser::getName()
{
return (*token)[name].toString();
}
char *XmlPullParser::getCharData()
{
return (*token)[0].toString();
}
char *XmlPullParser::getNamespaceUri()
{
if (_ns == -1)
_ns= resolve(NULL);
else
if (_ns == 0xffff)
_ns= resolve(token->get());
return strdup((*ns)[_ns].uri);
}
1.1 ws-axis/c/src/xml/tspp/XmlPullParser.hpp
Index: XmlPullParser.hpp
===================================================================
/***************************************************************************
*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*
***************************************************************************/
#if !defined(_XMLPULLPARSER_HPP__INCLUDED_)
#define _XMLPULLPARSER_HPP__INCLUDED_
#include <stdlib.h>
#include <string.h>
#include "Buffer.hpp"
#include "Array.hpp"
#include "InputStream.hpp"
#include "XmlPullParserException.hpp"
struct TOKEN
{
int type;
char *start, *end;
enum {
Unknown, Name, AttValue, Prefix, CharData
};
void relocate(int offset)
{
start -= offset;
end -= offset;
}
int length()
{
if (end)
return end-start+1;
else
throw new XmlPullParserException();
}
char *toString()
{
int len= length();
char *p= (char *)malloc(len+1);
strncpy(p, start, len);
p[len]= 0;
return p;
}
int compare(const char *s, int n)
{
return strncmp(start, s, n);
}
};
struct NAMESPACE
{
char *prefix;
char *uri;
};
struct ATTR
{
int prefix;
int name;
int value;
int ns;
};
struct DEPTH
{
int first;
int count;
int total;
};
class XmlPullParser
{
InputStream *s;
Buffer<char> *buffer;
int offset;
int last;
int _next;
int event;
Array<TOKEN> *token;
Array<NAMESPACE> *ns;
Array<ATTR> *attr;
Array<DEPTH> *depth;
int name;
int _ns;
enum {
S_0, S_1, S_2, S_3, S_4
} state;
int read();
void shift();
void relocateTokens(int offset);
int nextCh(int eof= 0);
char *lastCh();
void reset();
int parse();
void startToken();
void endToken(int type);
int resolve(TOKEN *prefix);
int isS(int c);
int parseS(int c);
int parseEq(int c);
int parseNameToken(int c);
int parseAttValueToken(int quote);
int parseAttribute(int c);
void parseXMLDecl();
void parseSTagOrEmptyElemTag(int c);
void parseETag();
int parseCharData();
public:
enum {
XMLDecl, STag, EmptyElemTag, ETag, Content
};
XmlPullParser(InputStream *in);
~XmlPullParser();
int next();
char *getName();
char *getPrefix();
char *getNamespaceUri();
int getAttributeCount();
char *getAttributeName(int ii);
char *getAttributePrefix(int ii);
char *getAttributeNamespaceUri(int ii);
char *getAttributeValue(int ii);
int getDepth();
int getNamespaceCount(int ii);
char *getNamespacePrefix(int ii);
char *getNamespaceUri(int ii);
char *getCharData();
};
#endif
1.1 ws-axis/c/src/xml/tspp/XmlPullParserException.cpp
Index: XmlPullParserException.cpp
===================================================================
/***************************************************************************
*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*
***************************************************************************/
1.1 ws-axis/c/src/xml/tspp/XmlPullParserException.hpp
Index: XmlPullParserException.hpp
===================================================================
/***************************************************************************
*
* Copyright 2003-2004 The Apache Software Foundation.
*
* 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.
*
***************************************************************************/
#if !defined(_XMLPULLPARSEREXCEPTION_HPP__INCLUDED_)
#define _XMLPULLPARSEREXCEPTION_HPP__INCLUDED_
class XmlPullParserException
{
};
#endif