1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "smdetect.hxx"
21 #include <cppuhelper/supportsservice.hxx>
22 #include <com/sun/star/io/XInputStream.hpp>
23 #include <sfx2/docfile.hxx>
24 #include <unotools/mediadescriptor.hxx>
26 #include "eqnolefilehdr.hxx"
28 using namespace ::com::sun::star
;
29 using namespace ::com::sun::star::uno
;
30 using namespace ::com::sun::star::io
;
31 using namespace ::com::sun::star::task
;
32 using namespace ::com::sun::star::beans
;
33 using namespace ::com::sun::star::lang
;
34 using utl::MediaDescriptor
;
36 SmFilterDetect::SmFilterDetect( const Reference
< XMultiServiceFactory
>& /*xFactory*/ )
40 SmFilterDetect::~SmFilterDetect()
44 OUString SAL_CALL
SmFilterDetect::detect( Sequence
< PropertyValue
>& lDescriptor
)
46 MediaDescriptor
aMediaDesc( lDescriptor
);
47 uno::Reference
< io::XInputStream
> xInStream ( aMediaDesc
[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY
);
48 if ( !xInStream
.is() )
52 aMedium
.UseInteractionHandler( false );
53 aMedium
.setStreamToLoadFrom( xInStream
, true );
55 SvStream
*pInStrm
= aMedium
.GetInStream();
56 if ( !pInStrm
|| pInStrm
->GetError() )
59 // Do not attempt to create an SotStorage on a
60 // 0-length stream as that would create the compound
61 // document header on the stream and effectively write to
63 pInStrm
->Seek( STREAM_SEEK_TO_BEGIN
);
64 if ( pInStrm
->remainingSize() == 0 )
67 bool bStorageOk
= false;
70 tools::SvRef
<SotStorage
> aStorage
= new SotStorage( pInStrm
, false );
71 bStorageOk
= !aStorage
->GetError();
74 if ( aStorage
->IsStream("Equation Native") )
77 if ( GetMathTypeVersion( aStorage
.get(), nVersion
) && nVersion
<=3 )
78 return OUString("math_MathType_3x");
82 catch (const css::ucb::ContentCreationException
&e
)
84 SAL_WARN("starmath", "SmFilterDetect::detect caught " << e
);
89 // 200 should be enough for the XML
90 // version, encoding and !DOCTYPE
92 static const sal_uInt16 nBufferSize
= 200;
93 char aBuffer
[nBufferSize
+1];
94 pInStrm
->Seek( STREAM_SEEK_TO_BEGIN
);
95 pInStrm
->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW
); // avoid BOM marker
96 auto nBytesRead
= pInStrm
->ReadBytes( aBuffer
, nBufferSize
);
99 aBuffer
[nBytesRead
] = 0;
100 bool bIsMathType
= false;
101 if (0 == strncmp( "<?xml", aBuffer
, 5))
102 bIsMathType
= (strstr( aBuffer
, "<math>" ) ||
103 strstr( aBuffer
, "<math " ) ||
104 strstr( aBuffer
, "<math:math " ));
106 // this is the old <math tag to MathML in the beginning of the XML file
107 bIsMathType
= (0 == strncmp( "<math ", aBuffer
, 6) ||
108 0 == strncmp( "<math> ", aBuffer
, 7) ||
109 0 == strncmp( "<math:math> ", aBuffer
, 12));
112 return OUString("math_MathML_XML_Math");
120 OUString SAL_CALL
SmFilterDetect::getImplementationName()
122 return impl_getStaticImplementationName();
126 sal_Bool SAL_CALL
SmFilterDetect::supportsService( const OUString
& sServiceName
)
128 return cppu::supportsService(this, sServiceName
);
132 Sequence
< OUString
> SAL_CALL
SmFilterDetect::getSupportedServiceNames()
134 return impl_getStaticSupportedServiceNames();
137 /* Helper for XServiceInfo */
138 Sequence
< OUString
> SmFilterDetect::impl_getStaticSupportedServiceNames()
140 return Sequence
< OUString
>{ "com.sun.star.frame.ExtendedTypeDetection" };
143 /* Helper for XServiceInfo */
144 OUString
SmFilterDetect::impl_getStaticImplementationName()
146 return OUString("com.sun.star.comp.math.FormatDetector");
149 /* Helper for registry */
150 Reference
< XInterface
> SmFilterDetect::impl_createInstance( const Reference
< XMultiServiceFactory
>& xServiceManager
)
152 return Reference
< XInterface
>( *new SmFilterDetect( xServiceManager
) );
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */