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 130 : OUString supportedByType( const OUString& clipBoardFormat, const OUString& resultString, const OUString& checkType)
39 : {
40 130 : OUString sTypeName;
41 130 : if ( clipBoardFormat.match("doctype:") )
42 : {
43 30 : OUString tryStr = clipBoardFormat.copy(8);
44 30 : if (resultString.indexOf(tryStr) >= 0)
45 : {
46 4 : sTypeName = checkType;
47 30 : }
48 : }
49 130 : return sTypeName;
50 : }
51 :
52 : }
53 :
54 16 : 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 16 : OUString sTypeName;
57 32 : OUString sUrl;
58 32 : Sequence<PropertyValue > lProps ;
59 :
60 32 : com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInStream;
61 16 : const PropertyValue * pValue = aArguments.getConstArray();
62 : sal_Int32 nLength;
63 32 : OUString resultString;
64 :
65 16 : nLength = aArguments.getLength();
66 16 : sal_Int32 location=nLength;
67 146 : for (sal_Int32 i = 0 ; i < nLength; i++)
68 : {
69 130 : if ( pValue[i].Name == "TypeName" )
70 : {
71 16 : location=i;
72 : }
73 114 : else if ( pValue[i].Name == "URL" )
74 : {
75 16 : pValue[i].Value >>= sUrl;
76 : }
77 98 : else if ( pValue[i].Name == "InputStream" )
78 : {
79 16 : pValue[i].Value >>= xInStream ;
80 : }
81 : }
82 : try
83 : {
84 16 : 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 16 : ::boost::scoped_ptr< SvStream > pInStream( ::utl::UcbStreamHelper::CreateStream( xInStream ) );
97 16 : pInStream->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW );
98 16 : sal_Size nUniPos = pInStream->Tell();
99 :
100 16 : const sal_uInt16 nSize = 4000;
101 16 : bool bTryUtf16 = false;
102 :
103 16 : if ( nUniPos == 0 ) // No BOM detected, try to guess UTF-16 endianness
104 : {
105 16 : sal_uInt16 nHeader = 0;
106 16 : pInStream->ReadUInt16( nHeader );
107 16 : if ( nHeader == 0x003C )
108 0 : bTryUtf16 = true;
109 16 : else if ( nHeader == 0x3C00 )
110 : {
111 0 : bTryUtf16 = true;
112 0 : pInStream->SetEndianSwap( !pInStream->IsEndianSwap() );
113 : }
114 16 : pInStream->Seek( STREAM_SEEK_TO_BEGIN );
115 : }
116 :
117 16 : if ( nUniPos == 3 || ( nUniPos == 0 && !bTryUtf16 ) ) // UTF-8 or non-Unicode
118 16 : resultString = OStringToOUString( read_uInt8s_ToOString( *pInStream, nSize ), RTL_TEXTENCODING_UTF8 );
119 0 : else if ( nUniPos == 2 || bTryUtf16 ) // UTF-16
120 0 : resultString = read_uInt16s_ToOUString( *pInStream, nSize );
121 :
122 16 : if ( !resultString.startsWith( "<?xml" ) )
123 : // This is not an XML stream. It makes no sense to try to detect
124 : // a non-XML file type here.
125 12 : return OUString();
126 :
127 : // test typedetect code
128 8 : Reference <XNameAccess> xTypeCont(mxCtx->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", mxCtx), UNO_QUERY);
129 8 : Sequence < OUString > myTypes= xTypeCont->getElementNames();
130 4 : nLength = myTypes.getLength();
131 :
132 4 : sal_Int32 new_nlength=0;
133 4 : sal_Int32 i = 0 ;
134 398 : while ((i < nLength) && (sTypeName.isEmpty()))
135 : {
136 390 : Any elem = xTypeCont->getByName(myTypes[i]);
137 390 : elem >>=lProps;
138 390 : new_nlength = lProps.getLength();
139 390 : sal_Int32 j =0;
140 5432 : while (j < new_nlength && (sTypeName.isEmpty()))
141 : {
142 4652 : OUString tmpStr;
143 4652 : lProps[j].Value >>=tmpStr;
144 4652 : if ( lProps[j].Name == "ClipboardFormat" && !tmpStr.isEmpty() )
145 : {
146 130 : sTypeName = supportedByType(tmpStr,resultString, myTypes[i]);
147 : }
148 4652 : j++;
149 4652 : }
150 390 : i++;
151 394 : }
152 : }
153 0 : catch (const Exception &)
154 : {
155 : OSL_FAIL( "An Exception occurred while opening File stream" );
156 : }
157 :
158 4 : if (!sTypeName.isEmpty())
159 : {
160 4 : if (location == aArguments.getLength())
161 : {
162 0 : aArguments.realloc(nLength+1);
163 0 : aArguments[location].Name = "TypeName";
164 : }
165 4 : aArguments[location].Value <<=sTypeName;
166 : }
167 :
168 20 : return sTypeName;
169 : }
170 :
171 : // XInitialization
172 0 : void SAL_CALL FilterDetect::initialize( const Sequence< Any >& /*aArguments*/ )
173 : throw (Exception, RuntimeException, std::exception)
174 : {
175 0 : }
176 :
177 10 : OUString FilterDetect_getImplementationName ()
178 : {
179 10 : return OUString( "com.sun.star.comp.filters.XMLFilterDetect" );
180 : }
181 :
182 10 : Sequence< OUString > FilterDetect_getSupportedServiceNames()
183 : {
184 10 : Sequence < OUString > aRet(1);
185 10 : aRet[0] = "com.sun.star.document.ExtendedTypeDetection";
186 10 : return aRet;
187 : }
188 :
189 16 : Reference< XInterface > FilterDetect_createInstance( const Reference< XComponentContext > & context)
190 : {
191 16 : return static_cast< cppu::OWeakObject * >( new FilterDetect( context ) );
192 : }
193 :
194 : // XServiceInfo
195 0 : OUString SAL_CALL FilterDetect::getImplementationName( )
196 : throw (RuntimeException, std::exception)
197 : {
198 0 : return FilterDetect_getImplementationName();
199 : }
200 :
201 0 : sal_Bool SAL_CALL FilterDetect::supportsService( const OUString& rServiceName )
202 : throw (RuntimeException, std::exception)
203 : {
204 0 : return cppu::supportsService( this, rServiceName );
205 : }
206 :
207 0 : Sequence< OUString > SAL_CALL FilterDetect::getSupportedServiceNames( )
208 : throw (RuntimeException, std::exception)
209 : {
210 0 : return FilterDetect_getSupportedServiceNames();
211 : }
212 :
213 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|