1d107581fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3d107581fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4d107581fSAndrew Rist * or more contributor license agreements. See the NOTICE file
5d107581fSAndrew Rist * distributed with this work for additional information
6d107581fSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7d107581fSAndrew Rist * to you under the Apache License, Version 2.0 (the
8d107581fSAndrew Rist * "License"); you may not use this file except in compliance
9d107581fSAndrew Rist * with the License. You may obtain a copy of the License at
10d107581fSAndrew Rist *
11d107581fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12d107581fSAndrew Rist *
13d107581fSAndrew Rist * Unless required by applicable law or agreed to in writing,
14d107581fSAndrew Rist * software distributed under the License is distributed on an
15d107581fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16d107581fSAndrew Rist * KIND, either express or implied. See the License for the
17d107581fSAndrew Rist * specific language governing permissions and limitations
18d107581fSAndrew Rist * under the License.
19d107581fSAndrew Rist *
20d107581fSAndrew Rist *************************************************************/
21d107581fSAndrew Rist
22d107581fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_starmath.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "smdetect.hxx"
28cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
30cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
31cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
32cdf0e10cSrcweir #include <com/sun/star/awt/XWindow.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp>
34cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
35cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
36cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
37cdf0e10cSrcweir #include <com/sun/star/task/XInteractionHandler.hpp>
38cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
39cdf0e10cSrcweir #include <com/sun/star/ucb/CommandAbortedException.hpp>
40cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveAppException.hpp>
41cdf0e10cSrcweir #include <com/sun/star/ucb/XContent.hpp>
42cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipIOException.hpp>
43cdf0e10cSrcweir #include <framework/interaction.hxx>
44cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
45cdf0e10cSrcweir #include <ucbhelper/simpleinteractionrequest.hxx>
46cdf0e10cSrcweir #include <rtl/ustring.h>
47cdf0e10cSrcweir #include <rtl/logfile.hxx>
48cdf0e10cSrcweir #include <svl/itemset.hxx>
49cdf0e10cSrcweir #include <vcl/window.hxx>
50cdf0e10cSrcweir #include <svl/eitem.hxx>
51cdf0e10cSrcweir #include <svl/stritem.hxx>
52cdf0e10cSrcweir #include <tools/urlobj.hxx>
53cdf0e10cSrcweir #include <vos/mutex.hxx>
54cdf0e10cSrcweir #include <svtools/sfxecode.hxx>
55cdf0e10cSrcweir #include <svtools/ehdl.hxx>
56cdf0e10cSrcweir #include <sot/storinfo.hxx>
57cdf0e10cSrcweir #include <vcl/svapp.hxx>
58cdf0e10cSrcweir #include <sfx2/app.hxx>
59cdf0e10cSrcweir #include <sfx2/sfxsids.hrc>
60cdf0e10cSrcweir #include <sfx2/request.hxx>
61cdf0e10cSrcweir #include <sfx2/docfile.hxx>
62cdf0e10cSrcweir #include <sfx2/docfilt.hxx>
63cdf0e10cSrcweir #include <sfx2/fcontnr.hxx>
64cdf0e10cSrcweir #include <sfx2/brokenpackageint.hxx>
65cdf0e10cSrcweir
66cdf0e10cSrcweir #include "document.hxx"
67cdf0e10cSrcweir #include "eqnolefilehdr.hxx"
68cdf0e10cSrcweir
69cdf0e10cSrcweir using namespace ::com::sun::star;
70cdf0e10cSrcweir using namespace ::com::sun::star::uno;
71cdf0e10cSrcweir using namespace ::com::sun::star::io;
72cdf0e10cSrcweir using namespace ::com::sun::star::frame;
73cdf0e10cSrcweir using namespace ::com::sun::star::task;
74cdf0e10cSrcweir using namespace ::com::sun::star::beans;
75cdf0e10cSrcweir using namespace ::com::sun::star::lang;
76cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
77cdf0e10cSrcweir using namespace ::rtl;
78cdf0e10cSrcweir
SmFilterDetect(const REFERENCE<::com::sun::star::lang::XMultiServiceFactory> &)79cdf0e10cSrcweir SmFilterDetect::SmFilterDetect( const REFERENCE < ::com::sun::star::lang::XMultiServiceFactory >& /*xFactory*/ )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir }
82cdf0e10cSrcweir
~SmFilterDetect()83cdf0e10cSrcweir SmFilterDetect::~SmFilterDetect()
84cdf0e10cSrcweir {
85cdf0e10cSrcweir }
86cdf0e10cSrcweir
detect(::com::sun::star::uno::Sequence<::com::sun::star::beans::PropertyValue> & lDescriptor)87cdf0e10cSrcweir ::rtl::OUString SAL_CALL SmFilterDetect::detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& lDescriptor ) throw( ::com::sun::star::uno::RuntimeException )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir REFERENCE< XInputStream > xStream;
90cdf0e10cSrcweir REFERENCE< XContent > xContent;
91cdf0e10cSrcweir REFERENCE< XInteractionHandler > xInteraction;
92cdf0e10cSrcweir String aURL;
93cdf0e10cSrcweir ::rtl::OUString sTemp;
94cdf0e10cSrcweir String aTypeName; // a name describing the type (from MediaDescriptor, usually from flat detection)
95cdf0e10cSrcweir String aPreselectedFilterName; // a name describing the filter to use (from MediaDescriptor, usually from UI action)
96cdf0e10cSrcweir
97cdf0e10cSrcweir ::rtl::OUString aDocumentTitle; // interesting only if set in this method
98cdf0e10cSrcweir
99cdf0e10cSrcweir // opening as template is done when a parameter tells to do so and a template filter can be detected
100cdf0e10cSrcweir // (otherwise no valid filter would be found) or if the detected filter is a template filter and
101cdf0e10cSrcweir // there is no parameter that forbids to open as template
102cdf0e10cSrcweir sal_Bool bOpenAsTemplate = sal_False;
103cdf0e10cSrcweir sal_Bool bWasReadOnly = sal_False, bReadOnly = sal_False;
104cdf0e10cSrcweir
105cdf0e10cSrcweir sal_Bool bRepairPackage = sal_False;
106cdf0e10cSrcweir sal_Bool bRepairAllowed = sal_False;
107cdf0e10cSrcweir
108cdf0e10cSrcweir // now some parameters that can already be in the array, but may be overwritten or new inserted here
109cdf0e10cSrcweir // remember their indices in the case new values must be added to the array
110cdf0e10cSrcweir sal_Int32 nPropertyCount = lDescriptor.getLength();
111cdf0e10cSrcweir sal_Int32 nIndexOfFilterName = -1;
112cdf0e10cSrcweir sal_Int32 nIndexOfInputStream = -1;
113cdf0e10cSrcweir sal_Int32 nIndexOfContent = -1;
114cdf0e10cSrcweir sal_Int32 nIndexOfReadOnlyFlag = -1;
115cdf0e10cSrcweir sal_Int32 nIndexOfTemplateFlag = -1;
116cdf0e10cSrcweir sal_Int32 nIndexOfDocumentTitle = -1;
117cdf0e10cSrcweir
118cdf0e10cSrcweir for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir // extract properties
121cdf0e10cSrcweir if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("URL")) )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir lDescriptor[nProperty].Value >>= sTemp;
124cdf0e10cSrcweir aURL = sTemp;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir else if( !aURL.Len() && lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FileName")) )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir lDescriptor[nProperty].Value >>= sTemp;
129cdf0e10cSrcweir aURL = sTemp;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("TypeName")) )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir lDescriptor[nProperty].Value >>= sTemp;
134cdf0e10cSrcweir aTypeName = sTemp;
135cdf0e10cSrcweir }
136cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FilterName")) )
137cdf0e10cSrcweir {
138cdf0e10cSrcweir lDescriptor[nProperty].Value >>= sTemp;
139cdf0e10cSrcweir aPreselectedFilterName = sTemp;
140cdf0e10cSrcweir
141cdf0e10cSrcweir // if the preselected filter name is not correct, it must be erased after detection
142cdf0e10cSrcweir // remember index of property to get access to it later
143cdf0e10cSrcweir nIndexOfFilterName = nProperty;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InputStream")) )
146cdf0e10cSrcweir nIndexOfInputStream = nProperty;
147cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly")) )
148cdf0e10cSrcweir nIndexOfReadOnlyFlag = nProperty;
149cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("UCBContent")) )
150cdf0e10cSrcweir nIndexOfContent = nProperty;
151cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("AsTemplate")) )
152cdf0e10cSrcweir {
153cdf0e10cSrcweir lDescriptor[nProperty].Value >>= bOpenAsTemplate;
154cdf0e10cSrcweir nIndexOfTemplateFlag = nProperty;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InteractionHandler")) )
157cdf0e10cSrcweir lDescriptor[nProperty].Value >>= xInteraction;
158cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("RepairPackage")) )
159cdf0e10cSrcweir lDescriptor[nProperty].Value >>= bRepairPackage;
160cdf0e10cSrcweir else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("DocumentTitle")) )
161cdf0e10cSrcweir nIndexOfDocumentTitle = nProperty;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir
164cdf0e10cSrcweir // can't check the type for external filters, so set the "dont" flag accordingly
165cdf0e10cSrcweir ::vos::OGuard aGuard( Application::GetSolarMutex() );
166cdf0e10cSrcweir //SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
167cdf0e10cSrcweir
168cdf0e10cSrcweir SfxApplication* pApp = SFX_APP();
169cdf0e10cSrcweir SfxAllItemSet *pSet = new SfxAllItemSet( pApp->GetPool() );
170cdf0e10cSrcweir TransformParameters( SID_OPENDOC, lDescriptor, *pSet );
171cdf0e10cSrcweir SFX_ITEMSET_ARG( pSet, pItem, SfxBoolItem, SID_DOC_READONLY, sal_False );
172cdf0e10cSrcweir
173cdf0e10cSrcweir bWasReadOnly = pItem && pItem->GetValue();
174cdf0e10cSrcweir
175cdf0e10cSrcweir String aFilterName;
176cdf0e10cSrcweir String aPrefix = String::CreateFromAscii( "private:factory/" );
177cdf0e10cSrcweir if( aURL.Match( aPrefix ) == aPrefix.Len() )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir const SfxFilter* pFilter = 0;
180cdf0e10cSrcweir String aPattern( aPrefix );
181cdf0e10cSrcweir aPattern += String::CreateFromAscii("smath");
182cdf0e10cSrcweir if ( aURL.Match( aPattern ) >= aPattern.Len() )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir pFilter = SfxFilter::GetDefaultFilterFromFactory( aURL );
185cdf0e10cSrcweir aTypeName = pFilter->GetTypeName();
186cdf0e10cSrcweir aFilterName = pFilter->GetName();
187cdf0e10cSrcweir }
188cdf0e10cSrcweir }
189cdf0e10cSrcweir else
190cdf0e10cSrcweir {
191cdf0e10cSrcweir // ctor of SfxMedium uses owner transition of ItemSet
192cdf0e10cSrcweir SfxMedium aMedium( aURL, bWasReadOnly ? STREAM_STD_READ : STREAM_STD_READWRITE, sal_False, NULL, pSet );
193cdf0e10cSrcweir aMedium.UseInteractionHandler( sal_True );
194cdf0e10cSrcweir
195cdf0e10cSrcweir sal_Bool bIsStorage = aMedium.IsStorage();
196cdf0e10cSrcweir if ( aMedium.GetErrorCode() == ERRCODE_NONE )
197cdf0e10cSrcweir {
198cdf0e10cSrcweir // remember input stream and content and put them into the descriptor later
199cdf0e10cSrcweir // should be done here since later the medium can switch to a version
200cdf0e10cSrcweir xStream = aMedium.GetInputStream();
201cdf0e10cSrcweir xContent = aMedium.GetContent();
202cdf0e10cSrcweir bReadOnly = aMedium.IsReadOnly();
203cdf0e10cSrcweir
204cdf0e10cSrcweir if ( bIsStorage )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir //TODO/LATER: factor this out!
207cdf0e10cSrcweir uno::Reference < embed::XStorage > xStorage = aMedium.GetStorage( sal_False );
208cdf0e10cSrcweir if ( aMedium.GetLastStorageCreationState() != ERRCODE_NONE )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir // error during storage creation means _here_ that the medium
211cdf0e10cSrcweir // is broken, but we can not handle it in medium since unpossibility
212cdf0e10cSrcweir // to create a storage does not _always_ means that the medium is broken
213cdf0e10cSrcweir aMedium.SetError( aMedium.GetLastStorageCreationState(), ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
214cdf0e10cSrcweir if ( xInteraction.is() )
215cdf0e10cSrcweir {
216cdf0e10cSrcweir OUString empty;
217cdf0e10cSrcweir try
218cdf0e10cSrcweir {
219cdf0e10cSrcweir InteractiveAppException xException( empty,
220cdf0e10cSrcweir REFERENCE< XInterface >(),
221cdf0e10cSrcweir InteractionClassification_ERROR,
222cdf0e10cSrcweir aMedium.GetError() );
223cdf0e10cSrcweir
224cdf0e10cSrcweir REFERENCE< XInteractionRequest > xRequest(
225cdf0e10cSrcweir new ucbhelper::SimpleInteractionRequest( makeAny( xException ),
226cdf0e10cSrcweir ucbhelper::CONTINUATION_APPROVE ) );
227cdf0e10cSrcweir xInteraction->handle( xRequest );
228cdf0e10cSrcweir }
229cdf0e10cSrcweir catch ( Exception & ) {};
230cdf0e10cSrcweir }
231cdf0e10cSrcweir }
232cdf0e10cSrcweir else
233cdf0e10cSrcweir {
234cdf0e10cSrcweir aFilterName.Erase();
235cdf0e10cSrcweir
236cdf0e10cSrcweir try
237cdf0e10cSrcweir {
238cdf0e10cSrcweir const SfxFilter* pFilter = aPreselectedFilterName.Len() ?
239cdf0e10cSrcweir SfxFilterMatcher().GetFilter4FilterName( aPreselectedFilterName ) : aTypeName.Len() ?
240cdf0e10cSrcweir SfxFilterMatcher(String::CreateFromAscii("smath")).GetFilter4EA( aTypeName ) : 0;
241cdf0e10cSrcweir String aTmpFilterName;
242cdf0e10cSrcweir if ( pFilter )
243cdf0e10cSrcweir aTmpFilterName = pFilter->GetName();
244cdf0e10cSrcweir aTypeName = SfxFilter::GetTypeFromStorage( xStorage, pFilter ? pFilter->IsAllowedAsTemplate() : sal_False, &aTmpFilterName );
245cdf0e10cSrcweir }
246cdf0e10cSrcweir catch( lang::WrappedTargetException& aWrap )
247cdf0e10cSrcweir {
248cdf0e10cSrcweir packages::zip::ZipIOException aZipException;
249cdf0e10cSrcweir
250cdf0e10cSrcweir // repairing is done only if this type is requested from outside
251cdf0e10cSrcweir if ( ( aWrap.TargetException >>= aZipException ) && aTypeName.Len() )
252cdf0e10cSrcweir {
253cdf0e10cSrcweir if ( xInteraction.is() )
254cdf0e10cSrcweir {
255cdf0e10cSrcweir // the package is broken one
256cdf0e10cSrcweir aDocumentTitle = aMedium.GetURLObject().getName(
257cdf0e10cSrcweir INetURLObject::LAST_SEGMENT,
258cdf0e10cSrcweir true,
259cdf0e10cSrcweir INetURLObject::DECODE_WITH_CHARSET );
260cdf0e10cSrcweir
261cdf0e10cSrcweir if ( !bRepairPackage )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir // ask the user whether he wants to try to repair
264cdf0e10cSrcweir RequestPackageReparation aRequest( aDocumentTitle );
265cdf0e10cSrcweir xInteraction->handle( aRequest.GetRequest() );
266cdf0e10cSrcweir bRepairAllowed = aRequest.isApproved();
267cdf0e10cSrcweir }
268cdf0e10cSrcweir
269cdf0e10cSrcweir if ( !bRepairAllowed )
270cdf0e10cSrcweir {
271cdf0e10cSrcweir // repair either not allowed or not successful
272cdf0e10cSrcweir NotifyBrokenPackage aNotifyRequest( aDocumentTitle );
273cdf0e10cSrcweir xInteraction->handle( aNotifyRequest.GetRequest() );
274cdf0e10cSrcweir }
275cdf0e10cSrcweir }
276cdf0e10cSrcweir
277cdf0e10cSrcweir if ( !bRepairAllowed )
278cdf0e10cSrcweir aTypeName.Erase();
279cdf0e10cSrcweir }
280cdf0e10cSrcweir }
281cdf0e10cSrcweir catch( uno::RuntimeException& )
282cdf0e10cSrcweir {
283cdf0e10cSrcweir throw;
284cdf0e10cSrcweir }
285cdf0e10cSrcweir catch( uno::Exception& )
286cdf0e10cSrcweir {
287cdf0e10cSrcweir aTypeName.Erase();
288cdf0e10cSrcweir }
289cdf0e10cSrcweir
290cdf0e10cSrcweir if ( aTypeName.Len() )
291cdf0e10cSrcweir {
292cdf0e10cSrcweir const SfxFilter* pFilter =
293cdf0e10cSrcweir SfxFilterMatcher( String::CreateFromAscii("smath") ).GetFilter4EA( aTypeName );
294cdf0e10cSrcweir if ( pFilter )
295cdf0e10cSrcweir aFilterName = pFilter->GetName();
296cdf0e10cSrcweir }
297cdf0e10cSrcweir }
298cdf0e10cSrcweir }
299cdf0e10cSrcweir else
300cdf0e10cSrcweir {
301*d30fb54cSRegina Henschel // DesignScience Equation Editor MathType 3.0 ?
302cdf0e10cSrcweir SvStream *pStrm = aMedium.GetInStream();
303cdf0e10cSrcweir aTypeName.Erase();
304cdf0e10cSrcweir if (pStrm && !pStrm->GetError())
305cdf0e10cSrcweir {
306cdf0e10cSrcweir SotStorageRef aStorage = new SotStorage ( pStrm, sal_False );
307cdf0e10cSrcweir if ( !aStorage->GetError() )
308cdf0e10cSrcweir {
309cdf0e10cSrcweir if ( aStorage->IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Equation Native" ) ) ) )
310cdf0e10cSrcweir {
311cdf0e10cSrcweir sal_uInt8 nVersion;
312cdf0e10cSrcweir if (GetMathTypeVersion( aStorage, nVersion ) && nVersion <=3)
313cdf0e10cSrcweir aTypeName.AssignAscii( "math_MathType_3x" );
314cdf0e10cSrcweir }
315cdf0e10cSrcweir }
316cdf0e10cSrcweir else
317cdf0e10cSrcweir {
318*d30fb54cSRegina Henschel // MathML? The SAX parser expects the 'math' root element incl.
319*d30fb54cSRegina Henschel // the namespace URL. Neither '<?xml' prolog nor <!doctype is needed.
320*d30fb54cSRegina Henschel // If the math element has a prefix (e.g. <bla:math), the
321*d30fb54cSRegina Henschel // prefix has to be defined in the namespace attribut
322*d30fb54cSRegina Henschel // (e.g. xmlns:bla="http://www.w3.org/1998/Math/MathML")
323*d30fb54cSRegina Henschel // #124636 is fixed too.
324cdf0e10cSrcweir pStrm->Seek( STREAM_SEEK_TO_BEGIN );
325*d30fb54cSRegina Henschel const size_t nBufSize=2048;
326*d30fb54cSRegina Henschel sal_uInt16 aBuffer[nBufSize]; // will be casted to an Unicode-Array below
327*d30fb54cSRegina Henschel sal_uInt8* pByte = reinterpret_cast<sal_uInt8*>(aBuffer);
328*d30fb54cSRegina Henschel const sal_uLong nBytesRead(pStrm->Read( pByte, nBufSize * 2 ) );
329*d30fb54cSRegina Henschel const sal_uLong nUnicodeCharsRead (nBytesRead / 2 );
330*d30fb54cSRegina Henschel
331*d30fb54cSRegina Henschel // For backwards searching an OUString is used. The conversion needs an
332*d30fb54cSRegina Henschel // encoding information. Default encoding is UTF-8, UTF-16 is possible
333*d30fb54cSRegina Henschel // (e.g. from MS "Math Input Control"), others are unlikely.
334*d30fb54cSRegina Henschel // Looking for Byte Order Mark
335*d30fb54cSRegina Henschel rtl_TextEncoding aEncoding = RTL_TEXTENCODING_UTF8;
336*d30fb54cSRegina Henschel bool bIsUnicode = false;
337*d30fb54cSRegina Henschel if (nBytesRead >= 2 && (aBuffer[0]==0xfffe || aBuffer[0]==0xfeff) )
338cdf0e10cSrcweir {
339*d30fb54cSRegina Henschel aEncoding = RTL_TEXTENCODING_UNICODE;
340*d30fb54cSRegina Henschel bIsUnicode = true;
341*d30fb54cSRegina Henschel if ( aBuffer[0] == 0xfffe)
342*d30fb54cSRegina Henschel { //swap bytes to make Big Endian
343*d30fb54cSRegina Henschel for (size_t i=0; i < nUnicodeCharsRead; ++i)
344*d30fb54cSRegina Henschel {
345*d30fb54cSRegina Henschel aBuffer[i] = (aBuffer[i]>>8) | (aBuffer[i]<<8) ;
346*d30fb54cSRegina Henschel }
347*d30fb54cSRegina Henschel }
348*d30fb54cSRegina Henschel }
3494ec5d36fSArmin Le Grand
350*d30fb54cSRegina Henschel bool isMathFile(false);
351*d30fb54cSRegina Henschel if ( nBytesRead > 56) // minimal <math xmlns="http://www.w3.org/1998/Math/MathML"></math>
352*d30fb54cSRegina Henschel {
353*d30fb54cSRegina Henschel const sal_Char* pChar = reinterpret_cast<sal_Char*>(aBuffer);
354*d30fb54cSRegina Henschel sal_Unicode* pUnicode = (sal_Unicode*) aBuffer;
355*d30fb54cSRegina Henschel
356*d30fb54cSRegina Henschel const OUString sFragment( (bIsUnicode)
357*d30fb54cSRegina Henschel ? OUString( pUnicode , nUnicodeCharsRead )
358*d30fb54cSRegina Henschel : OUString( pChar, nBytesRead, aEncoding) );
359*d30fb54cSRegina Henschel const sal_Int32 nFragmentLength(sFragment.getLength());
360*d30fb54cSRegina Henschel
361*d30fb54cSRegina Henschel // look for MathML URL http://www.w3.org/1998/Math/MathML
362*d30fb54cSRegina Henschel // #i53509 A MathML URL can be value of a namespace attribute, but can be as well
363*d30fb54cSRegina Henschel // inside a doctype e.g. [<!ENTITY mathml 'http://www.w3.org/1998/Math/MathML'>]
364*d30fb54cSRegina Henschel // or inside a schema reference e.g. s:schemaLocation="http://www.w3.org/1998/Math/MathML"
365*d30fb54cSRegina Henschel // Use a loop to get the correct one.
366*d30fb54cSRegina Henschel const OUString sURL( OUString::createFromAscii("http://www.w3.org/1998/Math/MathML"));
367*d30fb54cSRegina Henschel const sal_Int32 nURLLength = sURL.getLength();
368*d30fb54cSRegina Henschel const OUString sEQ( OUString::createFromAscii("=") );
369*d30fb54cSRegina Henschel const OUString sXMLNS( OUString::createFromAscii("xmlns") );
370*d30fb54cSRegina Henschel sal_Int32 nPosURL = -1; // for index of first character of URL
371*d30fb54cSRegina Henschel sal_Int32 nPosURLSearchStart = 0;
372*d30fb54cSRegina Henschel sal_Int32 nPosEQ = -1; // for index of equal sign
373*d30fb54cSRegina Henschel sal_Int32 nPosXMLNS = -1; // for index of first character of string "xmlns"
374*d30fb54cSRegina Henschel do
3754ec5d36fSArmin Le Grand {
376*d30fb54cSRegina Henschel nPosURL = sFragment.indexOf(sURL,nPosURLSearchStart);
377*d30fb54cSRegina Henschel if( nPosURL < 0 )
378*d30fb54cSRegina Henschel {
379*d30fb54cSRegina Henschel break; // no MathML URL, cannot be parsed
380*d30fb54cSRegina Henschel }
381*d30fb54cSRegina Henschel // need 'xmlns:prefix =' or 'xmlns =', look backwards, first for equal sign
382*d30fb54cSRegina Henschel nPosEQ = sFragment.lastIndexOf(sEQ,nPosURL);
383*d30fb54cSRegina Henschel if (nPosEQ >= 0 && nPosEQ >= nPosURLSearchStart)
3844ec5d36fSArmin Le Grand {
385*d30fb54cSRegina Henschel nPosXMLNS = sFragment.lastIndexOf(sXMLNS,nPosEQ);
386*d30fb54cSRegina Henschel if( nPosXMLNS >= nPosURLSearchStart )
387*d30fb54cSRegina Henschel { // an xmlns attribute is found, but it might belong to a schema
388*d30fb54cSRegina Henschel // get prefix if present
389*d30fb54cSRegina Henschel const OUString sPrefix = (sFragment.copy(nPosXMLNS+5,nPosEQ-(nPosXMLNS+5))).trim();
390*d30fb54cSRegina Henschel // such prefix definition must start with colon (will be removed below)
391*d30fb54cSRegina Henschel bool bHasPrefix( (sPrefix.isEmpty()) ? false : sPrefix.toChar() == sal_Unicode(':') );
392*d30fb54cSRegina Henschel // the math element starts either with '<prefix:math' or '<math'
393*d30fb54cSRegina Henschel const OUString sMathStart( (bHasPrefix)
394*d30fb54cSRegina Henschel ? OUString::createFromAscii("<") + sPrefix.copy(1,sPrefix.getLength()-1) + OUString::createFromAscii(":math")
395*d30fb54cSRegina Henschel : OUString::createFromAscii("<math") );
396*d30fb54cSRegina Henschel sal_Int32 nPosMath (sFragment.lastIndexOf(sMathStart,nPosXMLNS));
397*d30fb54cSRegina Henschel if( nPosMath >= 0)
398*d30fb54cSRegina Henschel { // xmlns attribute belongs to math element
399*d30fb54cSRegina Henschel isMathFile = true;
400*d30fb54cSRegina Henschel break;
4014ec5d36fSArmin Le Grand }
4024ec5d36fSArmin Le Grand }
4034ec5d36fSArmin Le Grand }
404*d30fb54cSRegina Henschel // MathML URL was wrong one, look for next
405*d30fb54cSRegina Henschel nPosURLSearchStart = nPosURL + nURLLength;
4064ec5d36fSArmin Le Grand }
407*d30fb54cSRegina Henschel while ( nPosURLSearchStart + nURLLength <= nFragmentLength);
4084ec5d36fSArmin Le Grand
4094ec5d36fSArmin Le Grand if(isMathFile)
410cdf0e10cSrcweir {
411cdf0e10cSrcweir static const sal_Char sFltrNm_2[] = MATHML_XML;
412cdf0e10cSrcweir static const sal_Char sTypeNm_2[] = "math_MathML_XML_Math";
4134ec5d36fSArmin Le Grand
414cdf0e10cSrcweir aFilterName.AssignAscii( sFltrNm_2 );
415cdf0e10cSrcweir aTypeName.AssignAscii( sTypeNm_2 );
416cdf0e10cSrcweir }
417cdf0e10cSrcweir }
418cdf0e10cSrcweir }
419cdf0e10cSrcweir
420cdf0e10cSrcweir if ( aTypeName.Len() )
421cdf0e10cSrcweir {
422cdf0e10cSrcweir const SfxFilter* pFilt = SfxFilterMatcher( String::CreateFromAscii("smath") ).GetFilter4EA( aTypeName );
423cdf0e10cSrcweir if ( pFilt )
424cdf0e10cSrcweir aFilterName = pFilt->GetName();
425cdf0e10cSrcweir }
426cdf0e10cSrcweir }
427cdf0e10cSrcweir }
428cdf0e10cSrcweir }
429cdf0e10cSrcweir }
430cdf0e10cSrcweir
431cdf0e10cSrcweir if ( nIndexOfInputStream == -1 && xStream.is() )
432cdf0e10cSrcweir {
433cdf0e10cSrcweir // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
434cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 );
435cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("InputStream");
436cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= xStream;
437cdf0e10cSrcweir nPropertyCount++;
438cdf0e10cSrcweir }
439cdf0e10cSrcweir
440cdf0e10cSrcweir if ( nIndexOfContent == -1 && xContent.is() )
441cdf0e10cSrcweir {
442cdf0e10cSrcweir // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
443cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 );
444cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("UCBContent");
445cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= xContent;
446cdf0e10cSrcweir nPropertyCount++;
447cdf0e10cSrcweir }
448cdf0e10cSrcweir
449cdf0e10cSrcweir if ( bReadOnly != bWasReadOnly )
450cdf0e10cSrcweir {
451cdf0e10cSrcweir if ( nIndexOfReadOnlyFlag == -1 )
452cdf0e10cSrcweir {
453cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 );
454cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("ReadOnly");
455cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= bReadOnly;
456cdf0e10cSrcweir nPropertyCount++;
457cdf0e10cSrcweir }
458cdf0e10cSrcweir else
459cdf0e10cSrcweir lDescriptor[nIndexOfReadOnlyFlag].Value <<= bReadOnly;
460cdf0e10cSrcweir }
461cdf0e10cSrcweir
462cdf0e10cSrcweir if ( !bRepairPackage && bRepairAllowed )
463cdf0e10cSrcweir {
464cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 );
465cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("RepairPackage");
466cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= bRepairAllowed;
467cdf0e10cSrcweir nPropertyCount++;
468cdf0e10cSrcweir
469cdf0e10cSrcweir bOpenAsTemplate = sal_True;
470cdf0e10cSrcweir
471cdf0e10cSrcweir // TODO/LATER: set progress bar that should be used
472cdf0e10cSrcweir }
473cdf0e10cSrcweir
474cdf0e10cSrcweir if ( bOpenAsTemplate )
475cdf0e10cSrcweir {
476cdf0e10cSrcweir if ( nIndexOfTemplateFlag == -1 )
477cdf0e10cSrcweir {
478cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 );
479cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("AsTemplate");
480cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= bOpenAsTemplate;
481cdf0e10cSrcweir nPropertyCount++;
482cdf0e10cSrcweir }
483cdf0e10cSrcweir else
484cdf0e10cSrcweir lDescriptor[nIndexOfTemplateFlag].Value <<= bOpenAsTemplate;
485cdf0e10cSrcweir }
486cdf0e10cSrcweir
487cdf0e10cSrcweir if ( aDocumentTitle.getLength() )
488cdf0e10cSrcweir {
489cdf0e10cSrcweir // the title was set here
490cdf0e10cSrcweir if ( nIndexOfDocumentTitle == -1 )
491cdf0e10cSrcweir {
492cdf0e10cSrcweir lDescriptor.realloc( nPropertyCount + 1 );
493cdf0e10cSrcweir lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("DocumentTitle");
494cdf0e10cSrcweir lDescriptor[nPropertyCount].Value <<= aDocumentTitle;
495cdf0e10cSrcweir nPropertyCount++;
496cdf0e10cSrcweir }
497cdf0e10cSrcweir else
498cdf0e10cSrcweir lDescriptor[nIndexOfDocumentTitle].Value <<= aDocumentTitle;
499cdf0e10cSrcweir }
500cdf0e10cSrcweir
501cdf0e10cSrcweir if ( !aFilterName.Len() )
502cdf0e10cSrcweir aTypeName.Erase();
503cdf0e10cSrcweir
504cdf0e10cSrcweir return aTypeName;
505cdf0e10cSrcweir }
506cdf0e10cSrcweir
SFX_IMPL_SINGLEFACTORY(SmFilterDetect)507cdf0e10cSrcweir SFX_IMPL_SINGLEFACTORY( SmFilterDetect )
508cdf0e10cSrcweir
509cdf0e10cSrcweir /* XServiceInfo */
510cdf0e10cSrcweir UNOOUSTRING SAL_CALL SmFilterDetect::getImplementationName() throw( UNORUNTIMEEXCEPTION )
511cdf0e10cSrcweir {
512cdf0e10cSrcweir return impl_getStaticImplementationName();
513cdf0e10cSrcweir }
514cdf0e10cSrcweir \
515cdf0e10cSrcweir /* XServiceInfo */
supportsService(const UNOOUSTRING & sServiceName)516cdf0e10cSrcweir sal_Bool SAL_CALL SmFilterDetect::supportsService( const UNOOUSTRING& sServiceName ) throw( UNORUNTIMEEXCEPTION )
517cdf0e10cSrcweir {
518cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > seqServiceNames = getSupportedServiceNames();
519cdf0e10cSrcweir const UNOOUSTRING* pArray = seqServiceNames.getConstArray();
520cdf0e10cSrcweir for ( sal_Int32 nCounter=0; nCounter<seqServiceNames.getLength(); nCounter++ )
521cdf0e10cSrcweir {
522cdf0e10cSrcweir if ( pArray[nCounter] == sServiceName )
523cdf0e10cSrcweir {
524cdf0e10cSrcweir return sal_True ;
525cdf0e10cSrcweir }
526cdf0e10cSrcweir }
527cdf0e10cSrcweir return sal_False ;
528cdf0e10cSrcweir }
529cdf0e10cSrcweir
530cdf0e10cSrcweir /* XServiceInfo */
getSupportedServiceNames()531cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > SAL_CALL SmFilterDetect::getSupportedServiceNames() throw( UNORUNTIMEEXCEPTION )
532cdf0e10cSrcweir {
533cdf0e10cSrcweir return impl_getStaticSupportedServiceNames();
534cdf0e10cSrcweir }
535cdf0e10cSrcweir
536cdf0e10cSrcweir /* Helper for XServiceInfo */
impl_getStaticSupportedServiceNames()537cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > SmFilterDetect::impl_getStaticSupportedServiceNames()
538cdf0e10cSrcweir {
539cdf0e10cSrcweir UNOMUTEXGUARD aGuard( UNOMUTEX::getGlobalMutex() );
540cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > seqServiceNames( 1 );
541cdf0e10cSrcweir seqServiceNames.getArray() [0] = UNOOUSTRING::createFromAscii( "com.sun.star.frame.ExtendedTypeDetection" );
542cdf0e10cSrcweir return seqServiceNames ;
543cdf0e10cSrcweir }
544cdf0e10cSrcweir
545cdf0e10cSrcweir /* Helper for XServiceInfo */
impl_getStaticImplementationName()546cdf0e10cSrcweir UNOOUSTRING SmFilterDetect::impl_getStaticImplementationName()
547cdf0e10cSrcweir {
548cdf0e10cSrcweir return UNOOUSTRING::createFromAscii( "com.sun.star.comp.math.FormatDetector" );
549cdf0e10cSrcweir }
550cdf0e10cSrcweir
551cdf0e10cSrcweir /* Helper for registry */
impl_createInstance(const UNOREFERENCE<UNOXMULTISERVICEFACTORY> & xServiceManager)552cdf0e10cSrcweir UNOREFERENCE< UNOXINTERFACE > SAL_CALL SmFilterDetect::impl_createInstance( const UNOREFERENCE< UNOXMULTISERVICEFACTORY >& xServiceManager ) throw( UNOEXCEPTION )
553cdf0e10cSrcweir {
554cdf0e10cSrcweir return UNOREFERENCE< UNOXINTERFACE >( *new SmFilterDetect( xServiceManager ) );
555cdf0e10cSrcweir }
556cdf0e10cSrcweir
557