]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/xmlparser.cpp
all: ported xml parsers to stream_t
[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 #include "../src/os_factory.hpp"
26
27 #include <sys/stat.h>
28
29 // Static variable to avoid initializing catalogs twice
30 static bool m_initialized = false;
31
32 XMLParser::XMLParser( intf_thread_t *pIntf, const string &rFileName ):
33     SkinObject( pIntf )
34 {
35     m_pReader = NULL;
36     m_pStream = NULL;
37     
38     m_pXML = xml_Create( pIntf );
39     if( !m_pXML )
40     {
41         msg_Err( getIntf(), "Failed to open XML parser" );
42         return;
43     }
44
45     // Avoid duplicate initialization (mutex needed ?)
46     if( !m_initialized )
47     {
48         LoadCatalog();
49         m_initialized = true;
50     }
51
52     m_pStream = stream_UrlNew( pIntf, rFileName.c_str() );
53     if( !m_pStream )
54     {
55         msg_Err( getIntf(), "Failed to open %s for reading",
56                  rFileName.c_str() );
57         return;
58     }
59     m_pReader = xml_ReaderCreate( m_pXML, m_pStream );
60     if( !m_pReader )
61     {
62         msg_Err( getIntf(), "Failed to open %s for parsing",
63                  rFileName.c_str() );
64         return;
65     }
66 }
67
68
69 XMLParser::~XMLParser()
70 {
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 );
74 }
75
76
77 void XMLParser::LoadCatalog()
78 {
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;
84     struct stat statBuf;
85
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++ )
89     {
90         string catalog_path = (*it) + sep + "skin.catalog";
91         if( !stat( catalog_path.c_str(), &statBuf ) )
92         {
93             msg_Dbg( getIntf(), "Using catalog %s", catalog_path.c_str() );
94             xml_CatalogLoad( m_pXML, catalog_path.c_str() );
95             break;
96         }
97     }
98     if( it == resPath.end() )
99     {
100         // Ok, try the default one
101         xml_CatalogLoad( m_pXML, 0 );
102     }
103
104     for( it = resPath.begin(); it != resPath.end(); it++ )
105     {
106         string path = (*it) + sep + "skin.dtd";
107         if( !stat( path.c_str(), &statBuf ) )
108         {
109             // DTD found
110             msg_Dbg( getIntf(), "Using DTD %s", path.c_str() );
111
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() );
116             break;
117         }
118     }
119     if( it == resPath.end() )
120     {
121         msg_Err( getIntf(), "Cannot find the skins DTD !");
122     }
123 }
124
125 bool XMLParser::parse()
126 {
127     if( !m_pReader ) return false;
128
129     m_errors = false;
130
131     int ret = xml_ReaderRead( m_pReader );
132     while( ret == 1 )
133     {
134         if( m_errors ) return false;
135
136         // Get the node type
137         int type = xml_ReaderNodeType( m_pReader );
138         switch( type )
139         {
140             // Error
141             case -1:
142                 return false;
143                 break;
144
145             case XML_READER_STARTELEM:
146             {
147                 // Read the element name
148                 char *eltName = xml_ReaderName( m_pReader );
149                 if( !eltName ) return false;
150
151                 // Read the attributes
152                 AttrList_t attributes;
153                 while( xml_ReaderNextAttr( m_pReader ) == VLC_SUCCESS )
154                 {
155                     char *name = xml_ReaderName( m_pReader );
156                     char *value = xml_ReaderValue( m_pReader );
157                     if( !name || !value ) return false;
158                     attributes[name] = value;
159                 }
160
161                 handleBeginElement( eltName, attributes );
162                 free( eltName );
163
164                 map<const char*, const char*, ltstr> ::iterator it =
165                     attributes.begin();
166                 while( it != attributes.end() )
167                 {
168                     free( (char *)it->first );
169                     free( (char *)it->second );
170                     it++;
171                 }
172                 break;
173             }
174
175             // End element
176             case XML_READER_ENDELEM:
177             {
178                 // Read the element name
179                 char *eltName = xml_ReaderName( m_pReader );
180                 if( !eltName ) return false;
181
182                 handleEndElement( eltName );
183                 free( eltName );
184                 break;
185             }
186         }
187         ret = xml_ReaderRead( m_pReader );
188     }
189     return (ret == 0 && !m_errors );
190 }