1d107581fSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3d107581fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4d107581fSAndrew Rist * or more contributor license agreements. See the NOTICE file 5d107581fSAndrew Rist * distributed with this work for additional information 6d107581fSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7d107581fSAndrew Rist * to you under the Apache License, Version 2.0 (the 8d107581fSAndrew Rist * "License"); you may not use this file except in compliance 9d107581fSAndrew Rist * with the License. You may obtain a copy of the License at 10d107581fSAndrew Rist * 11d107581fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12d107581fSAndrew Rist * 13d107581fSAndrew Rist * Unless required by applicable law or agreed to in writing, 14d107581fSAndrew Rist * software distributed under the License is distributed on an 15d107581fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16d107581fSAndrew Rist * KIND, either express or implied. See the License for the 17d107581fSAndrew Rist * specific language governing permissions and limitations 18d107581fSAndrew Rist * under the License. 19d107581fSAndrew Rist * 20d107581fSAndrew Rist *************************************************************/ 21d107581fSAndrew Rist 22d107581fSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_starmath.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "smdetect.hxx" 28cdf0e10cSrcweir 29cdf0e10cSrcweir //#include <framework/interaction.hxx> 30cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 31cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp> 32cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp> 33cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp> 34cdf0e10cSrcweir #include <com/sun/star/awt/XWindow.hpp> 35cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp> 36cdf0e10cSrcweir #ifndef _UNOTOOLS_PROCESSFACTORY_HXX 37cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 38cdf0e10cSrcweir #endif 39cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp> 40cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp> 41cdf0e10cSrcweir #include <com/sun/star/task/XInteractionHandler.hpp> 42cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp> 43cdf0e10cSrcweir #include <com/sun/star/ucb/CommandAbortedException.hpp> 44cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveAppException.hpp> 45cdf0e10cSrcweir #include <com/sun/star/ucb/XContent.hpp> 46cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipIOException.hpp> 47cdf0e10cSrcweir #include <framework/interaction.hxx> 48cdf0e10cSrcweir 49cdf0e10cSrcweir #ifndef _TOOLKIT_UNOHLP_HXX 50cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx> 51cdf0e10cSrcweir #endif 52cdf0e10cSrcweir #include <ucbhelper/simpleinteractionrequest.hxx> 53cdf0e10cSrcweir 54cdf0e10cSrcweir #include <rtl/ustring.h> 55cdf0e10cSrcweir #include <rtl/logfile.hxx> 56cdf0e10cSrcweir #include <svl/itemset.hxx> 57cdf0e10cSrcweir #include <vcl/window.hxx> 58cdf0e10cSrcweir #include <svl/eitem.hxx> 59cdf0e10cSrcweir #include <svl/stritem.hxx> 60cdf0e10cSrcweir #include <tools/urlobj.hxx> 61cdf0e10cSrcweir #include <vos/mutex.hxx> 62cdf0e10cSrcweir #include <svtools/sfxecode.hxx> 63cdf0e10cSrcweir #include <svtools/ehdl.hxx> 64cdf0e10cSrcweir #include <sot/storinfo.hxx> 65cdf0e10cSrcweir #include <vcl/svapp.hxx> 66cdf0e10cSrcweir #include <sfx2/app.hxx> 67cdf0e10cSrcweir #include <sfx2/sfxsids.hrc> 68cdf0e10cSrcweir #include <sfx2/request.hxx> 69cdf0e10cSrcweir #include <sfx2/docfile.hxx> 70cdf0e10cSrcweir #include <sfx2/docfilt.hxx> 71cdf0e10cSrcweir #include <sfx2/fcontnr.hxx> 72cdf0e10cSrcweir #include <sfx2/brokenpackageint.hxx> 73cdf0e10cSrcweir 74cdf0e10cSrcweir #include "document.hxx" 75cdf0e10cSrcweir #include "eqnolefilehdr.hxx" 76cdf0e10cSrcweir 77cdf0e10cSrcweir using namespace ::com::sun::star; 78cdf0e10cSrcweir using namespace ::com::sun::star::uno; 79cdf0e10cSrcweir using namespace ::com::sun::star::io; 80cdf0e10cSrcweir using namespace ::com::sun::star::frame; 81cdf0e10cSrcweir using namespace ::com::sun::star::task; 82cdf0e10cSrcweir using namespace ::com::sun::star::beans; 83cdf0e10cSrcweir using namespace ::com::sun::star::lang; 84cdf0e10cSrcweir using namespace ::com::sun::star::ucb; 85cdf0e10cSrcweir using namespace ::rtl; 86cdf0e10cSrcweir 87cdf0e10cSrcweir SmFilterDetect::SmFilterDetect( const REFERENCE < ::com::sun::star::lang::XMultiServiceFactory >& /*xFactory*/ ) 88cdf0e10cSrcweir { 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir SmFilterDetect::~SmFilterDetect() 92cdf0e10cSrcweir { 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir ::rtl::OUString SAL_CALL SmFilterDetect::detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& lDescriptor ) throw( ::com::sun::star::uno::RuntimeException ) 96cdf0e10cSrcweir { 97cdf0e10cSrcweir REFERENCE< XInputStream > xStream; 98cdf0e10cSrcweir REFERENCE< XContent > xContent; 99cdf0e10cSrcweir REFERENCE< XInteractionHandler > xInteraction; 100cdf0e10cSrcweir String aURL; 101cdf0e10cSrcweir ::rtl::OUString sTemp; 102cdf0e10cSrcweir String aTypeName; // a name describing the type (from MediaDescriptor, usually from flat detection) 103cdf0e10cSrcweir String aPreselectedFilterName; // a name describing the filter to use (from MediaDescriptor, usually from UI action) 104cdf0e10cSrcweir 105cdf0e10cSrcweir ::rtl::OUString aDocumentTitle; // interesting only if set in this method 106cdf0e10cSrcweir 107cdf0e10cSrcweir // opening as template is done when a parameter tells to do so and a template filter can be detected 108cdf0e10cSrcweir // (otherwise no valid filter would be found) or if the detected filter is a template filter and 109cdf0e10cSrcweir // there is no parameter that forbids to open as template 110cdf0e10cSrcweir sal_Bool bOpenAsTemplate = sal_False; 111cdf0e10cSrcweir sal_Bool bWasReadOnly = sal_False, bReadOnly = sal_False; 112cdf0e10cSrcweir 113cdf0e10cSrcweir sal_Bool bRepairPackage = sal_False; 114cdf0e10cSrcweir sal_Bool bRepairAllowed = sal_False; 115cdf0e10cSrcweir 116cdf0e10cSrcweir // now some parameters that can already be in the array, but may be overwritten or new inserted here 117cdf0e10cSrcweir // remember their indices in the case new values must be added to the array 118cdf0e10cSrcweir sal_Int32 nPropertyCount = lDescriptor.getLength(); 119cdf0e10cSrcweir sal_Int32 nIndexOfFilterName = -1; 120cdf0e10cSrcweir sal_Int32 nIndexOfInputStream = -1; 121cdf0e10cSrcweir sal_Int32 nIndexOfContent = -1; 122cdf0e10cSrcweir sal_Int32 nIndexOfReadOnlyFlag = -1; 123cdf0e10cSrcweir sal_Int32 nIndexOfTemplateFlag = -1; 124cdf0e10cSrcweir sal_Int32 nIndexOfDocumentTitle = -1; 125cdf0e10cSrcweir 126cdf0e10cSrcweir for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty ) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir // extract properties 129cdf0e10cSrcweir if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("URL")) ) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir lDescriptor[nProperty].Value >>= sTemp; 132cdf0e10cSrcweir aURL = sTemp; 133cdf0e10cSrcweir } 134cdf0e10cSrcweir else if( !aURL.Len() && lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FileName")) ) 135cdf0e10cSrcweir { 136cdf0e10cSrcweir lDescriptor[nProperty].Value >>= sTemp; 137cdf0e10cSrcweir aURL = sTemp; 138cdf0e10cSrcweir } 139cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("TypeName")) ) 140cdf0e10cSrcweir { 141cdf0e10cSrcweir lDescriptor[nProperty].Value >>= sTemp; 142cdf0e10cSrcweir aTypeName = sTemp; 143cdf0e10cSrcweir } 144cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FilterName")) ) 145cdf0e10cSrcweir { 146cdf0e10cSrcweir lDescriptor[nProperty].Value >>= sTemp; 147cdf0e10cSrcweir aPreselectedFilterName = sTemp; 148cdf0e10cSrcweir 149cdf0e10cSrcweir // if the preselected filter name is not correct, it must be erased after detection 150cdf0e10cSrcweir // remember index of property to get access to it later 151cdf0e10cSrcweir nIndexOfFilterName = nProperty; 152cdf0e10cSrcweir } 153cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InputStream")) ) 154cdf0e10cSrcweir nIndexOfInputStream = nProperty; 155cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly")) ) 156cdf0e10cSrcweir nIndexOfReadOnlyFlag = nProperty; 157cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("UCBContent")) ) 158cdf0e10cSrcweir nIndexOfContent = nProperty; 159cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("AsTemplate")) ) 160cdf0e10cSrcweir { 161cdf0e10cSrcweir lDescriptor[nProperty].Value >>= bOpenAsTemplate; 162cdf0e10cSrcweir nIndexOfTemplateFlag = nProperty; 163cdf0e10cSrcweir } 164cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InteractionHandler")) ) 165cdf0e10cSrcweir lDescriptor[nProperty].Value >>= xInteraction; 166cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("RepairPackage")) ) 167cdf0e10cSrcweir lDescriptor[nProperty].Value >>= bRepairPackage; 168cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("DocumentTitle")) ) 169cdf0e10cSrcweir nIndexOfDocumentTitle = nProperty; 170cdf0e10cSrcweir } 171cdf0e10cSrcweir 172cdf0e10cSrcweir // can't check the type for external filters, so set the "dont" flag accordingly 173cdf0e10cSrcweir ::vos::OGuard aGuard( Application::GetSolarMutex() ); 174cdf0e10cSrcweir //SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED; 175cdf0e10cSrcweir 176cdf0e10cSrcweir SfxApplication* pApp = SFX_APP(); 177cdf0e10cSrcweir SfxAllItemSet *pSet = new SfxAllItemSet( pApp->GetPool() ); 178cdf0e10cSrcweir TransformParameters( SID_OPENDOC, lDescriptor, *pSet ); 179cdf0e10cSrcweir SFX_ITEMSET_ARG( pSet, pItem, SfxBoolItem, SID_DOC_READONLY, sal_False ); 180cdf0e10cSrcweir 181cdf0e10cSrcweir bWasReadOnly = pItem && pItem->GetValue(); 182cdf0e10cSrcweir 183cdf0e10cSrcweir String aFilterName; 184cdf0e10cSrcweir String aPrefix = String::CreateFromAscii( "private:factory/" ); 185cdf0e10cSrcweir if( aURL.Match( aPrefix ) == aPrefix.Len() ) 186cdf0e10cSrcweir { 187cdf0e10cSrcweir const SfxFilter* pFilter = 0; 188cdf0e10cSrcweir String aPattern( aPrefix ); 189cdf0e10cSrcweir aPattern += String::CreateFromAscii("smath"); 190cdf0e10cSrcweir if ( aURL.Match( aPattern ) >= aPattern.Len() ) 191cdf0e10cSrcweir { 192cdf0e10cSrcweir pFilter = SfxFilter::GetDefaultFilterFromFactory( aURL ); 193cdf0e10cSrcweir aTypeName = pFilter->GetTypeName(); 194cdf0e10cSrcweir aFilterName = pFilter->GetName(); 195cdf0e10cSrcweir } 196cdf0e10cSrcweir } 197cdf0e10cSrcweir else 198cdf0e10cSrcweir { 199cdf0e10cSrcweir // ctor of SfxMedium uses owner transition of ItemSet 200cdf0e10cSrcweir SfxMedium aMedium( aURL, bWasReadOnly ? STREAM_STD_READ : STREAM_STD_READWRITE, sal_False, NULL, pSet ); 201cdf0e10cSrcweir aMedium.UseInteractionHandler( sal_True ); 202cdf0e10cSrcweir 203cdf0e10cSrcweir sal_Bool bIsStorage = aMedium.IsStorage(); 204cdf0e10cSrcweir if ( aMedium.GetErrorCode() == ERRCODE_NONE ) 205cdf0e10cSrcweir { 206cdf0e10cSrcweir // remember input stream and content and put them into the descriptor later 207cdf0e10cSrcweir // should be done here since later the medium can switch to a version 208cdf0e10cSrcweir xStream = aMedium.GetInputStream(); 209cdf0e10cSrcweir xContent = aMedium.GetContent(); 210cdf0e10cSrcweir bReadOnly = aMedium.IsReadOnly(); 211cdf0e10cSrcweir 212cdf0e10cSrcweir if ( bIsStorage ) 213cdf0e10cSrcweir { 214cdf0e10cSrcweir //TODO/LATER: factor this out! 215cdf0e10cSrcweir uno::Reference < embed::XStorage > xStorage = aMedium.GetStorage( sal_False ); 216cdf0e10cSrcweir if ( aMedium.GetLastStorageCreationState() != ERRCODE_NONE ) 217cdf0e10cSrcweir { 218cdf0e10cSrcweir // error during storage creation means _here_ that the medium 219cdf0e10cSrcweir // is broken, but we can not handle it in medium since unpossibility 220cdf0e10cSrcweir // to create a storage does not _always_ means that the medium is broken 221cdf0e10cSrcweir aMedium.SetError( aMedium.GetLastStorageCreationState(), ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) ); 222cdf0e10cSrcweir if ( xInteraction.is() ) 223cdf0e10cSrcweir { 224cdf0e10cSrcweir OUString empty; 225cdf0e10cSrcweir try 226cdf0e10cSrcweir { 227cdf0e10cSrcweir InteractiveAppException xException( empty, 228cdf0e10cSrcweir REFERENCE< XInterface >(), 229cdf0e10cSrcweir InteractionClassification_ERROR, 230cdf0e10cSrcweir aMedium.GetError() ); 231cdf0e10cSrcweir 232cdf0e10cSrcweir REFERENCE< XInteractionRequest > xRequest( 233cdf0e10cSrcweir new ucbhelper::SimpleInteractionRequest( makeAny( xException ), 234cdf0e10cSrcweir ucbhelper::CONTINUATION_APPROVE ) ); 235cdf0e10cSrcweir xInteraction->handle( xRequest ); 236cdf0e10cSrcweir } 237cdf0e10cSrcweir catch ( Exception & ) {}; 238cdf0e10cSrcweir } 239cdf0e10cSrcweir } 240cdf0e10cSrcweir else 241cdf0e10cSrcweir { 242cdf0e10cSrcweir aFilterName.Erase(); 243cdf0e10cSrcweir 244cdf0e10cSrcweir try 245cdf0e10cSrcweir { 246cdf0e10cSrcweir const SfxFilter* pFilter = aPreselectedFilterName.Len() ? 247cdf0e10cSrcweir SfxFilterMatcher().GetFilter4FilterName( aPreselectedFilterName ) : aTypeName.Len() ? 248cdf0e10cSrcweir SfxFilterMatcher(String::CreateFromAscii("smath")).GetFilter4EA( aTypeName ) : 0; 249cdf0e10cSrcweir String aTmpFilterName; 250cdf0e10cSrcweir if ( pFilter ) 251cdf0e10cSrcweir aTmpFilterName = pFilter->GetName(); 252cdf0e10cSrcweir aTypeName = SfxFilter::GetTypeFromStorage( xStorage, pFilter ? pFilter->IsAllowedAsTemplate() : sal_False, &aTmpFilterName ); 253cdf0e10cSrcweir } 254cdf0e10cSrcweir catch( lang::WrappedTargetException& aWrap ) 255cdf0e10cSrcweir { 256cdf0e10cSrcweir packages::zip::ZipIOException aZipException; 257cdf0e10cSrcweir 258cdf0e10cSrcweir // repairing is done only if this type is requested from outside 259cdf0e10cSrcweir if ( ( aWrap.TargetException >>= aZipException ) && aTypeName.Len() ) 260cdf0e10cSrcweir { 261cdf0e10cSrcweir if ( xInteraction.is() ) 262cdf0e10cSrcweir { 263cdf0e10cSrcweir // the package is broken one 264cdf0e10cSrcweir aDocumentTitle = aMedium.GetURLObject().getName( 265cdf0e10cSrcweir INetURLObject::LAST_SEGMENT, 266cdf0e10cSrcweir true, 267cdf0e10cSrcweir INetURLObject::DECODE_WITH_CHARSET ); 268cdf0e10cSrcweir 269cdf0e10cSrcweir if ( !bRepairPackage ) 270cdf0e10cSrcweir { 271cdf0e10cSrcweir // ask the user whether he wants to try to repair 272cdf0e10cSrcweir RequestPackageReparation aRequest( aDocumentTitle ); 273cdf0e10cSrcweir xInteraction->handle( aRequest.GetRequest() ); 274cdf0e10cSrcweir bRepairAllowed = aRequest.isApproved(); 275cdf0e10cSrcweir } 276cdf0e10cSrcweir 277cdf0e10cSrcweir if ( !bRepairAllowed ) 278cdf0e10cSrcweir { 279cdf0e10cSrcweir // repair either not allowed or not successful 280cdf0e10cSrcweir NotifyBrokenPackage aNotifyRequest( aDocumentTitle ); 281cdf0e10cSrcweir xInteraction->handle( aNotifyRequest.GetRequest() ); 282cdf0e10cSrcweir } 283cdf0e10cSrcweir } 284cdf0e10cSrcweir 285cdf0e10cSrcweir if ( !bRepairAllowed ) 286cdf0e10cSrcweir aTypeName.Erase(); 287cdf0e10cSrcweir } 288cdf0e10cSrcweir } 289cdf0e10cSrcweir catch( uno::RuntimeException& ) 290cdf0e10cSrcweir { 291cdf0e10cSrcweir throw; 292cdf0e10cSrcweir } 293cdf0e10cSrcweir catch( uno::Exception& ) 294cdf0e10cSrcweir { 295cdf0e10cSrcweir aTypeName.Erase(); 296cdf0e10cSrcweir } 297cdf0e10cSrcweir 298cdf0e10cSrcweir if ( aTypeName.Len() ) 299cdf0e10cSrcweir { 300cdf0e10cSrcweir const SfxFilter* pFilter = 301cdf0e10cSrcweir SfxFilterMatcher( String::CreateFromAscii("smath") ).GetFilter4EA( aTypeName ); 302cdf0e10cSrcweir if ( pFilter ) 303cdf0e10cSrcweir aFilterName = pFilter->GetName(); 304cdf0e10cSrcweir } 305cdf0e10cSrcweir } 306cdf0e10cSrcweir } 307cdf0e10cSrcweir else 308cdf0e10cSrcweir { 309*d30fb54cSRegina Henschel // DesignScience Equation Editor MathType 3.0 ? 310cdf0e10cSrcweir SvStream *pStrm = aMedium.GetInStream(); 311cdf0e10cSrcweir aTypeName.Erase(); 312cdf0e10cSrcweir if (pStrm && !pStrm->GetError()) 313cdf0e10cSrcweir { 314cdf0e10cSrcweir SotStorageRef aStorage = new SotStorage ( pStrm, sal_False ); 315cdf0e10cSrcweir if ( !aStorage->GetError() ) 316cdf0e10cSrcweir { 317cdf0e10cSrcweir if ( aStorage->IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Equation Native" ) ) ) ) 318cdf0e10cSrcweir { 319cdf0e10cSrcweir sal_uInt8 nVersion; 320cdf0e10cSrcweir if (GetMathTypeVersion( aStorage, nVersion ) && nVersion <=3) 321cdf0e10cSrcweir aTypeName.AssignAscii( "math_MathType_3x" ); 322cdf0e10cSrcweir } 323cdf0e10cSrcweir } 324cdf0e10cSrcweir else 325cdf0e10cSrcweir { 326*d30fb54cSRegina Henschel // MathML? The SAX parser expects the 'math' root element incl. 327*d30fb54cSRegina Henschel // the namespace URL. Neither '<?xml' prolog nor <!doctype is needed. 328*d30fb54cSRegina Henschel // If the math element has a prefix (e.g. <bla:math), the 329*d30fb54cSRegina Henschel // prefix has to be defined in the namespace attribut 330*d30fb54cSRegina Henschel // (e.g. xmlns:bla="http://www.w3.org/1998/Math/MathML") 331*d30fb54cSRegina Henschel // #124636 is fixed too. 332cdf0e10cSrcweir pStrm->Seek( STREAM_SEEK_TO_BEGIN ); 333*d30fb54cSRegina Henschel const size_t nBufSize=2048; 334*d30fb54cSRegina Henschel sal_uInt16 aBuffer[nBufSize]; // will be casted to an Unicode-Array below 335*d30fb54cSRegina Henschel sal_uInt8* pByte = reinterpret_cast<sal_uInt8*>(aBuffer); 336*d30fb54cSRegina Henschel const sal_uLong nBytesRead(pStrm->Read( pByte, nBufSize * 2 ) ); 337*d30fb54cSRegina Henschel const sal_uLong nUnicodeCharsRead (nBytesRead / 2 ); 338*d30fb54cSRegina Henschel 339*d30fb54cSRegina Henschel // For backwards searching an OUString is used. The conversion needs an 340*d30fb54cSRegina Henschel // encoding information. Default encoding is UTF-8, UTF-16 is possible 341*d30fb54cSRegina Henschel // (e.g. from MS "Math Input Control"), others are unlikely. 342*d30fb54cSRegina Henschel // Looking for Byte Order Mark 343*d30fb54cSRegina Henschel rtl_TextEncoding aEncoding = RTL_TEXTENCODING_UTF8; 344*d30fb54cSRegina Henschel bool bIsUnicode = false; 345*d30fb54cSRegina Henschel if (nBytesRead >= 2 && (aBuffer[0]==0xfffe || aBuffer[0]==0xfeff) ) 346cdf0e10cSrcweir { 347*d30fb54cSRegina Henschel aEncoding = RTL_TEXTENCODING_UNICODE; 348*d30fb54cSRegina Henschel bIsUnicode = true; 349*d30fb54cSRegina Henschel if ( aBuffer[0] == 0xfffe) 350*d30fb54cSRegina Henschel { //swap bytes to make Big Endian 351*d30fb54cSRegina Henschel for (size_t i=0; i < nUnicodeCharsRead; ++i) 352*d30fb54cSRegina Henschel { 353*d30fb54cSRegina Henschel aBuffer[i] = (aBuffer[i]>>8) | (aBuffer[i]<<8) ; 354*d30fb54cSRegina Henschel } 355*d30fb54cSRegina Henschel } 356*d30fb54cSRegina Henschel } 3574ec5d36fSArmin Le Grand 358*d30fb54cSRegina Henschel bool isMathFile(false); 359*d30fb54cSRegina Henschel if ( nBytesRead > 56) // minimal <math xmlns="http://www.w3.org/1998/Math/MathML"></math> 360*d30fb54cSRegina Henschel { 361*d30fb54cSRegina Henschel const sal_Char* pChar = reinterpret_cast<sal_Char*>(aBuffer); 362*d30fb54cSRegina Henschel sal_Unicode* pUnicode = (sal_Unicode*) aBuffer; 363*d30fb54cSRegina Henschel 364*d30fb54cSRegina Henschel const OUString sFragment( (bIsUnicode) 365*d30fb54cSRegina Henschel ? OUString( pUnicode , nUnicodeCharsRead ) 366*d30fb54cSRegina Henschel : OUString( pChar, nBytesRead, aEncoding) ); 367*d30fb54cSRegina Henschel const sal_Int32 nFragmentLength(sFragment.getLength()); 368*d30fb54cSRegina Henschel 369*d30fb54cSRegina Henschel // look for MathML URL http://www.w3.org/1998/Math/MathML 370*d30fb54cSRegina Henschel // #i53509 A MathML URL can be value of a namespace attribute, but can be as well 371*d30fb54cSRegina Henschel // inside a doctype e.g. [<!ENTITY mathml 'http://www.w3.org/1998/Math/MathML'>] 372*d30fb54cSRegina Henschel // or inside a schema reference e.g. s:schemaLocation="http://www.w3.org/1998/Math/MathML" 373*d30fb54cSRegina Henschel // Use a loop to get the correct one. 374*d30fb54cSRegina Henschel const OUString sURL( OUString::createFromAscii("http://www.w3.org/1998/Math/MathML")); 375*d30fb54cSRegina Henschel const sal_Int32 nURLLength = sURL.getLength(); 376*d30fb54cSRegina Henschel const OUString sEQ( OUString::createFromAscii("=") ); 377*d30fb54cSRegina Henschel const OUString sXMLNS( OUString::createFromAscii("xmlns") ); 378*d30fb54cSRegina Henschel sal_Int32 nPosURL = -1; // for index of first character of URL 379*d30fb54cSRegina Henschel sal_Int32 nPosURLSearchStart = 0; 380*d30fb54cSRegina Henschel sal_Int32 nPosEQ = -1; // for index of equal sign 381*d30fb54cSRegina Henschel sal_Int32 nPosXMLNS = -1; // for index of first character of string "xmlns" 382*d30fb54cSRegina Henschel do 3834ec5d36fSArmin Le Grand { 384*d30fb54cSRegina Henschel nPosURL = sFragment.indexOf(sURL,nPosURLSearchStart); 385*d30fb54cSRegina Henschel if( nPosURL < 0 ) 386*d30fb54cSRegina Henschel { 387*d30fb54cSRegina Henschel break; // no MathML URL, cannot be parsed 388*d30fb54cSRegina Henschel } 389*d30fb54cSRegina Henschel // need 'xmlns:prefix =' or 'xmlns =', look backwards, first for equal sign 390*d30fb54cSRegina Henschel nPosEQ = sFragment.lastIndexOf(sEQ,nPosURL); 391*d30fb54cSRegina Henschel if (nPosEQ >= 0 && nPosEQ >= nPosURLSearchStart) 3924ec5d36fSArmin Le Grand { 393*d30fb54cSRegina Henschel nPosXMLNS = sFragment.lastIndexOf(sXMLNS,nPosEQ); 394*d30fb54cSRegina Henschel if( nPosXMLNS >= nPosURLSearchStart ) 395*d30fb54cSRegina Henschel { // an xmlns attribute is found, but it might belong to a schema 396*d30fb54cSRegina Henschel // get prefix if present 397*d30fb54cSRegina Henschel const OUString sPrefix = (sFragment.copy(nPosXMLNS+5,nPosEQ-(nPosXMLNS+5))).trim(); 398*d30fb54cSRegina Henschel // such prefix definition must start with colon (will be removed below) 399*d30fb54cSRegina Henschel bool bHasPrefix( (sPrefix.isEmpty()) ? false : sPrefix.toChar() == sal_Unicode(':') ); 400*d30fb54cSRegina Henschel // the math element starts either with '<prefix:math' or '<math' 401*d30fb54cSRegina Henschel const OUString sMathStart( (bHasPrefix) 402*d30fb54cSRegina Henschel ? OUString::createFromAscii("<") + sPrefix.copy(1,sPrefix.getLength()-1) + OUString::createFromAscii(":math") 403*d30fb54cSRegina Henschel : OUString::createFromAscii("<math") ); 404*d30fb54cSRegina Henschel sal_Int32 nPosMath (sFragment.lastIndexOf(sMathStart,nPosXMLNS)); 405*d30fb54cSRegina Henschel if( nPosMath >= 0) 406*d30fb54cSRegina Henschel { // xmlns attribute belongs to math element 407*d30fb54cSRegina Henschel isMathFile = true; 408*d30fb54cSRegina Henschel break; 4094ec5d36fSArmin Le Grand } 4104ec5d36fSArmin Le Grand } 4114ec5d36fSArmin Le Grand } 412*d30fb54cSRegina Henschel // MathML URL was wrong one, look for next 413*d30fb54cSRegina Henschel nPosURLSearchStart = nPosURL + nURLLength; 4144ec5d36fSArmin Le Grand } 415*d30fb54cSRegina Henschel while ( nPosURLSearchStart + nURLLength <= nFragmentLength); 4164ec5d36fSArmin Le Grand 4174ec5d36fSArmin Le Grand if(isMathFile) 418cdf0e10cSrcweir { 419cdf0e10cSrcweir static const sal_Char sFltrNm_2[] = MATHML_XML; 420cdf0e10cSrcweir static const sal_Char sTypeNm_2[] = "math_MathML_XML_Math"; 4214ec5d36fSArmin Le Grand 422cdf0e10cSrcweir aFilterName.AssignAscii( sFltrNm_2 ); 423cdf0e10cSrcweir aTypeName.AssignAscii( sTypeNm_2 ); 424cdf0e10cSrcweir } 425cdf0e10cSrcweir } 426cdf0e10cSrcweir } 427cdf0e10cSrcweir 428cdf0e10cSrcweir if ( aTypeName.Len() ) 429cdf0e10cSrcweir { 430cdf0e10cSrcweir const SfxFilter* pFilt = SfxFilterMatcher( String::CreateFromAscii("smath") ).GetFilter4EA( aTypeName ); 431cdf0e10cSrcweir if ( pFilt ) 432cdf0e10cSrcweir aFilterName = pFilt->GetName(); 433cdf0e10cSrcweir } 434cdf0e10cSrcweir } 435cdf0e10cSrcweir } 436cdf0e10cSrcweir } 437cdf0e10cSrcweir } 438cdf0e10cSrcweir 439cdf0e10cSrcweir if ( nIndexOfInputStream == -1 && xStream.is() ) 440cdf0e10cSrcweir { 441cdf0e10cSrcweir // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice 442cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 ); 443cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("InputStream"); 444cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= xStream; 445cdf0e10cSrcweir nPropertyCount++; 446cdf0e10cSrcweir } 447cdf0e10cSrcweir 448cdf0e10cSrcweir if ( nIndexOfContent == -1 && xContent.is() ) 449cdf0e10cSrcweir { 450cdf0e10cSrcweir // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice 451cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 ); 452cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("UCBContent"); 453cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= xContent; 454cdf0e10cSrcweir nPropertyCount++; 455cdf0e10cSrcweir } 456cdf0e10cSrcweir 457cdf0e10cSrcweir if ( bReadOnly != bWasReadOnly ) 458cdf0e10cSrcweir { 459cdf0e10cSrcweir if ( nIndexOfReadOnlyFlag == -1 ) 460cdf0e10cSrcweir { 461cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 ); 462cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("ReadOnly"); 463cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= bReadOnly; 464cdf0e10cSrcweir nPropertyCount++; 465cdf0e10cSrcweir } 466cdf0e10cSrcweir else 467cdf0e10cSrcweir lDescriptor[nIndexOfReadOnlyFlag].Value <<= bReadOnly; 468cdf0e10cSrcweir } 469cdf0e10cSrcweir 470cdf0e10cSrcweir if ( !bRepairPackage && bRepairAllowed ) 471cdf0e10cSrcweir { 472cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 ); 473cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("RepairPackage"); 474cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= bRepairAllowed; 475cdf0e10cSrcweir nPropertyCount++; 476cdf0e10cSrcweir 477cdf0e10cSrcweir bOpenAsTemplate = sal_True; 478cdf0e10cSrcweir 479cdf0e10cSrcweir // TODO/LATER: set progress bar that should be used 480cdf0e10cSrcweir } 481cdf0e10cSrcweir 482cdf0e10cSrcweir if ( bOpenAsTemplate ) 483cdf0e10cSrcweir { 484cdf0e10cSrcweir if ( nIndexOfTemplateFlag == -1 ) 485cdf0e10cSrcweir { 486cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 ); 487cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("AsTemplate"); 488cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= bOpenAsTemplate; 489cdf0e10cSrcweir nPropertyCount++; 490cdf0e10cSrcweir } 491cdf0e10cSrcweir else 492cdf0e10cSrcweir lDescriptor[nIndexOfTemplateFlag].Value <<= bOpenAsTemplate; 493cdf0e10cSrcweir } 494cdf0e10cSrcweir 495cdf0e10cSrcweir if ( aDocumentTitle.getLength() ) 496cdf0e10cSrcweir { 497cdf0e10cSrcweir // the title was set here 498cdf0e10cSrcweir if ( nIndexOfDocumentTitle == -1 ) 499cdf0e10cSrcweir { 500cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 ); 501cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("DocumentTitle"); 502cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= aDocumentTitle; 503cdf0e10cSrcweir nPropertyCount++; 504cdf0e10cSrcweir } 505cdf0e10cSrcweir else 506cdf0e10cSrcweir lDescriptor[nIndexOfDocumentTitle].Value <<= aDocumentTitle; 507cdf0e10cSrcweir } 508cdf0e10cSrcweir 509cdf0e10cSrcweir if ( !aFilterName.Len() ) 510cdf0e10cSrcweir aTypeName.Erase(); 511cdf0e10cSrcweir 512cdf0e10cSrcweir return aTypeName; 513cdf0e10cSrcweir } 514cdf0e10cSrcweir 515cdf0e10cSrcweir SFX_IMPL_SINGLEFACTORY( SmFilterDetect ) 516cdf0e10cSrcweir 517cdf0e10cSrcweir /* XServiceInfo */ 518cdf0e10cSrcweir UNOOUSTRING SAL_CALL SmFilterDetect::getImplementationName() throw( UNORUNTIMEEXCEPTION ) 519cdf0e10cSrcweir { 520cdf0e10cSrcweir return impl_getStaticImplementationName(); 521cdf0e10cSrcweir } 522cdf0e10cSrcweir \ 523cdf0e10cSrcweir /* XServiceInfo */ 524cdf0e10cSrcweir sal_Bool SAL_CALL SmFilterDetect::supportsService( const UNOOUSTRING& sServiceName ) throw( UNORUNTIMEEXCEPTION ) 525cdf0e10cSrcweir { 526cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > seqServiceNames = getSupportedServiceNames(); 527cdf0e10cSrcweir const UNOOUSTRING* pArray = seqServiceNames.getConstArray(); 528cdf0e10cSrcweir for ( sal_Int32 nCounter=0; nCounter<seqServiceNames.getLength(); nCounter++ ) 529cdf0e10cSrcweir { 530cdf0e10cSrcweir if ( pArray[nCounter] == sServiceName ) 531cdf0e10cSrcweir { 532cdf0e10cSrcweir return sal_True ; 533cdf0e10cSrcweir } 534cdf0e10cSrcweir } 535cdf0e10cSrcweir return sal_False ; 536cdf0e10cSrcweir } 537cdf0e10cSrcweir 538cdf0e10cSrcweir /* XServiceInfo */ 539cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > SAL_CALL SmFilterDetect::getSupportedServiceNames() throw( UNORUNTIMEEXCEPTION ) 540cdf0e10cSrcweir { 541cdf0e10cSrcweir return impl_getStaticSupportedServiceNames(); 542cdf0e10cSrcweir } 543cdf0e10cSrcweir 544cdf0e10cSrcweir /* Helper for XServiceInfo */ 545cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > SmFilterDetect::impl_getStaticSupportedServiceNames() 546cdf0e10cSrcweir { 547cdf0e10cSrcweir UNOMUTEXGUARD aGuard( UNOMUTEX::getGlobalMutex() ); 548cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > seqServiceNames( 1 ); 549cdf0e10cSrcweir seqServiceNames.getArray() [0] = UNOOUSTRING::createFromAscii( "com.sun.star.frame.ExtendedTypeDetection" ); 550cdf0e10cSrcweir return seqServiceNames ; 551cdf0e10cSrcweir } 552cdf0e10cSrcweir 553cdf0e10cSrcweir /* Helper for XServiceInfo */ 554cdf0e10cSrcweir UNOOUSTRING SmFilterDetect::impl_getStaticImplementationName() 555cdf0e10cSrcweir { 556cdf0e10cSrcweir return UNOOUSTRING::createFromAscii( "com.sun.star.comp.math.FormatDetector" ); 557cdf0e10cSrcweir } 558cdf0e10cSrcweir 559cdf0e10cSrcweir /* Helper for registry */ 560cdf0e10cSrcweir UNOREFERENCE< UNOXINTERFACE > SAL_CALL SmFilterDetect::impl_createInstance( const UNOREFERENCE< UNOXMULTISERVICEFACTORY >& xServiceManager ) throw( UNOEXCEPTION ) 561cdf0e10cSrcweir { 562cdf0e10cSrcweir return UNOREFERENCE< UNOXINTERFACE >( *new SmFilterDetect( xServiceManager ) ); 563cdf0e10cSrcweir } 564cdf0e10cSrcweir 565