1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WriterFilterDetection.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <cppuhelper/implementationentry.hxx>
32 #include <WriterFilterDetection.hxx>
33 #include <comphelper/storagehelper.hxx>
34 #include <com/sun/star/io/XInputStream.hpp>
35 #include <sot/storage.hxx>
36 //#ifndef _SFXDOCFILE_HXX //todo: remove sfx2!
37 //#include <sfx2/docfile.hxx>
39 #include <unotools/ucbstreamhelper.hxx>
41 using namespace ::rtl
;
42 using namespace ::cppu
;
43 using namespace ::com::sun::star
;
45 /*-- 22.02.2007 12:17:53---------------------------------------------------
47 -----------------------------------------------------------------------*/
48 WriterFilterDetection::WriterFilterDetection(
49 const uno::Reference
< uno::XComponentContext
>& rxContext
) :
50 m_xContext( rxContext
)
53 /*-- 22.02.2007 12:17:53---------------------------------------------------
55 -----------------------------------------------------------------------*/
56 WriterFilterDetection::~WriterFilterDetection()
59 /*-- 22.02.2007 12:11:38---------------------------------------------------
61 -----------------------------------------------------------------------*/
62 OUString
WriterFilterDetection_getImplementationName () throw (uno::RuntimeException
)
64 return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.WriterFilterDetector" ) );
67 #define SERVICE_NAME1 "com.sun.star.document.ExtendedTypeDetection"
68 /*-- 22.02.2007 12:11:38---------------------------------------------------
70 -----------------------------------------------------------------------*/
71 OUString
WriterFilterDetection::detect( uno::Sequence
< beans::PropertyValue
>& rDescriptor
)
72 throw( uno::RuntimeException
)
76 sal_Int32 nPropertyCount
= rDescriptor
.getLength();
77 const beans::PropertyValue
* pValues
= rDescriptor
.getConstArray();
79 uno::Reference
< io::XStream
> xStream
;
80 uno::Reference
< io::XInputStream
> xInputStream
;
81 for( sal_Int32 nProperty
= 0; nProperty
< nPropertyCount
; ++nProperty
)
83 if( pValues
[nProperty
].Name
== OUString(RTL_CONSTASCII_USTRINGPARAM("TypeName")) )
84 rDescriptor
[nProperty
].Value
>>= sTypeName
;
85 else if( pValues
[nProperty
].Name
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM ( "URL" )) )
86 pValues
[nProperty
].Value
>>= sURL
;
87 else if( pValues
[nProperty
].Name
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM ( "Stream" )) )
88 pValues
[nProperty
].Value
>>= xStream
;
89 else if( pValues
[nProperty
].Name
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM ( "InputStream" )) )
90 pValues
[nProperty
].Value
>>= xInputStream
;
92 bool bBinary
= sTypeName
.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "writer_MS_Word_97" )) ||
93 sTypeName
.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "writer_MS_Word_97_Vorlage" ));
99 SvStream
* pStream
= ::utl::UcbStreamHelper::CreateStream( xInputStream
);
100 if ( pStream
&& SotStorage::IsStorageFile(pStream
) )
103 SotStorageRef xStg
= new SotStorage( pStream
, FALSE
);
105 bool bTable2
= xStg
->IsContained( rtl::OUString::createFromAscii("1Table" ));
106 SotStorageStreamRef xRef
=
108 xStg
->OpenSotStream(rtl::OUString::createFromAscii("WordDocument"),
110 STREAM_STD_READ
| STREAM_NOCREATE
);
112 if(bTable2
&& xStg
.Is())
118 bWord
= nWord
>= 0x6a && nWord
<= 0xc1;
124 uno::Reference
< embed::XStorage
> xDocStorage
;
125 if( sURL
.equalsAscii( "private:stream" ) )
126 xDocStorage
= comphelper::OStorageHelper::GetStorageFromInputStream( xInputStream
);
128 xDocStorage
= comphelper::OStorageHelper::GetStorageFromURL(
129 sURL
, embed::ElementModes::READ
);
130 if( xDocStorage
.is() )
132 uno::Sequence
< ::rtl::OUString
> aNames
= xDocStorage
->getElementNames();
133 const ::rtl::OUString
* pNames
= aNames
.getConstArray();
134 for(sal_Int32 nName
= 0; nName
< aNames
.getLength(); ++nName
)
136 if(pNames
[nName
].equalsAsciiL(RTL_CONSTASCII_STRINGPARAM ( "word" )))
139 if( !sTypeName
.getLength() )
140 sTypeName
= ::rtl::OUString(
141 RTL_CONSTASCII_STRINGPARAM( "writer_MS_Word_2007" ), RTL_TEXTENCODING_ASCII_US
);
148 catch(const uno::Exception
&)
150 OSL_ASSERT("exception while opening storage");
153 sTypeName
= ::rtl::OUString();
156 /*-- 22.02.2007 12:11:38---------------------------------------------------
158 -----------------------------------------------------------------------*/
159 sal_Bool
WriterFilterDetection_supportsService( const OUString
& ServiceName
) throw (uno::RuntimeException
)
161 return (ServiceName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1
) ) );
163 /*-- 22.02.2007 12:11:38---------------------------------------------------
165 -----------------------------------------------------------------------*/
166 uno::Sequence
< OUString
> WriterFilterDetection_getSupportedServiceNames( ) throw (uno::RuntimeException
)
168 uno::Sequence
< OUString
> aRet(1);
169 OUString
* pArray
= aRet
.getArray();
170 pArray
[0] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1
) );
174 /*-- 22.02.2007 12:11:38---------------------------------------------------
176 -----------------------------------------------------------------------*/
177 uno::Reference
< uno::XInterface
> WriterFilterDetection_createInstance( const uno::Reference
< uno::XComponentContext
>& xContext
)
178 throw( uno::Exception
)
180 return (cppu::OWeakObject
*) new WriterFilterDetection( xContext
);
182 /*-- 22.02.2007 12:11:38---------------------------------------------------
184 -----------------------------------------------------------------------*/
185 OUString
WriterFilterDetection::getImplementationName( ) throw (uno::RuntimeException
)
187 return WriterFilterDetection_getImplementationName();
189 /*-- 22.02.2007 12:11:38---------------------------------------------------
191 -----------------------------------------------------------------------*/
192 sal_Bool
WriterFilterDetection::supportsService( const OUString
& rServiceName
) throw (uno::RuntimeException
)
194 return WriterFilterDetection_supportsService( rServiceName
);
196 /*-- 22.02.2007 12:11:38---------------------------------------------------
198 -----------------------------------------------------------------------*/
199 uno::Sequence
< OUString
> WriterFilterDetection::getSupportedServiceNames( ) throw (uno::RuntimeException
)
201 return WriterFilterDetection_getSupportedServiceNames();