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 0 : SmFilterDetect::SmFilterDetect( const Reference < XMultiServiceFactory >& /*xFactory*/ )
39 : {
40 0 : }
41 :
42 0 : SmFilterDetect::~SmFilterDetect()
43 : {
44 0 : }
45 :
46 0 : OUString SAL_CALL SmFilterDetect::detect( Sequence< PropertyValue >& lDescriptor ) throw( RuntimeException, std::exception )
47 : {
48 0 : MediaDescriptor aMediaDesc( lDescriptor );
49 0 : uno::Reference< io::XInputStream > xInStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY );
50 0 : if ( !xInStream.is() )
51 0 : return OUString();
52 :
53 0 : SfxMedium aMedium;
54 0 : aMedium.UseInteractionHandler( false );
55 0 : aMedium.setStreamToLoadFrom( xInStream, true );
56 :
57 0 : SvStream *pInStrm = aMedium.GetInStream();
58 0 : 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 0 : pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
66 0 : if ( pInStrm->remainingSize() == 0 )
67 0 : return OUString();
68 :
69 0 : bool bStorageOk = false;
70 : try
71 : {
72 0 : SotStorageRef aStorage = new SotStorage( pInStrm, false );
73 0 : bStorageOk = !aStorage->GetError();
74 0 : 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 0 : }
83 : }
84 0 : catch (const css::ucb::ContentCreationException &e)
85 : {
86 : SAL_WARN("starmath", "SmFilterDetect::detect caught " << e.Message);
87 : }
88 :
89 0 : if (!bStorageOk)
90 : {
91 : // 200 should be enough for the XML
92 : // version, encoding and !DOCTYPE
93 : // stuff I hope?
94 0 : const sal_uInt16 nBufferSize = 200;
95 : char aBuffer[nBufferSize+1];
96 0 : aBuffer[nBufferSize] = 0;
97 0 : pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
98 0 : pInStrm->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW ); // avoid BOM marker
99 0 : sal_uLong nBytesRead = pInStrm->Read( aBuffer, nBufferSize );
100 0 : if (nBytesRead >= 6)
101 : {
102 0 : bool bIsMathType = false;
103 0 : 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 0 : bIsMathType = (0 == strncmp( "<math ", aBuffer, 6) ||
110 0 : 0 == strncmp( "<math> ", aBuffer, 7) ||
111 0 : 0 == strncmp( "<math:math> ", aBuffer, 12));
112 :
113 0 : if ( bIsMathType )
114 0 : return OUString("math_MathML_XML_Math");
115 : }
116 : }
117 :
118 0 : 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 0 : Sequence< OUString > SmFilterDetect::impl_getStaticSupportedServiceNames()
141 : {
142 0 : Sequence< OUString > seqServiceNames( 1 );
143 0 : seqServiceNames.getArray() [0] = "com.sun.star.frame.ExtendedTypeDetection";
144 0 : return seqServiceNames ;
145 : }
146 :
147 : /* Helper for XServiceInfo */
148 0 : OUString SmFilterDetect::impl_getStaticImplementationName()
149 : {
150 0 : return OUString("com.sun.star.comp.math.FormatDetector");
151 : }
152 :
153 : /* Helper for registry */
154 0 : Reference< XInterface > SAL_CALL SmFilterDetect::impl_createInstance( const Reference< XMultiServiceFactory >& xServiceManager ) throw( Exception )
155 : {
156 0 : return Reference< XInterface >( *new SmFilterDetect( xServiceManager ) );
157 : }
158 :
159 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|