1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DocTokAnalyzeService.cxx,v $
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 ************************************************************************/
32 Copyright 2005 Sun Microsystems, Inc.
35 #include "DocTokAnalyzeService.hxx"
38 #include <com/sun/star/io/XStream.hpp>
39 #include <com/sun/star/io/XInputStream.hpp>
40 #include <com/sun/star/io/XSeekable.hpp>
41 #include <com/sun/star/io/XTruncate.hpp>
42 #include <com/sun/star/task/XStatusIndicator.hpp>
43 #include <com/sun/star/container/XNameContainer.hpp>
44 #include <ucbhelper/contentbroker.hxx>
45 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
46 #include <osl/process.h>
47 #include <rtl/string.hxx>
50 #include <cppuhelper/implbase2.hxx>
51 #include <com/sun/star/embed/XTransactedObject.hpp>
52 #include <com/sun/star/embed/XStorage.hpp>
53 #include <com/sun/star/util/XCloseable.hpp>
54 #include <comphelper/storagehelper.hxx>
55 #include <com/sun/star/embed/XTransactedObject.hpp>
56 #include <com/sun/star/beans/PropertyValue.hpp>
57 #include <com/sun/star/beans/XPropertySet.hpp>
58 #include <comphelper/seqstream.hxx>
59 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
60 #include <com/sun/star/io/XInputStream.hpp>
61 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
62 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
63 #include <com/sun/star/uno/Any.hxx>
64 #include <com/sun/star/container/XNameContainer.hpp>
65 #include <resourcemodel/WW8ResourceModel.hxx>
66 #include <resourcemodel/exceptions.hxx>
67 #include <doctok/WW8Document.hxx>
71 using namespace ::com::sun::star
;
73 namespace writerfilter
{ namespace doctoktest
{
75 const sal_Char
AnalyzeService::SERVICE_NAME
[40] = "debugservices.doctok.AnalyzeService";
76 const sal_Char
AnalyzeService::IMPLEMENTATION_NAME
[40] = "debugservices.doctok.AnalyzeService";
80 uno::Reference
<io::XInputStream
> xInputStream
;
81 rtl::OUString mString
;
85 sal_uInt32
getEOLIndex()
87 sal_Int32 nIndex
= mString
.indexOf(mCRLF
);
91 nIndex
= mString
.indexOf(mLF
);
98 URLLister(uno::Reference
<com::sun::star::uno::XComponentContext
> xContext
,
99 uno::Reference
<lang::XMultiComponentFactory
> xFactory
,
100 rtl::OUString absFileUrl
)
102 uno::Reference
<com::sun::star::ucb::XSimpleFileAccess
> xFileAccess
103 (xFactory
->createInstanceWithContext
104 (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
105 ("com.sun.star.ucb.SimpleFileAccess")),
106 xContext
), uno::UNO_QUERY_THROW
);
107 xInputStream
= xFileAccess
->openFileRead(absFileUrl
) ;
109 mLF
= rtl::OUString::createFromAscii("\n");
110 mCRLF
= rtl::OUString::createFromAscii("\r\n");
113 rtl::OUString
getURL()
115 rtl::OUString aResult
;
117 sal_Int32 nIndex
= getEOLIndex();
121 uno::Sequence
<sal_Int8
> aSeq
;
122 sal_uInt32 nCount
= xInputStream
->readBytes(aSeq
, 1024);
126 rtl_String
* pNew
= 0;
127 rtl_string_newFromStr_WithLength
128 (&pNew
, reinterpret_cast<const sal_Char
*>(&aSeq
[0]),
131 rtl::OString
aTmp(pNew
);
132 rtl::OUString
aTail(rtl::OStringToOUString
133 (aTmp
, RTL_TEXTENCODING_ASCII_US
));
134 mString
= mString
.concat(aTail
);
137 nIndex
= getEOLIndex();
145 aResult
= mString
.copy(0, nIndex
);
147 if (mString
.match(mCRLF
, nIndex
))
148 mString
= mString
.copy(nIndex
+ 2);
150 mString
= mString
.copy(nIndex
+ 1);
157 AnalyzeService::AnalyzeService(const uno::Reference
< uno::XComponentContext
> &xContext_
) :
158 xContext( xContext_
)
162 sal_Int32 SAL_CALL
AnalyzeService::run
163 ( const uno::Sequence
< rtl::OUString
>& aArguments
)
164 throw (uno::RuntimeException
)
166 uno::Sequence
<uno::Any
> aUcbInitSequence(2);
167 aUcbInitSequence
[0] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Local"));
168 aUcbInitSequence
[1] <<=
169 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Office"));
170 uno::Reference
<lang::XMultiServiceFactory
>
171 xServiceFactory(xContext
->getServiceManager(), uno::UNO_QUERY_THROW
);
172 uno::Reference
<lang::XMultiComponentFactory
>
173 xFactory(xContext
->getServiceManager(), uno::UNO_QUERY_THROW
);
175 if (::ucbhelper::ContentBroker::initialize(xServiceFactory
, aUcbInitSequence
))
177 rtl::OUString arg
=aArguments
[0];
179 rtl_uString
*dir
=NULL
;
180 osl_getProcessWorkingDir(&dir
);
182 rtl::OUString absFileUrlUrls
;
183 osl_getAbsoluteFileURL(dir
, arg
.pData
, &absFileUrlUrls
.pData
);
185 URLLister
aLister(xContext
, xFactory
, absFileUrlUrls
);
187 fprintf(stdout
, "<analyze>\n");
189 rtl::OUString aURL
= aLister
.getURL();
191 while (aURL
.getLength() > 0)
193 uno::Reference
<com::sun::star::ucb::XSimpleFileAccess
> xFileAccess
194 (xFactory
->createInstanceWithContext
195 (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
196 ("com.sun.star.ucb.SimpleFileAccess")),
197 xContext
), uno::UNO_QUERY_THROW
);
200 aURL
.convertToString(&aStr
, RTL_TEXTENCODING_ASCII_US
,
201 OUSTRING_TO_OSTRING_CVTFLAGS
);
203 fprintf(stdout
, "<file><name>%s</name>\n", aStr
.getStr());
204 fprintf(stderr
, "%s\n", aStr
.getStr());
212 uno::Reference
<io::XInputStream
> xInputStream
=
213 xFileAccess
->openFileRead(aURL
);
215 doctok::WW8Stream::Pointer_t pDocStream
=
216 doctok::WW8DocumentFactory::createStream
217 (xContext
, xInputStream
);
219 if (pDocStream
.get() != NULL
)
221 doctok::WW8Document::Pointer_t pDocument
222 (doctok::WW8DocumentFactory::createDocument
225 Stream::Pointer_t pAnalyzer
=
226 writerfilter::createAnalyzer();
227 pDocument
->resolve(*pAnalyzer
);
232 "<exception>file open failed</exception>\n");
235 fprintf(stderr
, "done\n");
238 xInputStream
->closeInput();
242 fprintf(stdout
, "<exception>%s</exception>\n",
243 e
.getText().c_str());
249 fprintf(stdout
, "<exception>unknown</exception>\n");
254 fprintf(stdout
, "<status>ok</status>\n");
256 fprintf(stdout
, "<status>failed</status>\n");
258 aURL
= aLister
.getURL();
260 fprintf(stdout
, "</file>\n");
264 fprintf(stdout
, "</analyze>\n");
266 rtl_uString_release(dir
);
267 ::ucbhelper::ContentBroker::deinitialize();
273 fprintf(stdout
, "can't initialize UCB");
278 ::rtl::OUString
AnalyzeService_getImplementationName ()
280 return rtl::OUString::createFromAscii ( AnalyzeService::IMPLEMENTATION_NAME
);
283 sal_Bool SAL_CALL
AnalyzeService_supportsService( const ::rtl::OUString
& ServiceName
)
285 return ServiceName
.equals( rtl::OUString::createFromAscii( AnalyzeService::SERVICE_NAME
) );
287 uno::Sequence
< rtl::OUString
> SAL_CALL
AnalyzeService_getSupportedServiceNames( ) throw (uno::RuntimeException
)
289 uno::Sequence
< rtl::OUString
> aRet(1);
290 rtl::OUString
* pArray
= aRet
.getArray();
291 pArray
[0] = rtl::OUString::createFromAscii ( AnalyzeService::SERVICE_NAME
);
295 uno::Reference
< uno::XInterface
> SAL_CALL
AnalyzeService_createInstance( const uno::Reference
< uno::XComponentContext
> & xContext
) throw( uno::Exception
)
297 return (cppu::OWeakObject
*) new AnalyzeService( xContext
);
300 } } /* end namespace writerfilter::doctok */