/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /************************************************************************* * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * Copyright 2000, 2010 Oracle and/or its affiliates. * * OpenOffice.org - a multi-platform office productivity suite * * This file is part of OpenOffice.org. * * OpenOffice.org is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 * only, as published by the Free Software Foundation. * * OpenOffice.org is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License version 3 for more details * (a copy is included in the LICENSE file that accompanied this code). * * You should have received a copy of the GNU Lesser General Public License * version 3 along with OpenOffice.org. If not, see * * for a copy of the LGPLv3 License. * ************************************************************************/ #include #include #include #include #include #include "filterdetect.hxx" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using rtl::OUString; using com::sun::star::uno::Sequence; using com::sun::star::uno::Reference; using com::sun::star::uno::Any; using com::sun::star::uno::UNO_QUERY; using com::sun::star::uno::XInterface; using com::sun::star::uno::Exception; using com::sun::star::uno::RuntimeException; using com::sun::star::lang::XMultiServiceFactory; using com::sun::star::io::XActiveDataSource; using com::sun::star::io::XOutputStream; using com::sun::star::beans::PropertyValue; using com::sun::star::document::XExporter; using com::sun::star::document::XFilter; using com::sun::star::document::XExtendedFilterDetection; using com::sun::star::io::XInputStream; using com::sun::star::document::XImporter; using com::sun::star::xml::sax::InputSource; using com::sun::star::xml::sax::XDocumentHandler; using com::sun::star::xml::sax::XParser; using com::sun::star::task::XInteractionHandler; using namespace ::com::sun::star::frame; using namespace ::com::sun::star; using namespace com::sun::star::container; using namespace com::sun::star::uno; using namespace com::sun::star::beans; namespace { bool isXMLStream(const ::rtl::OString& aHeaderStrm) { const char* p = aHeaderStrm.getStr(); size_t n = aHeaderStrm.getLength(); size_t i = 0; // Skip all preceding blank characters. for (i = 0; i < n; ++i, ++p) { sal_Char c = *p; if (c == ' ' || c == '\n' || c == '\t') continue; break; } n -= i; // First text must be '= 0) { sTypeName = checkType; } } return sTypeName; } } ::rtl::OUString SAL_CALL FilterDetect::detect( com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >& aArguments ) throw( com::sun::star::uno::RuntimeException ) { ::rtl::OUString sTypeName; ::rtl::OUString sUrl; ::rtl::OUString originalTypeName; Sequence lProps ; com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInStream; ::rtl::OUString temp; const PropertyValue * pValue = aArguments.getConstArray(); sal_Int32 nLength; ::rtl::OString resultString; nLength = aArguments.getLength(); sal_Int32 location=nLength; for (sal_Int32 i = 0 ; i < nLength; i++) { if (pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "TypeName" ) )) { location=i; } else if (pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "URL" ) )) { pValue[i].Value >>= sUrl; } else if (pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "InputStream" ) )) { pValue[i].Value >>= xInStream ; } } try { Reference< com::sun::star::ucb::XCommandEnvironment > xEnv; if (!xInStream.is()) { ::ucbhelper::Content aContent(sUrl,xEnv); xInStream = aContent.openStream(); if (!xInStream.is()) { return sTypeName; } } com::sun::star::uno::Sequence< sal_Int8 > aData; /* long nBytesToRead= */ xInStream->available(); xInStream->skipBytes (0); long bytestRead =xInStream->readBytes (aData, 4000); resultString=::rtl::OString((const sal_Char *)aData.getConstArray(),bytestRead) ; if (!isXMLStream(resultString)) // This is not an XML stream. It makes no sense to try to detect // a non-XML file type here. return ::rtl::OUString(); // test typedetect code Reference xTypeCont(mxMSF->createInstance(OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.TypeDetection" ))),UNO_QUERY); Sequence < ::rtl::OUString > myTypes= xTypeCont->getElementNames(); nLength = myTypes.getLength(); sal_Int32 new_nlength=0; sal_Int32 i = 0 ; while ((i < nLength) && (sTypeName.getLength() == 0)) { Any elem = xTypeCont->getByName(myTypes[i]); elem >>=lProps; new_nlength = lProps.getLength(); sal_Int32 j =0; while (j < new_nlength && (sTypeName.getLength() == 0)) { ::rtl::OUString tmpStr; lProps[j].Value >>=tmpStr; if ((lProps[j].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("ClipboardFormat"))) && tmpStr.getLength()) { sTypeName = supportedByType(tmpStr,resultString, myTypes[i]); } j++; } i++; } } catch (const Exception &) { OSL_FAIL( "An Exception occurred while opening File stream" ); } if (sTypeName.getLength()) { if (location == aArguments.getLength()) { aArguments.realloc(nLength+1); aArguments[location].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "TypeName" )); } aArguments[location].Value <<=sTypeName; } return sTypeName; } // XInitialization void SAL_CALL FilterDetect::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException) { Sequence < PropertyValue > aAnySeq; sal_Int32 nLength = aArguments.getLength(); if ( nLength && ( aArguments[0] >>= aAnySeq ) ) { const PropertyValue * pValue = aAnySeq.getConstArray(); nLength = aAnySeq.getLength(); for ( sal_Int32 i = 0 ; i < nLength; i++) { if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Type" ) ) ) { pValue[i].Value >>= msFilterName; } else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "UserData" ) ) ) { pValue[i].Value >>= msUserData; } else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "TemplateName" ) ) ) { pValue[i].Value>>=msTemplateName; } } } } OUString FilterDetect_getImplementationName () throw (RuntimeException) { return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.filters.XMLFilterDetect" ) ); } #define SERVICE_NAME1 "com.sun.star.document.ExtendedTypeDetection" sal_Bool SAL_CALL FilterDetect_supportsService( const OUString& ServiceName ) throw (RuntimeException) { return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) ); } Sequence< OUString > SAL_CALL FilterDetect_getSupportedServiceNames( ) throw (RuntimeException) { Sequence < OUString > aRet(2); OUString* pArray = aRet.getArray(); pArray[0] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1 ) ); return aRet; } #undef SERVICE_NAME1 #undef SERVICE_NAME2 Reference< XInterface > SAL_CALL FilterDetect_createInstance( const Reference< XMultiServiceFactory > & rSMgr) throw( Exception ) { return (cppu::OWeakObject*) new FilterDetect( rSMgr ); } // XServiceInfo OUString SAL_CALL FilterDetect::getImplementationName( ) throw (RuntimeException) { return FilterDetect_getImplementationName(); } sal_Bool SAL_CALL FilterDetect::supportsService( const OUString& rServiceName ) throw (RuntimeException) { return FilterDetect_supportsService( rServiceName ); } Sequence< OUString > SAL_CALL FilterDetect::getSupportedServiceNames( ) throw (RuntimeException) { return FilterDetect_getSupportedServiceNames(); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */