Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <iostream>
21 : #include <stdlib.h>
22 : #include <ctype.h>
23 : #include <stdio.h>
24 : #include <cstring>
25 : #include "filterdetect.hxx"
26 : #include <osl/diagnose.h>
27 : #include <com/sun/star/io/XActiveDataSource.hpp>
28 : #include <com/sun/star/io/XOutputStream.hpp>
29 : #include <com/sun/star/io/XInputStream.hpp>
30 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
31 : #include <com/sun/star/xml/sax/InputSource.hpp>
32 : #include <com/sun/star/xml/sax/XParser.hpp>
33 : #include <com/sun/star/xml/XImportFilter.hpp>
34 : #include <com/sun/star/xml/XExportFilter.hpp>
35 : #include <com/sun/star/frame/XController.hpp>
36 : #include <com/sun/star/task/XStatusIndicator.hpp>
37 : #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
38 : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
39 : #include <com/sun/star/style/XStyleLoader.hpp>
40 : #include <com/sun/star/uno/XComponentContext.hpp>
41 : #include <com/sun/star/document/XExtendedFilterDetection.hpp>
42 : #include <com/sun/star/container/XNameAccess.hpp>
43 : #include <com/sun/star/beans/PropertyState.hpp>
44 : #include <cppuhelper/supportsservice.hxx>
45 : #include <ucbhelper/content.hxx>
46 : #include <unotools/ucbstreamhelper.hxx>
47 : #include <boost/scoped_ptr.hpp>
48 :
49 : using com::sun::star::uno::Sequence;
50 : using com::sun::star::uno::Reference;
51 : using com::sun::star::uno::Any;
52 : using com::sun::star::uno::UNO_QUERY;
53 : using com::sun::star::uno::XComponentContext;
54 : using com::sun::star::uno::XInterface;
55 : using com::sun::star::uno::Exception;
56 : using com::sun::star::uno::RuntimeException;
57 : using com::sun::star::io::XActiveDataSource;
58 : using com::sun::star::io::XOutputStream;
59 : using com::sun::star::beans::PropertyValue;
60 : using com::sun::star::document::XExporter;
61 : using com::sun::star::document::XFilter;
62 : using com::sun::star::document::XExtendedFilterDetection;
63 :
64 : using com::sun::star::io::XInputStream;
65 : using com::sun::star::document::XImporter;
66 : using com::sun::star::xml::sax::InputSource;
67 : using com::sun::star::xml::sax::XDocumentHandler;
68 : using com::sun::star::xml::sax::XParser;
69 :
70 : using namespace ::com::sun::star::frame;
71 : using namespace ::com::sun::star;
72 : using namespace com::sun::star::container;
73 : using namespace com::sun::star::uno;
74 : using namespace com::sun::star::beans;
75 :
76 : namespace {
77 :
78 0 : OUString supportedByType( const OUString& clipBoardFormat, const OUString& resultString, const OUString& checkType)
79 : {
80 0 : OUString sTypeName;
81 0 : if ( clipBoardFormat.match("doctype:") )
82 : {
83 0 : OUString tryStr = clipBoardFormat.copy(8);
84 0 : if (resultString.indexOf(tryStr) >= 0)
85 : {
86 0 : sTypeName = checkType;
87 0 : }
88 : }
89 0 : return sTypeName;
90 : }
91 :
92 : }
93 :
94 1 : OUString SAL_CALL FilterDetect::detect( com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >& aArguments ) throw( com::sun::star::uno::RuntimeException, std::exception )
95 : {
96 1 : OUString sTypeName;
97 2 : OUString sUrl;
98 2 : Sequence<PropertyValue > lProps ;
99 :
100 2 : com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInStream;
101 1 : const PropertyValue * pValue = aArguments.getConstArray();
102 : sal_Int32 nLength;
103 2 : OUString resultString;
104 :
105 1 : nLength = aArguments.getLength();
106 1 : sal_Int32 location=nLength;
107 10 : for (sal_Int32 i = 0 ; i < nLength; i++)
108 : {
109 9 : if ( pValue[i].Name == "TypeName" )
110 : {
111 1 : location=i;
112 : }
113 8 : else if ( pValue[i].Name == "URL" )
114 : {
115 1 : pValue[i].Value >>= sUrl;
116 : }
117 7 : else if ( pValue[i].Name == "InputStream" )
118 : {
119 1 : pValue[i].Value >>= xInStream ;
120 : }
121 : }
122 : try
123 : {
124 1 : if (!xInStream.is())
125 : {
126 : ::ucbhelper::Content aContent(
127 : sUrl, Reference< com::sun::star::ucb::XCommandEnvironment >(),
128 0 : mxCtx);
129 0 : xInStream = aContent.openStream();
130 0 : if (!xInStream.is())
131 : {
132 0 : return sTypeName;
133 0 : }
134 : }
135 :
136 1 : ::boost::scoped_ptr< SvStream > pInStream( ::utl::UcbStreamHelper::CreateStream( xInStream ) );
137 1 : pInStream->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW );
138 1 : sal_Size nUniPos = pInStream->Tell();
139 :
140 1 : const sal_uInt16 nSize = 4000;
141 1 : bool bTryUtf16 = false;
142 :
143 1 : if ( nUniPos == 0 ) // No BOM detected, try to guess UTF-16 endianness
144 : {
145 1 : sal_uInt16 sHeader = 0;
146 1 : pInStream->ReadUInt16( sHeader );
147 1 : if ( sHeader == 0x003C )
148 0 : bTryUtf16 = true;
149 1 : else if ( sHeader == 0x3C00 )
150 : {
151 0 : bTryUtf16 = true;
152 0 : pInStream->SetEndianSwap( !pInStream->IsEndianSwap() );
153 : }
154 1 : pInStream->Seek( STREAM_SEEK_TO_BEGIN );
155 : }
156 :
157 1 : if ( nUniPos == 3 || ( nUniPos == 0 && !bTryUtf16 ) ) // UTF-8 or non-Unicode
158 1 : resultString = OStringToOUString( read_uInt8s_ToOString( *pInStream, nSize ), RTL_TEXTENCODING_UTF8 );
159 0 : else if ( nUniPos == 2 || bTryUtf16 ) // UTF-16
160 0 : resultString = read_uInt16s_ToOUString( *pInStream, nSize );
161 :
162 1 : if ( !resultString.startsWith( "<?xml" ) )
163 : // This is not an XML stream. It makes no sense to try to detect
164 : // a non-XML file type here.
165 1 : return OUString();
166 :
167 : // test typedetect code
168 0 : Reference <XNameAccess> xTypeCont(mxCtx->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", mxCtx), UNO_QUERY);
169 0 : Sequence < OUString > myTypes= xTypeCont->getElementNames();
170 0 : nLength = myTypes.getLength();
171 :
172 0 : sal_Int32 new_nlength=0;
173 0 : sal_Int32 i = 0 ;
174 0 : while ((i < nLength) && (sTypeName.isEmpty()))
175 : {
176 0 : Any elem = xTypeCont->getByName(myTypes[i]);
177 0 : elem >>=lProps;
178 0 : new_nlength = lProps.getLength();
179 0 : sal_Int32 j =0;
180 0 : while (j < new_nlength && (sTypeName.isEmpty()))
181 : {
182 0 : OUString tmpStr;
183 0 : lProps[j].Value >>=tmpStr;
184 0 : if ( lProps[j].Name == "ClipboardFormat" && !tmpStr.isEmpty() )
185 : {
186 0 : sTypeName = supportedByType(tmpStr,resultString, myTypes[i]);
187 : }
188 0 : j++;
189 0 : }
190 0 : i++;
191 0 : }
192 : }
193 0 : catch (const Exception &)
194 : {
195 : OSL_FAIL( "An Exception occurred while opening File stream" );
196 : }
197 :
198 0 : if (!sTypeName.isEmpty())
199 : {
200 0 : if (location == aArguments.getLength())
201 : {
202 0 : aArguments.realloc(nLength+1);
203 0 : aArguments[location].Name = "TypeName";
204 : }
205 0 : aArguments[location].Value <<=sTypeName;
206 : }
207 :
208 1 : return sTypeName;
209 : }
210 :
211 : // XInitialization
212 :
213 0 : void SAL_CALL FilterDetect::initialize( const Sequence< Any >& aArguments )
214 : throw (Exception, RuntimeException, std::exception)
215 : {
216 0 : Sequence < PropertyValue > aAnySeq;
217 0 : sal_Int32 nLength = aArguments.getLength();
218 0 : if ( nLength && ( aArguments[0] >>= aAnySeq ) )
219 : {
220 0 : const PropertyValue * pValue = aAnySeq.getConstArray();
221 0 : nLength = aAnySeq.getLength();
222 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
223 : {
224 :
225 0 : if ( pValue[i].Name == "Type" )
226 : {
227 0 : pValue[i].Value >>= msFilterName;
228 :
229 : }
230 0 : else if ( pValue[i].Name == "UserData" )
231 : {
232 :
233 0 : pValue[i].Value >>= msUserData;
234 :
235 : }
236 0 : else if ( pValue[i].Name == "TemplateName" )
237 : {
238 :
239 0 : pValue[i].Value>>=msTemplateName;
240 : }
241 :
242 : }
243 0 : }
244 0 : }
245 :
246 :
247 :
248 1 : OUString FilterDetect_getImplementationName ()
249 : {
250 1 : return OUString( "com.sun.star.comp.filters.XMLFilterDetect" );
251 : }
252 :
253 1 : Sequence< OUString > FilterDetect_getSupportedServiceNames()
254 : {
255 1 : Sequence < OUString > aRet(1);
256 1 : aRet[0] = "com.sun.star.document.ExtendedTypeDetection";
257 1 : return aRet;
258 : }
259 :
260 1 : Reference< XInterface > FilterDetect_createInstance( const Reference< XComponentContext > & context)
261 : {
262 1 : return static_cast< cppu::OWeakObject * >( new FilterDetect( context ) );
263 : }
264 :
265 : // XServiceInfo
266 0 : OUString SAL_CALL FilterDetect::getImplementationName( )
267 : throw (RuntimeException, std::exception)
268 : {
269 0 : return FilterDetect_getImplementationName();
270 : }
271 0 : sal_Bool SAL_CALL FilterDetect::supportsService( const OUString& rServiceName )
272 : throw (RuntimeException, std::exception)
273 : {
274 0 : return cppu::supportsService( this, rServiceName );
275 : }
276 :
277 0 : Sequence< OUString > SAL_CALL FilterDetect::getSupportedServiceNames( )
278 : throw (RuntimeException, std::exception)
279 : {
280 0 : return FilterDetect_getSupportedServiceNames();
281 3 : }
282 :
283 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|