]> git.sesse.net Git - vlc/blob - modules/stream_filter/dash/xml/DOMParser.cpp
63ce99519bd90e9b7f93a06d0acd9c1890d6fcc8
[vlc] / modules / stream_filter / dash / xml / DOMParser.cpp
1 /*
2  * DOMParser.cpp
3  *****************************************************************************
4  * Copyright (C) 2010 - 2011 Klagenfurt University
5  *
6  * Created on: Aug 10, 2010
7  * Authors: Christopher Mueller <christopher.mueller@itec.uni-klu.ac.at>
8  *          Christian Timmerer  <christian.timmerer@itec.uni-klu.ac.at>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published
12  * by the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "DOMParser.h"
29
30 using namespace dash::xml;
31 using namespace dash::http;
32 using namespace dash::mpd;
33
34 DOMParser::DOMParser    (stream_t *stream)
35 {
36     this->stream = stream;
37     this->init();
38 }
39 DOMParser::~DOMParser   ()
40 {
41     if(this->vlc_reader)
42         xml_ReaderDelete(this->vlc_reader);
43     if ( this->vlc_xml )
44         xml_Delete( this->vlc_xml );
45 }
46
47 Node*   DOMParser::getRootNode              ()
48 {
49     return this->root;
50 }
51 bool    DOMParser::parse                    ()
52 {
53     this->vlc_xml = xml_Create(this->stream);
54
55     if(!this->vlc_xml)
56         return false;
57
58     this->vlc_reader = xml_ReaderCreate(this->vlc_xml, this->stream);
59
60     if(!this->vlc_reader)
61         return false;
62
63     this->root = this->processNode();
64
65     return true;
66 }
67 Node*   DOMParser::processNode              ()
68 {
69     const char *data;
70     int type = xml_ReaderNextNode(this->vlc_reader, &data);
71     if(type != XML_READER_TEXT && type != XML_READER_NONE && type != XML_READER_ENDELEM)
72     {
73         Node *node = new Node();
74
75         std::string name    = data;
76         bool        isEmpty = xml_ReaderIsEmptyElement(this->vlc_reader);
77         node->setName(name);
78
79         this->addAttributesToNode(node);
80
81         if(isEmpty)
82             return node;
83
84         Node *subnode = NULL;
85
86         while((subnode = this->processNode()) != NULL)
87             node->addSubNode(subnode);
88
89         return node;
90     }
91     return NULL;
92 }
93 void    DOMParser::addAttributesToNode      (Node *node)
94 {
95     const char *attrValue;
96     const char *attrName;
97
98     while((attrName = xml_ReaderNextAttr(this->vlc_reader, &attrValue)) != NULL)
99     {
100         std::string key     = attrName;
101         std::string value   = attrValue;
102         node->addAttribute(key, value);
103     }
104 }
105 void    DOMParser::print                    (Node *node, int offset)
106 {
107     for(int i = 0; i < offset; i++)
108         msg_Dbg(this->stream, " ");
109
110     msg_Dbg(this->stream, "%s", node->getName().c_str());
111
112     std::vector<std::string> keys = node->getAttributeKeys();
113
114     for(size_t i = 0; i < keys.size(); i++)
115         msg_Dbg(this->stream, " %s=%s", keys.at(i).c_str(), node->getAttributeValue(keys.at(i)).c_str());
116
117     msg_Dbg(this->stream, "\n");
118
119     offset++;
120
121     for(size_t i = 0; i < node->getSubNodes().size(); i++)
122     {
123         this->print(node->getSubNodes().at(i), offset);
124     }
125 }
126 void    DOMParser::init                     ()
127 {
128     this->root          = NULL;
129     this->vlc_reader    = NULL;
130 }
131 void    DOMParser::print                    ()
132 {
133     this->print(this->root, 0);
134 }
135 Profile DOMParser::getProfile               (dash::xml::Node *node)
136 {
137     std::string profile = node->getAttributeValue("profiles");
138
139     if(!profile.compare("urn:mpeg:mpegB:profile:dash:isoff-basic-on-demand:cm"))
140         return dash::mpd::BasicCM;
141
142     return dash::mpd::NotValid;
143 }
144 bool    DOMParser::isDash                   (stream_t *stream)
145 {
146     const uint8_t *peek;
147
148     const char* psz_namespace = "urn:mpeg:mpegB:schema:DASH:MPD:DIS2011";
149     if(stream_Peek(stream, &peek, 1024) < (int)strlen(psz_namespace))
150         return false;
151
152     const char *p = strstr((const char*)peek, psz_namespace );
153
154     return p != NULL;
155 }