update dev300-m58
[ooovba.git] / filter / source / xmlfilterdetect / filterdetect.cxx
blobfbaca94ac1a4a10be1f8044b12b28d55f5691794
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: filterdetect.cxx,v $
11 * $Revision: 1.12 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_filter.hxx"
34 #include <iostream>
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <stdio.h>
38 #include "filterdetect.hxx"
39 #include <osl/diagnose.h>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 #include <com/sun/star/io/XActiveDataSource.hpp>
42 #include <com/sun/star/io/XOutputStream.hpp>
43 #include <com/sun/star/io/XInputStream.hpp>
44 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
45 #include <com/sun/star/xml/sax/InputSource.hpp>
46 #include <com/sun/star/xml/sax/XParser.hpp>
47 #include <com/sun/star/xml/XImportFilter.hpp>
48 #include <com/sun/star/xml/XExportFilter.hpp>
49 #include <com/sun/star/frame/XModel.hpp>
50 #include <com/sun/star/frame/XController.hpp>
51 #include <com/sun/star/task/XStatusIndicator.hpp>
52 #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
53 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
54 #include <com/sun/star/style/XStyleLoader.hpp>
55 #include <com/sun/star/io/XInputStream.hpp>
56 #include <com/sun/star/document/XExtendedFilterDetection.hpp>
57 #include <com/sun/star/container/XNameAccess.hpp>
58 #include <com/sun/star/beans/PropertyState.hpp>
60 #include <ucbhelper/content.hxx>
61 #include <ucbhelper/contentbroker.hxx>
62 #include <ucbhelper/commandenvironment.hxx>
63 #include <unotools/ucbhelper.hxx>
64 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
68 using rtl::OUString;
69 using com::sun::star::uno::Sequence;
70 using com::sun::star::uno::Reference;
71 using com::sun::star::uno::Any;
72 using com::sun::star::uno::UNO_QUERY;
73 using com::sun::star::uno::XInterface;
74 using com::sun::star::uno::Exception;
75 using com::sun::star::uno::RuntimeException;
76 using com::sun::star::lang::XMultiServiceFactory;
77 using com::sun::star::io::XActiveDataSource;
78 using com::sun::star::io::XOutputStream;
79 using com::sun::star::beans::PropertyValue;
80 using com::sun::star::document::XExporter;
81 using com::sun::star::document::XFilter;
82 using com::sun::star::document::XExtendedFilterDetection;
84 using com::sun::star::io::XInputStream;
85 using com::sun::star::document::XImporter;
86 using com::sun::star::xml::sax::InputSource;
87 using com::sun::star::xml::sax::XDocumentHandler;
88 using com::sun::star::xml::sax::XParser;
89 using com::sun::star::task::XInteractionHandler;
91 using namespace ::com::sun::star::frame;
92 using namespace ::com::sun::star;
93 using namespace com::sun::star::container;
94 using namespace com::sun::star::uno;
95 using namespace com::sun::star::beans;
98 Reference< com::sun::star::frame::XModel > xModel;
100 ::rtl::OUString SAL_CALL supportedByType( const ::rtl::OUString clipBoardFormat , const ::rtl::OString resultString, const ::rtl::OUString checkType);
103 ::rtl::OUString SAL_CALL FilterDetect::detect( com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >& aArguments ) throw( com::sun::star::uno::RuntimeException )
105 ::rtl::OUString sTypeName = OUString::createFromAscii("");
106 ::rtl::OUString sUrl = OUString::createFromAscii("");
107 ::rtl::OUString originalTypeName;
108 Sequence<PropertyValue > lProps ;
110 com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInStream;
111 ::rtl::OUString temp;
112 //OSL_ENSURE( sal_False, " starting Detect" );
113 const PropertyValue * pValue = aArguments.getConstArray();
114 sal_Int32 nLength;
115 ::rtl::OString resultString;
117 nLength = aArguments.getLength();
118 sal_Int32 location=nLength;
119 for ( sal_Int32 i = 0 ; i < nLength; i++)
121 //OSL_ENSURE( sal_False, ::rtl::OUStringToOString(pValue[i].Name,RTL_TEXTENCODING_ASCII_US).getStr() );
122 if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "TypeName" ) ) )
124 //pValue[i].Value >>= originalTypeName;
125 location=i;
126 // OSL_ENSURE( sal_False, ::rtl::OUStringToOString(sTypeName,RTL_TEXTENCODING_ASCII_US).getStr() );
129 else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "URL" ) ) )
132 pValue[i].Value >>= sUrl;
133 //OSL_ENSURE( sal_False, ::rtl::OUStringToOString(sUrl,RTL_TEXTENCODING_ASCII_US).getStr() );
136 else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "InputStream" ) ) )
138 pValue[i].Value >>= xInStream ;
143 try{
144 Reference< com::sun::star::ucb::XCommandEnvironment > xEnv;
145 if (!xInStream.is())
147 ::ucbhelper::Content aContent(sUrl,xEnv);
148 xInStream = aContent.openStream();
149 if (!xInStream.is())
151 return sTypeName;
154 com::sun::star::uno::Sequence< sal_Int8 > aData;
155 /* long nBytesToRead= */ xInStream->available();
156 xInStream->skipBytes (0);
157 long bytestRead =xInStream->readBytes (aData, 4000);
158 resultString=::rtl::OString((const sal_Char *)aData.getConstArray(),bytestRead) ;
161 // test typedetect code
162 Reference <XNameAccess> xTypeCont(mxMSF->createInstance(OUString::createFromAscii("com.sun.star.document.TypeDetection")),UNO_QUERY);
163 Sequence < ::rtl::OUString > myTypes= xTypeCont->getElementNames();
164 nLength = myTypes.getLength();
167 sal_Int32 new_nlength=0;
168 sal_Int32 i = 0 ;
169 while( (i < nLength) && (sTypeName.equalsAscii("")))
172 Any elem = xTypeCont->getByName(myTypes[i]);
173 elem >>=lProps;
174 new_nlength = lProps.getLength();
175 sal_Int32 j =0;
176 while( j < new_nlength && sTypeName.equalsAscii(""))
178 ::rtl::OUString tmpStr =OUString::createFromAscii("");
179 lProps[j].Value >>=tmpStr;
180 if((lProps[j].Name.equalsAscii("ClipboardFormat")) && (!tmpStr.equalsAscii("")) )
182 sTypeName = supportedByType(tmpStr,resultString, myTypes[i]);
184 j++;
186 i++;
188 //end test
191 catch(Exception &)
193 OSL_ENSURE( sal_False, "An Exception occured while opening File stream" );
195 if(sTypeName.equalsAscii(""))
197 //sTypeName=::rtl::OUString::createFromAscii("writer_Flat_XML_File");
199 else
201 if ( location == aArguments.getLength() )
203 aArguments.realloc(nLength+1);
204 aArguments[location].Name = ::rtl::OUString::createFromAscii( "TypeName" );
206 aArguments[location].Value <<=sTypeName;
208 // OSL_ENSURE( sal_False, ::rtl::OUStringToOString(sTypeName,RTL_TEXTENCODING_ASCII_US).getStr() );
211 return sTypeName;
216 ::rtl::OUString SAL_CALL supportedByType( const ::rtl::OUString clipBoardFormat , const ::rtl::OString resultString, const ::rtl::OUString checkType)
219 ::rtl::OUString sTypeName= OUString::createFromAscii("");
220 if((clipBoardFormat.match(OUString::createFromAscii("doctype:"))))
222 ::rtl::OString tryStr = ::rtl::OUStringToOString(clipBoardFormat.copy(8),RTL_TEXTENCODING_ASCII_US).getStr();
223 // OSL_ENSURE( sal_False, tryStr);
224 if (resultString.indexOf(tryStr) >= 0)
226 sTypeName = checkType;
229 return sTypeName;
232 // XInitialization
234 void SAL_CALL FilterDetect::initialize( const Sequence< Any >& aArguments )
235 throw (Exception, RuntimeException)
237 Sequence < PropertyValue > aAnySeq;
238 sal_Int32 nLength = aArguments.getLength();
239 if ( nLength && ( aArguments[0] >>= aAnySeq ) )
241 const PropertyValue * pValue = aAnySeq.getConstArray();
242 nLength = aAnySeq.getLength();
243 for ( sal_Int32 i = 0 ; i < nLength; i++)
246 if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Type" ) ) )
248 pValue[i].Value >>= msFilterName;
251 else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "UserData" ) ) )
254 pValue[i].Value >>= msUserData;
257 else if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "TemplateName" ) ) )
260 pValue[i].Value>>=msTemplateName;
269 OUString FilterDetect_getImplementationName ()
270 throw (RuntimeException)
272 return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.filters.XMLFilterDetect" ) );
274 #define SERVICE_NAME1 "com.sun.star.document.ExtendedTypeDetection"
276 sal_Bool SAL_CALL FilterDetect_supportsService( const OUString& ServiceName )
277 throw (RuntimeException)
279 return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) );
281 Sequence< OUString > SAL_CALL FilterDetect_getSupportedServiceNames( )
282 throw (RuntimeException)
284 Sequence < OUString > aRet(2);
285 OUString* pArray = aRet.getArray();
286 pArray[0] = OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1 ) );
287 return aRet;
289 #undef SERVICE_NAME1
290 #undef SERVICE_NAME2
292 Reference< XInterface > SAL_CALL FilterDetect_createInstance( const Reference< XMultiServiceFactory > & rSMgr)
293 throw( Exception )
295 return (cppu::OWeakObject*) new FilterDetect( rSMgr );
298 // XServiceInfo
299 OUString SAL_CALL FilterDetect::getImplementationName( )
300 throw (RuntimeException)
302 return FilterDetect_getImplementationName();
304 sal_Bool SAL_CALL FilterDetect::supportsService( const OUString& rServiceName )
305 throw (RuntimeException)
307 return FilterDetect_supportsService( rServiceName );
309 Sequence< OUString > SAL_CALL FilterDetect::getSupportedServiceNames( )
310 throw (RuntimeException)
312 return FilterDetect_getSupportedServiceNames();