xml_ReaderUseDTD: remove useless parameter
[vlc/asuraparaju-public.git] / modules / gui / skins2 / parser / xmlparser.cpp
blob0d2be9a0901abc2d298df316b31326e610a72e92
1 /*****************************************************************************
2 * xmlparser.cpp
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
5 * $Id$
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 #include "xmlparser.hpp"
25 #include "../src/os_factory.hpp"
26 #include <vlc_url.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
30 #endif
32 XMLParser::XMLParser( intf_thread_t *pIntf, const string &rFileName,
33 bool useDTD ):
34 SkinObject( pIntf )
36 m_pReader = NULL;
37 m_pStream = NULL;
39 if( useDTD )
41 m_pXML = xml_Create( pIntf );
42 if( m_pXML )
43 LoadCatalog();
44 else
45 msg_Err( getIntf(), "DTD not supported" );
47 else
48 m_pXML = NULL;
50 char* psz_uri = make_URI( rFileName.c_str(), NULL );
51 m_pStream = stream_UrlNew( pIntf, psz_uri );
52 free( psz_uri );
54 if( !m_pStream )
56 msg_Err( getIntf(), "failed to open %s for reading",
57 rFileName.c_str() );
58 m_pReader = NULL;
59 return;
61 m_pReader = xml_ReaderCreate( m_pXML, m_pStream );
62 if( !m_pReader )
64 msg_Err( getIntf(), "failed to open %s for parsing",
65 rFileName.c_str() );
66 return;
69 if( m_pXML )
70 xml_ReaderUseDTD( m_pReader );
74 XMLParser::~XMLParser()
76 if( m_pReader ) xml_ReaderDelete( m_pReader );
77 if( m_pXML ) xml_Delete( m_pXML );
78 if( m_pStream ) stream_Delete( m_pStream );
82 void XMLParser::LoadCatalog()
84 // Get the resource path and look for the DTD
85 OSFactory *pOSFactory = OSFactory::instance( getIntf() );
86 const list<string> &resPath = pOSFactory->getResourcePath();
87 const string &sep = pOSFactory->getDirSeparator();
88 list<string>::const_iterator it;
90 #ifdef HAVE_SYS_STAT_H
91 struct stat statBuf;
93 // Try to load the catalog first (needed at least on win32 where
94 // we don't have a default catalog)
95 for( it = resPath.begin(); it != resPath.end(); it++ )
97 string catalog_path = (*it) + sep + "skin.catalog";
98 if( !stat( catalog_path.c_str(), &statBuf ) )
100 msg_Dbg( getIntf(), "Using catalog %s", catalog_path.c_str() );
101 xml_CatalogLoad( m_pXML, catalog_path.c_str() );
102 break;
105 if( it == resPath.end() )
107 // Ok, try the default one
108 xml_CatalogLoad( m_pXML, 0 );
111 for( it = resPath.begin(); it != resPath.end(); it++ )
113 string path = (*it) + sep + "skin.dtd";
114 if( !stat( path.c_str(), &statBuf ) )
116 // DTD found
117 msg_Dbg( getIntf(), "using DTD %s", path.c_str() );
119 // Add an entry in the default catalog
120 xml_CatalogAdd( m_pXML, "public",
121 "-//VideoLAN//DTD VLC Skins V"
122 SKINS_DTD_VERSION "//EN", path.c_str() );
123 break;
126 if( it == resPath.end() )
128 msg_Err( getIntf(), "cannot find the skins DTD");
130 #endif
133 bool XMLParser::parse()
135 if( !m_pReader ) return false;
137 m_errors = false;
139 int ret = xml_ReaderRead( m_pReader );
140 while( ret == 1 )
142 if( m_errors ) return false;
144 // Get the node type
145 int type = xml_ReaderNodeType( m_pReader );
146 switch( type )
148 // Error
149 case -1:
150 return false;
151 break;
153 case XML_READER_STARTELEM:
155 // Read the element name
156 char *eltName = xml_ReaderName( m_pReader );
157 if( !eltName ) return false;
159 // Read the attributes
160 AttrList_t attributes;
161 while( xml_ReaderNextAttr( m_pReader ) == VLC_SUCCESS )
163 char *name = xml_ReaderName( m_pReader );
164 char *value = xml_ReaderValue( m_pReader );
165 if( !name || !value )
167 free( name );
168 free( value );
169 return false;
171 attributes[name] = value;
174 handleBeginElement( eltName, attributes );
175 free( eltName );
177 map<const char*, const char*, ltstr> ::iterator it =
178 attributes.begin();
179 while( it != attributes.end() )
181 free( (char *)it->first );
182 free( (char *)it->second );
183 it++;
185 break;
188 // End element
189 case XML_READER_ENDELEM:
191 // Read the element name
192 char *eltName = xml_ReaderName( m_pReader );
193 if( !eltName ) return false;
195 handleEndElement( eltName );
196 free( eltName );
197 break;
200 ret = xml_ReaderRead( m_pReader );
202 return (ret == 0 && !m_errors );