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 <comphelper/processfactory.hxx>
25 
26 #include "OOXMLAnalyzeService.hxx"
27 #include <stdio.h>
28 #include <wchar.h>
29 #include <com/sun/star/io/XStream.hpp>
30 #include <com/sun/star/io/XInputStream.hpp>
31 #include <com/sun/star/io/XSeekable.hpp>
32 #include <com/sun/star/io/XTruncate.hpp>
33 #include <com/sun/star/task/XStatusIndicator.hpp>
34 #include <com/sun/star/container/XNameContainer.hpp>
35 #include <ucbhelper/contentbroker.hxx>
36 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
37 #include <osl/process.h>
38 #include <rtl/string.hxx>
39 #include <hash_set>
40 #include <assert.h>
41 #include <cppuhelper/implbase2.hxx>
42 #include <com/sun/star/embed/XTransactedObject.hpp>
43 #include <com/sun/star/embed/XStorage.hpp>
44 #include <com/sun/star/util/XCloseable.hpp>
45 #include <comphelper/storagehelper.hxx>
46 #include <com/sun/star/embed/XTransactedObject.hpp>
47 #include <com/sun/star/beans/PropertyValue.hpp>
48 #include <com/sun/star/beans/XPropertySet.hpp>
49 #include <comphelper/seqstream.hxx>
50 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
51 #include <com/sun/star/io/XInputStream.hpp>
52 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
54 #include <com/sun/star/uno/Any.hxx>
55 #include <com/sun/star/container/XNameContainer.hpp>
56 #include <resourcemodel/WW8ResourceModel.hxx>
57 #include <ooxml/OOXMLDocument.hxx>
58 
59 #include <ctype.h>
60 
61 using namespace ::com::sun::star;
62 
63 namespace writerfilter { namespace ooxmltest  {
64 
65 const sal_Char AnalyzeService::SERVICE_NAME[40] = "debugservices.ooxml.AnalyzeService";
66 const sal_Char AnalyzeService::IMPLEMENTATION_NAME[40] = "debugservices.ooxml.AnalyzeService";
67 
68 class URLLister
69 {
70     uno::Reference<io::XInputStream> xInputStream;
71     rtl::OUString mString;
72     rtl::OUString mCRLF;
73     rtl::OUString mLF;
74 
getEOLIndex()75     sal_uInt32 getEOLIndex()
76     {
77         sal_Int32 nIndex = mString.indexOf(mCRLF);
78 
79         if (nIndex == -1)
80         {
81             nIndex = mString.indexOf(mLF);
82         }
83 
84         return nIndex;
85     }
86 
87 public:
URLLister(uno::Reference<com::sun::star::uno::XComponentContext> xContext,uno::Reference<lang::XMultiComponentFactory> xFactory,rtl::OUString absFileUrl)88     URLLister(uno::Reference<com::sun::star::uno::XComponentContext> xContext,
89               uno::Reference<lang::XMultiComponentFactory> xFactory,
90               rtl::OUString absFileUrl)
91     {
92         uno::Reference<com::sun::star::ucb::XSimpleFileAccess> xFileAccess
93             (xFactory->createInstanceWithContext
94              (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
95                               ("com.sun.star.ucb.SimpleFileAccess")),
96               xContext), uno::UNO_QUERY_THROW);
97         xInputStream = xFileAccess->openFileRead(absFileUrl) ;
98 
99         mLF = rtl::OUString::createFromAscii("\n");
100         mCRLF = rtl::OUString::createFromAscii("\r\n");
101     }
102 
getURL()103     rtl::OUString getURL()
104     {
105         rtl::OUString aResult;
106 
107         sal_Int32 nIndex = getEOLIndex();
108 
109         while (nIndex == -1)
110         {
111             uno::Sequence<sal_Int8> aSeq;
112             sal_uInt32 nCount = xInputStream->readBytes(aSeq, 1024);
113 
114             if (nCount > 0)
115             {
116                 rtl_String * pNew = 0;
117                 rtl_string_newFromStr_WithLength
118                     (&pNew, reinterpret_cast<const sal_Char *>(&aSeq[0]),
119                      nCount);
120 
121                 rtl::OString aTmp(pNew);
122                 rtl::OUString aTail(rtl::OStringToOUString
123                                     (aTmp, RTL_TEXTENCODING_ASCII_US));
124                 mString = mString.concat(aTail);
125             }
126 
127             nIndex = getEOLIndex();
128 
129             if (nCount != 1024)
130                 break;
131         }
132 
133         if (nIndex > 0)
134         {
135             aResult = mString.copy(0, nIndex);
136 
137             if (mString.match(mCRLF, nIndex))
138                 mString = mString.copy(nIndex + 2);
139             else
140                 mString = mString.copy(nIndex + 1);
141         }
142 
143         return aResult;
144     }
145 };
146 
AnalyzeService(const uno::Reference<uno::XComponentContext> & xContext_)147 AnalyzeService::AnalyzeService(const uno::Reference< uno::XComponentContext > &xContext_) :
148 xContext( xContext_ )
149 {
150 }
151 
run(const uno::Sequence<rtl::OUString> & aArguments)152 sal_Int32 SAL_CALL AnalyzeService::run
153 ( const uno::Sequence< rtl::OUString >& aArguments )
154     throw (uno::RuntimeException)
155 {
156 	uno::Sequence<uno::Any> aUcbInitSequence(2);
157 	aUcbInitSequence[0] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Local"));
158 	aUcbInitSequence[1] <<=
159         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Office"));
160 	uno::Reference<lang::XMultiServiceFactory>
161         xServiceFactory(xContext->getServiceManager(), uno::UNO_QUERY_THROW);
162 	uno::Reference<lang::XMultiComponentFactory>
163         xFactory(xContext->getServiceManager(), uno::UNO_QUERY_THROW );
164 
165     if (::ucbhelper::ContentBroker::initialize(xServiceFactory, aUcbInitSequence))
166 	{
167 	    ::comphelper::setProcessServiceFactory(xServiceFactory);
168 
169         rtl::OUString arg=aArguments[0];
170 
171         rtl_uString *dir=NULL;
172         osl_getProcessWorkingDir(&dir);
173 
174         rtl::OUString absFileUrlUrls;
175         osl_getAbsoluteFileURL(dir, arg.pData, &absFileUrlUrls.pData);
176 
177         URLLister aLister(xContext, xFactory, absFileUrlUrls);
178 
179         fprintf(stdout, "<analyze>\n");
180 
181         writerfilter::analyzerIds();
182 
183         rtl::OUString aURL = aLister.getURL();
184 
185         while (aURL.getLength() > 0)
186         {
187             uno::Reference<com::sun::star::ucb::XSimpleFileAccess> xFileAccess
188                 (xFactory->createInstanceWithContext
189                  (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM
190                                   ("com.sun.star.ucb.SimpleFileAccess")),
191                   xContext), uno::UNO_QUERY_THROW );
192 
193             rtl::OString aStr;
194             aURL.convertToString(&aStr, RTL_TEXTENCODING_ASCII_US,
195                                  OUSTRING_TO_OSTRING_CVTFLAGS);
196 
197             fprintf(stdout, "<file><name>%s</name>\n", aStr.getStr());
198             fprintf(stderr, "%s\n", aStr.getStr());
199             fflush(stderr);
200 
201             bool bStatus = true;
202             try
203             {
204                 uno::Reference<io::XInputStream> xInputStream =
205                     xFileAccess->openFileRead(aURL);
206 
207 				if (xInputStream.is())
208                 {
209                     ooxml::OOXMLStream::Pointer_t pDocStream =
210                         ooxml::OOXMLDocumentFactory::createStream
211                         (xContext, xInputStream);
212 
213                     if (pDocStream.get() != NULL)
214                     {
215                         ooxml::OOXMLDocument::Pointer_t pDocument
216                             (ooxml::OOXMLDocumentFactory::createDocument
217                              (pDocStream));
218 
219                         Stream::Pointer_t pAnalyzer =
220                             writerfilter::createAnalyzer();
221                         pDocument->resolve(*pAnalyzer);
222                     }
223                     else
224                     {
225                         fprintf(stdout,
226                                 "<exception>file open failed</exception>\n");
227                         bStatus = false;
228                     }
229                     fprintf(stderr, "done\n");
230                 }
231 
232                 xInputStream->closeInput();
233             }
234             catch (...)
235             {
236                 fprintf(stdout, "<exception>unknown</exception>\n");
237                 bStatus = false;
238             }
239 
240             if (bStatus)
241                 fprintf(stdout, "<status>ok</status>\n");
242             else
243                 fprintf(stdout, "<status>failed</status>\n");
244 
245             aURL = aLister.getURL();
246 
247             fprintf(stdout, "</file>\n");
248             fflush(stdout);
249         }
250 
251         fprintf(stdout, "</analyze>\n");
252 
253         rtl_uString_release(dir);
254         ::ucbhelper::ContentBroker::deinitialize();
255 
256 
257 	}
258 	else
259 	{
260 		fprintf(stdout, "can't initialize UCB");
261 	}
262 	return 0;
263 }
264 
AnalyzeService_getImplementationName()265 ::rtl::OUString AnalyzeService_getImplementationName ()
266 {
267 	return rtl::OUString::createFromAscii ( AnalyzeService::IMPLEMENTATION_NAME );
268 }
269 
AnalyzeService_supportsService(const::rtl::OUString & ServiceName)270 sal_Bool SAL_CALL AnalyzeService_supportsService( const ::rtl::OUString& ServiceName )
271 {
272 	return ServiceName.equals( rtl::OUString::createFromAscii( AnalyzeService::SERVICE_NAME ) );
273 }
AnalyzeService_getSupportedServiceNames()274 uno::Sequence< rtl::OUString > SAL_CALL AnalyzeService_getSupportedServiceNames(  ) throw (uno::RuntimeException)
275 {
276 	uno::Sequence < rtl::OUString > aRet(1);
277 	rtl::OUString* pArray = aRet.getArray();
278 	pArray[0] =  rtl::OUString::createFromAscii ( AnalyzeService::SERVICE_NAME );
279 	return aRet;
280 }
281 
AnalyzeService_createInstance(const uno::Reference<uno::XComponentContext> & xContext)282 uno::Reference< uno::XInterface > SAL_CALL AnalyzeService_createInstance( const uno::Reference< uno::XComponentContext > & xContext) throw( uno::Exception )
283 {
284 	return (cppu::OWeakObject*) new AnalyzeService( xContext );
285 }
286 
287 } } /* end namespace writerfilter::doctok */
288