1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #include "xmlparser.hpp"
25 #include "../src/os_factory.hpp"
29 // Static variable to avoid initializing catalogs twice
30 static bool m_initialized = false;
32 XMLParser::XMLParser( intf_thread_t *pIntf, const string &rFileName ):
38 m_pXML = xml_Create( pIntf );
41 msg_Err( getIntf(), "Failed to open XML parser" );
45 // Avoid duplicate initialization (mutex needed ?)
52 m_pStream = stream_UrlNew( pIntf, rFileName.c_str() );
55 msg_Err( getIntf(), "Failed to open %s for reading",
59 m_pReader = xml_ReaderCreate( m_pXML, m_pStream );
62 msg_Err( getIntf(), "Failed to open %s for parsing",
69 XMLParser::~XMLParser()
71 if( m_pReader && m_pXML ) xml_ReaderDelete( m_pXML, m_pReader );
72 if( m_pXML ) xml_Delete( m_pXML );
73 if( m_pStream ) stream_Delete( m_pStream );
77 void XMLParser::LoadCatalog()
79 // Get the resource path and look for the DTD
80 OSFactory *pOSFactory = OSFactory::instance( getIntf() );
81 const list<string> &resPath = pOSFactory->getResourcePath();
82 const string &sep = pOSFactory->getDirSeparator();
83 list<string>::const_iterator it;
86 // Try to load the catalog first (needed at least on win32 where
87 // we don't have a default catalog)
88 for( it = resPath.begin(); it != resPath.end(); it++ )
90 string catalog_path = (*it) + sep + "skin.catalog";
91 if( !stat( catalog_path.c_str(), &statBuf ) )
93 msg_Dbg( getIntf(), "Using catalog %s", catalog_path.c_str() );
94 xml_CatalogLoad( m_pXML, catalog_path.c_str() );
98 if( it == resPath.end() )
100 // Ok, try the default one
101 xml_CatalogLoad( m_pXML, 0 );
104 for( it = resPath.begin(); it != resPath.end(); it++ )
106 string path = (*it) + sep + "skin.dtd";
107 if( !stat( path.c_str(), &statBuf ) )
110 msg_Dbg( getIntf(), "Using DTD %s", path.c_str() );
112 // Add an entry in the default catalog
113 xml_CatalogAdd( m_pXML, "public",
114 "-//VideoLAN//DTD VLC Skins V"
115 SKINS_DTD_VERSION "//EN", path.c_str() );
119 if( it == resPath.end() )
121 msg_Err( getIntf(), "Cannot find the skins DTD !");
125 bool XMLParser::parse()
127 if( !m_pReader ) return false;
131 int ret = xml_ReaderRead( m_pReader );
134 if( m_errors ) return false;
137 int type = xml_ReaderNodeType( m_pReader );
145 case XML_READER_STARTELEM:
147 // Read the element name
148 char *eltName = xml_ReaderName( m_pReader );
149 if( !eltName ) return false;
151 // Read the attributes
152 AttrList_t attributes;
153 while( xml_ReaderNextAttr( m_pReader ) == VLC_SUCCESS )
155 char *name = xml_ReaderName( m_pReader );
156 char *value = xml_ReaderValue( m_pReader );
157 if( !name || !value ) return false;
158 attributes[name] = value;
161 handleBeginElement( eltName, attributes );
164 map<const char*, const char*, ltstr> ::iterator it =
166 while( it != attributes.end() )
168 free( (char *)it->first );
169 free( (char *)it->second );
176 case XML_READER_ENDELEM:
178 // Read the element name
179 char *eltName = xml_ReaderName( m_pReader );
180 if( !eltName ) return false;
182 handleEndElement( eltName );
187 ret = xml_ReaderRead( m_pReader );
189 return (ret == 0 && !m_errors );