fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / framework / source / fwe / xml / xmlnamespaces.cxx
blobf52536e2d0989d160f8bb4680eb924fe96c30421
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 using namespace ::com::sun::star::xml::sax;
23 using namespace ::com::sun::star::uno;
25 namespace framework
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 );
60 else
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() );
75 else
77 if ( aNamespaceName.isEmpty() )
78 m_aDefaultNamespace = aValue;
79 else
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 ));
88 else
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!
100 int index;
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;
110 else
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() );
118 return aName;
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( ':' );
126 OUString aNamespace;
127 OUString aElementName = aName;
129 if ( index > 0 )
130 aNamespace = getNamespaceValue( aName.copy( 0, index ) );
131 else
132 aNamespace = m_aDefaultNamespace;
134 if ( !aNamespace.isEmpty() )
136 aElementName = aNamespace;
137 aElementName += "^";
139 else
140 return aName;
142 if ( index > 0 )
144 if ( aName.getLength() > index+1 )
145 aElementName += aName.copy( index+1 );
146 else
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() );
153 else
154 aElementName += aName;
156 return aElementName;
159 OUString XMLNamespaces::getNamespaceValue( const OUString& aNamespace ) const throw( SAXException )
161 if ( aNamespace.isEmpty() )
162 return m_aDefaultNamespace;
163 else
165 NamespaceMap::const_iterator p;
166 p = m_aNamespaceMap.find( aNamespace );
167 if ( p != m_aNamespaceMap.end() )
168 return p->second;
169 else
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: */