]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/xmlparser.cpp
a1dee471108d0e5481a2e9f245d96efa6018304a
[vlc] / modules / gui / skins2 / parser / xmlparser.cpp
1 /*****************************************************************************
2  * xmlparser.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include "xmlparser.hpp"
25
26 XMLParser::XMLParser( intf_thread_t *pIntf, const string &rFileName ):
27     SkinObject( pIntf )
28 {
29     m_pReader = xmlNewTextReaderFilename( rFileName.c_str() );
30     if( !m_pReader )
31     {
32         msg_Err( getIntf(), "Failed to open %s for parsing",
33                  rFileName.c_str() );
34         return;
35     }
36
37     // Activate DTD validation
38     xmlTextReaderSetParserProp( m_pReader, XML_PARSER_DEFAULTATTRS, 1 );
39     xmlTextReaderSetParserProp( m_pReader, XML_PARSER_VALIDATE, 1 );
40
41     // Set the error handler
42     xmlTextReaderSetErrorHandler( m_pReader, handleError, this );
43 }
44
45
46 XMLParser::~XMLParser()
47 {
48     if( m_pReader )
49     {
50         xmlFreeTextReader( m_pReader );
51     }
52 }
53
54
55 bool XMLParser::parse()
56 {
57     if( !m_pReader )
58     {
59         return false;
60     }
61
62     m_errors = false;
63
64     int ret = xmlTextReaderRead( m_pReader );
65     while (ret == 1)
66     {
67         if( m_errors )
68         {
69             return false;
70         }
71         // Get the node type
72         int type = xmlTextReaderNodeType( m_pReader );
73         switch (type )
74         {
75             // Error
76             case -1:
77                 return false;
78                 break;
79
80             // Begin element
81             case 1:
82             {
83                 // Read the element name
84                 const xmlChar *eltName = xmlTextReaderConstName( m_pReader );
85                 if( !eltName )
86                 {
87                     return false;
88                 }
89                 // Read the attributes
90                 AttrList_t attributes;
91                 while( xmlTextReaderMoveToNextAttribute( m_pReader ) == 1 )
92                 {
93                     const xmlChar *name = xmlTextReaderConstName( m_pReader );
94                     const xmlChar *value = xmlTextReaderConstValue( m_pReader );
95                     if( !name || !value )
96                     {
97                         return false;
98                     }
99                     attributes[(const char*)name] = (const char*)value;
100                 }
101                 handleBeginElement( (const char*)eltName, attributes);
102                 break;
103             }
104
105             // End element
106             case 15:
107                 // Read the element name
108                 const xmlChar *eltName = xmlTextReaderConstName( m_pReader );
109                 if( !eltName )
110                 {
111                     return false;
112                 }
113                 handleEndElement( (const char*)eltName );
114                 break;
115         }
116         ret = xmlTextReaderRead( m_pReader );
117     }
118     return (ret == 0 && !m_errors );
119 }
120
121
122 void XMLParser::handleError( void *pArg,  const char *pMsg,
123                              xmlParserSeverities severity,
124                              xmlTextReaderLocatorPtr locator)
125 {
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;
130 }
131