bump product version to 5.0.4.1
[LibreOffice.git] / starmath / source / smdetect.cxx
blobc16e4ded7ea02ecbb2cac2535ceb7c8198e427e2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <com/sun/star/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <com/sun/star/io/XInputStream.hpp>
25 #include <sfx2/docfile.hxx>
26 #include <unotools/mediadescriptor.hxx>
28 #include "eqnolefilehdr.hxx"
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::io;
33 using namespace ::com::sun::star::task;
34 using namespace ::com::sun::star::beans;
35 using namespace ::com::sun::star::lang;
36 using utl::MediaDescriptor;
38 SmFilterDetect::SmFilterDetect( const Reference < XMultiServiceFactory >& /*xFactory*/ )
42 SmFilterDetect::~SmFilterDetect()
46 OUString SAL_CALL SmFilterDetect::detect( Sequence< PropertyValue >& lDescriptor ) throw( RuntimeException, std::exception )
48 MediaDescriptor aMediaDesc( lDescriptor );
49 uno::Reference< io::XInputStream > xInStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY );
50 if ( !xInStream.is() )
51 return OUString();
53 SfxMedium aMedium;
54 aMedium.UseInteractionHandler( false );
55 aMedium.setStreamToLoadFrom( xInStream, true );
57 SvStream *pInStrm = aMedium.GetInStream();
58 if ( !pInStrm || pInStrm->GetError() )
59 return OUString();
61 // Do not attempt to create an SotStorage on a
62 // 0-length stream as that would create the compound
63 // document header on the stream and effectively write to
64 // disk!
65 pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
66 if ( pInStrm->remainingSize() == 0 )
67 return OUString();
69 bool bStorageOk = false;
70 try
72 tools::SvRef<SotStorage> aStorage = new SotStorage( pInStrm, false );
73 bStorageOk = !aStorage->GetError();
74 if (bStorageOk)
76 if ( aStorage->IsStream("Equation Native") )
78 sal_uInt8 nVersion;
79 if ( GetMathTypeVersion( aStorage, nVersion ) && nVersion <=3 )
80 return OUString("math_MathType_3x");
84 catch (const css::ucb::ContentCreationException &e)
86 SAL_WARN("starmath", "SmFilterDetect::detect caught " << e.Message);
89 if (!bStorageOk)
91 // 200 should be enough for the XML
92 // version, encoding and !DOCTYPE
93 // stuff I hope?
94 static const sal_uInt16 nBufferSize = 200;
95 char aBuffer[nBufferSize+1];
96 aBuffer[nBufferSize] = 0;
97 pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
98 pInStrm->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW ); // avoid BOM marker
99 sal_uLong nBytesRead = pInStrm->Read( aBuffer, nBufferSize );
100 if (nBytesRead >= 6)
102 bool bIsMathType = false;
103 if (0 == strncmp( "<?xml", aBuffer, 5))
104 bIsMathType = (strstr( aBuffer, "<math>" ) ||
105 strstr( aBuffer, "<math " ) ||
106 strstr( aBuffer, "<math:math " ));
107 else
108 // this is the old <math tag to MathML in the beginning of the XML file
109 bIsMathType = (0 == strncmp( "<math ", aBuffer, 6) ||
110 0 == strncmp( "<math> ", aBuffer, 7) ||
111 0 == strncmp( "<math:math> ", aBuffer, 12));
113 if ( bIsMathType )
114 return OUString("math_MathML_XML_Math");
118 return OUString();
121 /* XServiceInfo */
122 OUString SAL_CALL SmFilterDetect::getImplementationName() throw( RuntimeException, std::exception )
124 return impl_getStaticImplementationName();
127 /* XServiceInfo */
128 sal_Bool SAL_CALL SmFilterDetect::supportsService( const OUString& sServiceName ) throw( RuntimeException, std::exception )
130 return cppu::supportsService(this, sServiceName);
133 /* XServiceInfo */
134 Sequence< OUString > SAL_CALL SmFilterDetect::getSupportedServiceNames() throw( RuntimeException, std::exception )
136 return impl_getStaticSupportedServiceNames();
139 /* Helper for XServiceInfo */
140 Sequence< OUString > SmFilterDetect::impl_getStaticSupportedServiceNames()
142 return Sequence< OUString >{ "com.sun.star.frame.ExtendedTypeDetection" };
145 /* Helper for XServiceInfo */
146 OUString SmFilterDetect::impl_getStaticImplementationName()
148 return OUString("com.sun.star.comp.math.FormatDetector");
151 /* Helper for registry */
152 Reference< XInterface > SAL_CALL SmFilterDetect::impl_createInstance( const Reference< XMultiServiceFactory >& xServiceManager ) throw( Exception )
154 return Reference< XInterface >( *new SmFilterDetect( xServiceManager ) );
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */