Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / i18npool / source / localedata / saxparser.cxx
blobace747d8286f4b3388925bd496d363604198115b
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>
26 #include <stack>
28 #include <sal/main.h>
30 #include <com/sun/star/lang/XComponent.hpp>
32 #include <com/sun/star/xml/sax/SAXException.hpp>
33 #include <com/sun/star/xml/sax/Parser.hpp>
34 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
36 #include <cppuhelper/bootstrap.hxx>
37 #include <cppuhelper/implbase.hxx>
40 #include "LocaleNode.hxx"
42 using namespace ::std;
43 using namespace ::cppu;
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::xml::sax;
47 using namespace ::com::sun::star::io;
50 /************
51 * Sequence of bytes -> InputStream
52 ************/
53 class OInputStream : public WeakImplHelper < XInputStream >
55 public:
56 explicit OInputStream( const Sequence< sal_Int8 >&seq )
57 : nPos(0)
58 , m_seq(seq)
61 public:
62 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) override
64 nBytesToRead = std::min(nBytesToRead, m_seq.getLength() - nPos);
65 aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
66 nPos += nBytesToRead;
67 return nBytesToRead;
69 virtual sal_Int32 SAL_CALL readSomeBytes(
70 css::uno::Sequence< sal_Int8 >& aData,
71 sal_Int32 nMaxBytesToRead ) override
73 return readBytes( aData, nMaxBytesToRead );
75 virtual void SAL_CALL skipBytes( sal_Int32 /*nBytesToSkip*/ ) override
77 // not implemented
79 virtual sal_Int32 SAL_CALL available( ) override
81 return m_seq.getLength() - nPos;
83 virtual void SAL_CALL closeInput( ) override
85 // not needed
87 sal_Int32 nPos;
88 Sequence< sal_Int8> m_seq;
92 // Helper : create an input stream from a file
94 static Reference< XInputStream > createStreamFromFile(
95 const char *pcFile )
97 Reference< XInputStream > r;
99 FILE *f = fopen( pcFile , "rb" );
101 if (!f)
103 fprintf(stderr, "failure opening %s\n", pcFile);
104 return r;
107 if (fseek( f , 0 , SEEK_END ) == -1)
109 fprintf(stderr, "failure fseeking %s\n", pcFile);
110 fclose(f);
111 return r;
114 long nLength = ftell( f );
115 if (nLength == -1)
117 fprintf(stderr, "failure ftelling %s\n", pcFile);
118 fclose(f);
119 return r;
122 if (fseek( f , 0 , SEEK_SET ) == -1)
124 fprintf(stderr, "failure fseeking %s\n", pcFile);
125 fclose(f);
126 return r;
129 Sequence<sal_Int8> seqIn(nLength);
130 if (fread( seqIn.getArray(), nLength , 1 , f ) == 1)
131 r.set( new OInputStream( seqIn ) );
132 else
133 fprintf(stderr, "failure reading %s\n", pcFile);
134 fclose( f );
135 return r;
139 class TestDocumentHandler :
140 public WeakImplHelper< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
142 public:
143 TestDocumentHandler(const char* locale, const char* outFile )
144 : rootNode(nullptr)
145 , nError(0)
146 , theLocale(locale)
147 , of(outFile, locale)
151 virtual ~TestDocumentHandler( ) override
153 of.closeOutput();
154 delete rootNode;
158 public: // Error handler
159 virtual void SAL_CALL error(const Any& aSAXParseException) override
161 ++nError;
162 printf( "Error !\n" );
163 throw SAXException(
164 "error from error handler",
165 Reference < XInterface >() ,
166 aSAXParseException );
168 virtual void SAL_CALL fatalError(const Any& /*aSAXParseException*/) override
170 ++nError;
171 printf( "Fatal Error !\n" );
173 virtual void SAL_CALL warning(const Any& /*aSAXParseException*/) override
175 printf( "Warning !\n" );
179 public: // ExtendedDocumentHandler
182 stack<LocaleNode *> currentNode ;
183 LocaleNode * rootNode;
185 virtual void SAL_CALL startDocument() override
187 printf( "parsing document %s started\n", theLocale.c_str());
188 of.writeAsciiString("#include <sal/types.h>\n\n\n");
189 of.writeAsciiString("#include <stdio.h>\n\n");
190 of.writeAsciiString("extern \"C\" {\n\n");
193 virtual void SAL_CALL endDocument() override
195 if (rootNode)
197 rootNode->generateCode(of);
198 int err = rootNode->getError();
199 if (err)
201 printf( "Error: in data for %s: %d\n", theLocale.c_str(), err);
202 nError += err;
205 else
207 ++nError;
208 printf( "Error: no data for %s\n", theLocale.c_str());
210 printf( "parsing document %s finished\n", theLocale.c_str());
212 of.writeAsciiString("} // extern \"C\"\n\n");
213 of.closeOutput();
216 virtual void SAL_CALL startElement(const OUString& aName,
217 const Reference< XAttributeList > & xAttribs) override
220 LocaleNode * l = LocaleNode::createNode (aName, xAttribs);
221 if (!currentNode.empty() ) {
222 LocaleNode * ln = currentNode.top();
223 ln->addChild(l);
224 } else {
225 rootNode = l;
227 currentNode.push (l);
231 virtual void SAL_CALL endElement(const OUString& /*aName*/) override
233 currentNode.pop();
236 virtual void SAL_CALL characters(const OUString& aChars) override
239 LocaleNode * l = currentNode.top();
240 l->setValue (aChars);
243 virtual void SAL_CALL ignorableWhitespace(const OUString& /*aWhitespaces*/) override
247 virtual void SAL_CALL processingInstruction(const OUString& /*aTarget*/, const OUString& /*aData*/) override
249 // ignored
252 virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & /*xLocator*/) override
254 // ignored
257 virtual InputSource SAL_CALL resolveEntity(
258 const OUString& sPublicId,
259 const OUString& sSystemId) override
261 InputSource source;
262 source.sSystemId = sSystemId;
263 source.sPublicId = sPublicId;
265 source.aInputStream = createStreamFromFile(
266 OUStringToOString(sSystemId, RTL_TEXTENCODING_ASCII_US).getStr() );
268 return source;
271 virtual void SAL_CALL startCDATA() override
274 virtual void SAL_CALL endCDATA() override
277 virtual void SAL_CALL comment(const OUString& /*sComment*/) override
280 virtual void SAL_CALL unknown(const OUString& /*sString*/) override
284 virtual void SAL_CALL allowLineBreak() override
289 public:
290 int nError;
291 std::string theLocale;
292 OFileWriter of;
296 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
298 try {
299 if( argc < 4) {
300 printf( "usage : %s <locale> <XML inputfile> <destination file>\n", argv[0] );
301 exit( 1 );
304 Reference< XComponentContext > xContext(
305 defaultBootstrap_InitialComponentContext());
308 // parser demo
309 // read xml from a file and count elements
311 Reference< XParser > rParser = Parser::create(xContext);
313 int nError = 0;
314 // create and connect the document handler to the parser
315 TestDocumentHandler *pDocHandler = new TestDocumentHandler( argv[1], argv[3]);
317 Reference < XDocumentHandler > rDocHandler( static_cast<XDocumentHandler *>(pDocHandler) );
318 Reference< XEntityResolver > rEntityResolver( static_cast<XEntityResolver *>(pDocHandler) );
320 rParser->setDocumentHandler( rDocHandler );
321 rParser->setEntityResolver( rEntityResolver );
323 // create the input stream
324 InputSource source;
325 source.aInputStream = createStreamFromFile( argv[2] );
326 source.sSystemId = OUString::createFromAscii( argv[2] );
328 // start parsing
329 rParser->parseStream( source );
331 nError = pDocHandler->nError;
332 css::uno::Reference<css::lang::XComponent>(
333 xContext, css::uno::UNO_QUERY_THROW)->dispose();
334 return nError;
335 } catch (css::uno::Exception & e) {
336 std::cerr << "ERROR: " << e.Message << '\n';
337 return EXIT_FAILURE;
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */