1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <xml/xmlnamespaces.hxx>
21 
22 #include <com/sun/star/xml/sax/SAXException.hpp>
23 
24 using namespace ::com::sun::star::xml::sax;
25 using namespace ::com::sun::star::uno;
26 
27 namespace framework
28 {
29 
addNamespace(const OUString & aName,const OUString & aValue)30 void XMLNamespaces::addNamespace( const OUString& aName, const OUString& aValue )
31 {
32     NamespaceMap::iterator p;
33     OUString aNamespaceName( aName );
34 
35     // delete preceding "xmlns"
36     constexpr char aXMLAttributeNamespace[] = "xmlns";
37     if ( aNamespaceName.startsWith( aXMLAttributeNamespace ) )
38     {
39         constexpr sal_Int32 nXMLNamespaceLength = RTL_CONSTASCII_LENGTH(aXMLAttributeNamespace);
40         if ( aNamespaceName.getLength() == nXMLNamespaceLength )
41         {
42             aNamespaceName.clear();
43         }
44         else if ( aNamespaceName.getLength() >= nXMLNamespaceLength+2 )
45         {
46             aNamespaceName = aNamespaceName.copy( nXMLNamespaceLength+1 );
47         }
48         else
49         {
50             // a xml namespace without name is not allowed (e.g. "xmlns:" )
51             throw SAXException( "A xml namespace without name is not allowed!", Reference< XInterface >(), Any() );
52         }
53     }
54 
55     if ( aValue.isEmpty() && !aNamespaceName.isEmpty() )
56     {
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( "Clearing xml namespace only allowed for default namespace!", Reference< XInterface >(), Any() );
60     }
61 
62     if ( aNamespaceName.isEmpty() )
63         m_aDefaultNamespace = aValue;
64     else
65     {
66         p = m_aNamespaceMap.find( aNamespaceName );
67         if ( p != m_aNamespaceMap.end() )
68         {
69             // replace current namespace definition
70             m_aNamespaceMap.erase( p );
71             m_aNamespaceMap.emplace( aNamespaceName, aValue );
72         }
73         else
74         {
75             m_aNamespaceMap.emplace( aNamespaceName, aValue );
76         }
77     }
78 }
79 
applyNSToAttributeName(const OUString & aName) const80 OUString XMLNamespaces::applyNSToAttributeName( const OUString& aName ) const
81 {
82     // xml draft: there is no default namespace for attributes!
83 
84     int index;
85     if (( index = aName.indexOf( ':' )) > 0 )
86     {
87         if ( aName.getLength() <= index+1 )
88         {
89             // attribute with namespace but without name "namespace:" is not allowed!!
90             throw SAXException( "Attribute has no name only preceding namespace!", Reference< XInterface >(), Any() );
91         }
92         OUString aAttributeName = getNamespaceValue( aName.copy( 0, index )) + "^" + aName.copy( index+1);
93         return aAttributeName;
94     }
95 
96     return aName;
97 }
98 
applyNSToElementName(const OUString & aName) const99 OUString XMLNamespaces::applyNSToElementName( const OUString& aName ) const
100 {
101     // xml draft: element names can have a default namespace
102 
103     int         index = aName.indexOf( ':' );
104     OUString aNamespace;
105     OUString aElementName = aName;
106 
107     if ( index > 0 )
108         aNamespace = getNamespaceValue( aName.copy( 0, index ) );
109     else
110         aNamespace = m_aDefaultNamespace;
111 
112     if ( !aNamespace.isEmpty() )
113     {
114         aElementName = aNamespace + "^";
115     }
116     else
117         return aName;
118 
119     if ( index > 0 )
120     {
121         if ( aName.getLength() <= index+1 )
122         {
123             // attribute with namespace but without a name is not allowed (e.g. "cfg:" )
124             throw SAXException( "Attribute has no name only preceding namespace!", Reference< XInterface >(), Any() );
125         }
126         aElementName += aName.copy( index+1 );
127     }
128     else
129         aElementName += aName;
130 
131     return aElementName;
132 }
133 
getNamespaceValue(const OUString & aNamespace) const134 OUString const & XMLNamespaces::getNamespaceValue( const OUString& aNamespace ) const
135 {
136     if ( aNamespace.isEmpty() )
137         return m_aDefaultNamespace;
138     else
139     {
140         NamespaceMap::const_iterator p = m_aNamespaceMap.find( aNamespace );
141         if ( p == m_aNamespaceMap.end() )
142         {
143             // namespace not defined => throw exception!
144             throw SAXException( "XML namespace used but not defined!", Reference< XInterface >(), Any() );
145         }
146         return p->second;
147     }
148 }
149 
150 }
151 
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
153