1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <xml/xmlnamespaces.hxx>
22 using namespace ::com::sun::star::xml::sax
;
23 using namespace ::com::sun::star::uno
;
28 XMLNamespaces::XMLNamespaces()
29 : m_aXMLAttributeNamespace( "xmlns" )
33 XMLNamespaces::XMLNamespaces( const XMLNamespaces
& aXMLNamespaces
)
35 m_aDefaultNamespace
= aXMLNamespaces
.m_aDefaultNamespace
;
36 m_aNamespaceMap
= aXMLNamespaces
.m_aNamespaceMap
;
39 XMLNamespaces::~XMLNamespaces()
43 void XMLNamespaces::addNamespace( const OUString
& aName
, const OUString
& aValue
) throw( SAXException
)
45 NamespaceMap::iterator p
;
46 OUString
aNamespaceName( aName
);
47 sal_Int32 nXMLNamespaceLength
= m_aXMLAttributeNamespace
.getLength();
49 // delete preceding "xmlns"
50 if ( aNamespaceName
.startsWith( m_aXMLAttributeNamespace
) )
52 if ( aNamespaceName
.getLength() == nXMLNamespaceLength
)
54 aNamespaceName
.clear();
56 else if ( aNamespaceName
.getLength() >= nXMLNamespaceLength
+2 )
58 aNamespaceName
= aNamespaceName
.copy( nXMLNamespaceLength
+1 );
62 // a xml namespace without name is not allowed (e.g. "xmlns:" )
63 OUString
aErrorMessage( "A xml namespace without name is not allowed!" );
64 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
68 if ( aValue
.isEmpty() && !aNamespaceName
.isEmpty() )
70 // namespace should be reseted - as xml draft states this is only allowed
71 // for the default namespace - check and throw exception if check fails
72 OUString
aErrorMessage( "Clearing xml namespace only allowed for default namespace!" );
73 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
77 if ( aNamespaceName
.isEmpty() )
78 m_aDefaultNamespace
= aValue
;
81 p
= m_aNamespaceMap
.find( aNamespaceName
);
82 if ( p
!= m_aNamespaceMap
.end() )
84 // replace current namespace definition
85 m_aNamespaceMap
.erase( p
);
86 m_aNamespaceMap
.insert( NamespaceMap::value_type( aNamespaceName
, aValue
));
90 m_aNamespaceMap
.insert( NamespaceMap::value_type( aNamespaceName
, aValue
));
96 OUString
XMLNamespaces::applyNSToAttributeName( const OUString
& aName
) const throw( SAXException
)
98 // xml draft: there is no default namespace for attributes!
101 if (( index
= aName
.indexOf( ':' )) > 0 )
103 if ( aName
.getLength() > index
+1 )
105 OUString aAttributeName
= getNamespaceValue( aName
.copy( 0, index
) );
106 aAttributeName
+= "^";
107 aAttributeName
+= aName
.copy( index
+1 );
108 return aAttributeName
;
112 // attribute with namespace but without name "namespace:" is not allowed!!
113 OUString
aErrorMessage( "Attribute has no name only preceding namespace!" );
114 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
121 OUString
XMLNamespaces::applyNSToElementName( const OUString
& aName
) const throw( SAXException
)
123 // xml draft: element names can have a default namespace
125 int index
= aName
.indexOf( ':' );
127 OUString aElementName
= aName
;
130 aNamespace
= getNamespaceValue( aName
.copy( 0, index
) );
132 aNamespace
= m_aDefaultNamespace
;
134 if ( !aNamespace
.isEmpty() )
136 aElementName
= aNamespace
;
144 if ( aName
.getLength() > index
+1 )
145 aElementName
+= aName
.copy( index
+1 );
148 // attribute with namespace but without a name is not allowed (e.g. "cfg:" )
149 OUString
aErrorMessage( "Attribute has no name only preceding namespace!" );
150 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
154 aElementName
+= aName
;
159 OUString
XMLNamespaces::getNamespaceValue( const OUString
& aNamespace
) const throw( SAXException
)
161 if ( aNamespace
.isEmpty() )
162 return m_aDefaultNamespace
;
165 NamespaceMap::const_iterator p
;
166 p
= m_aNamespaceMap
.find( aNamespace
);
167 if ( p
!= m_aNamespaceMap
.end() )
171 // namespace not defined => throw exception!
172 OUString
aErrorMessage( "XML namespace used but not defined!" );
173 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */