]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/xmlparser.cpp
* modules/misc/xml/*, include/vlc_xml.h: new xml_ReaderUseDTD() api to enable/disable...
[vlc] / modules / gui / skins2 / parser / xmlparser.cpp
1 /*****************************************************************************
2  * xmlparser.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include "xmlparser.hpp"
25 #include "../src/os_factory.hpp"
26
27 #include <sys/stat.h>
28
29 // Static variable to avoid initializing catalogs twice
30 static bool m_initialized = false;
31
32 XMLParser::XMLParser( intf_thread_t *pIntf, const string &rFileName ):
33     SkinObject( pIntf )
34 {
35     m_pReader = NULL;
36     m_pStream = NULL;
37     
38     m_pXML = xml_Create( pIntf );
39     if( !m_pXML )
40     {
41         msg_Err( getIntf(), "Failed to open XML parser" );
42         return;
43     }
44
45     // Avoid duplicate initialization (mutex needed ?)
46     if( !m_initialized )
47     {
48         LoadCatalog();
49         m_initialized = true;
50     }
51
52     m_pStream = stream_UrlNew( pIntf, rFileName.c_str() );
53     if( !m_pStream )
54     {
55         msg_Err( getIntf(), "Failed to open %s for reading",
56                  rFileName.c_str() );
57         return;
58     }
59     m_pReader = xml_ReaderCreate( m_pXML, m_pStream );
60     if( !m_pReader )
61     {
62         msg_Err( getIntf(), "Failed to open %s for parsing",
63                  rFileName.c_str() );
64         return;
65     }
66
67     xml_ReaderUseDTD( m_pReader, VLC_TRUE );
68
69 }
70
71
72 XMLParser::~XMLParser()
73 {
74     if( m_pReader && m_pXML ) xml_ReaderDelete( m_pXML, m_pReader );
75     if( m_pXML ) xml_Delete( m_pXML );
76     if( m_pStream ) stream_Delete( m_pStream );
77 }
78
79
80 void XMLParser::LoadCatalog()
81 {
82     // Get the resource path and look for the DTD
83     OSFactory *pOSFactory = OSFactory::instance( getIntf() );
84     const list<string> &resPath = pOSFactory->getResourcePath();
85     const string &sep = pOSFactory->getDirSeparator();
86     list<string>::const_iterator it;
87     struct stat statBuf;
88
89     // Try to load the catalog first (needed at least on win32 where
90     // we don't have a default catalog)
91     for( it = resPath.begin(); it != resPath.end(); it++ )
92     {
93         string catalog_path = (*it) + sep + "skin.catalog";
94         if( !stat( catalog_path.c_str(), &statBuf ) )
95         {
96             msg_Dbg( getIntf(), "Using catalog %s", catalog_path.c_str() );
97             xml_CatalogLoad( m_pXML, catalog_path.c_str() );
98             break;
99         }
100     }
101     if( it == resPath.end() )
102     {
103         // Ok, try the default one
104         xml_CatalogLoad( m_pXML, 0 );
105     }
106
107     for( it = resPath.begin(); it != resPath.end(); it++ )
108     {
109         string path = (*it) + sep + "skin.dtd";
110         if( !stat( path.c_str(), &statBuf ) )
111         {
112             // DTD found
113             msg_Dbg( getIntf(), "Using DTD %s", path.c_str() );
114
115             // Add an entry in the default catalog
116             xml_CatalogAdd( m_pXML, "public",
117                             "-//VideoLAN//DTD VLC Skins V"
118                             SKINS_DTD_VERSION "//EN", path.c_str() );
119             break;
120         }
121     }
122     if( it == resPath.end() )
123     {
124         msg_Err( getIntf(), "Cannot find the skins DTD !");
125     }
126 }
127
128 bool XMLParser::parse()
129 {
130     if( !m_pReader ) return false;
131
132     m_errors = false;
133
134     int ret = xml_ReaderRead( m_pReader );
135     while( ret == 1 )
136     {
137         if( m_errors ) return false;
138
139         // Get the node type
140         int type = xml_ReaderNodeType( m_pReader );
141         switch( type )
142         {
143             // Error
144             case -1:
145                 return false;
146                 break;
147
148             case XML_READER_STARTELEM:
149             {
150                 // Read the element name
151                 char *eltName = xml_ReaderName( m_pReader );
152                 if( !eltName ) return false;
153
154                 // Read the attributes
155                 AttrList_t attributes;
156                 while( xml_ReaderNextAttr( m_pReader ) == VLC_SUCCESS )
157                 {
158                     char *name = xml_ReaderName( m_pReader );
159                     char *value = xml_ReaderValue( m_pReader );
160                     if( !name || !value ) return false;
161                     attributes[name] = value;
162                 }
163
164                 handleBeginElement( eltName, attributes );
165                 free( eltName );
166
167                 map<const char*, const char*, ltstr> ::iterator it =
168                     attributes.begin();
169                 while( it != attributes.end() )
170                 {
171                     free( (char *)it->first );
172                     free( (char *)it->second );
173                     it++;
174                 }
175                 break;
176             }
177
178             // End element
179             case XML_READER_ENDELEM:
180             {
181                 // Read the element name
182                 char *eltName = xml_ReaderName( m_pReader );
183                 if( !eltName ) return false;
184
185                 handleEndElement( eltName );
186                 free( eltName );
187                 break;
188             }
189         }
190         ret = xml_ReaderRead( m_pReader );
191     }
192     return (ret == 0 && !m_errors );
193 }