📄 xmlcursor.cxx
字号:
#if defined(HAVE_CONFIG_H)
#include "resip/stack/config.hxx"
#endif
#include "resip/stack/XMLCursor.hxx"
#include "resip/stack/Symbols.hxx"
#include "rutil/Logger.hxx"
#include "rutil/WinLeakCheck.hxx"
using namespace resip;
using namespace std;
#define RESIPROCATE_SUBSYSTEM Subsystem::CONTENTS
/**
Whitespace handling:
Are the following XML fragments equivalent?
Strictly interpreted, the root of the first XML document has one
child while the root of the second XML doucment has three children.
The line breaks and spaces after the <root> and before </root> are
tagless children.
--->
<root><child>child content</child></root>
<--
vs.
--->
<root>
<child>child content</child>
</root>
<--
Treating whitespace as children is consistent with the spec but not usually
convenient. <!ATTLIST poem xml:space (default|preserve) 'preserve'> is used to
control whitespace handling. Supporting this switch is painful. For now, treat
whitespace as non-significant.
*/
static char BANG[] = "!";
static char HYPHEN[] = "-";
//http://www.w3.org/TR/1998/REC-xml-19980210
static const Data COMMENT_START("<!--");
static const Data COMMENT_END("-->");
// An alternative to stripping comments out in preparse
// is to deal with them in the parse; ignore when after non-leaf element
// put a leaf after a comment after a leaf in the first leaf's children
// getValue() needs to copy first leaf and all 'child' leaves to mValue
//
// has the advantage of allowing
// 1. lazier parsing
// 2. embedded wierdnesses like <! > and <? >
XMLCursor::XMLCursor(const ParseBuffer& pb)
: mRoot(0),
mCursor(0),
mAttributesSet(false)
{
ParseBuffer lPb(pb);
skipProlog(lPb);
const char* start = lPb.position();
lPb.skipToChars(COMMENT_START);
if (!lPb.eof())
{
StackLog(<< "removing comments");
lPb.reset(start);
mData.reserve(lPb.end() - lPb.start());
const char* anchor = lPb.position();
{
DataStream str(mData);
Data temp;
while (true)
{
lPb.skipToChars(COMMENT_START);
if (!lPb.eof())
{
lPb.data(temp, anchor);
str << temp;
anchor = Node::skipComments(lPb);
}
else
{
break;
}
}
}
mRoot = new Node(ParseBuffer(mData.data(), mData.size()));
}
else
{
mRoot = new Node(ParseBuffer(start, pb.end() - start));
}
mCursor = mRoot;
if (mRoot->extractTag())
{
InfoLog(<< "XML: empty element no a legal root");
mRoot->mPb.fail(__FILE__, __LINE__);
}
mTag = mRoot->mTag;
decodeName(mRoot->mTag);
// check for # & and note -- make decode, decodeName do stuff if set
//<top></top> // no children
lPb.reset(lPb.start());
lPb.skipToChar(Symbols::RA_QUOTE[0]);
lPb.skipChar();
if (!WhitespaceSignificant)
{
lPb.skipWhitespace();
}
if (*lPb.position() == Symbols::LA_QUOTE[0] &&
*(lPb.position()+1) == Symbols::SLASH[0])
{
lPb.skipChar();
lPb.skipChar();
if (strncmp(mRoot->mTag.data(), lPb.position(), mRoot->mTag.size()) == 0)
{
// no children ever
mRoot->mPb.reset(mRoot->mPb.end());
return;
}
}
}
XMLCursor::~XMLCursor()
{
delete mRoot;
}
static const Data QUESTION_RA_QUOTE("?>");
void
XMLCursor::skipProlog(ParseBuffer& pb)
{
//'<?xml' VersionInfo '<xml?' EncodingDecl '?>'? '<?xml' SDDecl '?>'? S? '?>
// !dlb! much more complicated than this.. can contain comments
pb.skipToChars(QUESTION_RA_QUOTE);
pb.skipN(2);
pb.skipWhitespace();
}
void
XMLCursor::decode(Data& text)
{
}
void
XMLCursor::decodeName(Data& name)
{
}
void
XMLCursor::parseNextRootChild()
{
// no next child to parse?
if (mRoot->mPb.eof())
{
return;
}
// next child already parsed?
if (mRoot->mNext != mRoot->mChildren.end())
{
return;
}
// skip self tag
if (mRoot->mPb.position() == mRoot->mPb.start())
{
mRoot->mPb.skipToChar(Symbols::RA_QUOTE[0]);
mRoot->mPb.skipChar();
}
if (!WhitespaceSignificant)
{
mRoot->mPb.skipWhitespace();
}
// root end tag?
if (*mRoot->mPb.position() == Symbols::LA_QUOTE[0])
{
ParseBuffer pb(mRoot->mPb.position(),
mRoot->mPb.end() - mRoot->mPb.position());
pb.skipChar();
if (!pb.eof() && *pb.position() == Symbols::SLASH[0])
{
pb.skipChar();
// CodeWarrior isn't helpful enough to pick the "obvious" operator definition
// so we add volatile here so CW is completely unconfused what to do.
// second note - MSVC 7.0 won't compile the volatile - tried the following to fix
const char* end = pb.position();
if ( (const char*)pb.end() < end + mTag.size() )
{
InfoLog(<< "XML: unexpected end");
pb.fail(__FILE__, __LINE__);
}
if (strncmp(mTag.data(), pb.position(), mRoot->mTag.size()) == 0)
{
mRoot->mPb.skipToEnd();
return;
}
}
}
// leaf?
if (*mRoot->mPb.position() != Symbols::LA_QUOTE[0])
{
const char* anchor = mRoot->mPb.position();
mRoot->mPb.skipToChar(Symbols::LA_QUOTE[0]);
Node* leaf = new Node(ParseBuffer(anchor, mRoot->mPb.position() - anchor));
leaf->mIsLeaf = true;
mRoot->addChild(leaf);
}
else
{
Node* child = new Node(mRoot->mPb);
child->skipToEndTag();
// leave the parse buffer after the child
mRoot->mPb.reset(child->mPb.end());
mRoot->addChild(child);
}
// mNext always points at cursored child
mRoot->mNext = mRoot->mChildren.end();
mRoot->mNext--;
}
bool
XMLCursor::nextSibling()
{
if (atRoot())
{
StackLog(<< "XMLCursor::nextSibling" << *this->mCursor << " <<root>>");
return false;
}
StackLog(<< "XMLCursor::nextSibling" << *this->mCursor << " " << *this->mCursor->mParent);
if (mCursor->mParent == mRoot)
{
parseNextRootChild();
}
if (mCursor->mParent->mNext != mCursor->mParent->mChildren.end())
{
mCursor = *((mCursor->mParent->mNext)++);
mAttributesSet = false;
return true;
}
else
{
return false;
}
}
bool
XMLCursor::firstChild()
{
if (atRoot() &&
mRoot->mChildren.empty())
{
parseNextRootChild();
}
if (mCursor->mChildren.empty())
{
return false;
}
else
{
// mNext always points after cursored child
mCursor->mNext = mCursor->mChildren.begin();
mCursor->mNext++;
mCursor = mCursor->mChildren.front();
mAttributesSet = false;
return true;
}
}
bool
XMLCursor::parent()
{
if (atRoot())
{
return false;
}
mCursor = mCursor->mParent;
mAttributesSet = false;
return true;
}
void
XMLCursor::reset()
{
mCursor = mRoot;
mAttributesSet = false;
}
bool
XMLCursor::atRoot() const
{
return mCursor == mRoot;
}
bool
XMLCursor::atLeaf() const
{
return mCursor->mIsLeaf;
}
const Data&
XMLCursor::getTag() const
{
return mCursor->mTag;
}
//<foo >
//<foo>
//<foo/>
//<foo attr = 'value' attr="value">
//<foo attr = 'value' attr="value" >
//
//<foo attr = 'value' attr="value" />
const XMLCursor::AttributeMap&
XMLCursor::getAttributes() const
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -