2007-07-19 19:01:42 +02:00
# ifndef ARABICA_XSLT_NAMESPACE_ALIAS_HANDLER_HPP
# define ARABICA_XSLT_NAMESPACE_ALIAS_HANDLER_HPP
2007-09-05 00:55:47 +02:00
# include <XML/XMLCharacterClasses.hpp>
2007-07-19 19:01:42 +02:00
2012-11-12 22:46:08 +01:00
# include "xslt_constants.hpp"
2007-07-19 19:01:42 +02:00
namespace Arabica
{
namespace XSLT
{
2012-11-06 20:05:52 +01:00
template < class string_type , class string_adaptor >
class NamespaceAliasHandler : public SAX : : DefaultHandler < string_type , string_adaptor >
2007-07-19 19:01:42 +02:00
{
2012-11-12 22:46:08 +01:00
typedef StylesheetConstant < string_type , string_adaptor > SC ;
2012-11-15 23:03:42 +01:00
typedef AttributeValidators < string_type , string_adaptor > AV ;
2012-11-12 22:46:08 +01:00
2007-07-19 19:01:42 +02:00
public :
2012-11-06 20:05:52 +01:00
NamespaceAliasHandler ( CompilationContext < string_type , string_adaptor > & context ) :
2007-07-19 19:01:42 +02:00
context_ ( context ) ,
done_ ( false )
{
} // NamespaceAliasHandler
2012-11-06 20:05:52 +01:00
virtual void startElement ( const string_type & /* namespaceURI */ ,
const string_type & /* localName */ ,
const string_type & qName ,
const SAX : : Attributes < string_type , string_adaptor > & atts )
2007-07-19 19:01:42 +02:00
{
if ( ! done_ )
{
2012-11-15 23:03:42 +01:00
static const AV rules = AV : : rule ( SC : : stylesheet_prefix , true )
. rule ( SC : : result_prefix , true ) ;
2007-07-19 19:01:42 +02:00
2012-11-15 23:03:42 +01:00
std : : map < string_type , string_type > attrs = rules . gather ( qName , atts ) ;
2012-11-12 22:46:08 +01:00
string_type stylesheet_prefix = attrs [ SC : : stylesheet_prefix ] ;
string_type result_prefix = attrs [ SC : : result_prefix ] ;
2007-07-19 19:01:42 +02:00
2012-11-12 22:46:08 +01:00
if ( stylesheet_prefix = = SC : : hash_default )
stylesheet_prefix = string_adaptor : : empty_string ( ) ;
if ( result_prefix = = SC : : hash_default )
result_prefix = string_adaptor : : empty_string ( ) ;
2007-07-19 19:01:42 +02:00
2012-11-06 20:05:52 +01:00
std : : map < string_type , string_type > namespaces = context_ . inScopeNamespaces ( ) ;
2007-07-19 19:01:42 +02:00
if ( ( namespaces . find ( stylesheet_prefix ) = = namespaces . end ( ) ) & &
2012-11-21 07:59:40 +01:00
( ! string_adaptor : : empty ( stylesheet_prefix ) ) )
2012-11-12 22:46:08 +01:00
throw SAX : : SAXException ( string_adaptor : : asStdString ( SC : : namespace_alias ) + " " + string_adaptor : : asStdString ( stylesheet_prefix ) + " is not a declared namespace prefix " ) ;
2007-07-19 19:01:42 +02:00
if ( ( namespaces . find ( result_prefix ) = = namespaces . end ( ) ) & &
2012-11-21 07:59:40 +01:00
( ! string_adaptor : : empty ( result_prefix ) ) )
2012-11-12 22:46:08 +01:00
throw SAX : : SAXException ( string_adaptor : : asStdString ( SC : : namespace_alias ) + " " + string_adaptor : : asStdString ( result_prefix ) + " is not a declared namespace prefix " ) ;
2007-07-19 19:01:42 +02:00
context_ . addNamespaceAlias ( namespaces [ stylesheet_prefix ] , result_prefix , namespaces [ result_prefix ] ) ;
return ;
} // if(!done_)
2012-11-12 22:46:08 +01:00
throw SAX : : SAXException ( string_adaptor : : asStdString ( qName ) + " can not contain elements " ) ;
2007-07-19 19:01:42 +02:00
} // startElement
2012-11-06 20:05:52 +01:00
virtual void endElement ( const string_type & /* namespaceURI */ ,
const string_type & /* localName */ ,
const string_type & /* qName */ )
2007-07-19 19:01:42 +02:00
{
context_ . pop ( ) ;
} // endElement
2012-11-06 20:05:52 +01:00
virtual void characters ( const string_type & ch )
2007-07-19 19:01:42 +02:00
{
2012-11-12 22:46:08 +01:00
verifyNoCharacterData < string_type , string_adaptor > ( ch , SC : : namespace_alias ) ;
2007-07-19 19:01:42 +02:00
} // characters
private :
2012-11-06 20:05:52 +01:00
CompilationContext < string_type , string_adaptor > & context_ ;
2007-07-19 19:01:42 +02:00
bool done_ ;
} ; // class NamespaceAliasHandler
} //namespace XSLT
} //namespace Arabica
# endif