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