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 "filterdetect.hxx"
21 : #include <osl/diagnose.h>
22 : #include <com/sun/star/io/XInputStream.hpp>
23 : #include <com/sun/star/uno/XComponentContext.hpp>
24 : #include <com/sun/star/document/XExtendedFilterDetection.hpp>
25 : #include <com/sun/star/container/XNameAccess.hpp>
26 : #include <com/sun/star/beans/PropertyState.hpp>
27 : #include <cppuhelper/supportsservice.hxx>
28 : #include <ucbhelper/content.hxx>
29 : #include <unotools/ucbstreamhelper.hxx>
30 : #include <boost/scoped_ptr.hpp>
31 :
32 : using namespace com::sun::star::container;
33 : using namespace com::sun::star::uno;
34 : using namespace com::sun::star::beans;
35 :
36 : namespace {
37 :
38 658 : OUString supportedByType( const OUString& clipBoardFormat, const OUString& resultString, const OUString& checkType)
39 : {
40 658 : OUString sTypeName;
41 658 : if ( clipBoardFormat.match("doctype:") )
42 : {
43 115 : OUString tryStr = clipBoardFormat.copy(8);
44 115 : if (resultString.indexOf(tryStr) >= 0)
45 : {
46 13 : sTypeName = checkType;
47 115 : }
48 : }
49 658 : return sTypeName;
50 : }
51 :
52 : }
53 :
54 943 : OUString SAL_CALL FilterDetect::detect( com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >& aArguments ) throw( com::sun::star::uno::RuntimeException, std::exception )
55 : {
56 943 : OUString sTypeName;
57 1886 : OUString sUrl;
58 1886 : Sequence<PropertyValue > lProps ;
59 :
60 1886 : com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInStream;
61 943 : const PropertyValue * pValue = aArguments.getConstArray();
62 : sal_Int32 nLength;
63 1886 : OUString resultString;
64 :
65 943 : nLength = aArguments.getLength();
66 943 : sal_Int32 location=nLength;
67 8499 : for (sal_Int32 i = 0 ; i < nLength; i++)
68 : {
69 7556 : if ( pValue[i].Name == "TypeName" )
70 : {
71 943 : location=i;
72 : }
73 6613 : else if ( pValue[i].Name == "URL" )
74 : {
75 943 : pValue[i].Value >>= sUrl;
76 : }
77 5670 : else if ( pValue[i].Name == "InputStream" )
78 : {
79 943 : pValue[i].Value >>= xInStream ;
80 : }
81 : }
82 : try
83 : {
84 943 : if (!xInStream.is())
85 : {
86 : ::ucbhelper::Content aContent(
87 : sUrl, Reference< com::sun::star::ucb::XCommandEnvironment >(),
88 0 : mxCtx);
89 0 : xInStream = aContent.openStream();
90 0 : if (!xInStream.is())
91 : {
92 0 : return sTypeName;
93 0 : }
94 : }
95 :
96 943 : ::boost::scoped_ptr< SvStream > pInStream( ::utl::UcbStreamHelper::CreateStream( xInStream ) );
97 943 : pInStream->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW );
98 943 : sal_Size nUniPos = pInStream->Tell();
99 :
100 943 : const sal_uInt16 nSize = 4000;
101 943 : bool bTryUtf16 = false;
102 :
103 943 : if ( nUniPos == 0 ) // No BOM detected, try to guess UTF-16 endianness
104 : {
105 943 : sal_uInt16 nHeader = 0;
106 943 : pInStream->ReadUInt16( nHeader );
107 943 : if ( nHeader == 0x003C )
108 0 : bTryUtf16 = true;
109 943 : else if ( nHeader == 0x3C00 )
110 : {
111 0 : bTryUtf16 = true;
112 0 : pInStream->SetEndian( pInStream->GetEndian() == SvStreamEndian::LITTLE ? SvStreamEndian::BIG : SvStreamEndian::LITTLE );
113 : }
114 943 : pInStream->Seek( STREAM_SEEK_TO_BEGIN );
115 : }
116 :
117 943 : if ( nUniPos == 3 || ( nUniPos == 0 && !bTryUtf16 ) ) // UTF-8 or non-Unicode
118 : {
119 943 : OString const str(read_uInt8s_ToOString(*pInStream, nSize));
120 1886 : resultString = OUString(str.getStr(), str.getLength(),
121 : RTL_TEXTENCODING_ASCII_US,
122 1886 : RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT|RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT|RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT);
123 : }
124 0 : else if ( nUniPos == 2 || bTryUtf16 ) // UTF-16
125 0 : resultString = read_uInt16s_ToOUString( *pInStream, nSize );
126 :
127 943 : if ( !resultString.startsWith( "<?xml" ) )
128 : // This is not an XML stream. It makes no sense to try to detect
129 : // a non-XML file type here.
130 930 : return OUString();
131 :
132 : // test typedetect code
133 26 : Reference <XNameAccess> xTypeCont(mxCtx->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", mxCtx), UNO_QUERY);
134 26 : Sequence < OUString > myTypes= xTypeCont->getElementNames();
135 13 : nLength = myTypes.getLength();
136 :
137 13 : sal_Int32 new_nlength=0;
138 13 : sal_Int32 i = 0 ;
139 2356 : while ((i < nLength) && (sTypeName.isEmpty()))
140 : {
141 2330 : Any elem = xTypeCont->getByName(myTypes[i]);
142 2330 : elem >>=lProps;
143 2330 : new_nlength = lProps.getLength();
144 2330 : sal_Int32 j =0;
145 32607 : while (j < new_nlength && (sTypeName.isEmpty()))
146 : {
147 27947 : OUString tmpStr;
148 27947 : lProps[j].Value >>=tmpStr;
149 27947 : if ( lProps[j].Name == "ClipboardFormat" && !tmpStr.isEmpty() )
150 : {
151 658 : sTypeName = supportedByType(tmpStr,resultString, myTypes[i]);
152 : }
153 27947 : j++;
154 27947 : }
155 2330 : i++;
156 2343 : }
157 : }
158 0 : catch (const Exception &)
159 : {
160 : OSL_FAIL( "An Exception occurred while opening File stream" );
161 : }
162 :
163 13 : if (!sTypeName.isEmpty())
164 : {
165 13 : if (location == aArguments.getLength())
166 : {
167 0 : aArguments.realloc(nLength+1);
168 0 : aArguments[location].Name = "TypeName";
169 : }
170 13 : aArguments[location].Value <<=sTypeName;
171 : }
172 :
173 956 : return sTypeName;
174 : }
175 :
176 : // XInitialization
177 0 : void SAL_CALL FilterDetect::initialize( const Sequence< Any >& /*aArguments*/ )
178 : throw (Exception, RuntimeException, std::exception)
179 : {
180 0 : }
181 :
182 12 : OUString FilterDetect_getImplementationName ()
183 : {
184 12 : return OUString( "com.sun.star.comp.filters.XMLFilterDetect" );
185 : }
186 :
187 12 : Sequence< OUString > FilterDetect_getSupportedServiceNames()
188 : {
189 12 : Sequence < OUString > aRet(1);
190 12 : aRet[0] = "com.sun.star.document.ExtendedTypeDetection";
191 12 : return aRet;
192 : }
193 :
194 944 : Reference< XInterface > FilterDetect_createInstance( const Reference< XComponentContext > & context)
195 : {
196 944 : return static_cast< cppu::OWeakObject * >( new FilterDetect( context ) );
197 : }
198 :
199 : // XServiceInfo
200 1 : OUString SAL_CALL FilterDetect::getImplementationName( )
201 : throw (RuntimeException, std::exception)
202 : {
203 1 : return FilterDetect_getImplementationName();
204 : }
205 :
206 0 : sal_Bool SAL_CALL FilterDetect::supportsService( const OUString& rServiceName )
207 : throw (RuntimeException, std::exception)
208 : {
209 0 : return cppu::supportsService( this, rServiceName );
210 : }
211 :
212 1 : Sequence< OUString > SAL_CALL FilterDetect::getSupportedServiceNames( )
213 : throw (RuntimeException, std::exception)
214 : {
215 1 : return FilterDetect_getSupportedServiceNames();
216 : }
217 :
218 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|