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