update credits
[LibreOffice.git] / writerfilter / source / filter / WriterFilterDetection.cxx
blob926c7c0f423f7fc3796addc3bc59ed41d59a1c3f
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 <cppuhelper/implementationentry.hxx>
21 #include <WriterFilterDetection.hxx>
22 #include <comphelper/storagehelper.hxx>
23 #include <com/sun/star/io/XInputStream.hpp>
24 #include <sot/storage.hxx>
25 #include <unotools/ucbstreamhelper.hxx>
27 using namespace ::rtl;
28 using namespace ::cppu;
29 using namespace ::com::sun::star;
33 WriterFilterDetection::WriterFilterDetection(
34 const uno::Reference< uno::XComponentContext >& rxContext) :
35 m_xContext( rxContext )
40 WriterFilterDetection::~WriterFilterDetection()
45 OUString WriterFilterDetection_getImplementationName () throw (uno::RuntimeException)
47 return OUString ( "com.sun.star.comp.Writer.WriterFilterDetector" );
50 #define SERVICE_NAME1 "com.sun.star.document.ExtendedTypeDetection"
53 OUString WriterFilterDetection::detect( uno::Sequence< beans::PropertyValue >& rDescriptor )
54 throw( uno::RuntimeException )
56 OUString sTypeName;
57 bool bWord = false;
58 sal_Int32 nPropertyCount = rDescriptor.getLength();
59 const beans::PropertyValue* pValues = rDescriptor.getConstArray();
60 OUString sURL;
61 uno::Reference < io::XStream > xStream;
62 uno::Reference < io::XInputStream > xInputStream;
63 for( sal_Int32 nProperty = 0; nProperty < nPropertyCount; ++nProperty )
65 if ( pValues[nProperty].Name == "TypeName" )
66 rDescriptor[nProperty].Value >>= sTypeName;
67 else if ( pValues[nProperty].Name == "URL" )
68 pValues[nProperty].Value >>= sURL;
69 else if ( pValues[nProperty].Name == "Stream" )
70 pValues[nProperty].Value >>= xStream;
71 else if ( pValues[nProperty].Name == "InputStream" )
72 pValues[nProperty].Value >>= xInputStream;
74 bool bBinary = sTypeName == "writer_MS_Word_97" ||
75 sTypeName == "writer_MS_Word_97_Vorlage";
77 try
79 if(bBinary)
81 SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( xInputStream );
82 if ( pStream && SotStorage::IsStorageFile(pStream) )
85 SotStorageRef xStg = new SotStorage( pStream, sal_False );
87 bool bTable2 = xStg->IsContained(OUString("1Table"));
88 SotStorageStreamRef xRef = xStg->OpenSotStream(OUString("WordDocument"), STREAM_STD_READ | STREAM_NOCREATE );
90 if(bTable2 && xStg.Is())
92 xRef->Seek(2);
93 sal_Int16 nWord;
94 *xRef >> nWord;
95 //version detection
96 bWord = nWord >= 0x6a && nWord <= 0xc1;
100 else
102 uno::Reference< embed::XStorage > xDocStorage;
103 if ( sURL == "private:stream" )
104 xDocStorage = comphelper::OStorageHelper::GetStorageFromInputStream( xInputStream );
105 else
106 xDocStorage = comphelper::OStorageHelper::GetStorageFromURL(
107 sURL, embed::ElementModes::READ );
108 if( xDocStorage.is() )
110 uno::Sequence< OUString > aNames = xDocStorage->getElementNames();
111 const OUString* pNames = aNames.getConstArray();
112 for(sal_Int32 nName = 0; nName < aNames.getLength(); ++nName)
114 if ( pNames[nName] == "word" )
116 bWord = true;
117 if( sTypeName.isEmpty() )
118 sTypeName = "writer_MS_Word_2007";
119 break;
125 catch(const uno::Exception&)
127 OSL_FAIL("exception while opening storage");
129 if( !bWord )
130 sTypeName = OUString();
131 return sTypeName;
135 sal_Bool WriterFilterDetection_supportsService( const OUString& ServiceName ) throw (uno::RuntimeException)
137 return ServiceName == SERVICE_NAME1;
141 uno::Sequence< OUString > WriterFilterDetection_getSupportedServiceNames( ) throw (uno::RuntimeException)
143 uno::Sequence < OUString > aRet(1);
144 OUString* pArray = aRet.getArray();
145 pArray[0] = OUString ( SERVICE_NAME1 );
146 return aRet;
148 #undef SERVICE_NAME1
151 uno::Reference< uno::XInterface > WriterFilterDetection_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
152 throw( uno::Exception )
154 return (cppu::OWeakObject*) new WriterFilterDetection( xContext );
158 OUString WriterFilterDetection::getImplementationName( ) throw (uno::RuntimeException)
160 return WriterFilterDetection_getImplementationName();
164 sal_Bool WriterFilterDetection::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException)
166 return WriterFilterDetection_supportsService( rServiceName );
170 uno::Sequence< OUString > WriterFilterDetection::getSupportedServiceNames( ) throw (uno::RuntimeException)
172 return WriterFilterDetection_getSupportedServiceNames();
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */