2 Copyright (c) 2006 Gábor Lehel <illissius@gmail.com>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "xmlloader.h"
21 #include "xmlloader_p.h"
22 #include "xmlloader_p.moc"
24 #include <QApplication>
26 MetaBundle::XmlLoader::XmlLoader(): m_aborted( false ), m_target( 0 )
28 m_reader
.setContentHandler( this );
29 m_reader
.setErrorHandler( this );
32 MetaBundle::XmlLoader::~XmlLoader() {}
34 bool MetaBundle::XmlLoader::load( QXmlInputSource
*source
, QObject
*target
)
37 return m_reader
.parse( source
, false );
40 void MetaBundle::XmlLoader::abort()
45 QString
MetaBundle::XmlLoader::lastError() const
50 BundleList
MetaBundle::XmlLoader::loadBundles( QXmlInputSource
*source
, bool *ok
) //static
52 return SimpleLoader( source
, ok
).bundles
;
55 void MetaBundle::XmlLoader::loadInThread( QXmlInputSource
*source
, QObject
*target
) //static
57 ( new ThreadedLoader( source
, target
) )->start();
60 void MetaBundle::XmlLoader::newAttribute( const QString
&key
, const QString
&value
)
63 m_bundle
.setUrl( value
);
64 else if( key
== "uniqueid" )
65 m_bundle
.setUniqueId( value
);
66 else if( key
== "compilation" )
67 m_bundle
.setCompilation( MetaBundle::CompilationYes
);
69 m_attributes
<< QPair
<QString
, QString
>( key
, value
);
72 void MetaBundle::XmlLoader::newTag( const QString
&name
, const QString
&value
)
74 static int start
= 0; //most of the time, the columns should be in order
75 for( int i
= start
; i
< NUM_COLUMNS
; ++i
)
76 if( name
== exactColumnName( i
) )
96 m_bundle
.setExactText( i
, value
);
105 for( int i
= 0; i
< start
; ++i
)
106 if( m_currentElement
== exactColumnName( i
) )
126 m_bundle
.setExactText( i
, value
);
137 void MetaBundle::XmlLoader::bundleLoaded()
139 m_bundle
.checkExists();
140 emit
newBundle( m_bundle
, m_attributes
);
143 BundleLoadedEvent
e( m_bundle
, m_attributes
);
144 QApplication::sendEvent( m_target
, &e
);
148 void MetaBundle::XmlLoader::errorEncountered( const QString
&, int, int )
150 emit
error( m_lastError
);
153 BundleLoadedEvent
e( m_lastError
);
154 QApplication::sendEvent( m_target
, &e
);
158 bool MetaBundle::XmlLoader::startElement( const QString
&, const QString
&localName
, const QString
&, const QXmlAttributes
&atts
)
160 if( localName
== "item" )
163 m_attributes
.clear();
164 for( int i
= 0, n
= atts
.count(); i
< n
; ++i
)
165 newAttribute( atts
.localName( i
), atts
.value( i
) );
167 m_currentElement
.clear();
170 m_currentElement
= localName
;
175 bool MetaBundle::XmlLoader::endElement( const QString
&, const QString
&localName
, const QString
& )
177 if( localName
== "item" )
181 m_attributes
.clear();
186 m_currentElement
.clear();
191 bool MetaBundle::XmlLoader::characters( const QString
&ch
)
193 if( m_currentElement
.isNull() )
196 newTag( m_currentElement
, ch
);
201 bool MetaBundle::XmlLoader::endDocument()
203 if( !m_bundle
.isEmpty() )
209 bool MetaBundle::XmlLoader::fatalError( const QXmlParseException
&e
)
211 if( !m_bundle
.isEmpty() )
214 m_lastError
= QString( "Error loading XML: \"%1\", at line %2, column %3." )
215 .arg( e
.message(), QString::number( e
.lineNumber() ), QString::number( e
.columnNumber() ) );
216 errorEncountered( e
.message(), e
.lineNumber(), e
.columnNumber() );
221 #include "xmlloader.moc"