]> git.sesse.net Git - vlc/blob - modules/stream_filter/dash/xml/DOMParser.cpp
dash: DOMparser: Replacing an init method by an init list.
[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     root( NULL ),
36     stream( stream ),
37     vlc_xml( NULL ),
38     vlc_reader( NULL )
39 {
40 }
41
42 DOMParser::~DOMParser   ()
43 {
44     if(this->vlc_reader)
45         xml_ReaderDelete(this->vlc_reader);
46     if ( this->vlc_xml )
47         xml_Delete( this->vlc_xml );
48 }
49
50 Node*   DOMParser::getRootNode              ()
51 {
52     return this->root;
53 }
54 bool    DOMParser::parse                    ()
55 {
56     this->vlc_xml = xml_Create(this->stream);
57
58     if(!this->vlc_xml)
59         return false;
60
61     this->vlc_reader = xml_ReaderCreate(this->vlc_xml, this->stream);
62
63     if(!this->vlc_reader)
64         return false;
65
66     this->root = this->processNode();
67
68     return true;
69 }
70 Node*   DOMParser::processNode              ()
71 {
72     const char *data;
73     int type = xml_ReaderNextNode(this->vlc_reader, &data);
74     if(type != XML_READER_TEXT && type != XML_READER_NONE && type != XML_READER_ENDELEM)
75     {
76         Node *node = new Node();
77
78         std::string name    = data;
79         bool        isEmpty = xml_ReaderIsEmptyElement(this->vlc_reader);
80         node->setName(name);
81
82         this->addAttributesToNode(node);
83
84         if(isEmpty)
85             return node;
86
87         Node *subnode = NULL;
88
89         while((subnode = this->processNode()) != NULL)
90             node->addSubNode(subnode);
91
92         return node;
93     }
94     return NULL;
95 }
96 void    DOMParser::addAttributesToNode      (Node *node)
97 {
98     const char *attrValue;
99     const char *attrName;
100
101     while((attrName = xml_ReaderNextAttr(this->vlc_reader, &attrValue)) != NULL)
102     {
103         std::string key     = attrName;
104         std::string value   = attrValue;
105         node->addAttribute(key, value);
106     }
107 }
108 void    DOMParser::print                    (Node *node, int offset)
109 {
110     for(int i = 0; i < offset; i++)
111         msg_Dbg(this->stream, " ");
112
113     msg_Dbg(this->stream, "%s", node->getName().c_str());
114
115     std::vector<std::string> keys = node->getAttributeKeys();
116
117     for(size_t i = 0; i < keys.size(); i++)
118         msg_Dbg(this->stream, " %s=%s", keys.at(i).c_str(), node->getAttributeValue(keys.at(i)).c_str());
119
120     msg_Dbg(this->stream, "\n");
121
122     offset++;
123
124     for(size_t i = 0; i < node->getSubNodes().size(); i++)
125     {
126         this->print(node->getSubNodes().at(i), offset);
127     }
128 }
129
130 void    DOMParser::print                    ()
131 {
132     this->print(this->root, 0);
133 }
134 Profile DOMParser::getProfile               (dash::xml::Node *node)
135 {
136     std::string profile = node->getAttributeValue("profiles");
137
138     if(!profile.compare("urn:mpeg:mpegB:profile:dash:isoff-basic-on-demand:cm"))
139         return dash::mpd::BasicCM;
140
141     return dash::mpd::NotValid;
142 }
143 bool    DOMParser::isDash                   (stream_t *stream)
144 {
145     const uint8_t *peek;
146
147     const char* psz_namespace = "urn:mpeg:mpegB:schema:DASH:MPD:DIS2011";
148     if(stream_Peek(stream, &peek, 1024) < (int)strlen(psz_namespace))
149         return false;
150
151     const char *p = strstr((const char*)peek, psz_namespace );
152
153     return p != NULL;
154 }