Line data Source code
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"
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>
27 :
28 : #include "eqnolefilehdr.hxx"
29 :
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;
37 :
38 84 : SmFilterDetect::SmFilterDetect( const Reference < XMultiServiceFactory >& /*xFactory*/ )
39 : {
40 84 : }
41 :
42 168 : SmFilterDetect::~SmFilterDetect()
43 : {
44 168 : }
45 :
46 84 : OUString SAL_CALL SmFilterDetect::detect( Sequence< PropertyValue >& lDescriptor ) throw( RuntimeException, std::exception )
47 : {
48 84 : MediaDescriptor aMediaDesc( lDescriptor );
49 168 : uno::Reference< io::XInputStream > xInStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY );
50 84 : if ( !xInStream.is() )
51 0 : return OUString();
52 :
53 168 : SfxMedium aMedium;
54 84 : aMedium.UseInteractionHandler( false );
55 84 : aMedium.setStreamToLoadFrom( xInStream, true );
56 :
57 84 : SvStream *pInStrm = aMedium.GetInStream();
58 84 : if ( !pInStrm || pInStrm->GetError() )
59 0 : return OUString();
60 :
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 84 : pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
66 84 : if ( pInStrm->remainingSize() == 0 )
67 0 : return OUString();
68 :
69 84 : bool bStorageOk = false;
70 : try
71 : {
72 84 : tools::SvRef<SotStorage> aStorage = new SotStorage( pInStrm, false );
73 84 : bStorageOk = !aStorage->GetError();
74 84 : if (bStorageOk)
75 : {
76 0 : if ( aStorage->IsStream("Equation Native") )
77 : {
78 : sal_uInt8 nVersion;
79 0 : if ( GetMathTypeVersion( aStorage, nVersion ) && nVersion <=3 )
80 0 : return OUString("math_MathType_3x");
81 : }
82 84 : }
83 : }
84 0 : catch (const css::ucb::ContentCreationException &e)
85 : {
86 : SAL_WARN("starmath", "SmFilterDetect::detect caught " << e.Message);
87 : }
88 :
89 84 : if (!bStorageOk)
90 : {
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 84 : aBuffer[nBufferSize] = 0;
97 84 : pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
98 84 : pInStrm->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW ); // avoid BOM marker
99 84 : sal_uLong nBytesRead = pInStrm->Read( aBuffer, nBufferSize );
100 84 : if (nBytesRead >= 6)
101 : {
102 84 : bool bIsMathType = false;
103 84 : if (0 == strncmp( "<?xml", aBuffer, 5))
104 0 : bIsMathType = (strstr( aBuffer, "<math>" ) ||
105 0 : strstr( aBuffer, "<math " ) ||
106 0 : strstr( aBuffer, "<math:math " ));
107 : else
108 : // this is the old <math tag to MathML in the beginning of the XML file
109 168 : bIsMathType = (0 == strncmp( "<math ", aBuffer, 6) ||
110 168 : 0 == strncmp( "<math> ", aBuffer, 7) ||
111 168 : 0 == strncmp( "<math:math> ", aBuffer, 12));
112 :
113 84 : if ( bIsMathType )
114 0 : return OUString("math_MathML_XML_Math");
115 : }
116 : }
117 :
118 168 : return OUString();
119 : }
120 :
121 : /* XServiceInfo */
122 0 : OUString SAL_CALL SmFilterDetect::getImplementationName() throw( RuntimeException, std::exception )
123 : {
124 0 : return impl_getStaticImplementationName();
125 : }
126 :
127 : /* XServiceInfo */
128 0 : sal_Bool SAL_CALL SmFilterDetect::supportsService( const OUString& sServiceName ) throw( RuntimeException, std::exception )
129 : {
130 0 : return cppu::supportsService(this, sServiceName);
131 : }
132 :
133 : /* XServiceInfo */
134 0 : Sequence< OUString > SAL_CALL SmFilterDetect::getSupportedServiceNames() throw( RuntimeException, std::exception )
135 : {
136 0 : return impl_getStaticSupportedServiceNames();
137 : }
138 :
139 : /* Helper for XServiceInfo */
140 2 : Sequence< OUString > SmFilterDetect::impl_getStaticSupportedServiceNames()
141 : {
142 2 : return Sequence< OUString >{ "com.sun.star.frame.ExtendedTypeDetection" };
143 : }
144 :
145 : /* Helper for XServiceInfo */
146 4 : OUString SmFilterDetect::impl_getStaticImplementationName()
147 : {
148 4 : return OUString("com.sun.star.comp.math.FormatDetector");
149 : }
150 :
151 : /* Helper for registry */
152 84 : Reference< XInterface > SAL_CALL SmFilterDetect::impl_createInstance( const Reference< XMultiServiceFactory >& xServiceManager ) throw( Exception )
153 : {
154 84 : return Reference< XInterface >( *new SmFilterDetect( xServiceManager ) );
155 : }
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|