]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/xmlparser.cpp
Merge branch 'master' of git@git.videolan.org:vlc
[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
27 #ifdef HAVE_SYS_STAT_H
28 #   include <sys/stat.h>
29 #endif
30
31 // Static variable to avoid initializing catalogs twice
32 static bool m_initialized = false;
33
34 XMLParser::XMLParser( intf_thread_t *pIntf, const string &rFileName,
35                       bool useDTD ):
36     SkinObject( pIntf )
37 {
38     m_pReader = NULL;
39     m_pStream = NULL;
40
41     m_pXML = xml_Create( pIntf );
42     if( !m_pXML )
43     {
44         msg_Err( getIntf(), "failed to open XML parser" );
45         return;
46     }
47
48     // Avoid duplicate initialization (mutex needed ?)
49     if( !m_initialized )
50     {
51         LoadCatalog();
52         m_initialized = true;
53     }
54
55     m_pStream = stream_UrlNew( pIntf, rFileName.c_str() );
56     if( !m_pStream )
57     {
58         msg_Err( getIntf(), "failed to open %s for reading",
59                  rFileName.c_str() );
60         return;
61     }
62     m_pReader = xml_ReaderCreate( m_pXML, m_pStream );
63     if( !m_pReader )
64     {
65         msg_Err( getIntf(), "failed to open %s for parsing",
66                  rFileName.c_str() );
67         return;
68     }
69
70     xml_ReaderUseDTD( m_pReader, useDTD ? true : false );
71
72 }
73
74
75 XMLParser::~XMLParser()
76 {
77     if( m_pReader && m_pXML ) xml_ReaderDelete( m_pXML, 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 ) return false;
167                     attributes[name] = value;
168                 }
169
170                 handleBeginElement( eltName, attributes );
171                 free( eltName );
172
173                 map<const char*, const char*, ltstr> ::iterator it =
174                     attributes.begin();
175                 while( it != attributes.end() )
176                 {
177                     free( (char *)it->first );
178                     free( (char *)it->second );
179                     it++;
180                 }
181                 break;
182             }
183
184             // End element
185             case XML_READER_ENDELEM:
186             {
187                 // Read the element name
188                 char *eltName = xml_ReaderName( m_pReader );
189                 if( !eltName ) return false;
190
191                 handleEndElement( eltName );
192                 free( eltName );
193                 break;
194             }
195         }
196         ret = xml_ReaderRead( m_pReader );
197     }
198     return (ret == 0 && !m_errors );
199 }