tdf#163234: CairoSDPR: PixelSnap corrections
[LibreOffice.git] / framework / source / fwe / xml / xmlnamespaces.cxx
blobc4fffaf89fe815ca0dc6ead5db3f799123f0b52c
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 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 );
48 else
50 // a xml namespace without name is not allowed (e.g. "xmlns:" )
51 throw SAXException( u"A xml namespace without name is not allowed!"_ustr, 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( u"Clearing xml namespace only allowed for default namespace!"_ustr, Reference< XInterface >(), Any() );
62 if ( aNamespaceName.isEmpty() )
63 m_aDefaultNamespace = aValue;
64 else
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 );
73 else
75 m_aNamespaceMap.emplace( aNamespaceName, aValue );
80 OUString XMLNamespaces::applyNSToAttributeName( const OUString& aName ) const
82 // xml draft: there is no default namespace for attributes!
84 int index;
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( u"Attribute has no name only preceding namespace!"_ustr, Reference< XInterface >(), Any() );
92 OUString aAttributeName = getNamespaceValue( aName.copy( 0, index )) + "^" + aName.subView( index+1);
93 return aAttributeName;
96 return aName;
99 OUString XMLNamespaces::applyNSToElementName( const OUString& aName ) const
101 // xml draft: element names can have a default namespace
103 int index = aName.indexOf( ':' );
104 OUString aNamespace;
105 OUString aElementName = aName;
107 if ( index > 0 )
108 aNamespace = getNamespaceValue( aName.copy( 0, index ) );
109 else
110 aNamespace = m_aDefaultNamespace;
112 if ( !aNamespace.isEmpty() )
114 aElementName = aNamespace + "^";
116 else
117 return aName;
119 if ( index > 0 )
121 if ( aName.getLength() <= index+1 )
123 // attribute with namespace but without a name is not allowed (e.g. "cfg:" )
124 throw SAXException( u"Attribute has no name only preceding namespace!"_ustr, Reference< XInterface >(), Any() );
126 aElementName += aName.subView( index+1 );
128 else
129 aElementName += aName;
131 return aElementName;
134 OUString const & XMLNamespaces::getNamespaceValue( const OUString& aNamespace ) const
136 if ( aNamespace.isEmpty() )
137 return m_aDefaultNamespace;
138 else
140 NamespaceMap::const_iterator p = m_aNamespaceMap.find( aNamespace );
141 if ( p == m_aNamespaceMap.end() )
143 // namespace not defined => throw exception!
144 throw SAXException( u"XML namespace used but not defined!"_ustr, Reference< XInterface >(), Any() );
146 return p->second;
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */