summaryrefslogtreecommitdiff
path: root/src/player/TypeRegistry.cpp
blob: 0de3609030c9b37c5e087e521b863f90e0a20b40 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library 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 of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//
//  Original author of this file is Nick Hebner (hebnern@gmail.com).
//

#include "TypeRegistry.h"
#include "TypeDefinition.h"

#include "../base/MathHelper.h"
#include "../base/Exception.h"

#include <set>

using namespace std;

namespace avg {

TypeRegistry* TypeRegistry::s_pInstance = 0;

TypeRegistry::TypeRegistry()
{
}

TypeRegistry::~TypeRegistry()
{
}

TypeRegistry* TypeRegistry::get()
{
    if (!s_pInstance) {
        s_pInstance = new TypeRegistry();
    }
    return s_pInstance;
}

void TypeRegistry::registerType(const TypeDefinition& def, const char* pParentNames[])
{
    m_TypeDefs.insert(TypeDefMap::value_type(def.getName(), def));

    if (pParentNames) {
        string sChildArray[1];
        sChildArray[0] = def.getName();
        vector<string> sChildren = vectorFromCArray(1, sChildArray);
        const char **ppCurParentName = pParentNames;

        while (*ppCurParentName) {
            TypeDefinition def = getTypeDef(*ppCurParentName);
            def.addChildren(sChildren);
            updateDefinition(def);

            ++ppCurParentName;
        }
    }
}

void TypeRegistry::updateDefinition(const TypeDefinition& def)
{
    m_TypeDefs[def.getName()] = def;
}

ExportedObjectPtr TypeRegistry::createObject(const string& sType, 
        const xmlNodePtr xmlNode)
{
    const TypeDefinition& def = getTypeDef(sType);
    ArgList args(def.getDefaultArgs(), xmlNode);
    ObjectBuilder builder = def.getBuilder();
    ExportedObjectPtr pObj = builder(args);
    pObj->setTypeInfo(&def);
    return pObj;
}

ExportedObjectPtr TypeRegistry::createObject(const string& sType, const py::dict& pyDict)
{
    const TypeDefinition& def = getTypeDef(sType);
    py::dict effParams;
    effParams = pyDict;
    ArgList args(def.getDefaultArgs(), effParams);
    ObjectBuilder builder = def.getBuilder();
    ExportedObjectPtr pObj = builder(args);
    pObj->setTypeInfo(&def);
    return pObj;
}

string TypeRegistry::getDTD() const
{
    if (m_TypeDefs.empty()) {
        return string("");
    }
    
    stringstream ss;
    
    for (TypeDefMap::const_iterator defIt = m_TypeDefs.begin();
            defIt != m_TypeDefs.end(); defIt++) 
    {
        const TypeDefinition& def = defIt->second;
        if (!def.isAbstract()) {
            writeTypeDTD(def, ss);
        }
    }
   
    for (TypeDefMap::const_iterator defIt = m_TypeDefs.begin(); 
            defIt != m_TypeDefs.end(); defIt++) 
    {
        const TypeDefinition& def = defIt->second;
        if (!def.isAbstract()) {
            ss << def.getDTDElements();
        }
    }
   
    return ss.str();
}

TypeDefinition& TypeRegistry::getTypeDef(const string& sType)
{
    TypeDefMap::iterator it = m_TypeDefs.find(sType);
    if (it == m_TypeDefs.end()) {
        throw (Exception (AVG_ERR_XML_NODE_UNKNOWN, 
            string("Unknown node type ") + sType + " encountered."));
    }
    return it->second;
}

void TypeRegistry::writeTypeDTD(const TypeDefinition& def, stringstream& ss) const
{
    ss << "<!ELEMENT " << def.getName() << " " << def.getDTDChildrenString() << " >\n";
    if (!def.getDefaultArgs().getArgMap().empty()) {
        ss << "<!ATTLIST " << def.getName();
        for (ArgMap::const_iterator argIt = def.getDefaultArgs().getArgMap().begin(); 
            argIt != def.getDefaultArgs().getArgMap().end(); argIt++)
        {
            string argName = argIt->first;
            string argType = (argName == "id") ? "ID" : "CDATA";
            string argRequired = def.getDefaultArgs().getArg(argName)->isRequired() ?
                    "#REQUIRED" : "#IMPLIED";
            ss << "\n    " << argName << " " << argType << " " << argRequired;
        }
        ss << " >\n";
    }
}

}