nss: upgrade to release 3.73
[LibreOffice.git] / i18npool / source / localedata / saxparser.cxx
blobd26ad504f7b96a92d2357a45766496329c88793c
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>
38 #include <tools/long.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;
49 namespace {
51 /************
52 * Sequence of bytes -> InputStream
53 ************/
54 class OInputStream : public WeakImplHelper < XInputStream >
56 public:
57 explicit OInputStream( const Sequence< sal_Int8 >&seq )
58 : nPos(0)
59 , m_seq(seq)
62 public:
63 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) override
65 nBytesToRead = std::min(nBytesToRead, m_seq.getLength() - nPos);
66 aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
67 nPos += nBytesToRead;
68 return nBytesToRead;
70 virtual sal_Int32 SAL_CALL readSomeBytes(
71 css::uno::Sequence< sal_Int8 >& aData,
72 sal_Int32 nMaxBytesToRead ) override
74 return readBytes( aData, nMaxBytesToRead );
76 virtual void SAL_CALL skipBytes( sal_Int32 /*nBytesToSkip*/ ) override
78 // not implemented
80 virtual sal_Int32 SAL_CALL available( ) override
82 return m_seq.getLength() - nPos;
84 virtual void SAL_CALL closeInput( ) override
86 // not needed
88 sal_Int32 nPos;
89 Sequence< sal_Int8> m_seq;
94 // Helper : create an input stream from a file
96 static Reference< XInputStream > createStreamFromFile(
97 const char *pcFile )
99 Reference< XInputStream > r;
101 FILE *f = fopen( pcFile , "rb" );
103 if (!f)
105 fprintf(stderr, "failure opening %s\n", pcFile);
106 return r;
109 if (fseek( f , 0 , SEEK_END ) == -1)
111 fprintf(stderr, "failure fseeking %s\n", pcFile);
112 fclose(f);
113 return r;
116 tools::Long nLength = ftell( f );
117 if (nLength == -1)
119 fprintf(stderr, "failure ftelling %s\n", pcFile);
120 fclose(f);
121 return r;
124 if (fseek( f , 0 , SEEK_SET ) == -1)
126 fprintf(stderr, "failure fseeking %s\n", pcFile);
127 fclose(f);
128 return r;
131 Sequence<sal_Int8> seqIn(nLength);
132 if (fread( seqIn.getArray(), nLength , 1 , f ) == 1)
133 r.set( new OInputStream( seqIn ) );
134 else
135 fprintf(stderr, "failure reading %s\n", pcFile);
136 fclose( f );
137 return r;
140 namespace {
142 class TestDocumentHandler :
143 public WeakImplHelper< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
145 public:
146 TestDocumentHandler(const char* locale, const char* outFile )
147 : rootNode(nullptr)
148 , nError(0)
149 , theLocale(locale)
150 , of(outFile, locale)
154 virtual ~TestDocumentHandler( ) override
156 of.closeOutput();
157 delete rootNode;
161 public: // Error handler
162 virtual void SAL_CALL error(const Any& aSAXParseException) override
164 ++nError;
165 printf( "Error !\n" );
166 throw SAXException(
167 "error from error handler",
168 Reference < XInterface >() ,
169 aSAXParseException );
171 virtual void SAL_CALL fatalError(const Any& /*aSAXParseException*/) override
173 ++nError;
174 printf( "Fatal Error !\n" );
176 virtual void SAL_CALL warning(const Any& /*aSAXParseException*/) override
178 printf( "Warning !\n" );
182 public: // ExtendedDocumentHandler
185 stack<LocaleNode *> currentNode ;
186 LocaleNode * rootNode;
188 virtual void SAL_CALL startDocument() override
190 printf( "parsing document %s started\n", theLocale.c_str());
191 of.writeAsciiString("#include <sal/types.h>\n\n\n");
192 of.writeAsciiString("#include <stdio.h>\n\n");
193 of.writeAsciiString("extern \"C\" {\n\n");
196 virtual void SAL_CALL endDocument() override
198 if (rootNode)
200 rootNode->generateCode(of);
201 int err = rootNode->getError();
202 if (err)
204 printf( "Error: in data for %s: %d\n", theLocale.c_str(), err);
205 nError += err;
208 else
210 ++nError;
211 printf( "Error: no data for %s\n", theLocale.c_str());
213 printf( "parsing document %s finished\n", theLocale.c_str());
215 of.writeAsciiString("} // extern \"C\"\n\n");
216 of.closeOutput();
219 virtual void SAL_CALL startElement(const OUString& aName,
220 const Reference< XAttributeList > & xAttribs) override
223 LocaleNode * l = LocaleNode::createNode (aName, xAttribs);
224 if (!currentNode.empty() ) {
225 LocaleNode * ln = currentNode.top();
226 ln->addChild(l);
227 } else {
228 rootNode = l;
230 currentNode.push (l);
234 virtual void SAL_CALL endElement(const OUString& /*aName*/) override
236 currentNode.pop();
239 virtual void SAL_CALL characters(const OUString& aChars) override
242 LocaleNode * l = currentNode.top();
243 l->setValue (aChars);
246 virtual void SAL_CALL ignorableWhitespace(const OUString& /*aWhitespaces*/) override
250 virtual void SAL_CALL processingInstruction(const OUString& /*aTarget*/, const OUString& /*aData*/) override
252 // ignored
255 virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & /*xLocator*/) override
257 // ignored
260 virtual InputSource SAL_CALL resolveEntity(
261 const OUString& sPublicId,
262 const OUString& sSystemId) override
264 InputSource source;
265 source.sSystemId = sSystemId;
266 source.sPublicId = sPublicId;
268 source.aInputStream = createStreamFromFile(
269 OUStringToOString(sSystemId, RTL_TEXTENCODING_ASCII_US).getStr() );
271 return source;
274 virtual void SAL_CALL startCDATA() override
277 virtual void SAL_CALL endCDATA() override
280 virtual void SAL_CALL comment(const OUString& /*sComment*/) override
283 virtual void SAL_CALL unknown(const OUString& /*sString*/) override
287 virtual void SAL_CALL allowLineBreak() override
292 public:
293 int nError;
294 std::string theLocale;
295 OFileWriter of;
300 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
302 try {
303 if( argc < 4) {
304 printf( "usage : %s <locale> <XML inputfile> <destination file>\n", argv[0] );
305 exit( 1 );
308 Reference< XComponentContext > xContext(
309 defaultBootstrap_InitialComponentContext());
312 // parser demo
313 // read xml from a file and count elements
315 Reference< XParser > rParser = Parser::create(xContext);
317 int nError = 0;
318 // create and connect the document handler to the parser
319 TestDocumentHandler *pDocHandler = new TestDocumentHandler( argv[1], argv[3]);
321 Reference < XDocumentHandler > rDocHandler( static_cast<XDocumentHandler *>(pDocHandler) );
322 Reference< XEntityResolver > rEntityResolver( static_cast<XEntityResolver *>(pDocHandler) );
324 rParser->setDocumentHandler( rDocHandler );
325 rParser->setEntityResolver( rEntityResolver );
327 // create the input stream
328 InputSource source;
329 source.aInputStream = createStreamFromFile( argv[2] );
330 source.sSystemId = OUString::createFromAscii( argv[2] );
332 // start parsing
333 rParser->parseStream( source );
335 nError = pDocHandler->nError;
336 css::uno::Reference<css::lang::XComponent>(
337 xContext, css::uno::UNO_QUERY_THROW)->dispose();
338 return nError;
339 } catch (css::uno::Exception & e) {
340 std::cerr << "ERROR: " << e.Message << '\n';
341 return EXIT_FAILURE;
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */