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