]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/xmlparser.cpp
skins2: don't load XML catalog if DTD not requested
[vlc] / modules / gui / skins2 / parser / xmlparser.cpp
1 /*****************************************************************************
2  * xmlparser.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "xmlparser.hpp"
25 #include "../src/os_factory.hpp"
26 #include <vlc_url.h>
27
28 #ifdef HAVE_SYS_STAT_H
29 #   include <sys/stat.h>
30 #endif
31
32 // Static variable to avoid initializing catalogs twice
33 static bool m_initialized = false;
34
35 XMLParser::XMLParser( intf_thread_t *pIntf, const string &rFileName,
36                       bool useDTD ):
37     SkinObject( pIntf )
38 {
39     m_pReader = NULL;
40     m_pStream = NULL;
41
42     if( useDTD )
43     {
44         m_pXML = xml_Create( pIntf );
45         if( m_pXML )
46             LoadCatalog();
47         else
48         {
49             msg_Err( getIntf(), "DTD not supported" );
50             useDTD = false;
51         }
52     }
53     else
54         m_pXML = NULL;
55
56     char* psz_uri = make_URI( rFileName.c_str(), NULL );
57     m_pStream = stream_UrlNew( pIntf, psz_uri );
58     free( psz_uri );
59
60     if( !m_pStream )
61     {
62         msg_Err( getIntf(), "failed to open %s for reading",
63                  rFileName.c_str() );
64         return;
65     }
66     m_pReader = xml_ReaderCreate( m_pXML, m_pStream );
67     if( !m_pReader )
68     {
69         msg_Err( getIntf(), "failed to open %s for parsing",
70                  rFileName.c_str() );
71         return;
72     }
73
74     xml_ReaderUseDTD( m_pReader, useDTD );
75 }
76
77
78 XMLParser::~XMLParser()
79 {
80     if( m_pReader ) xml_ReaderDelete( m_pReader );
81     if( m_pXML ) xml_Delete( m_pXML );
82     if( m_pStream ) stream_Delete( m_pStream );
83 }
84
85
86 void XMLParser::LoadCatalog()
87 {
88     // Get the resource path and look for the DTD
89     OSFactory *pOSFactory = OSFactory::instance( getIntf() );
90     const list<string> &resPath = pOSFactory->getResourcePath();
91     const string &sep = pOSFactory->getDirSeparator();
92     list<string>::const_iterator it;
93
94 #ifdef HAVE_SYS_STAT_H
95     struct stat statBuf;
96
97     // Try to load the catalog first (needed at least on win32 where
98     // we don't have a default catalog)
99     for( it = resPath.begin(); it != resPath.end(); it++ )
100     {
101         string catalog_path = (*it) + sep + "skin.catalog";
102         if( !stat( catalog_path.c_str(), &statBuf ) )
103         {
104             msg_Dbg( getIntf(), "Using catalog %s", catalog_path.c_str() );
105             xml_CatalogLoad( m_pXML, catalog_path.c_str() );
106             break;
107         }
108     }
109     if( it == resPath.end() )
110     {
111         // Ok, try the default one
112         xml_CatalogLoad( m_pXML, 0 );
113     }
114
115     for( it = resPath.begin(); it != resPath.end(); it++ )
116     {
117         string path = (*it) + sep + "skin.dtd";
118         if( !stat( path.c_str(), &statBuf ) )
119         {
120             // DTD found
121             msg_Dbg( getIntf(), "using DTD %s", path.c_str() );
122
123             // Add an entry in the default catalog
124             xml_CatalogAdd( m_pXML, "public",
125                             "-//VideoLAN//DTD VLC Skins V"
126                             SKINS_DTD_VERSION "//EN", path.c_str() );
127             break;
128         }
129     }
130     if( it == resPath.end() )
131     {
132         msg_Err( getIntf(), "cannot find the skins DTD");
133     }
134 #endif
135 }
136
137 bool XMLParser::parse()
138 {
139     if( !m_pReader ) return false;
140
141     m_errors = false;
142
143     int ret = xml_ReaderRead( m_pReader );
144     while( ret == 1 )
145     {
146         if( m_errors ) return false;
147
148         // Get the node type
149         int type = xml_ReaderNodeType( m_pReader );
150         switch( type )
151         {
152             // Error
153             case -1:
154                 return false;
155                 break;
156
157             case XML_READER_STARTELEM:
158             {
159                 // Read the element name
160                 char *eltName = xml_ReaderName( m_pReader );
161                 if( !eltName ) return false;
162
163                 // Read the attributes
164                 AttrList_t attributes;
165                 while( xml_ReaderNextAttr( m_pReader ) == VLC_SUCCESS )
166                 {
167                     char *name = xml_ReaderName( m_pReader );
168                     char *value = xml_ReaderValue( m_pReader );
169                     if( !name || !value )
170                     {
171                         free( name );
172                         free( value );
173                         return false;
174                     }
175                     attributes[name] = value;
176                 }
177
178                 handleBeginElement( eltName, attributes );
179                 free( eltName );
180
181                 map<const char*, const char*, ltstr> ::iterator it =
182                     attributes.begin();
183                 while( it != attributes.end() )
184                 {
185                     free( (char *)it->first );
186                     free( (char *)it->second );
187                     it++;
188                 }
189                 break;
190             }
191
192             // End element
193             case XML_READER_ENDELEM:
194             {
195                 // Read the element name
196                 char *eltName = xml_ReaderName( m_pReader );
197                 if( !eltName ) return false;
198
199                 handleEndElement( eltName );
200                 free( eltName );
201                 break;
202             }
203         }
204         ret = xml_ReaderRead( m_pReader );
205     }
206     return (ret == 0 && !m_errors );
207 }