LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/filter/source/xmlfilterdetect - filterdetect.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 49 110 44.5 %
Date: 2013-07-09 Functions: 7 12 58.3 %
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 <iostream>
      21             : #include <stdlib.h>
      22             : #include <ctype.h>
      23             : #include <stdio.h>
      24             : #include <cstring>
      25             : #include "filterdetect.hxx"
      26             : #include <osl/diagnose.h>
      27             : #include <com/sun/star/io/XActiveDataSource.hpp>
      28             : #include <com/sun/star/io/XOutputStream.hpp>
      29             : #include <com/sun/star/io/XInputStream.hpp>
      30             : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
      31             : #include <com/sun/star/xml/sax/InputSource.hpp>
      32             : #include <com/sun/star/xml/sax/XParser.hpp>
      33             : #include <com/sun/star/xml/XImportFilter.hpp>
      34             : #include <com/sun/star/xml/XExportFilter.hpp>
      35             : #include <com/sun/star/frame/XController.hpp>
      36             : #include <com/sun/star/task/XStatusIndicator.hpp>
      37             : #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
      38             : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
      39             : #include <com/sun/star/style/XStyleLoader.hpp>
      40             : #include <com/sun/star/uno/XComponentContext.hpp>
      41             : #include <com/sun/star/document/XExtendedFilterDetection.hpp>
      42             : #include <com/sun/star/container/XNameAccess.hpp>
      43             : #include <com/sun/star/beans/PropertyState.hpp>
      44             : #include <ucbhelper/content.hxx>
      45             : 
      46             : using com::sun::star::uno::Sequence;
      47             : using com::sun::star::uno::Reference;
      48             : using com::sun::star::uno::Any;
      49             : using com::sun::star::uno::UNO_QUERY;
      50             : using com::sun::star::uno::XComponentContext;
      51             : using com::sun::star::uno::XInterface;
      52             : using com::sun::star::uno::Exception;
      53             : using com::sun::star::uno::RuntimeException;
      54             : using com::sun::star::io::XActiveDataSource;
      55             : using com::sun::star::io::XOutputStream;
      56             : using com::sun::star::beans::PropertyValue;
      57             : using com::sun::star::document::XExporter;
      58             : using com::sun::star::document::XFilter;
      59             : using com::sun::star::document::XExtendedFilterDetection;
      60             : 
      61             : using com::sun::star::io::XInputStream;
      62             : using com::sun::star::document::XImporter;
      63             : using com::sun::star::xml::sax::InputSource;
      64             : using com::sun::star::xml::sax::XDocumentHandler;
      65             : using com::sun::star::xml::sax::XParser;
      66             : 
      67             : using namespace ::com::sun::star::frame;
      68             : using namespace ::com::sun::star;
      69             : using namespace com::sun::star::container;
      70             : using namespace com::sun::star::uno;
      71             : using namespace com::sun::star::beans;
      72             : 
      73             : namespace {
      74             : 
      75           1 : bool isXMLStream(const OString& aHeaderStrm)
      76             : {
      77           1 :     const char* p = aHeaderStrm.getStr();
      78           1 :     size_t n = aHeaderStrm.getLength();
      79           1 :     size_t i = 0;
      80             : 
      81             :     // Skip all preceding blank characters.
      82           1 :     for (i = 0; i < n; ++i, ++p)
      83             :     {
      84           1 :         sal_Char c = *p;
      85           1 :         if (c == ' ' || c == '\n' || c == '\t')
      86           0 :             continue;
      87           1 :         break;
      88             :     }
      89             : 
      90           1 :     n -= i;
      91             : 
      92             :     // First text must be '<?xml', else it's not a valid XML file stream.
      93           1 :     const char* sInitChars = "<?xml";
      94           1 :     const size_t nInitCharLen = std::strlen(sInitChars);
      95           2 :     for (i = 0; i < n; ++i, ++p)
      96             :     {
      97           2 :         if (i < nInitCharLen)
      98             :         {
      99           2 :             if (*p != sInitChars[i])
     100           1 :                 return false;
     101             :         }
     102             :     }
     103           0 :     return true;
     104             : }
     105             : 
     106           0 : OUString supportedByType( const OUString clipBoardFormat ,  const OString resultString, const OUString checkType)
     107             : {
     108           0 :     OUString sTypeName;
     109           0 :     if ( clipBoardFormat.match(OUString("doctype:")) )
     110             :     {
     111           0 :         OString tryStr = OUStringToOString(clipBoardFormat.copy(8),RTL_TEXTENCODING_ASCII_US).getStr();
     112           0 :         if (resultString.indexOf(tryStr) >= 0)
     113             :         {
     114           0 :             sTypeName = checkType;
     115           0 :         }
     116             :     }
     117           0 :     return sTypeName;
     118             : }
     119             : 
     120             : }
     121             : 
     122           1 : OUString SAL_CALL FilterDetect::detect( com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >& aArguments ) throw( com::sun::star::uno::RuntimeException )
     123             : {
     124           1 :     OUString sTypeName;
     125           2 :     OUString sUrl;
     126           2 :     Sequence<PropertyValue > lProps ;
     127             : 
     128           2 :     com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInStream;
     129           1 :     const PropertyValue * pValue = aArguments.getConstArray();
     130             :     sal_Int32 nLength;
     131           2 :     OString resultString;
     132             : 
     133           1 :     nLength = aArguments.getLength();
     134           1 :     sal_Int32 location=nLength;
     135          10 :     for (sal_Int32 i = 0 ; i < nLength; i++)
     136             :     {
     137           9 :         if ( pValue[i].Name == "TypeName" )
     138             :         {
     139           1 :             location=i;
     140             :         }
     141           8 :         else if ( pValue[i].Name == "URL" )
     142             :         {
     143           1 :             pValue[i].Value >>= sUrl;
     144             :         }
     145           7 :         else if ( pValue[i].Name == "InputStream" )
     146             :         {
     147           1 :             pValue[i].Value >>= xInStream ;
     148             :         }
     149             :     }
     150             :     try
     151             :     {
     152           1 :         if (!xInStream.is())
     153             :         {
     154             :             ::ucbhelper::Content aContent(
     155             :                 sUrl, Reference< com::sun::star::ucb::XCommandEnvironment >(),
     156           0 :                 mxCtx);
     157           0 :             xInStream = aContent.openStream();
     158           0 :             if (!xInStream.is())
     159             :             {
     160           0 :                 return sTypeName;
     161           0 :             }
     162             :         }
     163           1 :         com::sun::star::uno::Sequence< sal_Int8 > aData;
     164           1 :         /* long nBytesToRead= */ xInStream->available();
     165           1 :         xInStream->skipBytes (0);
     166           1 :         long bytestRead =xInStream->readBytes (aData,  4000);
     167           1 :         resultString=OString((const sal_Char *)aData.getConstArray(),bytestRead) ;
     168             : 
     169           1 :         if (!isXMLStream(resultString))
     170             :             // This is not an XML stream.  It makes no sense to try to detect
     171             :             // a non-XML file type here.
     172           1 :             return OUString();
     173             : 
     174             :         // test typedetect code
     175           0 :         Reference <XNameAccess> xTypeCont(mxCtx->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", mxCtx), UNO_QUERY);
     176           0 :         Sequence < OUString > myTypes= xTypeCont->getElementNames();
     177           0 :         nLength = myTypes.getLength();
     178             : 
     179           0 :         sal_Int32 new_nlength=0;
     180           0 :         sal_Int32 i = 0 ;
     181           0 :         while ((i < nLength) && (sTypeName.isEmpty()))
     182             :         {
     183           0 :             Any elem = xTypeCont->getByName(myTypes[i]);
     184           0 :             elem >>=lProps;
     185           0 :             new_nlength = lProps.getLength();
     186           0 :             sal_Int32 j =0;
     187           0 :             while (j < new_nlength && (sTypeName.isEmpty()))
     188             :             {
     189           0 :                 OUString tmpStr;
     190           0 :                 lProps[j].Value >>=tmpStr;
     191           0 :                 if ( lProps[j].Name == "ClipboardFormat" && !tmpStr.isEmpty() )
     192             :                 {
     193           0 :                     sTypeName = supportedByType(tmpStr,resultString, myTypes[i]);
     194             :                 }
     195           0 :                 j++;
     196           0 :             }
     197           0 :             i++;
     198           0 :         }
     199             :     }
     200           0 :     catch (const Exception &)
     201             :     {
     202             :         OSL_FAIL( "An Exception occurred while opening File stream" );
     203             :     }
     204             : 
     205           0 :     if (!sTypeName.isEmpty())
     206             :     {
     207           0 :         if (location == aArguments.getLength())
     208             :         {
     209           0 :             aArguments.realloc(nLength+1);
     210           0 :             aArguments[location].Name = OUString( "TypeName" );
     211             :         }
     212           0 :         aArguments[location].Value <<=sTypeName;
     213             :     }
     214             : 
     215           1 :     return sTypeName;
     216             : }
     217             : 
     218             : // XInitialization
     219             : 
     220           0 : void SAL_CALL FilterDetect::initialize( const Sequence< Any >& aArguments )
     221             :     throw (Exception, RuntimeException)
     222             : {
     223           0 :     Sequence < PropertyValue > aAnySeq;
     224           0 :     sal_Int32 nLength = aArguments.getLength();
     225           0 :     if ( nLength && ( aArguments[0] >>= aAnySeq ) )
     226             :     {
     227           0 :         const PropertyValue * pValue = aAnySeq.getConstArray();
     228           0 :         nLength = aAnySeq.getLength();
     229           0 :         for ( sal_Int32 i = 0 ; i < nLength; i++)
     230             :         {
     231             : 
     232           0 :             if ( pValue[i].Name == "Type" )
     233             :             {
     234           0 :                  pValue[i].Value >>= msFilterName;
     235             : 
     236             :             }
     237           0 :             else if ( pValue[i].Name == "UserData" )
     238             :             {
     239             : 
     240           0 :                 pValue[i].Value >>= msUserData;
     241             : 
     242             :             }
     243           0 :             else if ( pValue[i].Name == "TemplateName" )
     244             :             {
     245             : 
     246           0 :               pValue[i].Value>>=msTemplateName;
     247             :             }
     248             : 
     249             :         }
     250           0 :     }
     251           0 : }
     252             : 
     253             : 
     254             : 
     255           1 : OUString FilterDetect_getImplementationName ()
     256             : {
     257           1 :     return OUString( "com.sun.star.comp.filters.XMLFilterDetect" );
     258             : }
     259             : #define SERVICE_NAME1 "com.sun.star.document.ExtendedTypeDetection"
     260             : 
     261           1 : Sequence< OUString > FilterDetect_getSupportedServiceNames()
     262             : {
     263           1 :     Sequence < OUString > aRet(1);
     264           1 :     aRet[0] = SERVICE_NAME1;
     265           1 :     return aRet;
     266             : }
     267             : 
     268           1 : Reference< XInterface > FilterDetect_createInstance( const Reference< XComponentContext > & context)
     269             : {
     270           1 :     return static_cast< cppu::OWeakObject * >( new FilterDetect( context ) );
     271             : }
     272             : 
     273             : // XServiceInfo
     274           0 : OUString SAL_CALL FilterDetect::getImplementationName(  )
     275             :     throw (RuntimeException)
     276             : {
     277           0 :     return FilterDetect_getImplementationName();
     278             : }
     279           0 : sal_Bool SAL_CALL FilterDetect::supportsService( const OUString& rServiceName )
     280             :     throw (RuntimeException)
     281             : {
     282           0 :     return rServiceName == SERVICE_NAME1;
     283             : }
     284           0 : Sequence< OUString > SAL_CALL FilterDetect::getSupportedServiceNames(  )
     285             :     throw (RuntimeException)
     286             : {
     287           0 :     return FilterDetect_getSupportedServiceNames();
     288           3 : }
     289             : 
     290             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10