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 #include <com/sun/star/xml/sax/SAXException.hpp>
24 using namespace ::com::sun::star::xml::sax
;
25 using namespace ::com::sun::star::uno
;
30 void XMLNamespaces::addNamespace( const OUString
& aName
, const OUString
& aValue
)
32 NamespaceMap::iterator p
;
33 OUString
aNamespaceName( aName
);
35 // delete preceding "xmlns"
36 if (std::u16string_view rest
; aNamespaceName
.startsWith("xmlns", &rest
))
40 aNamespaceName
.clear();
42 else if (rest
.size() > 1)
44 aNamespaceName
= rest
.substr(1);
48 // a xml namespace without name is not allowed (e.g. "xmlns:" )
49 throw SAXException( u
"A xml namespace without name is not allowed!"_ustr
, Reference
< XInterface
>(), Any() );
53 if ( aValue
.isEmpty() && !aNamespaceName
.isEmpty() )
55 // namespace should be reset - as xml draft states this is only allowed
56 // for the default namespace - check and throw exception if check fails
57 throw SAXException( u
"Clearing xml namespace only allowed for default namespace!"_ustr
, Reference
< XInterface
>(), Any() );
60 if ( aNamespaceName
.isEmpty() )
61 m_aDefaultNamespace
= aValue
;
64 p
= m_aNamespaceMap
.find( aNamespaceName
);
65 if ( p
!= m_aNamespaceMap
.end() )
67 // replace current namespace definition
68 m_aNamespaceMap
.erase( p
);
69 m_aNamespaceMap
.emplace( aNamespaceName
, aValue
);
73 m_aNamespaceMap
.emplace( aNamespaceName
, aValue
);
78 OUString
XMLNamespaces::applyNSToAttributeName( const OUString
& aName
) const
80 // xml draft: there is no default namespace for attributes!
83 if (( index
= aName
.indexOf( ':' )) > 0 )
85 if ( aName
.getLength() <= index
+1 )
87 // attribute with namespace but without name "namespace:" is not allowed!!
88 throw SAXException( u
"Attribute has no name only preceding namespace!"_ustr
, Reference
< XInterface
>(), Any() );
90 OUString aAttributeName
= getNamespaceValue( aName
.copy( 0, index
)) + "^" + aName
.subView( index
+1);
91 return aAttributeName
;
97 OUString
XMLNamespaces::applyNSToElementName( const OUString
& aName
) const
99 // xml draft: element names can have a default namespace
101 int index
= aName
.indexOf( ':' );
103 OUString aElementName
= aName
;
106 aNamespace
= getNamespaceValue( aName
.copy( 0, index
) );
108 aNamespace
= m_aDefaultNamespace
;
110 if ( !aNamespace
.isEmpty() )
112 aElementName
= aNamespace
+ "^";
119 if ( aName
.getLength() <= index
+1 )
121 // attribute with namespace but without a name is not allowed (e.g. "cfg:" )
122 throw SAXException( u
"Attribute has no name only preceding namespace!"_ustr
, Reference
< XInterface
>(), Any() );
124 aElementName
+= aName
.subView( index
+1 );
127 aElementName
+= aName
;
132 OUString
const & XMLNamespaces::getNamespaceValue( const OUString
& aNamespace
) const
134 if ( aNamespace
.isEmpty() )
135 return m_aDefaultNamespace
;
138 NamespaceMap::const_iterator p
= m_aNamespaceMap
.find( aNamespace
);
139 if ( p
== m_aNamespaceMap
.end() )
141 // namespace not defined => throw exception!
142 throw SAXException( u
"XML namespace used but not defined!"_ustr
, Reference
< XInterface
>(), Any() );
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */