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