1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_filter.hxx"
34 #include "filterdetect.hxx"
35 #include <osl/diagnose.h>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/io/XActiveDataSource.hpp>
38 #include <com/sun/star/io/XOutputStream.hpp>
39 #include <com/sun/star/io/XInputStream.hpp>
40 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
41 #include <com/sun/star/xml/sax/InputSource.hpp>
42 #include <com/sun/star/xml/sax/XParser.hpp>
43 #include <com/sun/star/xml/XImportFilter.hpp>
44 #include <com/sun/star/xml/XExportFilter.hpp>
45 #include <com/sun/star/frame/XModel.hpp>
46 #include <com/sun/star/frame/XController.hpp>
47 #include <com/sun/star/task/XStatusIndicator.hpp>
48 #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
49 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
50 #include <com/sun/star/style/XStyleLoader.hpp>
51 #include <com/sun/star/io/XInputStream.hpp>
52 #include <com/sun/star/document/XExtendedFilterDetection.hpp>
53 #include <com/sun/star/container/XNameAccess.hpp>
54 #include <com/sun/star/beans/PropertyState.hpp>
56 #include <ucbhelper/content.hxx>
57 #include <ucbhelper/contentbroker.hxx>
58 #include <ucbhelper/commandenvironment.hxx>
59 #include <unotools/ucbhelper.hxx>
60 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
65 using com::sun::star::uno::Sequence
;
66 using com::sun::star::uno::Reference
;
67 using com::sun::star::uno::Any
;
68 using com::sun::star::uno::UNO_QUERY
;
69 using com::sun::star::uno::XInterface
;
70 using com::sun::star::uno::Exception
;
71 using com::sun::star::uno::RuntimeException
;
72 using com::sun::star::lang::XMultiServiceFactory
;
73 using com::sun::star::io::XActiveDataSource
;
74 using com::sun::star::io::XOutputStream
;
75 using com::sun::star::beans::PropertyValue
;
76 using com::sun::star::document::XExporter
;
77 using com::sun::star::document::XFilter
;
78 using com::sun::star::document::XExtendedFilterDetection
;
80 using com::sun::star::io::XInputStream
;
81 using com::sun::star::document::XImporter
;
82 using com::sun::star::xml::sax::InputSource
;
83 using com::sun::star::xml::sax::XDocumentHandler
;
84 using com::sun::star::xml::sax::XParser
;
85 using com::sun::star::task::XInteractionHandler
;
87 using namespace ::com::sun::star::frame
;
88 using namespace ::com::sun::star
;
89 using namespace com::sun::star::container
;
90 using namespace com::sun::star::uno
;
91 using namespace com::sun::star::beans
;
94 Reference
< com::sun::star::frame::XModel
> xModel
;
96 ::rtl::OUString SAL_CALL
supportedByType( const ::rtl::OUString clipBoardFormat
, const ::rtl::OString resultString
, const ::rtl::OUString checkType
);
99 ::rtl::OUString SAL_CALL
FilterDetect::detect( com::sun::star::uno::Sequence
< com::sun::star::beans::PropertyValue
>& aArguments
) throw( com::sun::star::uno::RuntimeException
)
101 ::rtl::OUString sTypeName
= OUString::createFromAscii("");
102 ::rtl::OUString sUrl
= OUString::createFromAscii("");
103 ::rtl::OUString originalTypeName
;
104 Sequence
<PropertyValue
> lProps
;
106 com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> xInStream
;
107 ::rtl::OUString temp
;
108 //OSL_ENSURE( sal_False, " starting Detect" );
109 const PropertyValue
* pValue
= aArguments
.getConstArray();
111 ::rtl::OString resultString
;
113 nLength
= aArguments
.getLength();
114 sal_Int32 location
=nLength
;
115 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
117 //OSL_ENSURE( sal_False, ::rtl::OUStringToOString(pValue[i].Name,RTL_TEXTENCODING_ASCII_US).getStr() );
118 if ( pValue
[i
].Name
.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "TypeName" ) ) )
120 //pValue[i].Value >>= originalTypeName;
122 // OSL_ENSURE( sal_False, ::rtl::OUStringToOString(sTypeName,RTL_TEXTENCODING_ASCII_US).getStr() );
125 else if ( pValue
[i
].Name
.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "URL" ) ) )
128 pValue
[i
].Value
>>= sUrl
;
129 //OSL_ENSURE( sal_False, ::rtl::OUStringToOString(sUrl,RTL_TEXTENCODING_ASCII_US).getStr() );
132 else if ( pValue
[i
].Name
.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "InputStream" ) ) )
134 pValue
[i
].Value
>>= xInStream
;
140 Reference
< com::sun::star::ucb::XCommandEnvironment
> xEnv
;
143 ::ucbhelper::Content
aContent(sUrl
,xEnv
);
144 xInStream
= aContent
.openStream();
150 com::sun::star::uno::Sequence
< sal_Int8
> aData
;
151 /* long nBytesToRead= */ xInStream
->available();
152 xInStream
->skipBytes (0);
153 long bytestRead
=xInStream
->readBytes (aData
, 4000);
154 resultString
=::rtl::OString((const sal_Char
*)aData
.getConstArray(),bytestRead
) ;
157 // test typedetect code
158 Reference
<XNameAccess
> xTypeCont(mxMSF
->createInstance(OUString::createFromAscii("com.sun.star.document.TypeDetection")),UNO_QUERY
);
159 Sequence
< ::rtl::OUString
> myTypes
= xTypeCont
->getElementNames();
160 nLength
= myTypes
.getLength();
163 sal_Int32 new_nlength
=0;
165 while( (i
< nLength
) && (sTypeName
.equalsAscii("")))
168 Any elem
= xTypeCont
->getByName(myTypes
[i
]);
170 new_nlength
= lProps
.getLength();
172 while( j
< new_nlength
&& sTypeName
.equalsAscii(""))
174 ::rtl::OUString tmpStr
=OUString::createFromAscii("");
175 lProps
[j
].Value
>>=tmpStr
;
176 if((lProps
[j
].Name
.equalsAscii("ClipboardFormat")) && (!tmpStr
.equalsAscii("")) )
178 sTypeName
= supportedByType(tmpStr
,resultString
, myTypes
[i
]);
189 OSL_ENSURE( sal_False
, "An Exception occured while opening File stream" );
191 if(sTypeName
.equalsAscii(""))
193 //sTypeName=::rtl::OUString::createFromAscii("writer_Flat_XML_File");
197 if ( location
== aArguments
.getLength() )
199 aArguments
.realloc(nLength
+1);
200 aArguments
[location
].Name
= ::rtl::OUString::createFromAscii( "TypeName" );
202 aArguments
[location
].Value
<<=sTypeName
;
204 // OSL_ENSURE( sal_False, ::rtl::OUStringToOString(sTypeName,RTL_TEXTENCODING_ASCII_US).getStr() );
212 ::rtl::OUString SAL_CALL
supportedByType( const ::rtl::OUString clipBoardFormat
, const ::rtl::OString resultString
, const ::rtl::OUString checkType
)
215 ::rtl::OUString sTypeName
= OUString::createFromAscii("");
216 if((clipBoardFormat
.match(OUString::createFromAscii("doctype:"))))
218 ::rtl::OString tryStr
= ::rtl::OUStringToOString(clipBoardFormat
.copy(8),RTL_TEXTENCODING_ASCII_US
).getStr();
219 // OSL_ENSURE( sal_False, tryStr);
220 if (resultString
.indexOf(tryStr
) >= 0)
222 sTypeName
= checkType
;
230 void SAL_CALL
FilterDetect::initialize( const Sequence
< Any
>& aArguments
)
231 throw (Exception
, RuntimeException
)
233 Sequence
< PropertyValue
> aAnySeq
;
234 sal_Int32 nLength
= aArguments
.getLength();
235 if ( nLength
&& ( aArguments
[0] >>= aAnySeq
) )
237 const PropertyValue
* pValue
= aAnySeq
.getConstArray();
238 nLength
= aAnySeq
.getLength();
239 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
242 if ( pValue
[i
].Name
.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Type" ) ) )
244 pValue
[i
].Value
>>= msFilterName
;
247 else if ( pValue
[i
].Name
.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "UserData" ) ) )
250 pValue
[i
].Value
>>= msUserData
;
253 else if ( pValue
[i
].Name
.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "TemplateName" ) ) )
256 pValue
[i
].Value
>>=msTemplateName
;
265 OUString
FilterDetect_getImplementationName ()
266 throw (RuntimeException
)
268 return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.filters.XMLFilterDetect" ) );
270 #define SERVICE_NAME1 "com.sun.star.document.ExtendedTypeDetection"
272 sal_Bool SAL_CALL
FilterDetect_supportsService( const OUString
& ServiceName
)
273 throw (RuntimeException
)
275 return ServiceName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1
) );
277 Sequence
< OUString
> SAL_CALL
FilterDetect_getSupportedServiceNames( )
278 throw (RuntimeException
)
280 Sequence
< OUString
> aRet(2);
281 OUString
* pArray
= aRet
.getArray();
282 pArray
[0] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1
) );
288 Reference
< XInterface
> SAL_CALL
FilterDetect_createInstance( const Reference
< XMultiServiceFactory
> & rSMgr
)
291 return (cppu::OWeakObject
*) new FilterDetect( rSMgr
);
295 OUString SAL_CALL
FilterDetect::getImplementationName( )
296 throw (RuntimeException
)
298 return FilterDetect_getImplementationName();
300 sal_Bool SAL_CALL
FilterDetect::supportsService( const OUString
& rServiceName
)
301 throw (RuntimeException
)
303 return FilterDetect_supportsService( rServiceName
);
305 Sequence
< OUString
> SAL_CALL
FilterDetect::getSupportedServiceNames( )
306 throw (RuntimeException
)
308 return FilterDetect_getSupportedServiceNames();