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/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/implbase1.hxx>
41 #include <cppuhelper/implbase3.hxx>
43 #include <osl/diagnose.h>
45 #include "LocaleNode.hxx"
47 using namespace ::rtl
;
48 using namespace ::std
;
49 using namespace ::cppu
;
50 using namespace ::com::sun::star::uno
;
51 using namespace ::com::sun::star::lang
;
52 using namespace ::com::sun::star::xml::sax
;
53 using namespace ::com::sun::star::io
;
61 * Sequence of bytes -> InputStream
63 class OInputStream
: public WeakImplHelper1
< XInputStream
>
66 OInputStream( const Sequence
< sal_Int8
>&seq
) :
72 virtual sal_Int32 SAL_CALL
readBytes( Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
73 throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
) SAL_OVERRIDE
75 nBytesToRead
= (nBytesToRead
> m_seq
.getLength() - nPos
) ?
76 m_seq
.getLength() - nPos
:
78 aData
= Sequence
< sal_Int8
> ( &(m_seq
.getConstArray()[nPos
]) , nBytesToRead
);
82 virtual sal_Int32 SAL_CALL
readSomeBytes(
83 ::com::sun::star::uno::Sequence
< sal_Int8
>& aData
,
84 sal_Int32 nMaxBytesToRead
)
85 throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
) SAL_OVERRIDE
87 return readBytes( aData
, nMaxBytesToRead
);
89 virtual void SAL_CALL
skipBytes( sal_Int32
/*nBytesToSkip*/ )
90 throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
) SAL_OVERRIDE
94 virtual sal_Int32 SAL_CALL
available( )
95 throw(NotConnectedException
, IOException
, RuntimeException
, std::exception
) SAL_OVERRIDE
97 return m_seq
.getLength() - nPos
;
99 virtual void SAL_CALL
closeInput( )
100 throw(NotConnectedException
, IOException
, RuntimeException
, std::exception
) SAL_OVERRIDE
105 Sequence
< sal_Int8
> m_seq
;
109 // Helper : create an input stream from a file
111 Reference
< XInputStream
> createStreamFromFile(
114 Reference
< XInputStream
> r
;
116 FILE *f
= fopen( pcFile
, "rb" );
120 fprintf(stderr
, "failure opening %s\n", pcFile
);
124 if (fseek( f
, 0 , SEEK_END
) == -1)
126 fprintf(stderr
, "failure fseeking %s\n", pcFile
);
131 long nLength
= ftell( f
);
134 fprintf(stderr
, "failure ftelling %s\n", pcFile
);
139 if (fseek( f
, 0 , SEEK_SET
) == -1)
141 fprintf(stderr
, "failure fseeking %s\n", pcFile
);
146 Sequence
<sal_Int8
> seqIn(nLength
);
147 if (fread( seqIn
.getArray(), nLength
, 1 , f
) == 1)
148 r
= Reference
< XInputStream
> ( new OInputStream( seqIn
) );
150 fprintf(stderr
, "failure reading %s\n", pcFile
);
156 class TestDocumentHandler
:
157 public WeakImplHelper3
< XExtendedDocumentHandler
, XEntityResolver
, XErrorHandler
>
160 TestDocumentHandler(const char* locale
, const char* outFile
)
163 , of(outFile
, locale
)
165 strncpy( theLocale
, locale
, sizeof(theLocale
) );
166 theLocale
[sizeof(theLocale
)-1] = 0;
169 virtual ~TestDocumentHandler( )
176 public: // Error handler
177 virtual void SAL_CALL
error(const Any
& aSAXParseException
) throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
180 printf( "Error !\n" );
182 OUString( "error from error handler") ,
183 Reference
< XInterface
>() ,
184 aSAXParseException
);
186 virtual void SAL_CALL
fatalError(const Any
& /*aSAXParseException*/) throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
189 printf( "Fatal Error !\n" );
191 virtual void SAL_CALL
warning(const Any
& /*aSAXParseException*/) throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
193 printf( "Warning !\n" );
197 public: // ExtendedDocumentHandler
201 stack
<LocaleNode
*> currentNode
;
202 LocaleNode
* rootNode
;
204 virtual void SAL_CALL
startDocument(void) throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
206 printf( "parsing document %s started\n", theLocale
);
207 of
.writeAsciiString("#include <sal/types.h>\n\n\n");
208 of
.writeAsciiString("#include <stdio.h>\n\n");
209 of
.writeAsciiString("extern \"C\" {\n\n");
212 virtual void SAL_CALL
endDocument(void) throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
216 rootNode
->generateCode(of
);
217 int err
= rootNode
->getError();
220 printf( "Error: in data for %s: %d\n", theLocale
, err
);
227 printf( "Error: no data for %s\n", theLocale
);
229 printf( "parsing document %s finished\n", theLocale
);
231 of
.writeAsciiString("} // extern \"C\"\n\n");
235 virtual void SAL_CALL
startElement(const OUString
& aName
,
236 const Reference
< XAttributeList
> & xAttribs
)
237 throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
240 LocaleNode
* l
= LocaleNode::createNode (aName
, xAttribs
);
241 if (!currentNode
.empty() ) {
242 LocaleNode
* ln
= (LocaleNode
*) currentNode
.top();
247 currentNode
.push (l
);
251 virtual void SAL_CALL
endElement(const OUString
& /*aName*/) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
256 virtual void SAL_CALL
characters(const OUString
& aChars
) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
259 LocaleNode
* l
= currentNode
.top();
260 l
->setValue (aChars
);
263 virtual void SAL_CALL
ignorableWhitespace(const OUString
& /*aWhitespaces*/) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
267 virtual void SAL_CALL
processingInstruction(const OUString
& /*aTarget*/, const OUString
& /*aData*/) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
272 virtual void SAL_CALL
setDocumentLocator(const Reference
< XLocator
> & /*xLocator*/)
273 throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
278 virtual InputSource SAL_CALL
resolveEntity(
279 const OUString
& sPublicId
,
280 const OUString
& sSystemId
)
281 throw (RuntimeException
, std::exception
) SAL_OVERRIDE
284 source
.sSystemId
= sSystemId
;
285 source
.sPublicId
= sPublicId
;
287 source
.aInputStream
= createStreamFromFile(
288 OUStringToOString(sSystemId
, RTL_TEXTENCODING_ASCII_US
).getStr() );
293 virtual void SAL_CALL
startCDATA(void) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
296 virtual void SAL_CALL
endCDATA(void) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
299 virtual void SAL_CALL
comment(const OUString
& /*sComment*/) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
302 virtual void SAL_CALL
unknown(const OUString
& /*sString*/) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
306 virtual void SAL_CALL
allowLineBreak( void) throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
313 sal_Char theLocale
[50];
321 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
325 printf( "usage : %s <locaLe> <XML inputfile> <destination file>\n", argv
[0] );
329 Reference
< XComponentContext
> xContext(
330 defaultBootstrap_InitialComponentContext());
334 // read xml from a file and count elements
336 Reference
< XParser
> rParser
= Parser::create(xContext
);
339 // create and connect the document handler to the parser
340 TestDocumentHandler
*pDocHandler
= new TestDocumentHandler( argv
[1], argv
[3]);
342 Reference
< XDocumentHandler
> rDocHandler( (XDocumentHandler
*) pDocHandler
);
343 Reference
< XEntityResolver
> rEntityResolver( (XEntityResolver
*) pDocHandler
);
345 rParser
->setDocumentHandler( rDocHandler
);
346 rParser
->setEntityResolver( rEntityResolver
);
348 // create the input stream
350 source
.aInputStream
= createStreamFromFile( argv
[2] );
351 source
.sSystemId
= OUString::createFromAscii( argv
[2] );
354 rParser
->parseStream( source
);
356 nError
= pDocHandler
->nError
;
359 } catch (css::uno::Exception
& e
) {
360 std::cerr
<< "ERROR: " << e
.Message
<< '\n';
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */