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 : #include "sal/config.h"
21 :
22 : #include <cstdlib>
23 : #include <iostream>
24 : #include <stdio.h>
25 : #include <string.h>
26 : #include <stack>
27 :
28 : #include "sal/main.h"
29 :
30 : #include <com/sun/star/lang/XComponent.hpp>
31 :
32 : #include <com/sun/star/xml/sax/SAXParseException.hpp>
33 : #include <com/sun/star/xml/sax/Parser.hpp>
34 : #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
35 :
36 : #include <com/sun/star/io/XOutputStream.hpp>
37 : #include <com/sun/star/io/XActiveDataSource.hpp>
38 :
39 : #include <cppuhelper/bootstrap.hxx>
40 : #include <cppuhelper/implbase1.hxx>
41 : #include <cppuhelper/implbase3.hxx>
42 :
43 : #include <osl/diagnose.h>
44 :
45 : #include "LocaleNode.hxx"
46 :
47 : using namespace ::std;
48 : using namespace ::cppu;
49 : using namespace ::com::sun::star::uno;
50 : using namespace ::com::sun::star::lang;
51 : using namespace ::com::sun::star::xml::sax;
52 : using namespace ::com::sun::star::io;
53 :
54 :
55 :
56 :
57 :
58 :
59 : /************
60 : * Sequence of bytes -> InputStream
61 : ************/
62 482 : class OInputStream : public WeakImplHelper1 < XInputStream >
63 : {
64 : public:
65 241 : OInputStream( const Sequence< sal_Int8 >&seq ) :
66 : nPos( 0 ),
67 241 : m_seq( seq )
68 241 : {}
69 :
70 : public:
71 562 : virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
72 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
73 : {
74 562 : nBytesToRead = (nBytesToRead > m_seq.getLength() - nPos ) ?
75 482 : m_seq.getLength() - nPos :
76 1044 : nBytesToRead;
77 562 : aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
78 562 : nPos += nBytesToRead;
79 562 : return nBytesToRead;
80 : }
81 562 : virtual sal_Int32 SAL_CALL readSomeBytes(
82 : ::com::sun::star::uno::Sequence< sal_Int8 >& aData,
83 : sal_Int32 nMaxBytesToRead )
84 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
85 : {
86 562 : return readBytes( aData, nMaxBytesToRead );
87 : }
88 0 : virtual void SAL_CALL skipBytes( sal_Int32 /*nBytesToSkip*/ )
89 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
90 : {
91 : // not implemented
92 0 : }
93 0 : virtual sal_Int32 SAL_CALL available( )
94 : throw(NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
95 : {
96 0 : return m_seq.getLength() - nPos;
97 : }
98 0 : virtual void SAL_CALL closeInput( )
99 : throw(NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
100 : {
101 : // not needed
102 0 : }
103 : sal_Int32 nPos;
104 : Sequence< sal_Int8> m_seq;
105 : };
106 :
107 :
108 : // Helper : create an input stream from a file
109 :
110 241 : Reference< XInputStream > createStreamFromFile(
111 : const char *pcFile )
112 : {
113 241 : Reference< XInputStream > r;
114 :
115 241 : FILE *f = fopen( pcFile , "rb" );
116 :
117 241 : if (!f)
118 : {
119 0 : fprintf(stderr, "failure opening %s\n", pcFile);
120 0 : return r;
121 : }
122 :
123 241 : if (fseek( f , 0 , SEEK_END ) == -1)
124 : {
125 0 : fprintf(stderr, "failure fseeking %s\n", pcFile);
126 0 : fclose(f);
127 0 : return r;
128 : }
129 :
130 241 : long nLength = ftell( f );
131 241 : if (nLength == -1)
132 : {
133 0 : fprintf(stderr, "failure ftelling %s\n", pcFile);
134 0 : fclose(f);
135 0 : return r;
136 : }
137 :
138 241 : if (fseek( f , 0 , SEEK_SET ) == -1)
139 : {
140 0 : fprintf(stderr, "failure fseeking %s\n", pcFile);
141 0 : fclose(f);
142 0 : return r;
143 : }
144 :
145 482 : Sequence<sal_Int8> seqIn(nLength);
146 241 : if (fread( seqIn.getArray(), nLength , 1 , f ) == 1)
147 241 : r = Reference< XInputStream > ( new OInputStream( seqIn ) );
148 : else
149 0 : fprintf(stderr, "failure reading %s\n", pcFile);
150 241 : fclose( f );
151 241 : return r;
152 : }
153 :
154 :
155 : class TestDocumentHandler :
156 : public WeakImplHelper3< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
157 : {
158 : public:
159 241 : TestDocumentHandler(const char* locale, const char* outFile )
160 : : rootNode(0)
161 : , nError(0)
162 241 : , of(outFile, locale)
163 : {
164 241 : strncpy( theLocale, locale, sizeof(theLocale) );
165 241 : theLocale[sizeof(theLocale)-1] = 0;
166 241 : }
167 :
168 482 : virtual ~TestDocumentHandler( )
169 482 : {
170 241 : of.closeOutput();
171 241 : delete rootNode;
172 482 : }
173 :
174 :
175 : public: // Error handler
176 0 : virtual void SAL_CALL error(const Any& aSAXParseException) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
177 : {
178 0 : ++nError;
179 0 : printf( "Error !\n" );
180 : throw SAXException(
181 : "error from error handler",
182 : Reference < XInterface >() ,
183 0 : aSAXParseException );
184 : }
185 0 : virtual void SAL_CALL fatalError(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
186 : {
187 0 : ++nError;
188 0 : printf( "Fatal Error !\n" );
189 0 : }
190 0 : virtual void SAL_CALL warning(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
191 : {
192 0 : printf( "Warning !\n" );
193 0 : }
194 :
195 :
196 : public: // ExtendedDocumentHandler
197 :
198 :
199 :
200 : stack<LocaleNode *> currentNode ;
201 : LocaleNode * rootNode;
202 :
203 241 : virtual void SAL_CALL startDocument(void) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
204 : {
205 241 : printf( "parsing document %s started\n", theLocale);
206 241 : of.writeAsciiString("#include <sal/types.h>\n\n\n");
207 241 : of.writeAsciiString("#include <stdio.h>\n\n");
208 241 : of.writeAsciiString("extern \"C\" {\n\n");
209 241 : }
210 :
211 241 : virtual void SAL_CALL endDocument(void) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
212 : {
213 241 : if (rootNode)
214 : {
215 241 : rootNode->generateCode(of);
216 241 : int err = rootNode->getError();
217 241 : if (err)
218 : {
219 0 : printf( "Error: in data for %s: %d\n", theLocale, err);
220 0 : nError += err;
221 : }
222 : }
223 : else
224 : {
225 0 : ++nError;
226 0 : printf( "Error: no data for %s\n", theLocale);
227 : }
228 241 : printf( "parsing document %s finished\n", theLocale);
229 :
230 241 : of.writeAsciiString("} // extern \"C\"\n\n");
231 241 : of.closeOutput();
232 241 : }
233 :
234 50200 : virtual void SAL_CALL startElement(const OUString& aName,
235 : const Reference< XAttributeList > & xAttribs)
236 : throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
237 : {
238 :
239 50200 : LocaleNode * l = LocaleNode::createNode (aName, xAttribs);
240 50200 : if (!currentNode.empty() ) {
241 49959 : LocaleNode * ln = (LocaleNode *) currentNode.top();
242 49959 : ln->addChild(l);
243 : } else {
244 241 : rootNode = l;
245 : }
246 50200 : currentNode.push (l);
247 50200 : }
248 :
249 :
250 50200 : virtual void SAL_CALL endElement(const OUString& /*aName*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
251 : {
252 50200 : currentNode.pop();
253 50200 : }
254 :
255 163078 : virtual void SAL_CALL characters(const OUString& aChars) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
256 : {
257 :
258 163078 : LocaleNode * l = currentNode.top();
259 163078 : l->setValue (aChars);
260 163078 : }
261 :
262 0 : virtual void SAL_CALL ignorableWhitespace(const OUString& /*aWhitespaces*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
263 : {
264 0 : }
265 :
266 0 : virtual void SAL_CALL processingInstruction(const OUString& /*aTarget*/, const OUString& /*aData*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
267 : {
268 : // ignored
269 0 : }
270 :
271 241 : virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & /*xLocator*/)
272 : throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
273 : {
274 : // ignored
275 241 : }
276 :
277 0 : virtual InputSource SAL_CALL resolveEntity(
278 : const OUString& sPublicId,
279 : const OUString& sSystemId)
280 : throw (RuntimeException, std::exception) SAL_OVERRIDE
281 : {
282 0 : InputSource source;
283 0 : source.sSystemId = sSystemId;
284 0 : source.sPublicId = sPublicId;
285 :
286 0 : source.aInputStream = createStreamFromFile(
287 0 : OUStringToOString(sSystemId, RTL_TEXTENCODING_ASCII_US).getStr() );
288 :
289 0 : return source;
290 : }
291 :
292 0 : virtual void SAL_CALL startCDATA(void) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
293 : {
294 0 : }
295 0 : virtual void SAL_CALL endCDATA(void) throw (RuntimeException, std::exception) SAL_OVERRIDE
296 : {
297 0 : }
298 405 : virtual void SAL_CALL comment(const OUString& /*sComment*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
299 : {
300 405 : }
301 2959 : virtual void SAL_CALL unknown(const OUString& /*sString*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
302 : {
303 2959 : }
304 :
305 0 : virtual void SAL_CALL allowLineBreak( void) throw (SAXException, RuntimeException, std::exception ) SAL_OVERRIDE
306 : {
307 :
308 0 : }
309 :
310 : public:
311 : int nError;
312 : sal_Char theLocale[50];
313 : OFileWriter of;
314 : };
315 :
316 :
317 :
318 :
319 :
320 482 : SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
321 : {
322 : try {
323 241 : if( argc < 4) {
324 0 : printf( "usage : %s <locaLe> <XML inputfile> <destination file>\n", argv[0] );
325 0 : exit( 1 );
326 : }
327 :
328 : Reference< XComponentContext > xContext(
329 241 : defaultBootstrap_InitialComponentContext());
330 :
331 :
332 : // parser demo
333 : // read xml from a file and count elements
334 :
335 482 : Reference< XParser > rParser = Parser::create(xContext);
336 :
337 241 : int nError = 0;
338 : // create and connect the document handler to the parser
339 241 : TestDocumentHandler *pDocHandler = new TestDocumentHandler( argv[1], argv[3]);
340 :
341 482 : Reference < XDocumentHandler > rDocHandler( (XDocumentHandler *) pDocHandler );
342 482 : Reference< XEntityResolver > rEntityResolver( (XEntityResolver *) pDocHandler );
343 :
344 241 : rParser->setDocumentHandler( rDocHandler );
345 241 : rParser->setEntityResolver( rEntityResolver );
346 :
347 : // create the input stream
348 482 : InputSource source;
349 241 : source.aInputStream = createStreamFromFile( argv[2] );
350 241 : source.sSystemId = OUString::createFromAscii( argv[2] );
351 :
352 : // start parsing
353 241 : rParser->parseStream( source );
354 :
355 241 : nError = pDocHandler->nError;
356 : css::uno::Reference<css::lang::XComponent>(
357 241 : xContext, css::uno::UNO_QUERY_THROW)->dispose();
358 482 : return nError;
359 0 : } catch (css::uno::Exception & e) {
360 0 : std::cerr << "ERROR: " << e.Message << '\n';
361 0 : return EXIT_FAILURE;
362 : }
363 723 : }
364 :
365 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|