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