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 <ucbhelper/content.hxx>
45 :
46 : using rtl::OUString;
47 : using com::sun::star::uno::Sequence;
48 : using com::sun::star::uno::Reference;
49 : using com::sun::star::uno::Any;
50 : using com::sun::star::uno::UNO_QUERY;
51 : using com::sun::star::uno::XComponentContext;
52 : using com::sun::star::uno::XInterface;
53 : using com::sun::star::uno::Exception;
54 : using com::sun::star::uno::RuntimeException;
55 : using com::sun::star::io::XActiveDataSource;
56 : using com::sun::star::io::XOutputStream;
57 : using com::sun::star::beans::PropertyValue;
58 : using com::sun::star::document::XExporter;
59 : using com::sun::star::document::XFilter;
60 : using com::sun::star::document::XExtendedFilterDetection;
61 :
62 : using com::sun::star::io::XInputStream;
63 : using com::sun::star::document::XImporter;
64 : using com::sun::star::xml::sax::InputSource;
65 : using com::sun::star::xml::sax::XDocumentHandler;
66 : using com::sun::star::xml::sax::XParser;
67 :
68 : using namespace ::com::sun::star::frame;
69 : using namespace ::com::sun::star;
70 : using namespace com::sun::star::container;
71 : using namespace com::sun::star::uno;
72 : using namespace com::sun::star::beans;
73 :
74 : namespace {
75 :
76 0 : bool isXMLStream(const ::rtl::OString& aHeaderStrm)
77 : {
78 0 : const char* p = aHeaderStrm.getStr();
79 0 : size_t n = aHeaderStrm.getLength();
80 0 : size_t i = 0;
81 :
82 : // Skip all preceding blank characters.
83 0 : for (i = 0; i < n; ++i, ++p)
84 : {
85 0 : sal_Char c = *p;
86 0 : if (c == ' ' || c == '\n' || c == '\t')
87 0 : continue;
88 0 : break;
89 : }
90 :
91 0 : n -= i;
92 :
93 : // First text must be '<?xml', else it's not a valid XML file stream.
94 0 : const char* sInitChars = "<?xml";
95 0 : const size_t nInitCharLen = std::strlen(sInitChars);
96 0 : for (i = 0; i < n; ++i, ++p)
97 : {
98 0 : if (i < nInitCharLen)
99 : {
100 0 : if (*p != sInitChars[i])
101 0 : return false;
102 : }
103 : }
104 0 : return true;
105 : }
106 :
107 0 : ::rtl::OUString supportedByType( const ::rtl::OUString clipBoardFormat , const ::rtl::OString resultString, const ::rtl::OUString checkType)
108 : {
109 0 : ::rtl::OUString sTypeName;
110 0 : if ( clipBoardFormat.match(OUString("doctype:")) )
111 : {
112 0 : ::rtl::OString tryStr = ::rtl::OUStringToOString(clipBoardFormat.copy(8),RTL_TEXTENCODING_ASCII_US).getStr();
113 0 : if (resultString.indexOf(tryStr) >= 0)
114 : {
115 0 : sTypeName = checkType;
116 0 : }
117 : }
118 0 : return sTypeName;
119 : }
120 :
121 : }
122 :
123 0 : ::rtl::OUString SAL_CALL FilterDetect::detect( com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >& aArguments ) throw( com::sun::star::uno::RuntimeException )
124 : {
125 0 : ::rtl::OUString sTypeName;
126 0 : ::rtl::OUString sUrl;
127 0 : Sequence<PropertyValue > lProps ;
128 :
129 0 : com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInStream;
130 0 : const PropertyValue * pValue = aArguments.getConstArray();
131 : sal_Int32 nLength;
132 0 : ::rtl::OString resultString;
133 :
134 0 : nLength = aArguments.getLength();
135 0 : sal_Int32 location=nLength;
136 0 : for (sal_Int32 i = 0 ; i < nLength; i++)
137 : {
138 0 : if ( pValue[i].Name == "TypeName" )
139 : {
140 0 : location=i;
141 : }
142 0 : else if ( pValue[i].Name == "URL" )
143 : {
144 0 : pValue[i].Value >>= sUrl;
145 : }
146 0 : else if ( pValue[i].Name == "InputStream" )
147 : {
148 0 : pValue[i].Value >>= xInStream ;
149 : }
150 : }
151 : try
152 : {
153 0 : if (!xInStream.is())
154 : {
155 : ::ucbhelper::Content aContent(
156 : sUrl, Reference< com::sun::star::ucb::XCommandEnvironment >(),
157 0 : mxCtx);
158 0 : xInStream = aContent.openStream();
159 0 : if (!xInStream.is())
160 : {
161 0 : return sTypeName;
162 0 : }
163 : }
164 0 : com::sun::star::uno::Sequence< sal_Int8 > aData;
165 0 : /* long nBytesToRead= */ xInStream->available();
166 0 : xInStream->skipBytes (0);
167 0 : long bytestRead =xInStream->readBytes (aData, 4000);
168 0 : resultString=::rtl::OString((const sal_Char *)aData.getConstArray(),bytestRead) ;
169 :
170 0 : if (!isXMLStream(resultString))
171 : // This is not an XML stream. It makes no sense to try to detect
172 : // a non-XML file type here.
173 0 : return ::rtl::OUString();
174 :
175 : // test typedetect code
176 0 : Reference <XNameAccess> xTypeCont(mxCtx->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", mxCtx), UNO_QUERY);
177 0 : Sequence < ::rtl::OUString > myTypes= xTypeCont->getElementNames();
178 0 : nLength = myTypes.getLength();
179 :
180 0 : sal_Int32 new_nlength=0;
181 0 : sal_Int32 i = 0 ;
182 0 : while ((i < nLength) && (sTypeName.isEmpty()))
183 : {
184 0 : Any elem = xTypeCont->getByName(myTypes[i]);
185 0 : elem >>=lProps;
186 0 : new_nlength = lProps.getLength();
187 0 : sal_Int32 j =0;
188 0 : while (j < new_nlength && (sTypeName.isEmpty()))
189 : {
190 0 : ::rtl::OUString tmpStr;
191 0 : lProps[j].Value >>=tmpStr;
192 0 : if ( lProps[j].Name == "ClipboardFormat" && !tmpStr.isEmpty() )
193 : {
194 0 : sTypeName = supportedByType(tmpStr,resultString, myTypes[i]);
195 : }
196 0 : j++;
197 0 : }
198 0 : i++;
199 0 : }
200 : }
201 0 : catch (const Exception &)
202 : {
203 : OSL_FAIL( "An Exception occurred while opening File stream" );
204 : }
205 :
206 0 : if (!sTypeName.isEmpty())
207 : {
208 0 : if (location == aArguments.getLength())
209 : {
210 0 : aArguments.realloc(nLength+1);
211 0 : aArguments[location].Name = ::rtl::OUString( "TypeName" );
212 : }
213 0 : aArguments[location].Value <<=sTypeName;
214 : }
215 :
216 0 : return sTypeName;
217 : }
218 :
219 : // XInitialization
220 :
221 0 : void SAL_CALL FilterDetect::initialize( const Sequence< Any >& aArguments )
222 : throw (Exception, RuntimeException)
223 : {
224 0 : Sequence < PropertyValue > aAnySeq;
225 0 : sal_Int32 nLength = aArguments.getLength();
226 0 : if ( nLength && ( aArguments[0] >>= aAnySeq ) )
227 : {
228 0 : const PropertyValue * pValue = aAnySeq.getConstArray();
229 0 : nLength = aAnySeq.getLength();
230 0 : for ( sal_Int32 i = 0 ; i < nLength; i++)
231 : {
232 :
233 0 : if ( pValue[i].Name == "Type" )
234 : {
235 0 : pValue[i].Value >>= msFilterName;
236 :
237 : }
238 0 : else if ( pValue[i].Name == "UserData" )
239 : {
240 :
241 0 : pValue[i].Value >>= msUserData;
242 :
243 : }
244 0 : else if ( pValue[i].Name == "TemplateName" )
245 : {
246 :
247 0 : pValue[i].Value>>=msTemplateName;
248 : }
249 :
250 : }
251 0 : }
252 0 : }
253 :
254 :
255 :
256 0 : OUString FilterDetect_getImplementationName ()
257 : {
258 0 : return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.filters.XMLFilterDetect" ) );
259 : }
260 : #define SERVICE_NAME1 "com.sun.star.document.ExtendedTypeDetection"
261 :
262 0 : Sequence< OUString > FilterDetect_getSupportedServiceNames()
263 : {
264 0 : Sequence < OUString > aRet(1);
265 0 : aRet[0] = SERVICE_NAME1;
266 0 : return aRet;
267 : }
268 :
269 0 : Reference< XInterface > FilterDetect_createInstance( const Reference< XComponentContext > & context)
270 : {
271 0 : return static_cast< cppu::OWeakObject * >( new FilterDetect( context ) );
272 : }
273 :
274 : // XServiceInfo
275 0 : OUString SAL_CALL FilterDetect::getImplementationName( )
276 : throw (RuntimeException)
277 : {
278 0 : return FilterDetect_getImplementationName();
279 : }
280 0 : sal_Bool SAL_CALL FilterDetect::supportsService( const OUString& rServiceName )
281 : throw (RuntimeException)
282 : {
283 0 : return rServiceName == SERVICE_NAME1;
284 : }
285 0 : Sequence< OUString > SAL_CALL FilterDetect::getSupportedServiceNames( )
286 : throw (RuntimeException)
287 : {
288 0 : return FilterDetect_getSupportedServiceNames();
289 0 : }
290 :
291 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|