1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #include "DocTokAnalyzeService.hxx"
25 #include <stdio.h>
26 #include <wchar.h>
27 #include <com/sun/star/io/XStream.hpp>
28 #include <com/sun/star/io/XInputStream.hpp>
29 #include <com/sun/star/io/XSeekable.hpp>
30 #include <com/sun/star/io/XTruncate.hpp>
31 #include <com/sun/star/task/XStatusIndicator.hpp>
32 #include <com/sun/star/container/XNameContainer.hpp>
33 #include <ucbhelper/contentbroker.hxx>
34 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
35 #include <osl/process.h>
36 #include <rtl/string.hxx>
37 #include <hash_set>
38 #include <assert.h>
39 #include <cppuhelper/implbase2.hxx>
40 #include <com/sun/star/embed/XTransactedObject.hpp>
41 #include <com/sun/star/embed/XStorage.hpp>
42 #include <com/sun/star/util/XCloseable.hpp>
43 #include <comphelper/storagehelper.hxx>
44 #include <com/sun/star/embed/XTransactedObject.hpp>
45 #include <com/sun/star/beans/PropertyValue.hpp>
46 #include <com/sun/star/beans/XPropertySet.hpp>
47 #include <comphelper/seqstream.hxx>
48 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
49 #include <com/sun/star/io/XInputStream.hpp>
50 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
51 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
52 #include <com/sun/star/uno/Any.hxx>
53 #include <com/sun/star/container/XNameContainer.hpp>
54 #include <resourcemodel/WW8ResourceModel.hxx>
55 #include <resourcemodel/exceptions.hxx>
56 #include <doctok/WW8Document.hxx>
57
58 #include <ctype.h>
59
60 using namespace ::com::sun::star;
61
62 namespace writerfilter { namespace doctoktest {
63
64 const sal_Char AnalyzeService::SERVICE_NAME[40] = "debugservices.doctok.AnalyzeService";
65 const sal_Char AnalyzeService::IMPLEMENTATION_NAME[40] = "debugservices.doctok.AnalyzeService";
66
67 class URLLister
68 {
69 uno::Reference<io::XInputStream> xInputStream;
70 rtl::OUString mString;
71 rtl::OUString mCRLF;
72 rtl::OUString mLF;
73
getEOLIndex()74 sal_uInt32 getEOLIndex()
75 {
76 sal_Int32 nIndex = mString.indexOf(mCRLF);
77
78 if (nIndex == -1)
79 {
80 nIndex = mString.indexOf(mLF);
81 }
82
83 return nIndex;
84 }
85
86 public:
URLLister(uno::Reference<com::sun::star::uno::XComponentContext> xContext,uno::Reference<lang::XMultiComponentFactory> xFactory,rtl::OUString absFileUrl)87 URLLister(uno::Reference<com::sun::star::uno::XComponentContext> xContext,
88 uno::Reference<lang::XMultiComponentFactory> xFactory,
89 rtl::OUString absFileUrl)
90 {
91 uno::Reference<com::sun::star::ucb::XSimpleFileAccess> xFileAccess
92 (xFactory->createInstanceWithContext
93 (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
94 ("com.sun.star.ucb.SimpleFileAccess")),
95 xContext), uno::UNO_QUERY_THROW);
96 xInputStream = xFileAccess->openFileRead(absFileUrl) ;
97
98 mLF = rtl::OUString::createFromAscii("\n");
99 mCRLF = rtl::OUString::createFromAscii("\r\n");
100 }
101
getURL()102 rtl::OUString getURL()
103 {
104 rtl::OUString aResult;
105
106 sal_Int32 nIndex = getEOLIndex();
107
108 while (nIndex == -1)
109 {
110 uno::Sequence<sal_Int8> aSeq;
111 sal_uInt32 nCount = xInputStream->readBytes(aSeq, 1024);
112
113 if (nCount > 0)
114 {
115 rtl_String * pNew = 0;
116 rtl_string_newFromStr_WithLength
117 (&pNew, reinterpret_cast<const sal_Char *>(&aSeq[0]),
118 nCount);
119
120 rtl::OString aTmp(pNew);
121 rtl::OUString aTail(rtl::OStringToOUString
122 (aTmp, RTL_TEXTENCODING_ASCII_US));
123 mString = mString.concat(aTail);
124 }
125
126 nIndex = getEOLIndex();
127
128 if (nCount != 1024)
129 break;
130 }
131
132 if (nIndex > 0)
133 {
134 aResult = mString.copy(0, nIndex);
135
136 if (mString.match(mCRLF, nIndex))
137 mString = mString.copy(nIndex + 2);
138 else
139 mString = mString.copy(nIndex + 1);
140 }
141
142 return aResult;
143 }
144 };
145
AnalyzeService(const uno::Reference<uno::XComponentContext> & xContext_)146 AnalyzeService::AnalyzeService(const uno::Reference< uno::XComponentContext > &xContext_) :
147 xContext( xContext_ )
148 {
149 }
150
run(const uno::Sequence<rtl::OUString> & aArguments)151 sal_Int32 SAL_CALL AnalyzeService::run
152 ( const uno::Sequence< rtl::OUString >& aArguments )
153 throw (uno::RuntimeException)
154 {
155 uno::Sequence<uno::Any> aUcbInitSequence(2);
156 aUcbInitSequence[0] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Local"));
157 aUcbInitSequence[1] <<=
158 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Office"));
159 uno::Reference<lang::XMultiServiceFactory>
160 xServiceFactory(xContext->getServiceManager(), uno::UNO_QUERY_THROW);
161 uno::Reference<lang::XMultiComponentFactory>
162 xFactory(xContext->getServiceManager(), uno::UNO_QUERY_THROW );
163
164 if (::ucbhelper::ContentBroker::initialize(xServiceFactory, aUcbInitSequence))
165 {
166 rtl::OUString arg=aArguments[0];
167
168 rtl_uString *dir=NULL;
169 osl_getProcessWorkingDir(&dir);
170
171 rtl::OUString absFileUrlUrls;
172 osl_getAbsoluteFileURL(dir, arg.pData, &absFileUrlUrls.pData);
173
174 URLLister aLister(xContext, xFactory, absFileUrlUrls);
175
176 fprintf(stdout, "<analyze>\n");
177
178 rtl::OUString aURL = aLister.getURL();
179
180 while (aURL.getLength() > 0)
181 {
182 uno::Reference<com::sun::star::ucb::XSimpleFileAccess> xFileAccess
183 (xFactory->createInstanceWithContext
184 (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
185 ("com.sun.star.ucb.SimpleFileAccess")),
186 xContext), uno::UNO_QUERY_THROW );
187
188 rtl::OString aStr;
189 aURL.convertToString(&aStr, RTL_TEXTENCODING_ASCII_US,
190 OUSTRING_TO_OSTRING_CVTFLAGS);
191
192 fprintf(stdout, "<file><name>%s</name>\n", aStr.getStr());
193 fprintf(stderr, "%s\n", aStr.getStr());
194 fflush(stderr);
195
196 bool bStatus = true;
197 try
198 {
199 try
200 {
201 uno::Reference<io::XInputStream> xInputStream =
202 xFileAccess->openFileRead(aURL);
203 {
204 doctok::WW8Stream::Pointer_t pDocStream =
205 doctok::WW8DocumentFactory::createStream
206 (xContext, xInputStream);
207
208 if (pDocStream.get() != NULL)
209 {
210 doctok::WW8Document::Pointer_t pDocument
211 (doctok::WW8DocumentFactory::createDocument
212 (pDocStream));
213
214 Stream::Pointer_t pAnalyzer =
215 writerfilter::createAnalyzer();
216 pDocument->resolve(*pAnalyzer);
217 }
218 else
219 {
220 fprintf(stdout,
221 "<exception>file open failed</exception>\n");
222 bStatus = false;
223 }
224 fprintf(stderr, "done\n");
225 }
226
227 xInputStream->closeInput();
228 }
229 catch (Exception e)
230 {
231 fprintf(stdout, "<exception>%s</exception>\n",
232 e.getText().c_str());
233 bStatus = false;
234 }
235 }
236 catch (...)
237 {
238 fprintf(stdout, "<exception>unknown</exception>\n");
239 bStatus = false;
240 }
241
242 if (bStatus)
243 fprintf(stdout, "<status>ok</status>\n");
244 else
245 fprintf(stdout, "<status>failed</status>\n");
246
247 aURL = aLister.getURL();
248
249 fprintf(stdout, "</file>\n");
250 fflush(stdout);
251 }
252
253 fprintf(stdout, "</analyze>\n");
254
255 rtl_uString_release(dir);
256 ::ucbhelper::ContentBroker::deinitialize();
257
258
259 }
260 else
261 {
262 fprintf(stdout, "can't initialize UCB");
263 }
264 return 0;
265 }
266
AnalyzeService_getImplementationName()267 ::rtl::OUString AnalyzeService_getImplementationName ()
268 {
269 return rtl::OUString::createFromAscii ( AnalyzeService::IMPLEMENTATION_NAME );
270 }
271
AnalyzeService_supportsService(const::rtl::OUString & ServiceName)272 sal_Bool SAL_CALL AnalyzeService_supportsService( const ::rtl::OUString& ServiceName )
273 {
274 return ServiceName.equals( rtl::OUString::createFromAscii( AnalyzeService::SERVICE_NAME ) );
275 }
AnalyzeService_getSupportedServiceNames()276 uno::Sequence< rtl::OUString > SAL_CALL AnalyzeService_getSupportedServiceNames( ) throw (uno::RuntimeException)
277 {
278 uno::Sequence < rtl::OUString > aRet(1);
279 rtl::OUString* pArray = aRet.getArray();
280 pArray[0] = rtl::OUString::createFromAscii ( AnalyzeService::SERVICE_NAME );
281 return aRet;
282 }
283
AnalyzeService_createInstance(const uno::Reference<uno::XComponentContext> & xContext)284 uno::Reference< uno::XInterface > SAL_CALL AnalyzeService_createInstance( const uno::Reference< uno::XComponentContext > & xContext) throw( uno::Exception )
285 {
286 return (cppu::OWeakObject*) new AnalyzeService( xContext );
287 }
288
289 } } /* end namespace writerfilter::doctok */
290