merge the formfield patch from ooo-build
[ooovba.git] / writerfilter / unocomponent / debugservices / doctok / DocTokAnalyzeService.cxx
blob99b2acaa493640b2ff895fc433d8ac37b26084aa
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: DocTokAnalyzeService.cxx,v $
10 * $Revision: 1.12 $
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 /**
32 Copyright 2005 Sun Microsystems, Inc.
35 #include "DocTokAnalyzeService.hxx"
36 #include <stdio.h>
37 #include <wchar.h>
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>
48 #include <hash_set>
49 #include <assert.h>
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>
69 #include <ctype.h>
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";
78 class URLLister
80 uno::Reference<io::XInputStream> xInputStream;
81 rtl::OUString mString;
82 rtl::OUString mCRLF;
83 rtl::OUString mLF;
85 sal_uInt32 getEOLIndex()
87 sal_Int32 nIndex = mString.indexOf(mCRLF);
89 if (nIndex == -1)
91 nIndex = mString.indexOf(mLF);
94 return nIndex;
97 public:
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();
119 while (nIndex == -1)
121 uno::Sequence<sal_Int8> aSeq;
122 sal_uInt32 nCount = xInputStream->readBytes(aSeq, 1024);
124 if (nCount > 0)
126 rtl_String * pNew = 0;
127 rtl_string_newFromStr_WithLength
128 (&pNew, reinterpret_cast<const sal_Char *>(&aSeq[0]),
129 nCount);
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();
139 if (nCount != 1024)
140 break;
143 if (nIndex > 0)
145 aResult = mString.copy(0, nIndex);
147 if (mString.match(mCRLF, nIndex))
148 mString = mString.copy(nIndex + 2);
149 else
150 mString = mString.copy(nIndex + 1);
153 return aResult;
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 );
199 rtl::OString aStr;
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());
205 fflush(stderr);
207 bool bStatus = true;
208 try
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
223 (pDocStream));
225 Stream::Pointer_t pAnalyzer =
226 writerfilter::createAnalyzer();
227 pDocument->resolve(*pAnalyzer);
229 else
231 fprintf(stdout,
232 "<exception>file open failed</exception>\n");
233 bStatus = false;
235 fprintf(stderr, "done\n");
238 xInputStream->closeInput();
240 catch (Exception e)
242 fprintf(stdout, "<exception>%s</exception>\n",
243 e.getText().c_str());
244 bStatus = false;
247 catch (...)
249 fprintf(stdout, "<exception>unknown</exception>\n");
250 bStatus = false;
253 if (bStatus)
254 fprintf(stdout, "<status>ok</status>\n");
255 else
256 fprintf(stdout, "<status>failed</status>\n");
258 aURL = aLister.getURL();
260 fprintf(stdout, "</file>\n");
261 fflush(stdout);
264 fprintf(stdout, "</analyze>\n");
266 rtl_uString_release(dir);
267 ::ucbhelper::ContentBroker::deinitialize();
271 else
273 fprintf(stdout, "can't initialize UCB");
275 return 0;
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 );
292 return aRet;
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 */