merge the formfield patch from ooo-build
[ooovba.git] / svx / source / msfilter / msfiltertracer.cxx
bloba1505e066421d2f14e2316bbacad09051801122a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: msfiltertracer.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <svx/msfiltertracer.hxx>
34 #include <vcl/svapp.hxx>
35 #include <tools/urlobj.hxx>
36 #include <com/sun/star/uno/Sequence.h>
37 #include <com/sun/star/util/logging/LogLevel.hpp>
38 #include <com/sun/star/util/SearchAlgorithms.hpp>
39 #include <com/sun/star/util/SearchFlags.hpp>
40 #include <comphelper/processfactory.hxx>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/io/XActiveDataSource.hpp>
43 #include <svtools/FilterConfigItem.hxx>
44 #include <unotools/localfilehelper.hxx>
45 #include <unotools/streamwrap.hxx>
46 #include <unotools/ucbstreamhelper.hxx>
49 // --------------
50 // - Namespaces -
51 // --------------
53 using namespace ::com::sun::star;
55 MSFilterTracer::MSFilterTracer( const ::rtl::OUString& rConfigPath, uno::Sequence< beans::PropertyValue >* pConfigData ) :
56 mpCfgItem( new FilterConfigItem( rConfigPath, pConfigData ) ),
57 mpAttributeList( new SvXMLAttributeList() ),
58 mpStream( NULL ),
59 mbEnabled( sal_False ) // will be set to true in StartTracing()
61 if ( mpCfgItem->ReadBool( rtl::OUString::createFromAscii( "On" ), sal_False ) )
63 uno::Reference< lang::XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
64 if ( xMgr.is() )
66 /* the following methods try to read a property, if it is not available it will put the second
67 parameter as default into the property sequence of the FilterConfigItem. It means we ensure that
68 the property is available by trying to read it (the return value of the method is ignored) */
69 ::rtl::OUString aEmptyString;
70 mpCfgItem->ReadInt32( rtl::OUString::createFromAscii( "LogLevel" ), util::logging::LogLevel::ALL );
71 mpCfgItem->ReadString( rtl::OUString::createFromAscii( "ClassFilter" ), aEmptyString );
72 mpCfgItem->ReadString( rtl::OUString::createFromAscii( "MethodFilter" ), aEmptyString );
73 mpCfgItem->ReadString( rtl::OUString::createFromAscii( "MessageFilter" ), aEmptyString );
74 util::SearchAlgorithms eSearchAlgorithm = (util::SearchAlgorithms)
75 mpCfgItem->ReadInt32( rtl::OUString::createFromAscii( "SearchAlgorithm" ), util::SearchAlgorithms_ABSOLUTE );
77 // creating the name of the log file
78 rtl::OUString aPath( mpCfgItem->ReadString( rtl::OUString::createFromAscii( "Path" ), aEmptyString ) );
79 rtl::OUString aName( mpCfgItem->ReadString( rtl::OUString::createFromAscii( "Name" ), aEmptyString ) );
80 rtl::OUString aDocumentURL( mpCfgItem->ReadString( rtl::OUString::createFromAscii( "DocumentURL" ), aEmptyString ) );
81 INetURLObject aLogFile( aDocumentURL );
82 if ( aLogFile.GetMainURL( INetURLObject::NO_DECODE ).getLength() )
84 if ( aPath.getLength() )
86 String aOldName( aLogFile.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::NO_DECODE ) );
87 aLogFile = INetURLObject( aPath );
88 aLogFile.insertName( aOldName );
90 if ( aName.getLength() )
91 aLogFile.setName( aName );
93 else
95 if ( aPath.getLength() )
96 aLogFile = INetURLObject( aPath );
97 else
99 String aURLStr;
100 if( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( Application::GetAppFileName(), aURLStr ) )
102 aLogFile = INetURLObject(aURLStr);
103 aLogFile .removeSegment();
104 aLogFile .removeFinalSlash();
107 if ( !aName.getLength() )
108 aName = rtl::OUString::createFromAscii( "tracer" );
109 aLogFile.insertName( aName );
111 aLogFile.setExtension( rtl::OUString::createFromAscii( "log" ) );
113 // creating the file stream
114 mpStream = ::utl::UcbStreamHelper::CreateStream( aLogFile.GetMainURL( INetURLObject::NO_DECODE ), STREAM_WRITE | STREAM_TRUNC | STREAM_SHARE_DENYNONE );
115 if ( mpStream && !mpStream->GetError() )
117 // creating a wrapper for our stream
118 utl::OOutputStreamWrapper* pHelper = new ::utl::OOutputStreamWrapper( *mpStream );
119 uno::Reference< io::XOutputStream > xOutputStream( pHelper );
121 // instanciating the DocumentHandler, then setting the OutputStream
122 mxHandler = uno::Reference< xml::sax::XDocumentHandler >( xMgr->createInstance( rtl::OUString::createFromAscii( "com.sun.star.xml.sax.Writer" ) ), uno::UNO_QUERY );
123 uno::Reference< io::XActiveDataSource > xDocSrc( mxHandler, uno::UNO_QUERY );
124 xDocSrc->setOutputStream( xOutputStream );
125 mxHandler->startDocument();
126 mxHandler->ignorableWhitespace ( rtl::OUString::createFromAscii( " " ) );
128 // writing the "DocumentHandler" property, so the FilterTracer component
129 // will use it for the output
130 uno::Any aAny;
131 aAny <<= xDocSrc;
132 mpCfgItem->WriteAny( rtl::OUString::createFromAscii( "DocumentHandler" ), aAny );
134 SvXMLAttributeList* pAttrList = new SvXMLAttributeList;
135 pAttrList->AddAttribute( rtl::OUString::createFromAscii( "DocumentURL" ), aDocumentURL );
136 uno::Reference < xml::sax::XAttributeList > xAttributeList(pAttrList);
137 mxHandler->startElement( rtl::OUString::createFromAscii( "Document" ), xAttributeList );
140 uno::Sequence< uno::Any > aArgument( 1 );
141 uno::Sequence< beans::PropertyValue > aPropValues( mpCfgItem->GetFilterData() );
142 aArgument[ 0 ] <<= aPropValues;
143 mxFilterTracer = xMgr->createInstanceWithArguments( rtl::OUString::createFromAscii( "com.sun.star.util.FilterTracer" ), aArgument );
144 if ( mxFilterTracer.is() )
146 mxTextSearch = uno::Reference< util::XTextSearch >( mxFilterTracer, uno::UNO_QUERY );
147 mxLogger = uno::Reference< util::logging::XLogger >( mxFilterTracer, uno::UNO_QUERY );
148 if ( mxTextSearch.is() )
150 maSearchOptions.algorithmType = eSearchAlgorithm;
151 mxTextSearch->setOptions( maSearchOptions );
158 MSFilterTracer::~MSFilterTracer()
160 mxLogger = NULL;
161 mxFilterTracer = NULL;
162 if ( mxHandler.is() )
164 mxHandler->ignorableWhitespace ( rtl::OUString::createFromAscii( " " ) );
165 mxHandler->endElement( rtl::OUString::createFromAscii( "Document" ) );
166 mxHandler->ignorableWhitespace ( rtl::OUString::createFromAscii( " " ) );
167 mxHandler->endDocument();
168 mxHandler = NULL;
170 delete mpAttributeList;
171 delete mpCfgItem;
172 delete mpStream;
175 void MSFilterTracer::StartTracing()
177 mbEnabled = mpCfgItem->ReadBool( rtl::OUString::createFromAscii( "On" ), sal_False );
180 void MSFilterTracer::EndTracing()
182 mbEnabled = sal_False;
185 void MSFilterTracer::StartElement( const rtl::OUString& rName, uno::Reference< xml::sax::XAttributeList > xAttribs )
187 if ( mxHandler.is() )
188 mxHandler->startElement( rName, xAttribs );
191 void MSFilterTracer::EndElement( const rtl::OUString& rName )
193 if ( mxHandler.is() )
194 mxHandler->endElement( rName );
197 void MSFilterTracer::Trace( const rtl::OUString& rElement, const rtl::OUString& rMessage )
199 if ( mbEnabled && mxLogger.is() )
201 sal_Bool bFilter = sal_False;
202 if ( rMessage.getLength() && mxTextSearch.is() )
204 maSearchOptions.searchString = rMessage;
205 mxTextSearch->setOptions( maSearchOptions );
206 util::SearchResult aSearchResult = mxTextSearch->searchForward( rMessage, 0, rMessage.getLength() );
207 bFilter = aSearchResult.subRegExpressions != 0;
209 if ( !bFilter )
211 uno::Reference < xml::sax::XAttributeList > xAttrList( new SvXMLAttributeList( *mpAttributeList ) );
212 if ( mxHandler.is() )
213 mxHandler->startElement( rElement, xAttrList );
214 if ( rMessage.getLength() )
216 rtl::OUString aEmpty;
217 mxLogger->logp( 0, aEmpty, aEmpty, rMessage );
219 if ( mxHandler.is() )
220 mxHandler->endElement( rElement );
225 void MSFilterTracer::AddAttribute( const ::rtl::OUString& sName , const ::rtl::OUString& sValue )
227 if ( mbEnabled )
228 mpAttributeList->AddAttribute( sName, sValue );
230 void MSFilterTracer::ClearAttributes()
232 if ( mbEnabled )
233 mpAttributeList->Clear();
236 void MSFilterTracer::RemoveAttribute( const ::rtl::OUString& sName )
238 if ( mbEnabled )
239 mpAttributeList->RemoveAttribute( sName );
242 uno::Any MSFilterTracer::GetProperty( const rtl::OUString& rPropName, const uno::Any* pDefault ) const
244 uno::Any aDefault;
245 if ( pDefault )
246 aDefault = *pDefault;
247 return mpCfgItem->ReadAny( rPropName, aDefault );
250 void MSFilterTracer::SetProperty( const ::rtl::OUString& rPropName, const uno::Any& rProperty )
252 mpCfgItem->WriteAny( rPropName, rProperty );