merge the formfield patch from ooo-build
[ooovba.git] / writerfilter / unocomponent / debugservices / ooxml / OOXMLAnalyzeService.cxx
blob8092ead0f4628ab3cba04e9d66e4f43fd00e9252
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: OOXMLAnalyzeService.cxx,v $
10 * $Revision: 1.3 $
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 <comphelper/processfactory.hxx>
37 #include "OOXMLAnalyzeService.hxx"
38 #include <stdio.h>
39 #include <wchar.h>
40 #include <com/sun/star/io/XStream.hpp>
41 #include <com/sun/star/io/XInputStream.hpp>
42 #include <com/sun/star/io/XSeekable.hpp>
43 #include <com/sun/star/io/XTruncate.hpp>
44 #include <com/sun/star/task/XStatusIndicator.hpp>
45 #include <com/sun/star/container/XNameContainer.hpp>
46 #include <ucbhelper/contentbroker.hxx>
47 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
48 #include <osl/process.h>
49 #include <rtl/string.hxx>
50 #include <hash_set>
51 #include <assert.h>
52 #include <cppuhelper/implbase2.hxx>
53 #include <com/sun/star/embed/XTransactedObject.hpp>
54 #include <com/sun/star/embed/XStorage.hpp>
55 #include <com/sun/star/util/XCloseable.hpp>
56 #include <comphelper/storagehelper.hxx>
57 #include <com/sun/star/embed/XTransactedObject.hpp>
58 #include <com/sun/star/beans/PropertyValue.hpp>
59 #include <com/sun/star/beans/XPropertySet.hpp>
60 #include <comphelper/seqstream.hxx>
61 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
62 #include <com/sun/star/io/XInputStream.hpp>
63 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
64 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
65 #include <com/sun/star/uno/Any.hxx>
66 #include <com/sun/star/container/XNameContainer.hpp>
67 #include <resourcemodel/WW8ResourceModel.hxx>
68 #include <ooxml/OOXMLDocument.hxx>
70 #include <ctype.h>
72 using namespace ::com::sun::star;
74 namespace writerfilter { namespace ooxmltest {
76 const sal_Char AnalyzeService::SERVICE_NAME[40] = "debugservices.ooxml.AnalyzeService";
77 const sal_Char AnalyzeService::IMPLEMENTATION_NAME[40] = "debugservices.ooxml.AnalyzeService";
79 class URLLister
81 uno::Reference<io::XInputStream> xInputStream;
82 rtl::OUString mString;
83 rtl::OUString mCRLF;
84 rtl::OUString mLF;
86 sal_uInt32 getEOLIndex()
88 sal_Int32 nIndex = mString.indexOf(mCRLF);
90 if (nIndex == -1)
92 nIndex = mString.indexOf(mLF);
95 return nIndex;
98 public:
99 URLLister(uno::Reference<com::sun::star::uno::XComponentContext> xContext,
100 uno::Reference<lang::XMultiComponentFactory> xFactory,
101 rtl::OUString absFileUrl)
103 uno::Reference<com::sun::star::ucb::XSimpleFileAccess> xFileAccess
104 (xFactory->createInstanceWithContext
105 (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
106 ("com.sun.star.ucb.SimpleFileAccess")),
107 xContext), uno::UNO_QUERY_THROW);
108 xInputStream = xFileAccess->openFileRead(absFileUrl) ;
110 mLF = rtl::OUString::createFromAscii("\n");
111 mCRLF = rtl::OUString::createFromAscii("\r\n");
114 rtl::OUString getURL()
116 rtl::OUString aResult;
118 sal_Int32 nIndex = getEOLIndex();
120 while (nIndex == -1)
122 uno::Sequence<sal_Int8> aSeq;
123 sal_uInt32 nCount = xInputStream->readBytes(aSeq, 1024);
125 if (nCount > 0)
127 rtl_String * pNew = 0;
128 rtl_string_newFromStr_WithLength
129 (&pNew, reinterpret_cast<const sal_Char *>(&aSeq[0]),
130 nCount);
132 rtl::OString aTmp(pNew);
133 rtl::OUString aTail(rtl::OStringToOUString
134 (aTmp, RTL_TEXTENCODING_ASCII_US));
135 mString = mString.concat(aTail);
138 nIndex = getEOLIndex();
140 if (nCount != 1024)
141 break;
144 if (nIndex > 0)
146 aResult = mString.copy(0, nIndex);
148 if (mString.match(mCRLF, nIndex))
149 mString = mString.copy(nIndex + 2);
150 else
151 mString = mString.copy(nIndex + 1);
154 return aResult;
158 AnalyzeService::AnalyzeService(const uno::Reference< uno::XComponentContext > &xContext_) :
159 xContext( xContext_ )
163 sal_Int32 SAL_CALL AnalyzeService::run
164 ( const uno::Sequence< rtl::OUString >& aArguments )
165 throw (uno::RuntimeException)
167 uno::Sequence<uno::Any> aUcbInitSequence(2);
168 aUcbInitSequence[0] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Local"));
169 aUcbInitSequence[1] <<=
170 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Office"));
171 uno::Reference<lang::XMultiServiceFactory>
172 xServiceFactory(xContext->getServiceManager(), uno::UNO_QUERY_THROW);
173 uno::Reference<lang::XMultiComponentFactory>
174 xFactory(xContext->getServiceManager(), uno::UNO_QUERY_THROW );
176 if (::ucbhelper::ContentBroker::initialize(xServiceFactory, aUcbInitSequence))
178 ::comphelper::setProcessServiceFactory(xServiceFactory);
180 rtl::OUString arg=aArguments[0];
182 rtl_uString *dir=NULL;
183 osl_getProcessWorkingDir(&dir);
185 rtl::OUString absFileUrlUrls;
186 osl_getAbsoluteFileURL(dir, arg.pData, &absFileUrlUrls.pData);
188 URLLister aLister(xContext, xFactory, absFileUrlUrls);
190 fprintf(stdout, "<analyze>\n");
192 writerfilter::analyzerIds();
194 rtl::OUString aURL = aLister.getURL();
196 while (aURL.getLength() > 0)
198 uno::Reference<com::sun::star::ucb::XSimpleFileAccess> xFileAccess
199 (xFactory->createInstanceWithContext
200 (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
201 ("com.sun.star.ucb.SimpleFileAccess")),
202 xContext), uno::UNO_QUERY_THROW );
204 rtl::OString aStr;
205 aURL.convertToString(&aStr, RTL_TEXTENCODING_ASCII_US,
206 OUSTRING_TO_OSTRING_CVTFLAGS);
208 fprintf(stdout, "<file><name>%s</name>\n", aStr.getStr());
209 fprintf(stderr, "%s\n", aStr.getStr());
210 fflush(stderr);
212 bool bStatus = true;
213 try
215 uno::Reference<io::XInputStream> xInputStream =
216 xFileAccess->openFileRead(aURL);
218 if (xInputStream.is())
220 ooxml::OOXMLStream::Pointer_t pDocStream =
221 ooxml::OOXMLDocumentFactory::createStream
222 (xContext, xInputStream);
224 if (pDocStream.get() != NULL)
226 ooxml::OOXMLDocument::Pointer_t pDocument
227 (ooxml::OOXMLDocumentFactory::createDocument
228 (pDocStream));
230 Stream::Pointer_t pAnalyzer =
231 writerfilter::createAnalyzer();
232 pDocument->resolve(*pAnalyzer);
234 else
236 fprintf(stdout,
237 "<exception>file open failed</exception>\n");
238 bStatus = false;
240 fprintf(stderr, "done\n");
243 xInputStream->closeInput();
245 catch (...)
247 fprintf(stdout, "<exception>unknown</exception>\n");
248 bStatus = false;
251 if (bStatus)
252 fprintf(stdout, "<status>ok</status>\n");
253 else
254 fprintf(stdout, "<status>failed</status>\n");
256 aURL = aLister.getURL();
258 fprintf(stdout, "</file>\n");
259 fflush(stdout);
262 fprintf(stdout, "</analyze>\n");
264 rtl_uString_release(dir);
265 ::ucbhelper::ContentBroker::deinitialize();
269 else
271 fprintf(stdout, "can't initialize UCB");
273 return 0;
276 ::rtl::OUString AnalyzeService_getImplementationName ()
278 return rtl::OUString::createFromAscii ( AnalyzeService::IMPLEMENTATION_NAME );
281 sal_Bool SAL_CALL AnalyzeService_supportsService( const ::rtl::OUString& ServiceName )
283 return ServiceName.equals( rtl::OUString::createFromAscii( AnalyzeService::SERVICE_NAME ) );
285 uno::Sequence< rtl::OUString > SAL_CALL AnalyzeService_getSupportedServiceNames( ) throw (uno::RuntimeException)
287 uno::Sequence < rtl::OUString > aRet(1);
288 rtl::OUString* pArray = aRet.getArray();
289 pArray[0] = rtl::OUString::createFromAscii ( AnalyzeService::SERVICE_NAME );
290 return aRet;
293 uno::Reference< uno::XInterface > SAL_CALL AnalyzeService_createInstance( const uno::Reference< uno::XComponentContext > & xContext) throw( uno::Exception )
295 return (cppu::OWeakObject*) new AnalyzeService( xContext );
298 } } /* end namespace writerfilter::doctok */