Line data Source code
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 : using namespace ::com::sun::star::xml::sax;
23 : using namespace ::com::sun::star::uno;
24 :
25 : namespace framework
26 : {
27 :
28 35065 : XMLNamespaces::XMLNamespaces()
29 35065 : : m_aXMLAttributeNamespace( "xmlns" )
30 : {
31 35065 : }
32 :
33 35065 : XMLNamespaces::XMLNamespaces( const XMLNamespaces& aXMLNamespaces )
34 : {
35 35065 : m_aDefaultNamespace = aXMLNamespaces.m_aDefaultNamespace;
36 35065 : m_aNamespaceMap = aXMLNamespaces.m_aNamespaceMap;
37 35065 : }
38 :
39 70130 : XMLNamespaces::~XMLNamespaces()
40 : {
41 70130 : }
42 :
43 427 : void XMLNamespaces::addNamespace( const OUString& aName, const OUString& aValue ) throw( SAXException )
44 : {
45 427 : NamespaceMap::iterator p;
46 427 : OUString aNamespaceName( aName );
47 427 : sal_Int32 nXMLNamespaceLength = m_aXMLAttributeNamespace.getLength();
48 :
49 : // delete preceding "xmlns"
50 427 : if ( aNamespaceName.startsWith( m_aXMLAttributeNamespace ) )
51 : {
52 427 : if ( aNamespaceName.getLength() == nXMLNamespaceLength )
53 : {
54 0 : aNamespaceName.clear();
55 : }
56 427 : else if ( aNamespaceName.getLength() >= nXMLNamespaceLength+2 )
57 : {
58 427 : aNamespaceName = aNamespaceName.copy( nXMLNamespaceLength+1 );
59 : }
60 : else
61 : {
62 : // a xml namespace without name is not allowed (e.g. "xmlns:" )
63 0 : OUString aErrorMessage( "A xml namespace without name is not allowed!" );
64 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
65 : }
66 : }
67 :
68 427 : if ( aValue.isEmpty() && !aNamespaceName.isEmpty() )
69 : {
70 : // namespace should be reseted - as xml draft states this is only allowed
71 : // for the default namespace - check and throw exception if check fails
72 0 : OUString aErrorMessage( "Clearing xml namespace only allowed for default namespace!" );
73 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
74 : }
75 : else
76 : {
77 427 : if ( aNamespaceName.isEmpty() )
78 0 : m_aDefaultNamespace = aValue;
79 : else
80 : {
81 427 : p = m_aNamespaceMap.find( aNamespaceName );
82 427 : if ( p != m_aNamespaceMap.end() )
83 : {
84 : // replace current namespace definition
85 0 : m_aNamespaceMap.erase( p );
86 0 : m_aNamespaceMap.insert( NamespaceMap::value_type( aNamespaceName, aValue ));
87 : }
88 : else
89 : {
90 427 : m_aNamespaceMap.insert( NamespaceMap::value_type( aNamespaceName, aValue ));
91 : }
92 : }
93 427 : }
94 427 : }
95 :
96 33123 : OUString XMLNamespaces::applyNSToAttributeName( const OUString& aName ) const throw( SAXException )
97 : {
98 : // xml draft: there is no default namespace for attributes!
99 :
100 : int index;
101 33123 : if (( index = aName.indexOf( ':' )) > 0 )
102 : {
103 33123 : if ( aName.getLength() > index+1 )
104 : {
105 33123 : OUString aAttributeName = getNamespaceValue( aName.copy( 0, index ) );
106 33123 : aAttributeName += "^";
107 33123 : aAttributeName += aName.copy( index+1 );
108 33123 : return aAttributeName;
109 : }
110 : else
111 : {
112 : // attribute with namespace but without name "namespace:" is not allowed!!
113 0 : OUString aErrorMessage( "Attribute has no name only preceding namespace!" );
114 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
115 : }
116 : }
117 :
118 0 : return aName;
119 : }
120 :
121 70130 : OUString XMLNamespaces::applyNSToElementName( const OUString& aName ) const throw( SAXException )
122 : {
123 : // xml draft: element names can have a default namespace
124 :
125 70130 : int index = aName.indexOf( ':' );
126 70130 : OUString aNamespace;
127 140260 : OUString aElementName = aName;
128 :
129 70130 : if ( index > 0 )
130 70130 : aNamespace = getNamespaceValue( aName.copy( 0, index ) );
131 : else
132 0 : aNamespace = m_aDefaultNamespace;
133 :
134 70130 : if ( !aNamespace.isEmpty() )
135 : {
136 70130 : aElementName = aNamespace;
137 70130 : aElementName += "^";
138 : }
139 : else
140 0 : return aName;
141 :
142 70130 : if ( index > 0 )
143 : {
144 70130 : if ( aName.getLength() > index+1 )
145 70130 : aElementName += aName.copy( index+1 );
146 : else
147 : {
148 : // attribute with namespace but without a name is not allowed (e.g. "cfg:" )
149 0 : OUString aErrorMessage( "Attribute has no name only preceding namespace!" );
150 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
151 : }
152 : }
153 : else
154 0 : aElementName += aName;
155 :
156 140260 : return aElementName;
157 : }
158 :
159 103253 : OUString XMLNamespaces::getNamespaceValue( const OUString& aNamespace ) const throw( SAXException )
160 : {
161 103253 : if ( aNamespace.isEmpty() )
162 0 : return m_aDefaultNamespace;
163 : else
164 : {
165 103253 : NamespaceMap::const_iterator p;
166 103253 : p = m_aNamespaceMap.find( aNamespace );
167 103253 : if ( p != m_aNamespaceMap.end() )
168 103253 : return p->second;
169 : else
170 : {
171 : // namespace not defined => throw exception!
172 0 : OUString aErrorMessage( "XML namespace used but not defined!" );
173 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
174 : }
175 : }
176 : }
177 :
178 : }
179 :
180 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|