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 <com/sun/star/ucb/ContentCreationException.hpp>
24 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <sfx2/docfile.hxx>
26 #include <unotools/mediadescriptor.hxx>
27 #include <sal/log.hxx>
28 #include <sot/storage.hxx>
29 #include <tools/diagnose_ex.h>
31 #include "eqnolefilehdr.hxx"
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::io
;
36 using namespace ::com::sun::star::task
;
37 using namespace ::com::sun::star::beans
;
38 using namespace ::com::sun::star::lang
;
39 using utl::MediaDescriptor
;
41 SmFilterDetect::SmFilterDetect()
45 SmFilterDetect::~SmFilterDetect()
49 OUString SAL_CALL
SmFilterDetect::detect( Sequence
< PropertyValue
>& lDescriptor
)
51 MediaDescriptor
aMediaDesc( lDescriptor
);
52 uno::Reference
< io::XInputStream
> xInStream ( aMediaDesc
[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY
);
53 if ( !xInStream
.is() )
57 aMedium
.UseInteractionHandler( false );
58 aMedium
.setStreamToLoadFrom( xInStream
, true );
60 SvStream
*pInStrm
= aMedium
.GetInStream();
61 if ( !pInStrm
|| pInStrm
->GetError() )
64 // Do not attempt to create an SotStorage on a
65 // 0-length stream as that would create the compound
66 // document header on the stream and effectively write to
68 pInStrm
->Seek( STREAM_SEEK_TO_BEGIN
);
69 if ( pInStrm
->remainingSize() == 0 )
72 bool bStorageOk
= false;
75 tools::SvRef
<SotStorage
> aStorage
= new SotStorage( pInStrm
, false );
76 bStorageOk
= !aStorage
->GetError();
79 if ( aStorage
->IsStream("Equation Native") )
82 if ( GetMathTypeVersion( aStorage
.get(), nVersion
) && nVersion
<=3 )
83 return "math_MathType_3x";
87 catch (const css::ucb::ContentCreationException
&)
89 TOOLS_WARN_EXCEPTION("starmath", "SmFilterDetect::detect caught" );
94 // 200 should be enough for the XML
95 // version, encoding and !DOCTYPE
97 static const sal_uInt16 nBufferSize
= 200;
98 char aBuffer
[nBufferSize
+1];
99 pInStrm
->Seek( STREAM_SEEK_TO_BEGIN
);
100 pInStrm
->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW
); // avoid BOM marker
101 auto nBytesRead
= pInStrm
->ReadBytes( aBuffer
, nBufferSize
);
104 aBuffer
[nBytesRead
] = 0;
105 bool bIsMathType
= false;
106 if (0 == strncmp( "<?xml", aBuffer
, 5))
107 bIsMathType
= (strstr( aBuffer
, "<math>" ) ||
108 strstr( aBuffer
, "<math " ) ||
109 strstr( aBuffer
, "<math:math " ));
111 // this is the old <math tag to MathML in the beginning of the XML file
112 bIsMathType
= (0 == strncmp( "<math ", aBuffer
, 6) ||
113 0 == strncmp( "<math> ", aBuffer
, 7) ||
114 0 == strncmp( "<math:math> ", aBuffer
, 12));
117 return "math_MathML_XML_Math";
125 OUString SAL_CALL
SmFilterDetect::getImplementationName()
127 return "com.sun.star.comp.math.FormatDetector";
131 sal_Bool SAL_CALL
SmFilterDetect::supportsService( const OUString
& sServiceName
)
133 return cppu::supportsService(this, sServiceName
);
137 Sequence
< OUString
> SAL_CALL
SmFilterDetect::getSupportedServiceNames()
139 return { "com.sun.star.frame.ExtendedTypeDetection" };
142 extern "C" SAL_DLLPUBLIC_EXPORT
uno::XInterface
*
143 math_FormatDetector_get_implementation(uno::XComponentContext
* /*pCtx*/,
144 uno::Sequence
<uno::Any
> const& /*rSeq*/)
146 return cppu::acquire(new SmFilterDetect
);
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */