1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
51 * Sequence of bytes -> InputStream
53 class OInputStream
: public WeakImplHelper
< XInputStream
>
56 explicit OInputStream( const Sequence
< sal_Int8
>&seq
)
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
);
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
79 virtual sal_Int32 SAL_CALL
available( ) override
81 return m_seq
.getLength() - nPos
;
83 virtual void SAL_CALL
closeInput( ) override
88 Sequence
< sal_Int8
> m_seq
;
92 // Helper : create an input stream from a file
94 static Reference
< XInputStream
> createStreamFromFile(
97 Reference
< XInputStream
> r
;
99 FILE *f
= fopen( pcFile
, "rb" );
103 fprintf(stderr
, "failure opening %s\n", pcFile
);
107 if (fseek( f
, 0 , SEEK_END
) == -1)
109 fprintf(stderr
, "failure fseeking %s\n", pcFile
);
114 long nLength
= ftell( f
);
117 fprintf(stderr
, "failure ftelling %s\n", pcFile
);
122 if (fseek( f
, 0 , SEEK_SET
) == -1)
124 fprintf(stderr
, "failure fseeking %s\n", pcFile
);
129 Sequence
<sal_Int8
> seqIn(nLength
);
130 if (fread( seqIn
.getArray(), nLength
, 1 , f
) == 1)
131 r
.set( new OInputStream( seqIn
) );
133 fprintf(stderr
, "failure reading %s\n", pcFile
);
139 class TestDocumentHandler
:
140 public WeakImplHelper
< XExtendedDocumentHandler
, XEntityResolver
, XErrorHandler
>
143 TestDocumentHandler(const char* locale
, const char* outFile
)
147 , of(outFile
, locale
)
151 virtual ~TestDocumentHandler( ) override
158 public: // Error handler
159 virtual void SAL_CALL
error(const Any
& aSAXParseException
) override
162 printf( "Error !\n" );
164 "error from error handler",
165 Reference
< XInterface
>() ,
166 aSAXParseException
);
168 virtual void SAL_CALL
fatalError(const Any
& /*aSAXParseException*/) override
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
197 rootNode
->generateCode(of
);
198 int err
= rootNode
->getError();
201 printf( "Error: in data for %s: %d\n", theLocale
.c_str(), err
);
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");
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();
227 currentNode
.push (l
);
231 virtual void SAL_CALL
endElement(const OUString
& /*aName*/) override
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
252 virtual void SAL_CALL
setDocumentLocator(const Reference
< XLocator
> & /*xLocator*/) override
257 virtual InputSource SAL_CALL
resolveEntity(
258 const OUString
& sPublicId
,
259 const OUString
& sSystemId
) override
262 source
.sSystemId
= sSystemId
;
263 source
.sPublicId
= sPublicId
;
265 source
.aInputStream
= createStreamFromFile(
266 OUStringToOString(sSystemId
, RTL_TEXTENCODING_ASCII_US
).getStr() );
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
291 std::string theLocale
;
296 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
300 printf( "usage : %s <locale> <XML inputfile> <destination file>\n", argv
[0] );
304 Reference
< XComponentContext
> xContext(
305 defaultBootstrap_InitialComponentContext());
309 // read xml from a file and count elements
311 Reference
< XParser
> rParser
= Parser::create(xContext
);
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
325 source
.aInputStream
= createStreamFromFile( argv
[2] );
326 source
.sSystemId
= OUString::createFromAscii( argv
[2] );
329 rParser
->parseStream( source
);
331 nError
= pDocHandler
->nError
;
332 css::uno::Reference
<css::lang::XComponent
>(
333 xContext
, css::uno::UNO_QUERY_THROW
)->dispose();
335 } catch (css::uno::Exception
& e
) {
336 std::cerr
<< "ERROR: " << e
.Message
<< '\n';
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */