/src/libreoffice/framework/source/fwe/xml/xmlnamespaces.cxx
Line | Count | Source |
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 | | |
30 | | void XMLNamespaces::addNamespace( const OUString& aName, const OUString& aValue ) |
31 | 0 | { |
32 | 0 | NamespaceMap::iterator p; |
33 | 0 | OUString aNamespaceName( aName ); |
34 | | |
35 | | // delete preceding "xmlns" |
36 | 0 | if (std::u16string_view rest; aNamespaceName.startsWith("xmlns", &rest)) |
37 | 0 | { |
38 | 0 | if (rest.empty()) |
39 | 0 | { |
40 | 0 | aNamespaceName.clear(); |
41 | 0 | } |
42 | 0 | else if (rest.size() > 1) |
43 | 0 | { |
44 | 0 | aNamespaceName = rest.substr(1); |
45 | 0 | } |
46 | 0 | else |
47 | 0 | { |
48 | | // a xml namespace without name is not allowed (e.g. "xmlns:" ) |
49 | 0 | throw SAXException( u"A xml namespace without name is not allowed!"_ustr, Reference< XInterface >(), Any() ); |
50 | 0 | } |
51 | 0 | } |
52 | | |
53 | 0 | if ( aValue.isEmpty() && !aNamespaceName.isEmpty() ) |
54 | 0 | { |
55 | | // namespace should be reset - as xml draft states this is only allowed |
56 | | // for the default namespace - check and throw exception if check fails |
57 | 0 | throw SAXException( u"Clearing xml namespace only allowed for default namespace!"_ustr, Reference< XInterface >(), Any() ); |
58 | 0 | } |
59 | | |
60 | 0 | if ( aNamespaceName.isEmpty() ) |
61 | 0 | m_aDefaultNamespace = aValue; |
62 | 0 | else |
63 | 0 | { |
64 | 0 | p = m_aNamespaceMap.find( aNamespaceName ); |
65 | 0 | if ( p != m_aNamespaceMap.end() ) |
66 | 0 | { |
67 | | // replace current namespace definition |
68 | 0 | m_aNamespaceMap.erase( p ); |
69 | 0 | m_aNamespaceMap.emplace( aNamespaceName, aValue ); |
70 | 0 | } |
71 | 0 | else |
72 | 0 | { |
73 | 0 | m_aNamespaceMap.emplace( aNamespaceName, aValue ); |
74 | 0 | } |
75 | 0 | } |
76 | 0 | } |
77 | | |
78 | | OUString XMLNamespaces::applyNSToAttributeName( const OUString& aName ) const |
79 | 0 | { |
80 | | // xml draft: there is no default namespace for attributes! |
81 | |
|
82 | 0 | int index; |
83 | 0 | if (( index = aName.indexOf( ':' )) > 0 ) |
84 | 0 | { |
85 | 0 | if ( aName.getLength() <= index+1 ) |
86 | 0 | { |
87 | | // attribute with namespace but without name "namespace:" is not allowed!! |
88 | 0 | throw SAXException( u"Attribute has no name only preceding namespace!"_ustr, Reference< XInterface >(), Any() ); |
89 | 0 | } |
90 | 0 | OUString aAttributeName = getNamespaceValue( aName.copy( 0, index )) + "^" + aName.subView( index+1); |
91 | 0 | return aAttributeName; |
92 | 0 | } |
93 | | |
94 | 0 | return aName; |
95 | 0 | } |
96 | | |
97 | | OUString XMLNamespaces::applyNSToElementName( const OUString& aName ) const |
98 | 0 | { |
99 | | // xml draft: element names can have a default namespace |
100 | |
|
101 | 0 | int index = aName.indexOf( ':' ); |
102 | 0 | OUString aNamespace; |
103 | 0 | OUString aElementName = aName; |
104 | |
|
105 | 0 | if ( index > 0 ) |
106 | 0 | aNamespace = getNamespaceValue( aName.copy( 0, index ) ); |
107 | 0 | else |
108 | 0 | aNamespace = m_aDefaultNamespace; |
109 | |
|
110 | 0 | if ( !aNamespace.isEmpty() ) |
111 | 0 | { |
112 | 0 | aElementName = aNamespace + "^"; |
113 | 0 | } |
114 | 0 | else |
115 | 0 | return aName; |
116 | | |
117 | 0 | if ( index > 0 ) |
118 | 0 | { |
119 | 0 | if ( aName.getLength() <= index+1 ) |
120 | 0 | { |
121 | | // attribute with namespace but without a name is not allowed (e.g. "cfg:" ) |
122 | 0 | throw SAXException( u"Attribute has no name only preceding namespace!"_ustr, Reference< XInterface >(), Any() ); |
123 | 0 | } |
124 | 0 | aElementName += aName.subView( index+1 ); |
125 | 0 | } |
126 | 0 | else |
127 | 0 | aElementName += aName; |
128 | | |
129 | 0 | return aElementName; |
130 | 0 | } |
131 | | |
132 | | OUString const & XMLNamespaces::getNamespaceValue( const OUString& aNamespace ) const |
133 | 0 | { |
134 | 0 | if ( aNamespace.isEmpty() ) |
135 | 0 | return m_aDefaultNamespace; |
136 | 0 | else |
137 | 0 | { |
138 | 0 | NamespaceMap::const_iterator p = m_aNamespaceMap.find( aNamespace ); |
139 | 0 | if ( p == m_aNamespaceMap.end() ) |
140 | 0 | { |
141 | | // namespace not defined => throw exception! |
142 | 0 | throw SAXException( u"XML namespace used but not defined!"_ustr, Reference< XInterface >(), Any() ); |
143 | 0 | } |
144 | 0 | return p->second; |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | | } |
149 | | |
150 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |