LCOV - code coverage report
Current view: top level - framework/source/fwe/xml - xmlnamespaces.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 50 69 72.5 %
Date: 2014-04-11 Functions: 7 8 87.5 %
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             : #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       21440 : XMLNamespaces::XMLNamespaces()
      29       21440 :     : m_aXMLAttributeNamespace( "xmlns" )
      30             : {
      31       21440 : }
      32             : 
      33       21440 : XMLNamespaces::XMLNamespaces( const XMLNamespaces& aXMLNamespaces )
      34             : {
      35       21440 :     m_aDefaultNamespace = aXMLNamespaces.m_aDefaultNamespace;
      36       21440 :     m_aNamespaceMap = aXMLNamespaces.m_aNamespaceMap;
      37       21440 : }
      38             : 
      39       42880 : XMLNamespaces::~XMLNamespaces()
      40             : {
      41       42880 : }
      42             : 
      43         365 : void XMLNamespaces::addNamespace( const OUString& aName, const OUString& aValue ) throw( SAXException )
      44             : {
      45         365 :     NamespaceMap::iterator p;
      46         365 :     OUString aNamespaceName( aName );
      47         365 :     sal_Int32 nXMLNamespaceLength = m_aXMLAttributeNamespace.getLength();
      48             : 
      49             :     // delete preceding "xmlns"
      50         365 :     if ( aNamespaceName.compareTo( m_aXMLAttributeNamespace, nXMLNamespaceLength ) == 0 )
      51             :     {
      52         365 :         if ( aNamespaceName.getLength() == nXMLNamespaceLength )
      53             :         {
      54           0 :             aNamespaceName = OUString();
      55             :         }
      56         365 :         else if ( aNamespaceName.getLength() >= nXMLNamespaceLength+2 )
      57             :         {
      58         365 :             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         365 :     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         365 :         if ( aNamespaceName.isEmpty() )
      78           0 :             m_aDefaultNamespace = aValue;
      79             :         else
      80             :         {
      81         365 :             p = m_aNamespaceMap.find( aNamespaceName );
      82         365 :             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         365 :                 m_aNamespaceMap.insert( NamespaceMap::value_type( aNamespaceName, aValue ));
      91             :             }
      92             :         }
      93         365 :     }
      94         365 : }
      95             : 
      96       20804 : 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       20804 :     if (( index = aName.indexOf( ':' )) > 0 )
     102             :     {
     103       20804 :         if ( aName.getLength() > index+1 )
     104             :         {
     105       20804 :             OUString aAttributeName = getNamespaceValue( aName.copy( 0, index ) );
     106       20804 :             aAttributeName += "^";
     107       20804 :             aAttributeName += aName.copy( index+1 );
     108       20804 :             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       42880 : OUString XMLNamespaces::applyNSToElementName( const OUString& aName ) const   throw( SAXException )
     122             : {
     123             :     // xml draft: element names can have a default namespace
     124             : 
     125       42880 :     int         index = aName.indexOf( ':' );
     126       42880 :     OUString aNamespace;
     127       85760 :     OUString aElementName = aName;
     128             : 
     129       42880 :     if ( index > 0 )
     130       42880 :         aNamespace = getNamespaceValue( aName.copy( 0, index ) );
     131             :     else
     132           0 :         aNamespace = m_aDefaultNamespace;
     133             : 
     134       42880 :     if ( !aNamespace.isEmpty() )
     135             :     {
     136       42880 :         aElementName = aNamespace;
     137       42880 :         aElementName += "^";
     138             :     }
     139             :     else
     140           0 :         return aName;
     141             : 
     142       42880 :     if ( index > 0 )
     143             :     {
     144       42880 :         if ( aName.getLength() > index+1 )
     145       42880 :             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       85760 :     return aElementName;
     157             : }
     158             : 
     159       63684 : OUString XMLNamespaces::getNamespaceValue( const OUString& aNamespace ) const throw( SAXException )
     160             : {
     161       63684 :     if ( aNamespace.isEmpty() )
     162           0 :         return m_aDefaultNamespace;
     163             :     else
     164             :     {
     165       63684 :         NamespaceMap::const_iterator p;
     166       63684 :         p = m_aNamespaceMap.find( aNamespace );
     167       63684 :         if ( p != m_aNamespaceMap.end() )
     168       63684 :             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: */

Generated by: LCOV version 1.10