19e0fc027SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
39e0fc027SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
49e0fc027SAndrew Rist * or more contributor license agreements. See the NOTICE file
59e0fc027SAndrew Rist * distributed with this work for additional information
69e0fc027SAndrew Rist * regarding copyright ownership. The ASF licenses this file
79e0fc027SAndrew Rist * to you under the Apache License, Version 2.0 (the
89e0fc027SAndrew Rist * "License"); you may not use this file except in compliance
99e0fc027SAndrew Rist * with the License. You may obtain a copy of the License at
109e0fc027SAndrew Rist *
119e0fc027SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
129e0fc027SAndrew Rist *
139e0fc027SAndrew Rist * Unless required by applicable law or agreed to in writing,
149e0fc027SAndrew Rist * software distributed under the License is distributed on an
159e0fc027SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169e0fc027SAndrew Rist * KIND, either express or implied. See the License for the
179e0fc027SAndrew Rist * specific language governing permissions and limitations
189e0fc027SAndrew Rist * under the License.
199e0fc027SAndrew Rist *
209e0fc027SAndrew Rist *************************************************************/
219e0fc027SAndrew Rist
229e0fc027SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_filter.hxx"
26cdf0e10cSrcweir #include <iostream>
27cdf0e10cSrcweir #include <stdlib.h>
28cdf0e10cSrcweir #include <ctype.h>
29cdf0e10cSrcweir #include <stdio.h>
30cdf0e10cSrcweir #include "filterdetect.hxx"
31cdf0e10cSrcweir #include <osl/diagnose.h>
32cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
34cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
35cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
36cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
37cdf0e10cSrcweir #include <com/sun/star/xml/sax/InputSource.hpp>
38cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
39cdf0e10cSrcweir #include <com/sun/star/xml/XImportFilter.hpp>
40cdf0e10cSrcweir #include <com/sun/star/xml/XExportFilter.hpp>
41cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
42cdf0e10cSrcweir #include <com/sun/star/frame/XController.hpp>
43cdf0e10cSrcweir #include <com/sun/star/task/XStatusIndicator.hpp>
44cdf0e10cSrcweir #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
45cdf0e10cSrcweir #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
46cdf0e10cSrcweir #include <com/sun/star/style/XStyleLoader.hpp>
47cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
48cdf0e10cSrcweir #include <com/sun/star/document/XExtendedFilterDetection.hpp>
49cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
50cdf0e10cSrcweir #include <com/sun/star/beans/PropertyState.hpp>
51cdf0e10cSrcweir
52cdf0e10cSrcweir #include <ucbhelper/content.hxx>
53cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
54cdf0e10cSrcweir #include <ucbhelper/commandenvironment.hxx>
55cdf0e10cSrcweir #include <unotools/ucbhelper.hxx>
56cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandEnvironment.hpp>
57cdf0e10cSrcweir
581c11026dSHongyun An //UOF v2 deep type detection
59d3069caaSHongyun An #include "../xsltfilter/uof2storage.cxx"
60d3069caaSHongyun An #include <rtl/string.hxx>
611c11026dSHongyun An
62cdf0e10cSrcweir
63cdf0e10cSrcweir using rtl::OUString;
64cdf0e10cSrcweir using com::sun::star::uno::Sequence;
65cdf0e10cSrcweir using com::sun::star::uno::Reference;
66cdf0e10cSrcweir using com::sun::star::uno::Any;
67cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
68cdf0e10cSrcweir using com::sun::star::uno::XInterface;
69cdf0e10cSrcweir using com::sun::star::uno::Exception;
70cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
71cdf0e10cSrcweir using com::sun::star::lang::XMultiServiceFactory;
72cdf0e10cSrcweir using com::sun::star::io::XActiveDataSource;
73cdf0e10cSrcweir using com::sun::star::io::XOutputStream;
74cdf0e10cSrcweir using com::sun::star::beans::PropertyValue;
75cdf0e10cSrcweir using com::sun::star::document::XExporter;
76cdf0e10cSrcweir using com::sun::star::document::XFilter;
77cdf0e10cSrcweir using com::sun::star::document::XExtendedFilterDetection;
78cdf0e10cSrcweir
79cdf0e10cSrcweir using com::sun::star::io::XInputStream;
80cdf0e10cSrcweir using com::sun::star::document::XImporter;
81cdf0e10cSrcweir using com::sun::star::xml::sax::InputSource;
82cdf0e10cSrcweir using com::sun::star::xml::sax::XDocumentHandler;
83cdf0e10cSrcweir using com::sun::star::xml::sax::XParser;
84cdf0e10cSrcweir using com::sun::star::task::XInteractionHandler;
85cdf0e10cSrcweir
86cdf0e10cSrcweir using namespace ::com::sun::star::frame;
87cdf0e10cSrcweir using namespace ::com::sun::star;
88cdf0e10cSrcweir using namespace com::sun::star::container;
89cdf0e10cSrcweir using namespace com::sun::star::uno;
90cdf0e10cSrcweir using namespace com::sun::star::beans;
91cdf0e10cSrcweir
92cdf0e10cSrcweir
93cdf0e10cSrcweir Reference< com::sun::star::frame::XModel > xModel;
94cdf0e10cSrcweir
95cdf0e10cSrcweir ::rtl::OUString SAL_CALL supportedByType( const ::rtl::OUString clipBoardFormat , const ::rtl::OString resultString, const ::rtl::OUString checkType);
96cdf0e10cSrcweir
97cdf0e10cSrcweir
detect(com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> & aArguments)98cdf0e10cSrcweir ::rtl::OUString SAL_CALL FilterDetect::detect( com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >& aArguments ) throw( com::sun::star::uno::RuntimeException )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir ::rtl::OUString sTypeName = OUString::createFromAscii("");
101cdf0e10cSrcweir ::rtl::OUString sUrl = OUString::createFromAscii("");
102cdf0e10cSrcweir ::rtl::OUString originalTypeName;
103cdf0e10cSrcweir Sequence<PropertyValue > lProps ;
104cdf0e10cSrcweir
105cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInStream;
1061c11026dSHongyun An com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xIn;
107cdf0e10cSrcweir ::rtl::OUString temp;
108cdf0e10cSrcweir //OSL_ENSURE( sal_False, " starting Detect" );
109cdf0e10cSrcweir const PropertyValue * pValue = aArguments.getConstArray();
110cdf0e10cSrcweir sal_Int32 nLength;
111cdf0e10cSrcweir ::rtl::OString resultString;
112cdf0e10cSrcweir
113cdf0e10cSrcweir nLength = aArguments.getLength();
114cdf0e10cSrcweir sal_Int32 location=nLength;
115cdf0e10cSrcweir for ( sal_Int32 i = 0 ; i < nLength; i++)
116cdf0e10cSrcweir {
117cdf0e10cSrcweir //OSL_ENSURE( sal_False, ::rtl::OUStringToOString(pValue[i].Name,RTL_TEXTENCODING_ASCII_US).getStr() );
118cdf0e10cSrcweir if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "TypeName" ) ) )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir //pValue[i].Value >>= originalTypeName;
121cdf0e10cSrcweir location=i;
122cdf0e10cSrcweir // OSL_ENSURE( sal_False, ::rtl::OUStringToOString(sTypeName,RTL_TEXTENCODING_ASCII_US).getStr() );
123cdf0e10cSrcweir
124cdf0e10cSrcweir }
125cdf0e10cSrcweir else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "URL" ) ) )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir
128cdf0e10cSrcweir pValue[i].Value >>= sUrl;
129cdf0e10cSrcweir //OSL_ENSURE( sal_False, ::rtl::OUStringToOString(sUrl,RTL_TEXTENCODING_ASCII_US).getStr() );
130cdf0e10cSrcweir
131cdf0e10cSrcweir }
132cdf0e10cSrcweir else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "InputStream" ) ) )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir pValue[i].Value >>= xInStream ;
1351c11026dSHongyun An pValue[i].Value >>= xIn ;
136cdf0e10cSrcweir }
137cdf0e10cSrcweir
138cdf0e10cSrcweir }
139cdf0e10cSrcweir try{
140cdf0e10cSrcweir Reference< com::sun::star::ucb::XCommandEnvironment > xEnv;
141cdf0e10cSrcweir if (!xInStream.is())
142cdf0e10cSrcweir {
143cdf0e10cSrcweir ::ucbhelper::Content aContent(sUrl,xEnv);
144cdf0e10cSrcweir xInStream = aContent.openStream();
145cdf0e10cSrcweir if (!xInStream.is())
146cdf0e10cSrcweir {
147cdf0e10cSrcweir return sTypeName;
148cdf0e10cSrcweir }
149cdf0e10cSrcweir }
150cdf0e10cSrcweir com::sun::star::uno::Sequence< sal_Int8 > aData;
151cdf0e10cSrcweir /* long nBytesToRead= */ xInStream->available();
152cdf0e10cSrcweir xInStream->skipBytes (0);
153cdf0e10cSrcweir long bytestRead =xInStream->readBytes (aData, 4000);
154cdf0e10cSrcweir resultString=::rtl::OString((const sal_Char *)aData.getConstArray(),bytestRead) ;
155cdf0e10cSrcweir
156cdf0e10cSrcweir
157cdf0e10cSrcweir // test typedetect code
158cdf0e10cSrcweir Reference <XNameAccess> xTypeCont(mxMSF->createInstance(OUString::createFromAscii("com.sun.star.document.TypeDetection")),UNO_QUERY);
159cdf0e10cSrcweir Sequence < ::rtl::OUString > myTypes= xTypeCont->getElementNames();
160cdf0e10cSrcweir nLength = myTypes.getLength();
161cdf0e10cSrcweir
162cdf0e10cSrcweir
163cdf0e10cSrcweir sal_Int32 new_nlength=0;
164cdf0e10cSrcweir sal_Int32 i = 0 ;
165cdf0e10cSrcweir while( (i < nLength) && (sTypeName.equalsAscii("")))
166cdf0e10cSrcweir {
167cdf0e10cSrcweir
168cdf0e10cSrcweir Any elem = xTypeCont->getByName(myTypes[i]);
169cdf0e10cSrcweir elem >>=lProps;
170cdf0e10cSrcweir new_nlength = lProps.getLength();
171cdf0e10cSrcweir sal_Int32 j =0;
172cdf0e10cSrcweir while( j < new_nlength && sTypeName.equalsAscii(""))
173cdf0e10cSrcweir {
174cdf0e10cSrcweir ::rtl::OUString tmpStr =OUString::createFromAscii("");
175cdf0e10cSrcweir lProps[j].Value >>=tmpStr;
176cdf0e10cSrcweir if((lProps[j].Name.equalsAscii("ClipboardFormat")) && (!tmpStr.equalsAscii("")) )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir sTypeName = supportedByType(tmpStr,resultString, myTypes[i]);
179cdf0e10cSrcweir }
180cdf0e10cSrcweir j++;
181cdf0e10cSrcweir }
182cdf0e10cSrcweir i++;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir //end test
185cdf0e10cSrcweir
186cdf0e10cSrcweir }
187cdf0e10cSrcweir catch(Exception &)
188cdf0e10cSrcweir {
189*07a3d7f1SPedro Giffuni OSL_ENSURE( sal_False, "An Exception occurred while opening File stream" );
190cdf0e10cSrcweir }
191cdf0e10cSrcweir if(sTypeName.equalsAscii(""))
192cdf0e10cSrcweir {
193cdf0e10cSrcweir //sTypeName=::rtl::OUString::createFromAscii("writer_Flat_XML_File");
1941c11026dSHongyun An //UOF v2.0 deep type detection
195d3069caaSHongyun An if(sUrl.indexOf( ::rtl::OUString::createFromAscii(".uot") ) != -1 ||
196d3069caaSHongyun An sUrl.indexOf( ::rtl::OUString::createFromAscii(".uos") ) != -1 ||
197d3069caaSHongyun An sUrl.indexOf( ::rtl::OUString::createFromAscii(".uop") ) != -1)
198d3069caaSHongyun An {
199d3069caaSHongyun An if(xIn.is())
200d3069caaSHongyun An {
201d3069caaSHongyun An XSLT::UOF2Storage aUOF2Storage(mxMSF, xIn);
202d3069caaSHongyun An if(aUOF2Storage.isValidUOF2Doc())
203d3069caaSHongyun An {
204d3069caaSHongyun An xIn->skipBytes(0);
205d3069caaSHongyun An Reference< XInputStream > xUOFInputStream = aUOF2Storage.getMainStorageRef()->openInputStream(XSLT::UOFELEMNAME);
206d3069caaSHongyun An if(xUOFInputStream.is())
207d3069caaSHongyun An {
208d3069caaSHongyun An Sequence< sal_Int8 > aSeq;
209d3069caaSHongyun An xUOFInputStream->readBytes(aSeq, 2000);
210d3069caaSHongyun An ::rtl::OString sUOFXML( reinterpret_cast< sal_Char* >(aSeq.getArray()));
211d3069caaSHongyun An OUString sOUSUOFXML( OStringToOUString(sUOFXML, RTL_TEXTENCODING_UTF8));
212d3069caaSHongyun An if(sOUSUOFXML.getLength())
213d3069caaSHongyun An {
214d3069caaSHongyun An const OUString sText( OUString::createFromAscii("vnd.uof.text"));
215d3069caaSHongyun An const OUString sCalc( OUString::createFromAscii("vnd.uof.spreadsheet"));
216d3069caaSHongyun An const OUString sImpress( OUString::createFromAscii("vnd.uof.presentation"));
217d3069caaSHongyun An
218d3069caaSHongyun An if(sOUSUOFXML.indexOf(sText) != -1)
219d3069caaSHongyun An sTypeName = OUString::createFromAscii("writer_NSO_UOF2");
220d3069caaSHongyun An else if(sOUSUOFXML.indexOf(sCalc) != -1)
221d3069caaSHongyun An sTypeName = OUString::createFromAscii("calc_NSO_UOF2");
222d3069caaSHongyun An else if(sOUSUOFXML.indexOf(sImpress) != -1)
223d3069caaSHongyun An sTypeName = OUString::createFromAscii("impress_NSO_UOF2");
224d3069caaSHongyun An }
225d3069caaSHongyun An }
226d3069caaSHongyun An }
227d3069caaSHongyun An }
228d3069caaSHongyun An }
229cdf0e10cSrcweir }
230cdf0e10cSrcweir else
231cdf0e10cSrcweir {
232cdf0e10cSrcweir if ( location == aArguments.getLength() )
233cdf0e10cSrcweir {
234cdf0e10cSrcweir aArguments.realloc(nLength+1);
235cdf0e10cSrcweir aArguments[location].Name = ::rtl::OUString::createFromAscii( "TypeName" );
236cdf0e10cSrcweir }
237cdf0e10cSrcweir aArguments[location].Value <<=sTypeName;
238cdf0e10cSrcweir }
239cdf0e10cSrcweir // OSL_ENSURE( sal_False, ::rtl::OUStringToOString(sTypeName,RTL_TEXTENCODING_ASCII_US).getStr() );
240cdf0e10cSrcweir
241cdf0e10cSrcweir
242cdf0e10cSrcweir return sTypeName;
243cdf0e10cSrcweir }
244cdf0e10cSrcweir
245cdf0e10cSrcweir
246cdf0e10cSrcweir
supportedByType(const::rtl::OUString clipBoardFormat,const::rtl::OString resultString,const::rtl::OUString checkType)247cdf0e10cSrcweir ::rtl::OUString SAL_CALL supportedByType( const ::rtl::OUString clipBoardFormat , const ::rtl::OString resultString, const ::rtl::OUString checkType)
248cdf0e10cSrcweir {
249cdf0e10cSrcweir
250cdf0e10cSrcweir ::rtl::OUString sTypeName= OUString::createFromAscii("");
251cdf0e10cSrcweir if((clipBoardFormat.match(OUString::createFromAscii("doctype:"))))
252cdf0e10cSrcweir {
253cdf0e10cSrcweir ::rtl::OString tryStr = ::rtl::OUStringToOString(clipBoardFormat.copy(8),RTL_TEXTENCODING_ASCII_US).getStr();
254cdf0e10cSrcweir // OSL_ENSURE( sal_False, tryStr);
255cdf0e10cSrcweir if (resultString.indexOf(tryStr) >= 0)
256cdf0e10cSrcweir {
257cdf0e10cSrcweir sTypeName = checkType;
258cdf0e10cSrcweir }
259cdf0e10cSrcweir }
260cdf0e10cSrcweir return sTypeName;
261cdf0e10cSrcweir }
262cdf0e10cSrcweir
263cdf0e10cSrcweir // XInitialization
264cdf0e10cSrcweir
initialize(const Sequence<Any> & aArguments)265cdf0e10cSrcweir void SAL_CALL FilterDetect::initialize( const Sequence< Any >& aArguments )
266cdf0e10cSrcweir throw (Exception, RuntimeException)
267cdf0e10cSrcweir {
268cdf0e10cSrcweir Sequence < PropertyValue > aAnySeq;
269cdf0e10cSrcweir sal_Int32 nLength = aArguments.getLength();
270cdf0e10cSrcweir if ( nLength && ( aArguments[0] >>= aAnySeq ) )
271cdf0e10cSrcweir {
272cdf0e10cSrcweir const PropertyValue * pValue = aAnySeq.getConstArray();
273cdf0e10cSrcweir nLength = aAnySeq.getLength();
274cdf0e10cSrcweir for ( sal_Int32 i = 0 ; i < nLength; i++)
275cdf0e10cSrcweir {
276cdf0e10cSrcweir
277cdf0e10cSrcweir if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Type" ) ) )
278cdf0e10cSrcweir {
279cdf0e10cSrcweir pValue[i].Value >>= msFilterName;
280cdf0e10cSrcweir
281cdf0e10cSrcweir }
282cdf0e10cSrcweir else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "UserData" ) ) )
283cdf0e10cSrcweir {
284cdf0e10cSrcweir
285cdf0e10cSrcweir pValue[i].Value >>= msUserData;
286cdf0e10cSrcweir
287cdf0e10cSrcweir }
288cdf0e10cSrcweir else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "TemplateName" ) ) )
289cdf0e10cSrcweir {
290cdf0e10cSrcweir
291cdf0e10cSrcweir pValue[i].Value>>=msTemplateName;
292cdf0e10cSrcweir }
293cdf0e10cSrcweir
294cdf0e10cSrcweir }
295cdf0e10cSrcweir }
296cdf0e10cSrcweir }
297cdf0e10cSrcweir
298cdf0e10cSrcweir
299cdf0e10cSrcweir
FilterDetect_getImplementationName()300cdf0e10cSrcweir OUString FilterDetect_getImplementationName ()
301cdf0e10cSrcweir throw (RuntimeException)
302cdf0e10cSrcweir {
303cdf0e10cSrcweir return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.filters.XMLFilterDetect" ) );
304cdf0e10cSrcweir }
305cdf0e10cSrcweir #define SERVICE_NAME1 "com.sun.star.document.ExtendedTypeDetection"
306cdf0e10cSrcweir
FilterDetect_supportsService(const OUString & ServiceName)307cdf0e10cSrcweir sal_Bool SAL_CALL FilterDetect_supportsService( const OUString& ServiceName )
308cdf0e10cSrcweir throw (RuntimeException)
309cdf0e10cSrcweir {
310cdf0e10cSrcweir return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) );
311cdf0e10cSrcweir }
FilterDetect_getSupportedServiceNames()312cdf0e10cSrcweir Sequence< OUString > SAL_CALL FilterDetect_getSupportedServiceNames( )
313cdf0e10cSrcweir throw (RuntimeException)
314cdf0e10cSrcweir {
315cdf0e10cSrcweir Sequence < OUString > aRet(2);
316cdf0e10cSrcweir OUString* pArray = aRet.getArray();
317cdf0e10cSrcweir pArray[0] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1 ) );
318cdf0e10cSrcweir return aRet;
319cdf0e10cSrcweir }
320cdf0e10cSrcweir #undef SERVICE_NAME1
321cdf0e10cSrcweir #undef SERVICE_NAME2
322cdf0e10cSrcweir
FilterDetect_createInstance(const Reference<XMultiServiceFactory> & rSMgr)323cdf0e10cSrcweir Reference< XInterface > SAL_CALL FilterDetect_createInstance( const Reference< XMultiServiceFactory > & rSMgr)
324cdf0e10cSrcweir throw( Exception )
325cdf0e10cSrcweir {
326cdf0e10cSrcweir return (cppu::OWeakObject*) new FilterDetect( rSMgr );
327cdf0e10cSrcweir }
328cdf0e10cSrcweir
329cdf0e10cSrcweir // XServiceInfo
getImplementationName()330cdf0e10cSrcweir OUString SAL_CALL FilterDetect::getImplementationName( )
331cdf0e10cSrcweir throw (RuntimeException)
332cdf0e10cSrcweir {
333cdf0e10cSrcweir return FilterDetect_getImplementationName();
334cdf0e10cSrcweir }
supportsService(const OUString & rServiceName)335cdf0e10cSrcweir sal_Bool SAL_CALL FilterDetect::supportsService( const OUString& rServiceName )
336cdf0e10cSrcweir throw (RuntimeException)
337cdf0e10cSrcweir {
338cdf0e10cSrcweir return FilterDetect_supportsService( rServiceName );
339cdf0e10cSrcweir }
getSupportedServiceNames()340cdf0e10cSrcweir Sequence< OUString > SAL_CALL FilterDetect::getSupportedServiceNames( )
341cdf0e10cSrcweir throw (RuntimeException)
342cdf0e10cSrcweir {
343cdf0e10cSrcweir return FilterDetect_getSupportedServiceNames();
344cdf0e10cSrcweir }
345