[vlc-commits] demux: dash: fix and debug DOM Parsing
Francois Cartegnie
git at videolan.org
Wed Dec 24 22:38:14 CET 2014
vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Wed Dec 24 22:26:56 2014 +0100| [53beda0af7fd8550b9e846b84245ee7ddd6b79ca] | committer: Francois Cartegnie
demux: dash: fix and debug DOM Parsing
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=53beda0af7fd8550b9e846b84245ee7ddd6b79ca
---
modules/stream_filter/dash/xml/DOMParser.cpp | 80 ++++++++++++++++++--------
modules/stream_filter/dash/xml/Node.cpp | 28 +++++----
modules/stream_filter/dash/xml/Node.h | 2 +-
3 files changed, 73 insertions(+), 37 deletions(-)
diff --git a/modules/stream_filter/dash/xml/DOMParser.cpp b/modules/stream_filter/dash/xml/DOMParser.cpp
index 447e14f..1ebc3d5 100644
--- a/modules/stream_filter/dash/xml/DOMParser.cpp
+++ b/modules/stream_filter/dash/xml/DOMParser.cpp
@@ -29,6 +29,7 @@
#include "../Helper.h"
#include <vector>
+#include <stack>
#include <vlc_xml.h>
#include <vlc_stream.h>
@@ -68,43 +69,74 @@ bool DOMParser::parse ()
if(!this->vlc_reader)
return false;
- this->root = this->processNode();
- if ( this->root == NULL )
+ root = processNode();
+ if ( root == NULL )
return false;
return true;
}
-Node* DOMParser::processNode ()
+
+Node* DOMParser::processNode()
{
const char *data;
- int type = xml_ReaderNextNode(this->vlc_reader, &data);
- if(type != -1 && type != XML_READER_NONE && type != XML_READER_ENDELEM)
- {
- Node *node = new Node();
- node->setType( type );
+ int type;
+ std::stack<Node *> lifo;
- if ( type != XML_READER_TEXT )
+ while( (type = xml_ReaderNextNode(vlc_reader, &data)) > 0 )
+ {
+ switch(type)
{
- std::string name = data;
- bool isEmpty = xml_ReaderIsEmptyElement(this->vlc_reader);
- node->setName(name);
-
- this->addAttributesToNode(node);
+ case XML_READER_STARTELEM:
+ {
+ bool empty = xml_ReaderIsEmptyElement(vlc_reader);
+ Node *node = new (std::nothrow) Node();
+ if(node)
+ {
+ if(!lifo.empty())
+ lifo.top()->addSubNode(node);
+ lifo.push(node);
+
+ node->setName(std::string(data));
+ addAttributesToNode(node);
+ }
+
+ if(empty)
+ lifo.pop();
+ break;
+ }
+
+ case XML_READER_TEXT:
+ {
+ if(!lifo.empty())
+ lifo.top()->setText(std::string(data));
+ break;
+ }
+
+ case XML_READER_ENDELEM:
+ {
+ if(lifo.empty())
+ return NULL;
+
+ Node *node = lifo.top();
+ lifo.pop();
+ if(lifo.empty())
+ return node;
+ }
+
+ default:
+ break;
+ }
+ }
- if(isEmpty)
- return node;
+ while( lifo.size() > 1 )
+ lifo.pop();
- Node *subnode = NULL;
+ if(!lifo.empty())
+ delete lifo.top();
- while((subnode = this->processNode()) != NULL)
- node->addSubNode(subnode);
- }
- else
- node->setText( data );
- return node;
- }
return NULL;
}
+
void DOMParser::addAttributesToNode (Node *node)
{
const char *attrValue;
diff --git a/modules/stream_filter/dash/xml/Node.cpp b/modules/stream_filter/dash/xml/Node.cpp
index ace85ee..c2140cf 100644
--- a/modules/stream_filter/dash/xml/Node.cpp
+++ b/modules/stream_filter/dash/xml/Node.cpp
@@ -94,20 +94,9 @@ std::vector<std::string> Node::getAttributeKeys () const
return keys;
}
-bool Node::hasText () const
-{
- return false;
-}
-
const std::string& Node::getText () const
{
- if ( this->type == XML_READER_TEXT )
- return this->text;
- else
- {
- assert( this->subNodes.size() == 1 );
- return this->subNodes[0]->getText();
- }
+ return text;
}
void Node::setText(const std::string &text)
@@ -129,3 +118,18 @@ void Node::setType(int type)
{
this->type = type;
}
+
+std::vector<std::string> Node::toString(int indent) const
+{
+ std::vector<std::string> ret;
+ std::string text(indent, ' ');
+ text.append(getName());
+ ret.push_back(text);
+ std::vector<Node *>::const_iterator l;
+ for(l = subNodes.begin(); l < subNodes.end(); l++)
+ {
+ std::vector<std::string> sub = (*l)->toString(indent + 1);
+ ret.insert(ret.end(), sub.begin(), sub.end());
+ }
+ return ret;
+}
diff --git a/modules/stream_filter/dash/xml/Node.h b/modules/stream_filter/dash/xml/Node.h
index cca07fd..ebd3ff9 100644
--- a/modules/stream_filter/dash/xml/Node.h
+++ b/modules/stream_filter/dash/xml/Node.h
@@ -48,12 +48,12 @@ namespace dash
void addAttribute (const std::string& key, const std::string& value);
const std::string& getAttributeValue (const std::string& key) const;
std::vector<std::string> getAttributeKeys () const;
- bool hasText () const;
const std::string& getText () const;
void setText( const std::string &text );
const std::map<std::string, std::string>& getAttributes () const;
int getType() const;
void setType( int type );
+ std::vector<std::string> toString(int) const;
private:
static const std::string EmptyString;
More information about the vlc-commits
mailing list