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