tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / framework / source / fwe / xml / xmlnamespaces.cxx
blobd8afe175a1590db25b592a527fd347ad90901280
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
27 namespace framework
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))
38 if (rest.empty())
40 aNamespaceName.clear();
42 else if (rest.size() > 1)
44 aNamespaceName = rest.substr(1);
46 else
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;
62 else
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 );
71 else
73 m_aNamespaceMap.emplace( aNamespaceName, aValue );
78 OUString XMLNamespaces::applyNSToAttributeName( const OUString& aName ) const
80 // xml draft: there is no default namespace for attributes!
82 int index;
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;
94 return aName;
97 OUString XMLNamespaces::applyNSToElementName( const OUString& aName ) const
99 // xml draft: element names can have a default namespace
101 int index = aName.indexOf( ':' );
102 OUString aNamespace;
103 OUString aElementName = aName;
105 if ( index > 0 )
106 aNamespace = getNamespaceValue( aName.copy( 0, index ) );
107 else
108 aNamespace = m_aDefaultNamespace;
110 if ( !aNamespace.isEmpty() )
112 aElementName = aNamespace + "^";
114 else
115 return aName;
117 if ( index > 0 )
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 );
126 else
127 aElementName += aName;
129 return aElementName;
132 OUString const & XMLNamespaces::getNamespaceValue( const OUString& aNamespace ) const
134 if ( aNamespace.isEmpty() )
135 return m_aDefaultNamespace;
136 else
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() );
144 return p->second;
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */