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"
26 XMLParser::XMLParser( intf_thread_t *pIntf, const string &rFileName ):
29 m_pReader = xmlNewTextReaderFilename( rFileName.c_str() );
32 msg_Err( getIntf(), "Failed to open %s for parsing",
37 // Activate DTD validation
38 xmlTextReaderSetParserProp( m_pReader, XML_PARSER_DEFAULTATTRS, 1 );
39 xmlTextReaderSetParserProp( m_pReader, XML_PARSER_VALIDATE, 1 );
41 // Set the error handler
42 xmlTextReaderSetErrorHandler( m_pReader, handleError, this );
46 XMLParser::~XMLParser()
50 xmlFreeTextReader( m_pReader );
55 bool XMLParser::parse()
64 int ret = xmlTextReaderRead( m_pReader );
72 int type = xmlTextReaderNodeType( m_pReader );
83 // Read the element name
84 const xmlChar *eltName = xmlTextReaderConstName( m_pReader );
89 // Read the attributes
90 AttrList_t attributes;
91 while( xmlTextReaderMoveToNextAttribute( m_pReader ) == 1 )
93 const xmlChar *name = xmlTextReaderConstName( m_pReader );
94 const xmlChar *value = xmlTextReaderConstValue( m_pReader );
99 attributes[(const char*)name] = (const char*)value;
101 handleBeginElement( (const char*)eltName, attributes);
107 // Read the element name
108 const xmlChar *eltName = xmlTextReaderConstName( m_pReader );
113 handleEndElement( (const char*)eltName );
116 ret = xmlTextReaderRead( m_pReader );
118 return (ret == 0 && !m_errors );
122 void XMLParser::handleError( void *pArg, const char *pMsg,
123 xmlParserSeverities severity,
124 xmlTextReaderLocatorPtr locator)
126 XMLParser *pThis = (XMLParser*)pArg;
127 int line = xmlTextReaderLocatorLineNumber( locator );
128 msg_Err( pThis->getIntf(), "XML parser error (line %d) : %s", line, pMsg );
129 pThis->m_errors = true;