DOMParser.cpp 4.28 KB
Newer Older
Christopher Mueller's avatar
Christopher Mueller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * DOMParser.cpp
 *****************************************************************************
 * Copyright (C) 2010 - 2011 Klagenfurt University
 *
 * Created on: Aug 10, 2010
 * Authors: Christopher Mueller <christopher.mueller@itec.uni-klu.ac.at>
 *          Christian Timmerer  <christian.timmerer@itec.uni-klu.ac.at>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "DOMParser.h"

using namespace dash::xml;
using namespace dash::http;
using namespace dash::mpd;

34
35
36
37
38
DOMParser::DOMParser    (stream_t *stream) :
    root( NULL ),
    stream( stream ),
    vlc_xml( NULL ),
    vlc_reader( NULL )
Christopher Mueller's avatar
Christopher Mueller committed
39
40
{
}
41

Christopher Mueller's avatar
Christopher Mueller committed
42
43
DOMParser::~DOMParser   ()
{
44
    delete this->root;
Christopher Mueller's avatar
Christopher Mueller committed
45
46
    if(this->vlc_reader)
        xml_ReaderDelete(this->vlc_reader);
47
48
    if ( this->vlc_xml )
        xml_Delete( this->vlc_xml );
Christopher Mueller's avatar
Christopher Mueller committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
}

Node*   DOMParser::getRootNode              ()
{
    return this->root;
}
bool    DOMParser::parse                    ()
{
    this->vlc_xml = xml_Create(this->stream);

    if(!this->vlc_xml)
        return false;

    this->vlc_reader = xml_ReaderCreate(this->vlc_xml, this->stream);

    if(!this->vlc_reader)
        return false;

    this->root = this->processNode();

    return true;
}
Node*   DOMParser::processNode              ()
{
    const char *data;
    int type = xml_ReaderNextNode(this->vlc_reader, &data);
75
    if(type != -1 && type != XML_READER_TEXT && type != XML_READER_NONE && type != XML_READER_ENDELEM)
Christopher Mueller's avatar
Christopher Mueller committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    {
        Node *node = new Node();

        std::string name    = data;
        bool        isEmpty = xml_ReaderIsEmptyElement(this->vlc_reader);
        node->setName(name);

        this->addAttributesToNode(node);

        if(isEmpty)
            return node;

        Node *subnode = NULL;

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
90
        while((subnode = this->processNode()) != NULL)
Christopher Mueller's avatar
Christopher Mueller committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
            node->addSubNode(subnode);

        return node;
    }
    return NULL;
}
void    DOMParser::addAttributesToNode      (Node *node)
{
    const char *attrValue;
    const char *attrName;

    while((attrName = xml_ReaderNextAttr(this->vlc_reader, &attrValue)) != NULL)
    {
        std::string key     = attrName;
        std::string value   = attrValue;
        node->addAttribute(key, value);
    }
}
void    DOMParser::print                    (Node *node, int offset)
{
    for(int i = 0; i < offset; i++)
        msg_Dbg(this->stream, " ");

    msg_Dbg(this->stream, "%s", node->getName().c_str());

    std::vector<std::string> keys = node->getAttributeKeys();

118
    for(size_t i = 0; i < keys.size(); i++)
Christopher Mueller's avatar
Christopher Mueller committed
119
120
121
122
123
124
        msg_Dbg(this->stream, " %s=%s", keys.at(i).c_str(), node->getAttributeValue(keys.at(i)).c_str());

    msg_Dbg(this->stream, "\n");

    offset++;

125
    for(size_t i = 0; i < node->getSubNodes().size(); i++)
Christopher Mueller's avatar
Christopher Mueller committed
126
127
128
129
    {
        this->print(node->getSubNodes().at(i), offset);
    }
}
130

Christopher Mueller's avatar
Christopher Mueller committed
131
132
133
134
135
136
137
138
139
140
141
142
143
void    DOMParser::print                    ()
{
    this->print(this->root, 0);
}
Profile DOMParser::getProfile               (dash::xml::Node *node)
{
    std::string profile = node->getAttributeValue("profiles");

    if(!profile.compare("urn:mpeg:mpegB:profile:dash:isoff-basic-on-demand:cm"))
        return dash::mpd::BasicCM;

    return dash::mpd::NotValid;
}
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
144
bool    DOMParser::isDash                   (stream_t *stream)
Christopher Mueller's avatar
Christopher Mueller committed
145
{
146
    const char* psz_namespace = "urn:mpeg:mpegB:schema:DASH:MPD:DIS2011";
Christopher Mueller's avatar
Christopher Mueller committed
147

148
149
150
151
    const uint8_t *peek;
    int peek_size = stream_Peek(stream, &peek, 1024);
    if (peek_size < (int)strlen(psz_namespace))
        return false;
Christopher Mueller's avatar
Christopher Mueller committed
152

153
154
    std::string header((const char*)peek, peek_size);
    return header.find(psz_namespace) != std::string::npos;
Christopher Mueller's avatar
Christopher Mueller committed
155
}