1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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"
27 #ifdef HAVE_SYS_STAT_H
28 # include <sys/stat.h>
31 // Static variable to avoid initializing catalogs twice
32 static bool m_initialized = false;
34 XMLParser::XMLParser( intf_thread_t *pIntf, const string &rFileName,
41 m_pXML = xml_Create( pIntf );
44 msg_Err( getIntf(), "Failed to open XML parser" );
48 // Avoid duplicate initialization (mutex needed ?)
55 m_pStream = stream_UrlNew( pIntf, rFileName.c_str() );
58 msg_Err( getIntf(), "Failed to open %s for reading",
62 m_pReader = xml_ReaderCreate( m_pXML, m_pStream );
65 msg_Err( getIntf(), "Failed to open %s for parsing",
70 xml_ReaderUseDTD( m_pReader, useDTD ? VLC_TRUE : VLC_FALSE );
75 XMLParser::~XMLParser()
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 );
83 void XMLParser::LoadCatalog()
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;
91 #ifdef HAVE_SYS_STAT_H
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++ )
98 string catalog_path = (*it) + sep + "skin.catalog";
99 if( !stat( catalog_path.c_str(), &statBuf ) )
101 msg_Dbg( getIntf(), "Using catalog %s", catalog_path.c_str() );
102 xml_CatalogLoad( m_pXML, catalog_path.c_str() );
106 if( it == resPath.end() )
108 // Ok, try the default one
109 xml_CatalogLoad( m_pXML, 0 );
112 for( it = resPath.begin(); it != resPath.end(); it++ )
114 string path = (*it) + sep + "skin.dtd";
115 if( !stat( path.c_str(), &statBuf ) )
118 msg_Dbg( getIntf(), "Using DTD %s", path.c_str() );
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() );
127 if( it == resPath.end() )
129 msg_Err( getIntf(), "Cannot find the skins DTD !");
134 bool XMLParser::parse()
136 if( !m_pReader ) return false;
140 int ret = xml_ReaderRead( m_pReader );
143 if( m_errors ) return false;
146 int type = xml_ReaderNodeType( m_pReader );
154 case XML_READER_STARTELEM:
156 // Read the element name
157 char *eltName = xml_ReaderName( m_pReader );
158 if( !eltName ) return false;
160 // Read the attributes
161 AttrList_t attributes;
162 while( xml_ReaderNextAttr( m_pReader ) == VLC_SUCCESS )
164 char *name = xml_ReaderName( m_pReader );
165 char *value = xml_ReaderValue( m_pReader );
166 if( !name || !value ) return false;
167 attributes[name] = value;
170 handleBeginElement( eltName, attributes );
173 map<const char*, const char*, ltstr> ::iterator it =
175 while( it != attributes.end() )
177 free( (char *)it->first );
178 free( (char *)it->second );
185 case XML_READER_ENDELEM:
187 // Read the element name
188 char *eltName = xml_ReaderName( m_pReader );
189 if( !eltName ) return false;
191 handleEndElement( eltName );
196 ret = xml_ReaderRead( m_pReader );
198 return (ret == 0 && !m_errors );