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