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 constexpr char aXMLAttributeNamespace
[] = "xmlns";
37 if ( aNamespaceName
.startsWith( aXMLAttributeNamespace
) )
39 constexpr sal_Int32 nXMLNamespaceLength
= RTL_CONSTASCII_LENGTH(aXMLAttributeNamespace
);
40 if ( aNamespaceName
.getLength() == nXMLNamespaceLength
)
42 aNamespaceName
.clear();
44 else if ( aNamespaceName
.getLength() >= nXMLNamespaceLength
+2 )
46 aNamespaceName
= aNamespaceName
.copy( nXMLNamespaceLength
+1 );
50 // a xml namespace without name is not allowed (e.g. "xmlns:" )
51 throw SAXException( "A xml namespace without name is not allowed!", Reference
< XInterface
>(), Any() );
55 if ( aValue
.isEmpty() && !aNamespaceName
.isEmpty() )
57 // namespace should be reset - as xml draft states this is only allowed
58 // for the default namespace - check and throw exception if check fails
59 throw SAXException( "Clearing xml namespace only allowed for default namespace!", Reference
< XInterface
>(), Any() );
62 if ( aNamespaceName
.isEmpty() )
63 m_aDefaultNamespace
= aValue
;
66 p
= m_aNamespaceMap
.find( aNamespaceName
);
67 if ( p
!= m_aNamespaceMap
.end() )
69 // replace current namespace definition
70 m_aNamespaceMap
.erase( p
);
71 m_aNamespaceMap
.emplace( aNamespaceName
, aValue
);
75 m_aNamespaceMap
.emplace( aNamespaceName
, aValue
);
80 OUString
XMLNamespaces::applyNSToAttributeName( const OUString
& aName
) const
82 // xml draft: there is no default namespace for attributes!
85 if (( index
= aName
.indexOf( ':' )) > 0 )
87 if ( aName
.getLength() <= index
+1 )
89 // attribute with namespace but without name "namespace:" is not allowed!!
90 throw SAXException( "Attribute has no name only preceding namespace!", Reference
< XInterface
>(), Any() );
92 OUString aAttributeName
= getNamespaceValue( aName
.copy( 0, index
)) + "^" + aName
.subView( index
+1);
93 return aAttributeName
;
99 OUString
XMLNamespaces::applyNSToElementName( const OUString
& aName
) const
101 // xml draft: element names can have a default namespace
103 int index
= aName
.indexOf( ':' );
105 OUString aElementName
= aName
;
108 aNamespace
= getNamespaceValue( aName
.copy( 0, index
) );
110 aNamespace
= m_aDefaultNamespace
;
112 if ( !aNamespace
.isEmpty() )
114 aElementName
= aNamespace
+ "^";
121 if ( aName
.getLength() <= index
+1 )
123 // attribute with namespace but without a name is not allowed (e.g. "cfg:" )
124 throw SAXException( "Attribute has no name only preceding namespace!", Reference
< XInterface
>(), Any() );
126 aElementName
+= aName
.subView( index
+1 );
129 aElementName
+= aName
;
134 OUString
const & XMLNamespaces::getNamespaceValue( const OUString
& aNamespace
) const
136 if ( aNamespace
.isEmpty() )
137 return m_aDefaultNamespace
;
140 NamespaceMap::const_iterator p
= m_aNamespaceMap
.find( aNamespace
);
141 if ( p
== m_aNamespaceMap
.end() )
143 // namespace not defined => throw exception!
144 throw SAXException( "XML namespace used but not defined!", Reference
< XInterface
>(), Any() );
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */