Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : :
21 : : #include <stdio.h>
22 : : #include <string.h>
23 : : #include <stack>
24 : :
25 : : #include "sal/main.h"
26 : :
27 : : #include <com/sun/star/lang/XComponent.hpp>
28 : :
29 : : #include <com/sun/star/xml/sax/SAXParseException.hpp>
30 : : #include <com/sun/star/xml/sax/XParser.hpp>
31 : : #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
32 : :
33 : : #include <com/sun/star/io/XOutputStream.hpp>
34 : : #include <com/sun/star/io/XActiveDataSource.hpp>
35 : :
36 : : #include <cppuhelper/servicefactory.hxx>
37 : : #include <cppuhelper/implbase1.hxx>
38 : : #include <cppuhelper/implbase3.hxx>
39 : :
40 : : #include <osl/diagnose.h>
41 : :
42 : : #include "LocaleNode.hxx"
43 : :
44 : : using namespace ::rtl;
45 : : using namespace ::std;
46 : : using namespace ::cppu;
47 : : using namespace ::com::sun::star::uno;
48 : : using namespace ::com::sun::star::lang;
49 : : using namespace ::com::sun::star::registry;
50 : : using namespace ::com::sun::star::xml::sax;
51 : : using namespace ::com::sun::star::io;
52 : :
53 : :
54 : :
55 : :
56 : :
57 : :
58 : : /************
59 : : * Sequence of bytes -> InputStream
60 : : ************/
61 [ + - ][ - + ]: 394 : class OInputStream : public WeakImplHelper1 < XInputStream >
62 : : {
63 : : public:
64 : 197 : OInputStream( const Sequence< sal_Int8 >&seq ) :
65 : : nPos( 0 ),
66 [ + - ]: 197 : m_seq( seq )
67 : 197 : {}
68 : :
69 : : public:
70 : 470 : virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
71 : : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
72 : : {
73 : 470 : nBytesToRead = (nBytesToRead > m_seq.getLength() - nPos ) ?
74 : 394 : m_seq.getLength() - nPos :
75 [ + + ]: 864 : nBytesToRead;
76 [ + - ]: 470 : aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
77 : 470 : nPos += nBytesToRead;
78 : 470 : return nBytesToRead;
79 : : }
80 : 470 : virtual sal_Int32 SAL_CALL readSomeBytes(
81 : : ::com::sun::star::uno::Sequence< sal_Int8 >& aData,
82 : : sal_Int32 nMaxBytesToRead )
83 : : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
84 : : {
85 : 470 : return readBytes( aData, nMaxBytesToRead );
86 : : }
87 : 0 : virtual void SAL_CALL skipBytes( sal_Int32 /*nBytesToSkip*/ )
88 : : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
89 : : {
90 : : // not implemented
91 : 0 : }
92 : 0 : virtual sal_Int32 SAL_CALL available( )
93 : : throw(NotConnectedException, IOException, RuntimeException)
94 : : {
95 : 0 : return m_seq.getLength() - nPos;
96 : : }
97 : 0 : virtual void SAL_CALL closeInput( )
98 : : throw(NotConnectedException, IOException, RuntimeException)
99 : : {
100 : : // not needed
101 : 0 : }
102 : : sal_Int32 nPos;
103 : : Sequence< sal_Int8> m_seq;
104 : : };
105 : :
106 : : //-------------------------------
107 : : // Helper : create an input stream from a file
108 : : //------------------------------
109 : 197 : Reference< XInputStream > createStreamFromFile(
110 : : const char *pcFile )
111 : : {
112 : 197 : FILE *f = fopen( pcFile , "rb" );
113 : 197 : Reference< XInputStream > r;
114 : :
115 [ + - ]: 197 : if( f ) {
116 : 197 : fseek( f , 0 , SEEK_END );
117 [ + - ]: 197 : size_t nLength = ftell( f );
118 : 197 : fseek( f , 0 , SEEK_SET );
119 : :
120 [ + - ]: 197 : Sequence<sal_Int8> seqIn(nLength);
121 [ + - ][ + - ]: 197 : if (fread( seqIn.getArray() , nLength , 1 , f ) == 1)
[ + - ]
122 [ + - ][ + - ]: 197 : r = Reference< XInputStream > ( new OInputStream( seqIn ) );
[ + - ][ + - ]
123 : : else
124 [ # # ]: 0 : fprintf(stderr, "failure reading %s\n", pcFile);
125 [ + - ][ + - ]: 197 : fclose( f );
126 : : }
127 : 197 : return r;
128 : : }
129 : :
130 : :
131 : : class TestDocumentHandler :
132 : : public WeakImplHelper3< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
133 : : {
134 : : public:
135 : 197 : TestDocumentHandler(const char* locale, const char* outFile )
136 : : : rootNode(0)
137 : : , nError(0)
138 [ + - ][ + - ]: 197 : , of(outFile, locale)
[ + - ]
139 : : {
140 : 197 : strncpy( theLocale, locale, sizeof(theLocale) );
141 : 197 : theLocale[sizeof(theLocale)-1] = 0;
142 : 197 : }
143 : :
144 : 394 : ~TestDocumentHandler( )
145 [ + - ]: 197 : {
146 [ + - ]: 197 : of.closeOutput();
147 [ + - ][ + - ]: 197 : delete rootNode;
148 [ - + ]: 394 : }
149 : :
150 : :
151 : : public: // Error handler
152 : 0 : virtual void SAL_CALL error(const Any& aSAXParseException) throw (SAXException, RuntimeException)
153 : : {
154 : 0 : ++nError;
155 : 0 : printf( "Error !\n" );
156 : : throw SAXException(
157 : : OUString( "error from error handler") ,
158 : : Reference < XInterface >() ,
159 [ # # ]: 0 : aSAXParseException );
160 : : }
161 : 0 : virtual void SAL_CALL fatalError(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException)
162 : : {
163 : 0 : ++nError;
164 : 0 : printf( "Fatal Error !\n" );
165 : 0 : }
166 : 0 : virtual void SAL_CALL warning(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException)
167 : : {
168 : 0 : printf( "Warning !\n" );
169 : 0 : }
170 : :
171 : :
172 : : public: // ExtendedDocumentHandler
173 : :
174 : :
175 : :
176 : : stack<LocaleNode *> currentNode ;
177 : : sal_Bool fElement ;
178 : : LocaleNode * rootNode;
179 : :
180 : 197 : virtual void SAL_CALL startDocument(void) throw (SAXException, RuntimeException)
181 : : {
182 : 197 : printf( "parsing document %s started\n", theLocale);
183 : 197 : of.writeAsciiString("#include <sal/types.h>\n\n\n");
184 : 197 : of.writeAsciiString("#include <stdio.h> // debug printfs\n\n");
185 : 197 : of.writeAsciiString("extern \"C\" {\n\n");
186 : 197 : }
187 : :
188 : 197 : virtual void SAL_CALL endDocument(void) throw (SAXException, RuntimeException)
189 : : {
190 [ + - ]: 197 : if (rootNode)
191 : : {
192 : 197 : rootNode->generateCode(of);
193 : 197 : int err = rootNode->getError();
194 [ - + ]: 197 : if (err)
195 : : {
196 : 0 : printf( "Error: in data for %s: %d\n", theLocale, err);
197 : 0 : nError += err;
198 : : }
199 : : }
200 : : else
201 : : {
202 : 0 : ++nError;
203 : 0 : printf( "Error: no data for %s\n", theLocale);
204 : : }
205 : 197 : printf( "parsing document %s finished\n", theLocale);
206 : :
207 : 197 : of.writeAsciiString("} // extern \"C\"\n\n");
208 : 197 : of.closeOutput();
209 : 197 : }
210 : :
211 : 43100 : virtual void SAL_CALL startElement(const OUString& aName,
212 : : const Reference< XAttributeList > & xAttribs)
213 : : throw (SAXException,RuntimeException)
214 : : {
215 : :
216 [ + - ]: 43100 : LocaleNode * l = LocaleNode::createNode (aName, xAttribs);
217 [ + + ]: 43100 : if (!currentNode.empty() ) {
218 [ + - ]: 42903 : LocaleNode * ln = (LocaleNode *) currentNode.top();
219 [ + - ]: 42903 : ln->addChild(l);
220 : : } else {
221 : 197 : rootNode = l;
222 : : }
223 [ + - ]: 43100 : currentNode.push (l);
224 : 43100 : }
225 : :
226 : :
227 : 43100 : virtual void SAL_CALL endElement(const OUString& /*aName*/) throw (SAXException,RuntimeException)
228 : : {
229 : 43100 : currentNode.pop();
230 : 43100 : }
231 : :
232 : 140290 : virtual void SAL_CALL characters(const OUString& aChars) throw (SAXException,RuntimeException)
233 : : {
234 : :
235 : 140290 : LocaleNode * l = currentNode.top();
236 : 140290 : l->setValue (aChars);
237 : 140290 : }
238 : :
239 : 0 : virtual void SAL_CALL ignorableWhitespace(const OUString& /*aWhitespaces*/) throw (SAXException,RuntimeException)
240 : : {
241 : 0 : }
242 : :
243 : 0 : virtual void SAL_CALL processingInstruction(const OUString& /*aTarget*/, const OUString& /*aData*/) throw (SAXException,RuntimeException)
244 : : {
245 : : // ignored
246 : 0 : }
247 : :
248 : 197 : virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & /*xLocator*/)
249 : : throw (SAXException,RuntimeException)
250 : : {
251 : : // ignored
252 : 197 : }
253 : :
254 : 0 : virtual InputSource SAL_CALL resolveEntity(
255 : : const OUString& sPublicId,
256 : : const OUString& sSystemId)
257 : : throw (RuntimeException)
258 : : {
259 : 0 : InputSource source;
260 : 0 : source.sSystemId = sSystemId;
261 : 0 : source.sPublicId = sPublicId;
262 : :
263 : : source.aInputStream = createStreamFromFile(
264 [ # # ][ # # ]: 0 : OUStringToOString(sSystemId, RTL_TEXTENCODING_ASCII_US).getStr() );
[ # # ]
265 : :
266 : 0 : return source;
267 : : }
268 : :
269 : 0 : virtual void SAL_CALL startCDATA(void) throw (SAXException,RuntimeException)
270 : : {
271 : 0 : }
272 : 0 : virtual void SAL_CALL endCDATA(void) throw (RuntimeException)
273 : : {
274 : 0 : }
275 : 293 : virtual void SAL_CALL comment(const OUString& /*sComment*/) throw (SAXException,RuntimeException)
276 : : {
277 : 293 : }
278 : 2397 : virtual void SAL_CALL unknown(const OUString& /*sString*/) throw (SAXException,RuntimeException)
279 : : {
280 : 2397 : }
281 : :
282 : 0 : virtual void SAL_CALL allowLineBreak( void) throw (SAXException, RuntimeException )
283 : : {
284 : :
285 : 0 : }
286 : :
287 : : public:
288 : : int nError;
289 : : sal_Char theLocale[50];
290 : : OFileWriter of;
291 : : };
292 : :
293 : :
294 : :
295 : :
296 : :
297 : 197 : SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
298 : : {
299 : :
300 : :
301 [ - + ]: 197 : if( argc < 6) {
302 [ # # ]: 0 : printf( "usage : %s <locaLe> <XML inputfile> <destination file> <services.rdb location> <types.rdb location>\n", argv[0] );
303 : 0 : exit( 1 );
304 : : }
305 : :
306 : : // create service manager
307 : 197 : Reference< XMultiServiceFactory > xSMgr;
308 : : try
309 : : {
310 : : xSMgr = createRegistryServiceFactory(
311 : 197 : ::rtl::OUString::createFromAscii(argv[4]),
312 [ + - ][ + - ]: 197 : ::rtl::OUString::createFromAscii(argv[5]), true );
313 : : }
314 [ # # ]: 0 : catch ( const Exception &e )
315 : : {
316 : : printf( "Exception on createRegistryServiceFactory %s\n",
317 [ # # # # ]: 0 : OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() );
318 : 0 : exit(1);
319 : : }
320 : :
321 : : //--------------------------------
322 : : // parser demo
323 : : // read xml from a file and count elements
324 : : //--------------------------------
325 [ + - ]: 197 : Reference< XInterface > x = xSMgr->createInstance(
326 [ + - ]: 197 : OUString("com.sun.star.xml.sax.Parser") );
327 : 197 : int nError = 0;
328 [ + - ]: 197 : if( x.is() )
329 : : {
330 [ + - ]: 197 : Reference< XParser > rParser( x , UNO_QUERY );
331 : :
332 : : // create and connect the document handler to the parser
333 [ + - ]: 197 : TestDocumentHandler *pDocHandler = new TestDocumentHandler( argv[1], argv[3]);
334 : :
335 [ + - ][ + - ]: 197 : Reference < XDocumentHandler > rDocHandler( (XDocumentHandler *) pDocHandler );
336 [ + - ][ + - ]: 197 : Reference< XEntityResolver > rEntityResolver( (XEntityResolver *) pDocHandler );
337 : :
338 [ + - ][ + - ]: 197 : rParser->setDocumentHandler( rDocHandler );
339 [ + - ][ + - ]: 197 : rParser->setEntityResolver( rEntityResolver );
340 : :
341 : : // create the input stream
342 [ + - ]: 197 : InputSource source;
343 [ + - ][ + - ]: 197 : source.aInputStream = createStreamFromFile( argv[2] );
344 : 197 : source.sSystemId = OUString::createFromAscii( argv[2] );
345 : :
346 : : try
347 : : {
348 : : // start parsing
349 [ + - ][ + - ]: 197 : rParser->parseStream( source );
350 : : }
351 : :
352 [ # # ]: 0 : catch( const Exception & e )
353 : : {
354 [ # # ]: 0 : OString o1 = OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8 );
355 [ # # ]: 0 : printf( "Exception during parsing : %s\n" , o1.getStr() );
356 : 0 : exit(1);
357 : : }
358 [ + - ]: 197 : nError = pDocHandler->nError;
359 : : }
360 : : else
361 : : {
362 [ # # ]: 0 : printf( "couldn't create sax-parser component\n" );
363 : 0 : exit(1);
364 : : }
365 : :
366 : 197 : return nError;
367 : : }
368 : :
369 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|