Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <xml/xmlnamespaces.hxx>
31 : :
32 : : using namespace ::com::sun::star::xml::sax;
33 : : using namespace ::com::sun::star::uno;
34 : :
35 : : namespace framework
36 : : {
37 : :
38 : 41584 : XMLNamespaces::XMLNamespaces()
39 [ + - ][ + - ]: 41584 : : m_aXMLAttributeNamespace( RTL_CONSTASCII_USTRINGPARAM( "xmlns" ))
40 : : {
41 : 41584 : }
42 : :
43 [ + - ]: 41584 : XMLNamespaces::XMLNamespaces( const XMLNamespaces& aXMLNamespaces )
44 : : {
45 : 41584 : m_aDefaultNamespace = aXMLNamespaces.m_aDefaultNamespace;
46 [ + - ]: 41584 : m_aNamespaceMap = aXMLNamespaces.m_aNamespaceMap;
47 : 41584 : }
48 : :
49 : 83168 : XMLNamespaces::~XMLNamespaces()
50 : : {
51 [ - + ]: 83168 : }
52 : :
53 : 728 : void XMLNamespaces::addNamespace( const ::rtl::OUString& aName, const ::rtl::OUString& aValue ) throw( SAXException )
54 : : {
55 : 728 : NamespaceMap::iterator p;
56 : 728 : ::rtl::OUString aNamespaceName( aName );
57 : 728 : sal_Int32 nXMLNamespaceLength = m_aXMLAttributeNamespace.getLength();
58 : :
59 : : // delete preceding "xmlns"
60 [ + - ]: 728 : if ( aNamespaceName.compareTo( m_aXMLAttributeNamespace, nXMLNamespaceLength ) == 0 )
61 : : {
62 [ - + ]: 728 : if ( aNamespaceName.getLength() == nXMLNamespaceLength )
63 : : {
64 : 0 : aNamespaceName = ::rtl::OUString();
65 : : }
66 [ + - ]: 728 : else if ( aNamespaceName.getLength() >= nXMLNamespaceLength+2 )
67 : : {
68 : 728 : aNamespaceName = aNamespaceName.copy( nXMLNamespaceLength+1 );
69 : : }
70 : : else
71 : : {
72 : : // a xml namespace without name is not allowed (e.g. "xmlns:" )
73 [ # # ]: 0 : ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "A xml namespace without name is not allowed!" ));
74 [ # # ]: 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
75 : : }
76 : : }
77 : :
78 [ - + ][ # # ]: 728 : if ( aValue.isEmpty() && !aNamespaceName.isEmpty() )
[ - + ]
79 : : {
80 : : // namespace should be reseted - as xml draft states this is only allowed
81 : : // for the default namespace - check and throw exception if check fails
82 [ # # ]: 0 : ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "Clearing xml namespace only allowed for default namespace!" ));
83 [ # # ]: 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
84 : : }
85 : : else
86 : : {
87 [ - + ]: 728 : if ( aNamespaceName.isEmpty() )
88 : 0 : m_aDefaultNamespace = aValue;
89 : : else
90 : : {
91 [ + - ]: 728 : p = m_aNamespaceMap.find( aNamespaceName );
92 [ - + ]: 728 : if ( p != m_aNamespaceMap.end() )
93 : : {
94 : : // replace current namespace definition
95 [ # # ]: 0 : m_aNamespaceMap.erase( p );
96 [ # # ]: 0 : m_aNamespaceMap.insert( NamespaceMap::value_type( aNamespaceName, aValue ));
97 : : }
98 : : else
99 : : {
100 [ + - ]: 728 : m_aNamespaceMap.insert( NamespaceMap::value_type( aNamespaceName, aValue ));
101 : : }
102 : : }
103 : 728 : }
104 : 728 : }
105 : :
106 : 45094 : ::rtl::OUString XMLNamespaces::applyNSToAttributeName( const ::rtl::OUString& aName ) const throw( SAXException )
107 : : {
108 : : // xml draft: there is no default namespace for attributes!
109 : :
110 : : int index;
111 [ + - ]: 45094 : if (( index = aName.indexOf( ':' )) > 0 )
112 : : {
113 [ + - ]: 45094 : if ( aName.getLength() > index+1 )
114 : : {
115 [ + - ]: 45094 : ::rtl::OUString aAttributeName = getNamespaceValue( aName.copy( 0, index ) );
116 [ + - ]: 45094 : aAttributeName += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("^"));
117 : 45094 : aAttributeName += aName.copy( index+1 );
118 : 45094 : return aAttributeName;
119 : : }
120 : : else
121 : : {
122 : : // attribute with namespace but without name "namespace:" is not allowed!!
123 [ # # ]: 0 : ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "Attribute has no name only preceding namespace!" ));
124 [ # # ]: 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
125 : : }
126 : : }
127 : :
128 : 45094 : return aName;
129 : : }
130 : :
131 : 83168 : ::rtl::OUString XMLNamespaces::applyNSToElementName( const ::rtl::OUString& aName ) const throw( SAXException )
132 : : {
133 : : // xml draft: element names can have a default namespace
134 : :
135 : 83168 : int index = aName.indexOf( ':' );
136 : 83168 : ::rtl::OUString aNamespace;
137 : 83168 : ::rtl::OUString aElementName = aName;
138 : :
139 [ + - ]: 83168 : if ( index > 0 )
140 [ + - ]: 83168 : aNamespace = getNamespaceValue( aName.copy( 0, index ) );
141 : : else
142 : 0 : aNamespace = m_aDefaultNamespace;
143 : :
144 [ + - ]: 83168 : if ( !aNamespace.isEmpty() )
145 : : {
146 : 83168 : aElementName = aNamespace;
147 [ + - ]: 83168 : aElementName += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("^"));
148 : : }
149 : : else
150 : 0 : return aName;
151 : :
152 [ + - ]: 83168 : if ( index > 0 )
153 : : {
154 [ + - ]: 83168 : if ( aName.getLength() > index+1 )
155 : 83168 : aElementName += aName.copy( index+1 );
156 : : else
157 : : {
158 : : // attribute with namespace but without a name is not allowed (e.g. "cfg:" )
159 [ # # ]: 0 : ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "Attribute has no name only preceding namespace!" ));
160 [ # # ]: 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
161 : : }
162 : : }
163 : : else
164 : 0 : aElementName += aName;
165 : :
166 : 83168 : return aElementName;
167 : : }
168 : :
169 : 128262 : ::rtl::OUString XMLNamespaces::getNamespaceValue( const ::rtl::OUString& aNamespace ) const throw( SAXException )
170 : : {
171 [ - + ]: 128262 : if ( aNamespace.isEmpty() )
172 : 0 : return m_aDefaultNamespace;
173 : : else
174 : : {
175 : 128262 : NamespaceMap::const_iterator p;
176 [ + - ]: 128262 : p = m_aNamespaceMap.find( aNamespace );
177 [ + - ]: 128262 : if ( p != m_aNamespaceMap.end() )
178 : 128262 : return p->second;
179 : : else
180 : : {
181 : : // namespace not defined => throw exception!
182 [ # # ]: 0 : ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "XML namespace used but not defined!" ));
183 [ # # ]: 128262 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
184 : : }
185 : : }
186 : : }
187 : :
188 : : }
189 : :
190 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|