1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*6d739b60SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*6d739b60SAndrew Rist * distributed with this work for additional information
6*6d739b60SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*6d739b60SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist * with the License. You may obtain a copy of the License at
10*6d739b60SAndrew Rist *
11*6d739b60SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*6d739b60SAndrew Rist *
13*6d739b60SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist * KIND, either express or implied. See the License for the
17*6d739b60SAndrew Rist * specific language governing permissions and limitations
18*6d739b60SAndrew Rist * under the License.
19*6d739b60SAndrew Rist *
20*6d739b60SAndrew Rist *************************************************************/
21*6d739b60SAndrew Rist
22*6d739b60SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <xml/xmlnamespaces.hxx>
28cdf0e10cSrcweir
29cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
30cdf0e10cSrcweir using namespace ::com::sun::star::uno;
31cdf0e10cSrcweir
32cdf0e10cSrcweir const ::rtl::OUString aXMLAttributeNamespace( RTL_CONSTASCII_USTRINGPARAM( "xmlns" ));
33cdf0e10cSrcweir
34cdf0e10cSrcweir namespace framework
35cdf0e10cSrcweir {
36cdf0e10cSrcweir
XMLNamespaces()37cdf0e10cSrcweir XMLNamespaces::XMLNamespaces()
38cdf0e10cSrcweir {
39cdf0e10cSrcweir }
40cdf0e10cSrcweir
XMLNamespaces(const XMLNamespaces & aXMLNamespaces)41cdf0e10cSrcweir XMLNamespaces::XMLNamespaces( const XMLNamespaces& aXMLNamespaces )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir m_aDefaultNamespace = aXMLNamespaces.m_aDefaultNamespace;
44cdf0e10cSrcweir m_aNamespaceMap = aXMLNamespaces.m_aNamespaceMap;
45cdf0e10cSrcweir }
46cdf0e10cSrcweir
~XMLNamespaces()47cdf0e10cSrcweir XMLNamespaces::~XMLNamespaces()
48cdf0e10cSrcweir {
49cdf0e10cSrcweir }
50cdf0e10cSrcweir
addNamespace(const::rtl::OUString & aName,const::rtl::OUString & aValue)51cdf0e10cSrcweir void XMLNamespaces::addNamespace( const ::rtl::OUString& aName, const ::rtl::OUString& aValue ) throw( SAXException )
52cdf0e10cSrcweir {
53cdf0e10cSrcweir NamespaceMap::iterator p;
54cdf0e10cSrcweir ::rtl::OUString aNamespaceName( aName );
55cdf0e10cSrcweir sal_Int32 nXMLNamespaceLength = aXMLAttributeNamespace.getLength();
56cdf0e10cSrcweir
57cdf0e10cSrcweir // delete preceding "xmlns"
58cdf0e10cSrcweir if ( aNamespaceName.compareTo( aXMLAttributeNamespace, nXMLNamespaceLength ) == 0 )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir if ( aNamespaceName.getLength() == nXMLNamespaceLength )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir aNamespaceName = ::rtl::OUString();
63cdf0e10cSrcweir }
64cdf0e10cSrcweir else if ( aNamespaceName.getLength() >= nXMLNamespaceLength+2 )
65cdf0e10cSrcweir {
66cdf0e10cSrcweir aNamespaceName = aNamespaceName.copy( nXMLNamespaceLength+1 );
67cdf0e10cSrcweir }
68cdf0e10cSrcweir else
69cdf0e10cSrcweir {
70cdf0e10cSrcweir // a xml namespace without name is not allowed (e.g. "xmlns:" )
71cdf0e10cSrcweir ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "A xml namespace without name is not allowed!" ));
72cdf0e10cSrcweir throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
73cdf0e10cSrcweir }
74cdf0e10cSrcweir }
75cdf0e10cSrcweir
76cdf0e10cSrcweir if ( aValue.getLength() == 0 && aNamespaceName.getLength() > 0 )
77cdf0e10cSrcweir {
78cdf0e10cSrcweir // namespace should be reseted - as xml draft states this is only allowed
79cdf0e10cSrcweir // for the default namespace - check and throw exception if check fails
80cdf0e10cSrcweir ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "Clearing xml namespace only allowed for default namespace!" ));
81cdf0e10cSrcweir throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
82cdf0e10cSrcweir }
83cdf0e10cSrcweir else
84cdf0e10cSrcweir {
85cdf0e10cSrcweir if ( aNamespaceName.getLength() == 0 )
86cdf0e10cSrcweir m_aDefaultNamespace = aValue;
87cdf0e10cSrcweir else
88cdf0e10cSrcweir {
89cdf0e10cSrcweir p = m_aNamespaceMap.find( aNamespaceName );
90cdf0e10cSrcweir if ( p != m_aNamespaceMap.end() )
91cdf0e10cSrcweir {
92cdf0e10cSrcweir // replace current namespace definition
93cdf0e10cSrcweir m_aNamespaceMap.erase( p );
94cdf0e10cSrcweir m_aNamespaceMap.insert( NamespaceMap::value_type( aNamespaceName, aValue ));
95cdf0e10cSrcweir }
96cdf0e10cSrcweir else
97cdf0e10cSrcweir {
98cdf0e10cSrcweir m_aNamespaceMap.insert( NamespaceMap::value_type( aNamespaceName, aValue ));
99cdf0e10cSrcweir }
100cdf0e10cSrcweir }
101cdf0e10cSrcweir }
102cdf0e10cSrcweir }
103cdf0e10cSrcweir
applyNSToAttributeName(const::rtl::OUString & aName) const104cdf0e10cSrcweir ::rtl::OUString XMLNamespaces::applyNSToAttributeName( const ::rtl::OUString& aName ) const throw( SAXException )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir // xml draft: there is no default namespace for attributes!
107cdf0e10cSrcweir
108cdf0e10cSrcweir int index;
109cdf0e10cSrcweir if (( index = aName.indexOf( ':' )) > 0 )
110cdf0e10cSrcweir {
111cdf0e10cSrcweir if ( aName.getLength() > index+1 )
112cdf0e10cSrcweir {
113cdf0e10cSrcweir ::rtl::OUString aAttributeName = getNamespaceValue( aName.copy( 0, index ) );
114cdf0e10cSrcweir aAttributeName += ::rtl::OUString::createFromAscii( "^" );
115cdf0e10cSrcweir aAttributeName += aName.copy( index+1 );
116cdf0e10cSrcweir return aAttributeName;
117cdf0e10cSrcweir }
118cdf0e10cSrcweir else
119cdf0e10cSrcweir {
120cdf0e10cSrcweir // attribute with namespace but without name "namespace:" is not allowed!!
121cdf0e10cSrcweir ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "Attribute has no name only preceding namespace!" ));
122cdf0e10cSrcweir throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
123cdf0e10cSrcweir }
124cdf0e10cSrcweir }
125cdf0e10cSrcweir
126cdf0e10cSrcweir return aName;
127cdf0e10cSrcweir }
128cdf0e10cSrcweir
applyNSToElementName(const::rtl::OUString & aName) const129cdf0e10cSrcweir ::rtl::OUString XMLNamespaces::applyNSToElementName( const ::rtl::OUString& aName ) const throw( SAXException )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir // xml draft: element names can have a default namespace
132cdf0e10cSrcweir
133cdf0e10cSrcweir int index = aName.indexOf( ':' );
134cdf0e10cSrcweir ::rtl::OUString aNamespace;
135cdf0e10cSrcweir ::rtl::OUString aElementName = aName;
136cdf0e10cSrcweir
137cdf0e10cSrcweir if ( index > 0 )
138cdf0e10cSrcweir aNamespace = getNamespaceValue( aName.copy( 0, index ) );
139cdf0e10cSrcweir else
140cdf0e10cSrcweir aNamespace = m_aDefaultNamespace;
141cdf0e10cSrcweir
142cdf0e10cSrcweir if ( aNamespace.getLength() > 0 )
143cdf0e10cSrcweir {
144cdf0e10cSrcweir aElementName = aNamespace;
145cdf0e10cSrcweir aElementName += ::rtl::OUString::createFromAscii( "^" );
146cdf0e10cSrcweir }
147cdf0e10cSrcweir else
148cdf0e10cSrcweir return aName;
149cdf0e10cSrcweir
150cdf0e10cSrcweir if ( index > 0 )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir if ( aName.getLength() > index+1 )
153cdf0e10cSrcweir aElementName += aName.copy( index+1 );
154cdf0e10cSrcweir else
155cdf0e10cSrcweir {
156cdf0e10cSrcweir // attribute with namespace but without a name is not allowed (e.g. "cfg:" )
157cdf0e10cSrcweir ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "Attribute has no name only preceding namespace!" ));
158cdf0e10cSrcweir throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
159cdf0e10cSrcweir }
160cdf0e10cSrcweir }
161cdf0e10cSrcweir else
162cdf0e10cSrcweir aElementName += aName;
163cdf0e10cSrcweir
164cdf0e10cSrcweir return aElementName;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir
getNamespaceValue(const::rtl::OUString & aNamespace) const167cdf0e10cSrcweir ::rtl::OUString XMLNamespaces::getNamespaceValue( const ::rtl::OUString& aNamespace ) const throw( SAXException )
168cdf0e10cSrcweir {
169cdf0e10cSrcweir if ( aNamespace.getLength() == 0 )
170cdf0e10cSrcweir return m_aDefaultNamespace;
171cdf0e10cSrcweir else
172cdf0e10cSrcweir {
173cdf0e10cSrcweir NamespaceMap::const_iterator p;
174cdf0e10cSrcweir p = m_aNamespaceMap.find( aNamespace );
175cdf0e10cSrcweir if ( p != m_aNamespaceMap.end() )
176cdf0e10cSrcweir return p->second;
177cdf0e10cSrcweir else
178cdf0e10cSrcweir {
179cdf0e10cSrcweir // namespace not defined => throw exception!
180cdf0e10cSrcweir ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "XML namespace used but not defined!" ));
181cdf0e10cSrcweir throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
182cdf0e10cSrcweir }
183cdf0e10cSrcweir }
184cdf0e10cSrcweir }
185cdf0e10cSrcweir
186cdf0e10cSrcweir }
187cdf0e10cSrcweir
188