LCOV - code coverage report
Current view: top level - libreoffice/framework/source/fwe/xml - xmlnamespaces.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 69 0.0 %
Date: 2012-12-27 Functions: 0 8 0.0 %
Legend: Lines: hit not hit

          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           0 : XMLNamespaces::XMLNamespaces()
      30           0 :     : m_aXMLAttributeNamespace( RTL_CONSTASCII_USTRINGPARAM( "xmlns" ))
      31             : {
      32           0 : }
      33             : 
      34           0 : XMLNamespaces::XMLNamespaces( const XMLNamespaces& aXMLNamespaces )
      35             : {
      36           0 :     m_aDefaultNamespace = aXMLNamespaces.m_aDefaultNamespace;
      37           0 :     m_aNamespaceMap = aXMLNamespaces.m_aNamespaceMap;
      38           0 : }
      39             : 
      40           0 : XMLNamespaces::~XMLNamespaces()
      41             : {
      42           0 : }
      43             : 
      44           0 : void XMLNamespaces::addNamespace( const ::rtl::OUString& aName, const ::rtl::OUString& aValue ) throw( SAXException )
      45             : {
      46           0 :     NamespaceMap::iterator p;
      47           0 :     ::rtl::OUString aNamespaceName( aName );
      48           0 :     sal_Int32 nXMLNamespaceLength = m_aXMLAttributeNamespace.getLength();
      49             : 
      50             :     // delete preceding "xmlns"
      51           0 :     if ( aNamespaceName.compareTo( m_aXMLAttributeNamespace, nXMLNamespaceLength ) == 0 )
      52             :     {
      53           0 :         if ( aNamespaceName.getLength() == nXMLNamespaceLength )
      54             :         {
      55           0 :             aNamespaceName = ::rtl::OUString();
      56             :         }
      57           0 :         else if ( aNamespaceName.getLength() >= nXMLNamespaceLength+2 )
      58             :         {
      59           0 :             aNamespaceName = aNamespaceName.copy( nXMLNamespaceLength+1 );
      60             :         }
      61             :         else
      62             :         {
      63             :             // a xml namespace without name is not allowed (e.g. "xmlns:" )
      64           0 :             ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "A xml namespace without name is not allowed!" ));
      65           0 :             throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
      66             :         }
      67             :     }
      68             : 
      69           0 :     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 :         ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "Clearing xml namespace only allowed for default namespace!" ));
      74           0 :         throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
      75             :     }
      76             :     else
      77             :     {
      78           0 :         if ( aNamespaceName.isEmpty() )
      79           0 :             m_aDefaultNamespace = aValue;
      80             :         else
      81             :         {
      82           0 :             p = m_aNamespaceMap.find( aNamespaceName );
      83           0 :             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           0 :                 m_aNamespaceMap.insert( NamespaceMap::value_type( aNamespaceName, aValue ));
      92             :             }
      93             :         }
      94           0 :     }
      95           0 : }
      96             : 
      97           0 : ::rtl::OUString XMLNamespaces::applyNSToAttributeName( const ::rtl::OUString& aName ) const throw( SAXException )
      98             : {
      99             :     // xml draft: there is no default namespace for attributes!
     100             : 
     101             :     int index;
     102           0 :     if (( index = aName.indexOf( ':' )) > 0 )
     103             :     {
     104           0 :         if ( aName.getLength() > index+1 )
     105             :         {
     106           0 :             ::rtl::OUString aAttributeName = getNamespaceValue( aName.copy( 0, index ) );
     107           0 :             aAttributeName += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("^"));
     108           0 :             aAttributeName += aName.copy( index+1 );
     109           0 :             return aAttributeName;
     110             :         }
     111             :         else
     112             :         {
     113             :             // attribute with namespace but without name "namespace:" is not allowed!!
     114           0 :             ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "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           0 : ::rtl::OUString XMLNamespaces::applyNSToElementName( const ::rtl::OUString& aName ) const   throw( SAXException )
     123             : {
     124             :     // xml draft: element names can have a default namespace
     125             : 
     126           0 :     int         index = aName.indexOf( ':' );
     127           0 :     ::rtl::OUString aNamespace;
     128           0 :     ::rtl::OUString aElementName = aName;
     129             : 
     130           0 :     if ( index > 0 )
     131           0 :         aNamespace = getNamespaceValue( aName.copy( 0, index ) );
     132             :     else
     133           0 :         aNamespace = m_aDefaultNamespace;
     134             : 
     135           0 :     if ( !aNamespace.isEmpty() )
     136             :     {
     137           0 :         aElementName = aNamespace;
     138           0 :         aElementName += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("^"));
     139             :     }
     140             :     else
     141           0 :         return aName;
     142             : 
     143           0 :     if ( index > 0 )
     144             :     {
     145           0 :         if ( aName.getLength() > index+1 )
     146           0 :             aElementName += aName.copy( index+1 );
     147             :         else
     148             :         {
     149             :             // attribute with namespace but without a name is not allowed (e.g. "cfg:" )
     150           0 :             ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "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           0 :     return aElementName;
     158             : }
     159             : 
     160           0 : ::rtl::OUString XMLNamespaces::getNamespaceValue( const ::rtl::OUString& aNamespace ) const throw( SAXException )
     161             : {
     162           0 :     if ( aNamespace.isEmpty() )
     163           0 :         return m_aDefaultNamespace;
     164             :     else
     165             :     {
     166           0 :         NamespaceMap::const_iterator p;
     167           0 :         p = m_aNamespaceMap.find( aNamespace );
     168           0 :         if ( p != m_aNamespaceMap.end() )
     169           0 :             return p->second;
     170             :         else
     171             :         {
     172             :             // namespace not defined => throw exception!
     173           0 :             ::rtl::OUString aErrorMessage( RTL_CONSTASCII_USTRINGPARAM( "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: */

Generated by: LCOV version 1.10