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>
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
;
52 * Sequence of bytes -> InputStream
54 class OInputStream
: public WeakImplHelper
< XInputStream
>
57 explicit OInputStream( const Sequence
< sal_Int8
>&seq
)
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
);
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
80 virtual sal_Int32 SAL_CALL
available( ) override
82 return m_seq
.getLength() - nPos
;
84 virtual void SAL_CALL
closeInput( ) override
89 Sequence
< sal_Int8
> m_seq
;
94 // Helper : create an input stream from a file
96 static Reference
< XInputStream
> createStreamFromFile(
99 Reference
< XInputStream
> r
;
101 FILE *f
= fopen( pcFile
, "rb" );
105 fprintf(stderr
, "failure opening %s\n", pcFile
);
109 if (fseek( f
, 0 , SEEK_END
) == -1)
111 fprintf(stderr
, "failure fseeking %s\n", pcFile
);
116 tools::Long nLength
= ftell( f
);
119 fprintf(stderr
, "failure ftelling %s\n", pcFile
);
124 if (fseek( f
, 0 , SEEK_SET
) == -1)
126 fprintf(stderr
, "failure fseeking %s\n", pcFile
);
131 Sequence
<sal_Int8
> seqIn(nLength
);
132 if (fread( seqIn
.getArray(), nLength
, 1 , f
) == 1)
133 r
.set( new OInputStream( seqIn
) );
135 fprintf(stderr
, "failure reading %s\n", pcFile
);
142 class TestDocumentHandler
:
143 public WeakImplHelper
< XExtendedDocumentHandler
, XEntityResolver
, XErrorHandler
>
146 TestDocumentHandler(const char* locale
, const char* outFile
)
150 , of(outFile
, locale
)
154 virtual ~TestDocumentHandler( ) override
161 public: // Error handler
162 virtual void SAL_CALL
error(const Any
& aSAXParseException
) override
165 printf( "Error !\n" );
167 "error from error handler",
168 Reference
< XInterface
>() ,
169 aSAXParseException
);
171 virtual void SAL_CALL
fatalError(const Any
& /*aSAXParseException*/) override
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
200 rootNode
->generateCode(of
);
201 int err
= rootNode
->getError();
204 printf( "Error: in data for %s: %d\n", theLocale
.c_str(), err
);
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");
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();
230 currentNode
.push (l
);
234 virtual void SAL_CALL
endElement(const OUString
& /*aName*/) override
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
255 virtual void SAL_CALL
setDocumentLocator(const Reference
< XLocator
> & /*xLocator*/) override
260 virtual InputSource SAL_CALL
resolveEntity(
261 const OUString
& sPublicId
,
262 const OUString
& sSystemId
) override
265 source
.sSystemId
= sSystemId
;
266 source
.sPublicId
= sPublicId
;
268 source
.aInputStream
= createStreamFromFile(
269 OUStringToOString(sSystemId
, RTL_TEXTENCODING_ASCII_US
).getStr() );
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
294 std::string theLocale
;
300 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
304 printf( "usage : %s <locale> <XML inputfile> <destination file>\n", argv
[0] );
308 Reference
< XComponentContext
> xContext(
309 defaultBootstrap_InitialComponentContext());
313 // read xml from a file and count elements
315 Reference
< XParser
> rParser
= Parser::create(xContext
);
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
329 source
.aInputStream
= createStreamFromFile( argv
[2] );
330 source
.sSystemId
= OUString::createFromAscii( argv
[2] );
333 rParser
->parseStream( source
);
335 nError
= pDocHandler
->nError
;
336 css::uno::Reference
<css::lang::XComponent
>(
337 xContext
, css::uno::UNO_QUERY_THROW
)->dispose();
339 } catch (css::uno::Exception
& e
) {
340 std::cerr
<< "ERROR: " << e
.Message
<< '\n';
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */