LibreOffice Module starmath (master) 1
smdetect.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "smdetect.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>
27#include <sot/storage.hxx>
29
30#include "eqnolefilehdr.hxx"
31
32using namespace ::com::sun::star;
33using namespace ::com::sun::star::uno;
34using namespace ::com::sun::star::io;
35using namespace ::com::sun::star::task;
36using namespace ::com::sun::star::beans;
37using namespace ::com::sun::star::lang;
39
41{
42}
43
45{
46}
47
48OUString SAL_CALL SmFilterDetect::detect( Sequence< PropertyValue >& lDescriptor )
49{
50 MediaDescriptor aMediaDesc( lDescriptor );
51 uno::Reference< io::XInputStream > xInStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM], uno::UNO_QUERY );
52 if ( !xInStream.is() )
53 return OUString();
54
55 SfxMedium aMedium;
56 aMedium.UseInteractionHandler( false );
57 aMedium.setStreamToLoadFrom( xInStream, true );
58
59 SvStream *pInStrm = aMedium.GetInStream();
60 if ( !pInStrm || pInStrm->GetError() )
61 return OUString();
62
63 // Do not attempt to create an SotStorage on a
64 // 0-length stream as that would create the compound
65 // document header on the stream and effectively write to
66 // disk!
67 pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
68 if ( pInStrm->remainingSize() == 0 )
69 return OUString();
70
71 bool bStorageOk = false;
72 try
73 {
74 tools::SvRef<SotStorage> aStorage = new SotStorage( pInStrm, false );
75 bStorageOk = !aStorage->GetError();
76 if (bStorageOk)
77 {
78 if ( aStorage->IsStream("Equation Native") )
79 {
81 if ( GetMathTypeVersion( aStorage.get(), nVersion ) && nVersion <=3 )
82 return "math_MathType_3x";
83 }
84 }
85 }
86 catch (const css::ucb::ContentCreationException &)
87 {
88 TOOLS_WARN_EXCEPTION("starmath", "SmFilterDetect::detect caught" );
89 }
90
91 if (!bStorageOk)
92 {
93 // 200 should be enough for the XML
94 // version, encoding and !DOCTYPE
95 // stuff I hope?
96 static const sal_uInt16 nBufferSize = 200;
97 char aBuffer[nBufferSize+1];
98 pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
99 pInStrm->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW ); // avoid BOM marker
100 auto nBytesRead = pInStrm->ReadBytes( aBuffer, nBufferSize );
101 if (nBytesRead >= 6)
102 {
103 aBuffer[nBytesRead] = 0;
104 bool bIsMathType = false;
105 if (0 == strncmp( "<?xml", aBuffer, 5))
106 bIsMathType = (strstr( aBuffer, "<math>" ) ||
107 strstr( aBuffer, "<math " ) ||
108 strstr( aBuffer, "<math:math " ));
109 else
110 // this is the old <math tag to MathML in the beginning of the XML file
111 bIsMathType = (0 == strncmp( "<math ", aBuffer, 6) ||
112 0 == strncmp( "<math> ", aBuffer, 7) ||
113 0 == strncmp( "<math:math> ", aBuffer, 12));
114
115 if ( bIsMathType )
116 return "math_MathML_XML_Math";
117 }
118 }
119
120 return OUString();
121}
122
123/* XServiceInfo */
125{
126 return "com.sun.star.comp.math.FormatDetector";
127}
128
129/* XServiceInfo */
130sal_Bool SAL_CALL SmFilterDetect::supportsService( const OUString& sServiceName )
131{
133}
134
135/* XServiceInfo */
136Sequence< OUString > SAL_CALL SmFilterDetect::getSupportedServiceNames()
137{
138 return { "com.sun.star.frame.ExtendedTypeDetection" };
139}
140
141extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
142math_FormatDetector_get_implementation(uno::XComponentContext* /*pCtx*/,
143 uno::Sequence<uno::Any> const& /*rSeq*/)
144{
145 return cppu::acquire(new SmFilterDetect);
146}
147
148/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral sServiceName
void setStreamToLoadFrom(const css::uno::Reference< css::io::XInputStream > &xInputStream, bool bIsReadOnly)
void UseInteractionHandler(bool)
SvStream * GetInStream()
virtual OUString SAL_CALL detect(css::uno::Sequence< css::beans::PropertyValue > &lDescriptor) override
Definition: smdetect.cxx:48
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
Definition: smdetect.cxx:130
virtual OUString SAL_CALL getImplementationName() override
Definition: smdetect.cxx:124
virtual ~SmFilterDetect() override
Definition: smdetect.cxx:44
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: smdetect.cxx:136
void StartReadingUnicodeText(rtl_TextEncoding eReadBomCharSet)
sal_uInt64 Seek(sal_uInt64 nPos)
std::size_t ReadBytes(void *pData, std::size_t nSize)
ErrCode GetError() const
sal_uInt64 remainingSize()
T * get() const
#define TOOLS_WARN_EXCEPTION(area, stream)
bool GetMathTypeVersion(SotStorage *pStor, sal_uInt8 &nVersion)
sal_Int16 nVersion
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SAL_DLLPUBLIC_EXPORT uno::XInterface * math_FormatDetector_get_implementation(uno::XComponentContext *, uno::Sequence< uno::Any > const &)
Definition: smdetect.cxx:142
#define STREAM_SEEK_TO_BEGIN
unsigned char sal_uInt8
unsigned char sal_Bool
std::unique_ptr< char[]> aBuffer