summaryrefslogtreecommitdiff
path: root/starmath/source/smdetect.cxx
blob: 51a8f42f666bac7b2a702e9094e54873654a8614 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "smdetect.hxx"
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <cppuhelper/supportsservice.hxx>
#include <com/sun/star/io/XInputStream.hpp>
#include <sfx2/docfile.hxx>
#include <unotools/mediadescriptor.hxx>

#include "eqnolefilehdr.hxx"

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::io;
using namespace ::com::sun::star::task;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::lang;
using utl::MediaDescriptor;

SmFilterDetect::SmFilterDetect( const Reference < XMultiServiceFactory >& /*xFactory*/ )
{
}

SmFilterDetect::~SmFilterDetect()
{
}

OUString SAL_CALL SmFilterDetect::detect( Sequence< PropertyValue >& lDescriptor ) throw( RuntimeException, std::exception )
{
    MediaDescriptor aMediaDesc( lDescriptor );
    uno::Reference< io::XInputStream > xInStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY );
    if ( !xInStream.is() )
        return OUString();

    SfxMedium aMedium;
    aMedium.UseInteractionHandler( false );
    aMedium.setStreamToLoadFrom( xInStream, true );

    SvStream *pInStrm = aMedium.GetInStream();
    if ( !pInStrm || pInStrm->GetError() )
        return OUString();

    // Do not attempt to create an SotStorage on a
    // 0-length stream as that would create the compound
    // document header on the stream and effectively write to
    // disk!
    pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
    if ( pInStrm->remainingSize() == 0 )
        return OUString();

    SotStorageRef aStorage = new SotStorage( pInStrm, false );
    if ( !aStorage->GetError() )
    {
        if ( aStorage->IsStream("Equation Native") )
        {
            sal_uInt8 nVersion;
            if ( GetMathTypeVersion( aStorage, nVersion ) && nVersion <=3 )
                return OUString("math_MathType_3x");
        }
    }
    else
    {
        // 200 should be enough for the XML
        // version, encoding and !DOCTYPE
        // stuff I hope?
        const sal_uInt16 nBufferSize = 200;
        char aBuffer[nBufferSize+1];
        aBuffer[nBufferSize] = 0;
        pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
        pInStrm->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW ); // avoid BOM marker
        sal_uLong nBytesRead = pInStrm->Read( aBuffer, nBufferSize );
        if (nBytesRead >= 6)
        {
            bool bIsMathType = false;
            if (0 == strncmp( "<?xml", aBuffer, 5))
                bIsMathType = (strstr( aBuffer, "<math>" ) ||
                               strstr( aBuffer, "<math " ) ||
                               strstr( aBuffer, "<math:math " ));
            else
                // this is the old <math tag to MathML in the beginning of the XML file
                bIsMathType = (0 == strncmp( "<math ", aBuffer, 6) ||
                               0 == strncmp( "<math> ", aBuffer, 7) ||
                               0 == strncmp( "<math:math> ", aBuffer, 12));

            if ( bIsMathType )
                return OUString("math_MathML_XML_Math");
        }
    }

    return OUString();
}

/* XServiceInfo */
OUString SAL_CALL SmFilterDetect::getImplementationName() throw( RuntimeException, std::exception )
{
    return impl_getStaticImplementationName();
}

/* XServiceInfo */
sal_Bool SAL_CALL SmFilterDetect::supportsService( const OUString& sServiceName ) throw( RuntimeException, std::exception )
{
    return cppu::supportsService(this, sServiceName);
}

/* XServiceInfo */
Sequence< OUString > SAL_CALL SmFilterDetect::getSupportedServiceNames() throw( RuntimeException, std::exception )
{
    return impl_getStaticSupportedServiceNames();
}

/* Helper for XServiceInfo */
Sequence< OUString > SmFilterDetect::impl_getStaticSupportedServiceNames()
{
    Sequence< OUString > seqServiceNames( 1 );
    seqServiceNames.getArray() [0] = "com.sun.star.frame.ExtendedTypeDetection";
    return seqServiceNames ;
}

/* Helper for XServiceInfo */
OUString SmFilterDetect::impl_getStaticImplementationName()
{
    return OUString("com.sun.star.comp.math.FormatDetector");
}

/* Helper for registry */
Reference< XInterface > SAL_CALL SmFilterDetect::impl_createInstance( const Reference< XMultiServiceFactory >& xServiceManager ) throw( Exception )
{
    return Reference< XInterface >( *new SmFilterDetect( xServiceManager ) );
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */