Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / i18npool / source / localedata / saxparser.cxx
blobf367a5d50ea1d24158ba0af53da114e605bfe64c
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/implbase.hxx>
42 #include <osl/diagnose.h>
44 #include "LocaleNode.hxx"
46 using namespace ::std;
47 using namespace ::cppu;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::xml::sax;
51 using namespace ::com::sun::star::io;
54 /************
55 * Sequence of bytes -> InputStream
56 ************/
57 class OInputStream : public WeakImplHelper < XInputStream >
59 public:
60 explicit OInputStream( const Sequence< sal_Int8 >&seq )
61 : nPos(0)
62 , m_seq(seq)
65 public:
66 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
67 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) override
69 nBytesToRead = (nBytesToRead > m_seq.getLength() - nPos ) ?
70 m_seq.getLength() - nPos :
71 nBytesToRead;
72 aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
73 nPos += nBytesToRead;
74 return nBytesToRead;
76 virtual sal_Int32 SAL_CALL readSomeBytes(
77 css::uno::Sequence< sal_Int8 >& aData,
78 sal_Int32 nMaxBytesToRead )
79 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) override
81 return readBytes( aData, nMaxBytesToRead );
83 virtual void SAL_CALL skipBytes( sal_Int32 /*nBytesToSkip*/ )
84 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) override
86 // not implemented
88 virtual sal_Int32 SAL_CALL available( )
89 throw(NotConnectedException, IOException, RuntimeException, std::exception) override
91 return m_seq.getLength() - nPos;
93 virtual void SAL_CALL closeInput( )
94 throw(NotConnectedException, IOException, RuntimeException, std::exception) override
96 // not needed
98 sal_Int32 nPos;
99 Sequence< sal_Int8> m_seq;
103 // Helper : create an input stream from a file
105 Reference< XInputStream > createStreamFromFile(
106 const char *pcFile )
108 Reference< XInputStream > r;
110 FILE *f = fopen( pcFile , "rb" );
112 if (!f)
114 fprintf(stderr, "failure opening %s\n", pcFile);
115 return r;
118 if (fseek( f , 0 , SEEK_END ) == -1)
120 fprintf(stderr, "failure fseeking %s\n", pcFile);
121 fclose(f);
122 return r;
125 long nLength = ftell( f );
126 if (nLength == -1)
128 fprintf(stderr, "failure ftelling %s\n", pcFile);
129 fclose(f);
130 return r;
133 if (fseek( f , 0 , SEEK_SET ) == -1)
135 fprintf(stderr, "failure fseeking %s\n", pcFile);
136 fclose(f);
137 return r;
140 Sequence<sal_Int8> seqIn(nLength);
141 if (fread( seqIn.getArray(), nLength , 1 , f ) == 1)
142 r.set( new OInputStream( seqIn ) );
143 else
144 fprintf(stderr, "failure reading %s\n", pcFile);
145 fclose( f );
146 return r;
150 class TestDocumentHandler :
151 public WeakImplHelper< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
153 public:
154 TestDocumentHandler(const char* locale, const char* outFile )
155 : rootNode(nullptr)
156 , nError(0)
157 , of(outFile, locale)
159 strncpy( theLocale, locale, sizeof(theLocale) );
160 theLocale[sizeof(theLocale)-1] = 0;
163 virtual ~TestDocumentHandler( )
165 of.closeOutput();
166 delete rootNode;
170 public: // Error handler
171 virtual void SAL_CALL error(const Any& aSAXParseException) throw (SAXException, RuntimeException, std::exception) override
173 ++nError;
174 printf( "Error !\n" );
175 throw SAXException(
176 "error from error handler",
177 Reference < XInterface >() ,
178 aSAXParseException );
180 virtual void SAL_CALL fatalError(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException, std::exception) override
182 ++nError;
183 printf( "Fatal Error !\n" );
185 virtual void SAL_CALL warning(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException, std::exception) override
187 printf( "Warning !\n" );
191 public: // ExtendedDocumentHandler
194 stack<LocaleNode *> currentNode ;
195 LocaleNode * rootNode;
197 virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException, std::exception) override
199 printf( "parsing document %s started\n", theLocale);
200 of.writeAsciiString("#include <sal/types.h>\n\n\n");
201 of.writeAsciiString("#include <stdio.h>\n\n");
202 of.writeAsciiString("extern \"C\" {\n\n");
205 virtual void SAL_CALL endDocument() throw (SAXException, RuntimeException, std::exception) override
207 if (rootNode)
209 rootNode->generateCode(of);
210 int err = rootNode->getError();
211 if (err)
213 printf( "Error: in data for %s: %d\n", theLocale, err);
214 nError += err;
217 else
219 ++nError;
220 printf( "Error: no data for %s\n", theLocale);
222 printf( "parsing document %s finished\n", theLocale);
224 of.writeAsciiString("} // extern \"C\"\n\n");
225 of.closeOutput();
228 virtual void SAL_CALL startElement(const OUString& aName,
229 const Reference< XAttributeList > & xAttribs)
230 throw (SAXException,RuntimeException, std::exception) override
233 LocaleNode * l = LocaleNode::createNode (aName, xAttribs);
234 if (!currentNode.empty() ) {
235 LocaleNode * ln = currentNode.top();
236 ln->addChild(l);
237 } else {
238 rootNode = l;
240 currentNode.push (l);
244 virtual void SAL_CALL endElement(const OUString& /*aName*/) throw (SAXException,RuntimeException, std::exception) override
246 currentNode.pop();
249 virtual void SAL_CALL characters(const OUString& aChars) throw (SAXException,RuntimeException, std::exception) override
252 LocaleNode * l = currentNode.top();
253 l->setValue (aChars);
256 virtual void SAL_CALL ignorableWhitespace(const OUString& /*aWhitespaces*/) throw (SAXException,RuntimeException, std::exception) override
260 virtual void SAL_CALL processingInstruction(const OUString& /*aTarget*/, const OUString& /*aData*/) throw (SAXException,RuntimeException, std::exception) override
262 // ignored
265 virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & /*xLocator*/)
266 throw (SAXException,RuntimeException, std::exception) override
268 // ignored
271 virtual InputSource SAL_CALL resolveEntity(
272 const OUString& sPublicId,
273 const OUString& sSystemId)
274 throw (RuntimeException, std::exception) override
276 InputSource source;
277 source.sSystemId = sSystemId;
278 source.sPublicId = sPublicId;
280 source.aInputStream = createStreamFromFile(
281 OUStringToOString(sSystemId, RTL_TEXTENCODING_ASCII_US).getStr() );
283 return source;
286 virtual void SAL_CALL startCDATA() throw (SAXException,RuntimeException, std::exception) override
289 virtual void SAL_CALL endCDATA() throw (RuntimeException, std::exception) override
292 virtual void SAL_CALL comment(const OUString& /*sComment*/) throw (SAXException,RuntimeException, std::exception) override
295 virtual void SAL_CALL unknown(const OUString& /*sString*/) throw (SAXException,RuntimeException, std::exception) override
299 virtual void SAL_CALL allowLineBreak() throw (SAXException, RuntimeException, std::exception ) override
304 public:
305 int nError;
306 sal_Char theLocale[50];
307 OFileWriter of;
311 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
313 try {
314 if( argc < 4) {
315 printf( "usage : %s <locale> <XML inputfile> <destination file>\n", argv[0] );
316 exit( 1 );
319 Reference< XComponentContext > xContext(
320 defaultBootstrap_InitialComponentContext());
323 // parser demo
324 // read xml from a file and count elements
326 Reference< XParser > rParser = Parser::create(xContext);
328 int nError = 0;
329 // create and connect the document handler to the parser
330 TestDocumentHandler *pDocHandler = new TestDocumentHandler( argv[1], argv[3]);
332 Reference < XDocumentHandler > rDocHandler( static_cast<XDocumentHandler *>(pDocHandler) );
333 Reference< XEntityResolver > rEntityResolver( static_cast<XEntityResolver *>(pDocHandler) );
335 rParser->setDocumentHandler( rDocHandler );
336 rParser->setEntityResolver( rEntityResolver );
338 // create the input stream
339 InputSource source;
340 source.aInputStream = createStreamFromFile( argv[2] );
341 source.sSystemId = OUString::createFromAscii( argv[2] );
343 // start parsing
344 rParser->parseStream( source );
346 nError = pDocHandler->nError;
347 css::uno::Reference<css::lang::XComponent>(
348 xContext, css::uno::UNO_QUERY_THROW)->dispose();
349 return nError;
350 } catch (css::uno::Exception & e) {
351 std::cerr << "ERROR: " << e.Message << '\n';
352 return EXIT_FAILURE;
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */