merge the formfield patch from ooo-build
[ooovba.git] / framework / source / xml / xmlnamespaces.cxx
blobbfe32e18eb58bae232e9b2967f2cb33b06df6b26
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlnamespaces.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 #include <xml/xmlnamespaces.hxx>
36 using namespace ::com::sun::star::xml::sax;
37 using namespace ::com::sun::star::uno;
39 const ::rtl::OUString aXMLAttributeNamespace( RTL_CONSTASCII_USTRINGPARAM( "xmlns" ));
41 namespace framework
44 XMLNamespaces::XMLNamespaces()
48 XMLNamespaces::XMLNamespaces( const XMLNamespaces& aXMLNamespaces )
50 m_aDefaultNamespace = aXMLNamespaces.m_aDefaultNamespace;
51 m_aNamespaceMap = aXMLNamespaces.m_aNamespaceMap;
54 XMLNamespaces::~XMLNamespaces()
58 void XMLNamespaces::addNamespace( const ::rtl::OUString& aName, const ::rtl::OUString& aValue ) throw( SAXException )
60 NamespaceMap::iterator p;
61 ::rtl::OUString aNamespaceName( aName );
62 sal_Int32 nXMLNamespaceLength = aXMLAttributeNamespace.getLength();
64 // delete preceding "xmlns"
65 if ( aNamespaceName.compareTo( aXMLAttributeNamespace, nXMLNamespaceLength ) == 0 )
67 if ( aNamespaceName.getLength() == nXMLNamespaceLength )
69 aNamespaceName = ::rtl::OUString();
71 else if ( aNamespaceName.getLength() >= nXMLNamespaceLength+2 )
73 aNamespaceName = aNamespaceName.copy( nXMLNamespaceLength+1 );
75 else
77 // a xml namespace without name is not allowed (e.g. "xmlns:" )
78 ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "A xml namespace without name is not allowed!" ));
79 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
83 if ( aValue.getLength() == 0 && aNamespaceName.getLength() > 0 )
85 // namespace should be reseted - as xml draft states this is only allowed
86 // for the default namespace - check and throw exception if check fails
87 ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "Clearing xml namespace only allowed for default namespace!" ));
88 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
90 else
92 if ( aNamespaceName.getLength() == 0 )
93 m_aDefaultNamespace = aValue;
94 else
96 p = m_aNamespaceMap.find( aNamespaceName );
97 if ( p != m_aNamespaceMap.end() )
99 // replace current namespace definition
100 m_aNamespaceMap.erase( p );
101 m_aNamespaceMap.insert( NamespaceMap::value_type( aNamespaceName, aValue ));
103 else
105 m_aNamespaceMap.insert( NamespaceMap::value_type( aNamespaceName, aValue ));
111 ::rtl::OUString XMLNamespaces::applyNSToAttributeName( const ::rtl::OUString& aName ) const throw( SAXException )
113 // xml draft: there is no default namespace for attributes!
115 int index;
116 if (( index = aName.indexOf( ':' )) > 0 )
118 if ( aName.getLength() > index+1 )
120 ::rtl::OUString aAttributeName = getNamespaceValue( aName.copy( 0, index ) );
121 aAttributeName += ::rtl::OUString::createFromAscii( "^" );
122 aAttributeName += aName.copy( index+1 );
123 return aAttributeName;
125 else
127 // attribute with namespace but without name "namespace:" is not allowed!!
128 ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "Attribute has no name only preceding namespace!" ));
129 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
133 return aName;
136 ::rtl::OUString XMLNamespaces::applyNSToElementName( const ::rtl::OUString& aName ) const throw( SAXException )
138 // xml draft: element names can have a default namespace
140 int index = aName.indexOf( ':' );
141 ::rtl::OUString aNamespace;
142 ::rtl::OUString aElementName = aName;
144 if ( index > 0 )
145 aNamespace = getNamespaceValue( aName.copy( 0, index ) );
146 else
147 aNamespace = m_aDefaultNamespace;
149 if ( aNamespace.getLength() > 0 )
151 aElementName = aNamespace;
152 aElementName += ::rtl::OUString::createFromAscii( "^" );
154 else
155 return aName;
157 if ( index > 0 )
159 if ( aName.getLength() > index+1 )
160 aElementName += aName.copy( index+1 );
161 else
163 // attribute with namespace but without a name is not allowed (e.g. "cfg:" )
164 ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "Attribute has no name only preceding namespace!" ));
165 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
168 else
169 aElementName += aName;
171 return aElementName;
174 ::rtl::OUString XMLNamespaces::getNamespaceValue( const ::rtl::OUString& aNamespace ) const throw( SAXException )
176 if ( aNamespace.getLength() == 0 )
177 return m_aDefaultNamespace;
178 else
180 NamespaceMap::const_iterator p;
181 p = m_aNamespaceMap.find( aNamespace );
182 if ( p != m_aNamespaceMap.end() )
183 return p->second;
184 else
186 // namespace not defined => throw exception!
187 ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "XML namespace used but not defined!" ));
188 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );