update emoji autocorrect entries from po-files
[LibreOffice.git] / i18npool / source / localedata / saxparser.cxx
blob1674478a9c6a74614c69d02b9686ac7528ad0366
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 ::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;
59 /************
60 * Sequence of bytes -> InputStream
61 ************/
62 class OInputStream : public WeakImplHelper1 < XInputStream >
64 public:
65 explicit OInputStream( const Sequence< sal_Int8 >&seq )
66 : nPos(0)
67 , m_seq(seq)
70 public:
71 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
72 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
74 nBytesToRead = (nBytesToRead > m_seq.getLength() - nPos ) ?
75 m_seq.getLength() - nPos :
76 nBytesToRead;
77 aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
78 nPos += nBytesToRead;
79 return nBytesToRead;
81 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
86 return readBytes( aData, nMaxBytesToRead );
88 virtual void SAL_CALL skipBytes( sal_Int32 /*nBytesToSkip*/ )
89 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
91 // not implemented
93 virtual sal_Int32 SAL_CALL available( )
94 throw(NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
96 return m_seq.getLength() - nPos;
98 virtual void SAL_CALL closeInput( )
99 throw(NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE
101 // not needed
103 sal_Int32 nPos;
104 Sequence< sal_Int8> m_seq;
108 // Helper : create an input stream from a file
110 Reference< XInputStream > createStreamFromFile(
111 const char *pcFile )
113 Reference< XInputStream > r;
115 FILE *f = fopen( pcFile , "rb" );
117 if (!f)
119 fprintf(stderr, "failure opening %s\n", pcFile);
120 return r;
123 if (fseek( f , 0 , SEEK_END ) == -1)
125 fprintf(stderr, "failure fseeking %s\n", pcFile);
126 fclose(f);
127 return r;
130 long nLength = ftell( f );
131 if (nLength == -1)
133 fprintf(stderr, "failure ftelling %s\n", pcFile);
134 fclose(f);
135 return r;
138 if (fseek( f , 0 , SEEK_SET ) == -1)
140 fprintf(stderr, "failure fseeking %s\n", pcFile);
141 fclose(f);
142 return r;
145 Sequence<sal_Int8> seqIn(nLength);
146 if (fread( seqIn.getArray(), nLength , 1 , f ) == 1)
147 r = Reference< XInputStream > ( new OInputStream( seqIn ) );
148 else
149 fprintf(stderr, "failure reading %s\n", pcFile);
150 fclose( f );
151 return r;
155 class TestDocumentHandler :
156 public WeakImplHelper3< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
158 public:
159 TestDocumentHandler(const char* locale, const char* outFile )
160 : rootNode(0)
161 , nError(0)
162 , of(outFile, locale)
164 strncpy( theLocale, locale, sizeof(theLocale) );
165 theLocale[sizeof(theLocale)-1] = 0;
168 virtual ~TestDocumentHandler( )
170 of.closeOutput();
171 delete rootNode;
175 public: // Error handler
176 virtual void SAL_CALL error(const Any& aSAXParseException) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
178 ++nError;
179 printf( "Error !\n" );
180 throw SAXException(
181 "error from error handler",
182 Reference < XInterface >() ,
183 aSAXParseException );
185 virtual void SAL_CALL fatalError(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
187 ++nError;
188 printf( "Fatal Error !\n" );
190 virtual void SAL_CALL warning(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
192 printf( "Warning !\n" );
196 public: // ExtendedDocumentHandler
200 stack<LocaleNode *> currentNode ;
201 LocaleNode * rootNode;
203 virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
205 printf( "parsing document %s started\n", theLocale);
206 of.writeAsciiString("#include <sal/types.h>\n\n\n");
207 of.writeAsciiString("#include <stdio.h>\n\n");
208 of.writeAsciiString("extern \"C\" {\n\n");
211 virtual void SAL_CALL endDocument() throw (SAXException, RuntimeException, std::exception) SAL_OVERRIDE
213 if (rootNode)
215 rootNode->generateCode(of);
216 int err = rootNode->getError();
217 if (err)
219 printf( "Error: in data for %s: %d\n", theLocale, err);
220 nError += err;
223 else
225 ++nError;
226 printf( "Error: no data for %s\n", theLocale);
228 printf( "parsing document %s finished\n", theLocale);
230 of.writeAsciiString("} // extern \"C\"\n\n");
231 of.closeOutput();
234 virtual void SAL_CALL startElement(const OUString& aName,
235 const Reference< XAttributeList > & xAttribs)
236 throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
239 LocaleNode * l = LocaleNode::createNode (aName, xAttribs);
240 if (!currentNode.empty() ) {
241 LocaleNode * ln = currentNode.top();
242 ln->addChild(l);
243 } else {
244 rootNode = l;
246 currentNode.push (l);
250 virtual void SAL_CALL endElement(const OUString& /*aName*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
252 currentNode.pop();
255 virtual void SAL_CALL characters(const OUString& aChars) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
258 LocaleNode * l = currentNode.top();
259 l->setValue (aChars);
262 virtual void SAL_CALL ignorableWhitespace(const OUString& /*aWhitespaces*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
266 virtual void SAL_CALL processingInstruction(const OUString& /*aTarget*/, const OUString& /*aData*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
268 // ignored
271 virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & /*xLocator*/)
272 throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
274 // ignored
277 virtual InputSource SAL_CALL resolveEntity(
278 const OUString& sPublicId,
279 const OUString& sSystemId)
280 throw (RuntimeException, std::exception) SAL_OVERRIDE
282 InputSource source;
283 source.sSystemId = sSystemId;
284 source.sPublicId = sPublicId;
286 source.aInputStream = createStreamFromFile(
287 OUStringToOString(sSystemId, RTL_TEXTENCODING_ASCII_US).getStr() );
289 return source;
292 virtual void SAL_CALL startCDATA() throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
295 virtual void SAL_CALL endCDATA() throw (RuntimeException, std::exception) SAL_OVERRIDE
298 virtual void SAL_CALL comment(const OUString& /*sComment*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
301 virtual void SAL_CALL unknown(const OUString& /*sString*/) throw (SAXException,RuntimeException, std::exception) SAL_OVERRIDE
305 virtual void SAL_CALL allowLineBreak() throw (SAXException, RuntimeException, std::exception ) SAL_OVERRIDE
310 public:
311 int nError;
312 sal_Char theLocale[50];
313 OFileWriter of;
320 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
322 try {
323 if( argc < 4) {
324 printf( "usage : %s <locaLe> <XML inputfile> <destination file>\n", argv[0] );
325 exit( 1 );
328 Reference< XComponentContext > xContext(
329 defaultBootstrap_InitialComponentContext());
332 // parser demo
333 // read xml from a file and count elements
335 Reference< XParser > rParser = Parser::create(xContext);
337 int nError = 0;
338 // create and connect the document handler to the parser
339 TestDocumentHandler *pDocHandler = new TestDocumentHandler( argv[1], argv[3]);
341 Reference < XDocumentHandler > rDocHandler( (XDocumentHandler *) pDocHandler );
342 Reference< XEntityResolver > rEntityResolver( (XEntityResolver *) pDocHandler );
344 rParser->setDocumentHandler( rDocHandler );
345 rParser->setEntityResolver( rEntityResolver );
347 // create the input stream
348 InputSource source;
349 source.aInputStream = createStreamFromFile( argv[2] );
350 source.sSystemId = OUString::createFromAscii( argv[2] );
352 // start parsing
353 rParser->parseStream( source );
355 nError = pDocHandler->nError;
356 css::uno::Reference<css::lang::XComponent>(
357 xContext, css::uno::UNO_QUERY_THROW)->dispose();
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: */