]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/xmlparser.cpp
* ALL: New XML module type.
[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_pXML = xml_Create( pIntf );
36     if( !m_pXML )
37     {
38         msg_Err( getIntf(), "Failed to open XML parser" );
39         m_pReader = NULL;
40         return;
41     }
42
43     // Avoid duplicate initialization (mutex needed ?)
44     if( !m_initialized )
45     {
46         LoadCatalog();
47         m_initialized = true;
48     }
49
50     m_pReader = xml_ReaderCreate( m_pXML, rFileName.c_str() );
51     if( !m_pReader )
52     {
53         msg_Err( getIntf(), "Failed to open %s for parsing",
54                  rFileName.c_str() );
55         return;
56     }
57 }
58
59
60 XMLParser::~XMLParser()
61 {
62     if( m_pReader && m_pXML ) xml_ReaderDelete( m_pXML, m_pReader );
63     if( m_pXML ) xml_Delete( m_pXML );
64 }
65
66
67 void XMLParser::LoadCatalog()
68 {
69     // Get the resource path and look for the DTD
70     OSFactory *pOSFactory = OSFactory::instance( getIntf() );
71     const list<string> &resPath = pOSFactory->getResourcePath();
72     const string &sep = pOSFactory->getDirSeparator();
73     list<string>::const_iterator it;
74     struct stat statBuf;
75
76     // Try to load the catalog first (needed at least on win32 where
77     // we don't have a default catalog)
78     for( it = resPath.begin(); it != resPath.end(); it++ )
79     {
80         string catalog_path = (*it) + sep + "skin.catalog";
81         if( !stat( catalog_path.c_str(), &statBuf ) )
82         {
83             msg_Dbg( getIntf(), "Using catalog %s", catalog_path.c_str() );
84             xml_CatalogLoad( m_pXML, catalog_path.c_str() );
85             break;
86         }
87     }
88     if( it == resPath.end() )
89     {
90         // Ok, try the default one
91         xml_CatalogLoad( m_pXML, 0 );
92     }
93
94     for( it = resPath.begin(); it != resPath.end(); it++ )
95     {
96         string path = (*it) + sep + "skin.dtd";
97         if( !stat( path.c_str(), &statBuf ) )
98         {
99             // DTD found
100             msg_Dbg( getIntf(), "Using DTD %s", path.c_str() );
101
102             // Add an entry in the default catalog
103             xml_CatalogAdd( m_pXML, "public",
104                             "-//VideoLAN//DTD VLC Skins V"
105                             SKINS_DTD_VERSION "//EN", path.c_str() );
106             break;
107         }
108     }
109     if( it == resPath.end() )
110     {
111         msg_Err( getIntf(), "Cannot find the skins DTD !");
112     }
113 }
114
115 bool XMLParser::parse()
116 {
117     if( !m_pReader ) return false;
118
119     m_errors = false;
120
121     int ret = xml_ReaderRead( m_pReader );
122     while( ret == 1 )
123     {
124         if( m_errors ) return false;
125
126         // Get the node type
127         int type = xml_ReaderNodeType( m_pReader );
128         switch( type )
129         {
130             // Error
131             case -1:
132                 return false;
133                 break;
134
135             case XML_READER_STARTELEM:
136             {
137                 // Read the element name
138                 char *eltName = xml_ReaderName( m_pReader );
139                 if( !eltName ) return false;
140
141                 // Read the attributes
142                 AttrList_t attributes;
143                 while( xml_ReaderNextAttr( m_pReader ) == VLC_SUCCESS )
144                 {
145                     char *name = xml_ReaderName( m_pReader );
146                     char *value = xml_ReaderValue( m_pReader );
147                     if( !name || !value ) return false;
148                     attributes[name] = value;
149                 }
150
151                 handleBeginElement( eltName, attributes );
152
153                 map<const char*, const char*, ltstr> ::iterator it =
154                     attributes.begin();
155                 while( it != attributes.end() )
156                 {
157                     free( (char *)it->second );
158                     it++;
159                 }
160                 break;
161             }
162
163             // End element
164             case XML_READER_ENDELEM:
165             {
166                 // Read the element name
167                 char *eltName = xml_ReaderName( m_pReader );
168                 if( !eltName ) return false;
169
170                 handleEndElement( eltName );
171                 free( eltName );
172                 break;
173             }
174         }
175         ret = xml_ReaderRead( m_pReader );
176     }
177     return (ret == 0 && !m_errors );
178 }