Bump version to 4.3-4
[LibreOffice.git] / i18npool / source / localedata / saxparser.cxx
blobd3647aa6bf566e8cd344ae707dbe4599449604eb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
22 #include <cstdlib>
23 #include <iostream>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stack>
28 #include "sal/main.h"
30 #include <com/sun/star/lang/XComponent.hpp>
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>
36 #include <com/sun/star/io/XOutputStream.hpp>
37 #include <com/sun/star/io/XActiveDataSource.hpp>
39 #include <cppuhelper/bootstrap.hxx>
40 #include <cppuhelper/implbase1.hxx>
41 #include <cppuhelper/implbase3.hxx>
43 #include <osl/diagnose.h>
45 #include "LocaleNode.hxx"
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;
60 /************
61 * Sequence of bytes -> InputStream
62 ************/
63 class OInputStream : public WeakImplHelper1 < XInputStream >
65 public:
66 OInputStream( const Sequence< sal_Int8 >&seq ) :
67 nPos( 0 ),
68 m_seq( seq )
71 public:
72 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
73 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
75 nBytesToRead = (nBytesToRead > m_seq.getLength() - nPos ) ?
76 m_seq.getLength() - nPos :
77 nBytesToRead;
78 aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
79 nPos += nBytesToRead;
80 return nBytesToRead;
82 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, std::exception) SAL_OVERRIDE
87 return readBytes( aData, nMaxBytesToRead );
89 virtual void SAL_CALL skipBytes( sal_Int32 /*nBytesToSkip*/ )
90 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
92 // not implemented
94 virtual sal_Int32 SAL_CALL available( )
95 throw(NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
97 return m_seq.getLength() - nPos;
99 virtual void SAL_CALL closeInput( )
100 throw(NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
102 // not needed
104 sal_Int32 nPos;
105 Sequence< sal_Int8> m_seq;
109 // Helper : create an input stream from a file
111 Reference< XInputStream > createStreamFromFile(
112 const char *pcFile )
114 Reference< XInputStream > r;
116 FILE *f = fopen( pcFile , "rb" );
118 if (!f)
120 fprintf(stderr, "failure opening %s\n", pcFile);
121 return r;
124 if (fseek( f , 0 , SEEK_END ) == -1)
126 fprintf(stderr, "failure fseeking %s\n", pcFile);
127 fclose(f);
128 return r;
131 long nLength = ftell( f );
132 if (nLength == -1)
134 fprintf(stderr, "failure ftelling %s\n", pcFile);
135 fclose(f);
136 return r;
139 if (fseek( f , 0 , SEEK_SET ) == -1)
141 fprintf(stderr, "failure fseeking %s\n", pcFile);
142 fclose(f);
143 return r;
146 Sequence<sal_Int8> seqIn(nLength);
147 if (fread( seqIn.getArray(), nLength , 1 , f ) == 1)
148 r = Reference< XInputStream > ( new OInputStream( seqIn ) );
149 else
150 fprintf(stderr, "failure reading %s\n", pcFile);
151 fclose( f );
152 return r;
156 class TestDocumentHandler :
157 public WeakImplHelper3< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
159 public:
160 TestDocumentHandler(const char* locale, const char* outFile )
161 : rootNode(0)
162 , nError(0)
163 , of(outFile, locale)
165 strncpy( theLocale, locale, sizeof(theLocale) );
166 theLocale[sizeof(theLocale)-1] = 0;
169 virtual ~TestDocumentHandler( )
171 of.closeOutput();
172 delete rootNode;
176 public: // Error handler
177 virtual void SAL_CALL error(const Any& aSAXParseException) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
179 ++nError;
180 printf( "Error !\n" );
181 throw SAXException(
182 OUString( "error from error handler") ,
183 Reference < XInterface >() ,
184 aSAXParseException );
186 virtual void SAL_CALL fatalError(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
188 ++nError;
189 printf( "Fatal Error !\n" );
191 virtual void SAL_CALL warning(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
193 printf( "Warning !\n" );
197 public: // ExtendedDocumentHandler
201 stack<LocaleNode *> currentNode ;
202 LocaleNode * rootNode;
204 virtual void SAL_CALL startDocument(void) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
206 printf( "parsing document %s started\n", theLocale);
207 of.writeAsciiString("#include <sal/types.h>\n\n\n");
208 of.writeAsciiString("#include <stdio.h>\n\n");
209 of.writeAsciiString("extern \"C\" {\n\n");
212 virtual void SAL_CALL endDocument(void) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
214 if (rootNode)
216 rootNode->generateCode(of);
217 int err = rootNode->getError();
218 if (err)
220 printf( "Error: in data for %s: %d\n", theLocale, err);
221 nError += err;
224 else
226 ++nError;
227 printf( "Error: no data for %s\n", theLocale);
229 printf( "parsing document %s finished\n", theLocale);
231 of.writeAsciiString("} // extern \"C\"\n\n");
232 of.closeOutput();
235 virtual void SAL_CALL startElement(const OUString& aName,
236 const Reference< XAttributeList > & xAttribs)
237 throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
240 LocaleNode * l = LocaleNode::createNode (aName, xAttribs);
241 if (!currentNode.empty() ) {
242 LocaleNode * ln = (LocaleNode *) currentNode.top();
243 ln->addChild(l);
244 } else {
245 rootNode = l;
247 currentNode.push (l);
251 virtual void SAL_CALL endElement(const OUString& /*aName*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
253 currentNode.pop();
256 virtual void SAL_CALL characters(const OUString& aChars) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
259 LocaleNode * l = currentNode.top();
260 l->setValue (aChars);
263 virtual void SAL_CALL ignorableWhitespace(const OUString& /*aWhitespaces*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
267 virtual void SAL_CALL processingInstruction(const OUString& /*aTarget*/, const OUString& /*aData*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
269 // ignored
272 virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & /*xLocator*/)
273 throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
275 // ignored
278 virtual InputSource SAL_CALL resolveEntity(
279 const OUString& sPublicId,
280 const OUString& sSystemId)
281 throw (RuntimeException, std::exception) SAL_OVERRIDE
283 InputSource source;
284 source.sSystemId = sSystemId;
285 source.sPublicId = sPublicId;
287 source.aInputStream = createStreamFromFile(
288 OUStringToOString(sSystemId, RTL_TEXTENCODING_ASCII_US).getStr() );
290 return source;
293 virtual void SAL_CALL startCDATA(void) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
296 virtual void SAL_CALL endCDATA(void) throw (RuntimeException, std::exception) SAL_OVERRIDE
299 virtual void SAL_CALL comment(const OUString& /*sComment*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
302 virtual void SAL_CALL unknown(const OUString& /*sString*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
306 virtual void SAL_CALL allowLineBreak( void) throw (SAXException, RuntimeException, std::exception ) SAL_OVERRIDE
311 public:
312 int nError;
313 sal_Char theLocale[50];
314 OFileWriter of;
321 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
323 try {
324 if( argc < 4) {
325 printf( "usage : %s <locaLe> <XML inputfile> <destination file>\n", argv[0] );
326 exit( 1 );
329 Reference< XComponentContext > xContext(
330 defaultBootstrap_InitialComponentContext());
333 // parser demo
334 // read xml from a file and count elements
336 Reference< XParser > rParser = Parser::create(xContext);
338 int nError = 0;
339 // create and connect the document handler to the parser
340 TestDocumentHandler *pDocHandler = new TestDocumentHandler( argv[1], argv[3]);
342 Reference < XDocumentHandler > rDocHandler( (XDocumentHandler *) pDocHandler );
343 Reference< XEntityResolver > rEntityResolver( (XEntityResolver *) pDocHandler );
345 rParser->setDocumentHandler( rDocHandler );
346 rParser->setEntityResolver( rEntityResolver );
348 // create the input stream
349 InputSource source;
350 source.aInputStream = createStreamFromFile( argv[2] );
351 source.sSystemId = OUString::createFromAscii( argv[2] );
353 // start parsing
354 rParser->parseStream( source );
356 nError = pDocHandler->nError;
358 return nError;
359 } catch (css::uno::Exception & e) {
360 std::cerr << "ERROR: " << e.Message << '\n';
361 return EXIT_FAILURE;
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */