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