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 ::std
;
48 using namespace ::cppu
;
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::lang
;
51 using namespace ::com::sun::star::xml::sax
;
52 using namespace ::com::sun::star::io
;
60 * Sequence of bytes -> InputStream
62 class OInputStream
: public WeakImplHelper1
< XInputStream
>
65 explicit OInputStream( const Sequence
< sal_Int8
>&seq
)
71 virtual sal_Int32 SAL_CALL
readBytes( Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
72 throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
) SAL_OVERRIDE
74 nBytesToRead
= (nBytesToRead
> m_seq
.getLength() - nPos
) ?
75 m_seq
.getLength() - nPos
:
77 aData
= Sequence
< sal_Int8
> ( &(m_seq
.getConstArray()[nPos
]) , nBytesToRead
);
81 virtual sal_Int32 SAL_CALL
readSomeBytes(
82 ::com::sun::star::uno::Sequence
< sal_Int8
>& aData
,
83 sal_Int32 nMaxBytesToRead
)
84 throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
) SAL_OVERRIDE
86 return readBytes( aData
, nMaxBytesToRead
);
88 virtual void SAL_CALL
skipBytes( sal_Int32
/*nBytesToSkip*/ )
89 throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
) SAL_OVERRIDE
93 virtual sal_Int32 SAL_CALL
available( )
94 throw(NotConnectedException
, IOException
, RuntimeException
, std::exception
) SAL_OVERRIDE
96 return m_seq
.getLength() - nPos
;
98 virtual void SAL_CALL
closeInput( )
99 throw(NotConnectedException
, IOException
, RuntimeException
, std::exception
) SAL_OVERRIDE
104 Sequence
< sal_Int8
> m_seq
;
108 // Helper : create an input stream from a file
110 Reference
< XInputStream
> createStreamFromFile(
113 Reference
< XInputStream
> r
;
115 FILE *f
= fopen( pcFile
, "rb" );
119 fprintf(stderr
, "failure opening %s\n", pcFile
);
123 if (fseek( f
, 0 , SEEK_END
) == -1)
125 fprintf(stderr
, "failure fseeking %s\n", pcFile
);
130 long nLength
= ftell( f
);
133 fprintf(stderr
, "failure ftelling %s\n", pcFile
);
138 if (fseek( f
, 0 , SEEK_SET
) == -1)
140 fprintf(stderr
, "failure fseeking %s\n", pcFile
);
145 Sequence
<sal_Int8
> seqIn(nLength
);
146 if (fread( seqIn
.getArray(), nLength
, 1 , f
) == 1)
147 r
= Reference
< XInputStream
> ( new OInputStream( seqIn
) );
149 fprintf(stderr
, "failure reading %s\n", pcFile
);
155 class TestDocumentHandler
:
156 public WeakImplHelper3
< XExtendedDocumentHandler
, XEntityResolver
, XErrorHandler
>
159 TestDocumentHandler(const char* locale
, const char* outFile
)
162 , of(outFile
, locale
)
164 strncpy( theLocale
, locale
, sizeof(theLocale
) );
165 theLocale
[sizeof(theLocale
)-1] = 0;
168 virtual ~TestDocumentHandler( )
175 public: // Error handler
176 virtual void SAL_CALL
error(const Any
& aSAXParseException
) throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
179 printf( "Error !\n" );
181 "error from error handler",
182 Reference
< XInterface
>() ,
183 aSAXParseException
);
185 virtual void SAL_CALL
fatalError(const Any
& /*aSAXParseException*/) throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
188 printf( "Fatal Error !\n" );
190 virtual void SAL_CALL
warning(const Any
& /*aSAXParseException*/) throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
192 printf( "Warning !\n" );
196 public: // ExtendedDocumentHandler
200 stack
<LocaleNode
*> currentNode
;
201 LocaleNode
* rootNode
;
203 virtual void SAL_CALL
startDocument() throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
205 printf( "parsing document %s started\n", theLocale
);
206 of
.writeAsciiString("#include <sal/types.h>\n\n\n");
207 of
.writeAsciiString("#include <stdio.h>\n\n");
208 of
.writeAsciiString("extern \"C\" {\n\n");
211 virtual void SAL_CALL
endDocument() throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
215 rootNode
->generateCode(of
);
216 int err
= rootNode
->getError();
219 printf( "Error: in data for %s: %d\n", theLocale
, err
);
226 printf( "Error: no data for %s\n", theLocale
);
228 printf( "parsing document %s finished\n", theLocale
);
230 of
.writeAsciiString("} // extern \"C\"\n\n");
234 virtual void SAL_CALL
startElement(const OUString
& aName
,
235 const Reference
< XAttributeList
> & xAttribs
)
236 throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
239 LocaleNode
* l
= LocaleNode::createNode (aName
, xAttribs
);
240 if (!currentNode
.empty() ) {
241 LocaleNode
* ln
= currentNode
.top();
246 currentNode
.push (l
);
250 virtual void SAL_CALL
endElement(const OUString
& /*aName*/) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
255 virtual void SAL_CALL
characters(const OUString
& aChars
) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
258 LocaleNode
* l
= currentNode
.top();
259 l
->setValue (aChars
);
262 virtual void SAL_CALL
ignorableWhitespace(const OUString
& /*aWhitespaces*/) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
266 virtual void SAL_CALL
processingInstruction(const OUString
& /*aTarget*/, const OUString
& /*aData*/) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
271 virtual void SAL_CALL
setDocumentLocator(const Reference
< XLocator
> & /*xLocator*/)
272 throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
277 virtual InputSource SAL_CALL
resolveEntity(
278 const OUString
& sPublicId
,
279 const OUString
& sSystemId
)
280 throw (RuntimeException
, std::exception
) SAL_OVERRIDE
283 source
.sSystemId
= sSystemId
;
284 source
.sPublicId
= sPublicId
;
286 source
.aInputStream
= createStreamFromFile(
287 OUStringToOString(sSystemId
, RTL_TEXTENCODING_ASCII_US
).getStr() );
292 virtual void SAL_CALL
startCDATA() throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
295 virtual void SAL_CALL
endCDATA() throw (RuntimeException
, std::exception
) SAL_OVERRIDE
298 virtual void SAL_CALL
comment(const OUString
& /*sComment*/) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
301 virtual void SAL_CALL
unknown(const OUString
& /*sString*/) throw (SAXException
,RuntimeException
, std::exception
) SAL_OVERRIDE
305 virtual void SAL_CALL
allowLineBreak() throw (SAXException
, RuntimeException
, std::exception
) SAL_OVERRIDE
312 sal_Char theLocale
[50];
320 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
324 printf( "usage : %s <locaLe> <XML inputfile> <destination file>\n", argv
[0] );
328 Reference
< XComponentContext
> xContext(
329 defaultBootstrap_InitialComponentContext());
333 // read xml from a file and count elements
335 Reference
< XParser
> rParser
= Parser::create(xContext
);
338 // create and connect the document handler to the parser
339 TestDocumentHandler
*pDocHandler
= new TestDocumentHandler( argv
[1], argv
[3]);
341 Reference
< XDocumentHandler
> rDocHandler( (XDocumentHandler
*) pDocHandler
);
342 Reference
< XEntityResolver
> rEntityResolver( (XEntityResolver
*) pDocHandler
);
344 rParser
->setDocumentHandler( rDocHandler
);
345 rParser
->setEntityResolver( rEntityResolver
);
347 // create the input stream
349 source
.aInputStream
= createStreamFromFile( argv
[2] );
350 source
.sSystemId
= OUString::createFromAscii( argv
[2] );
353 rParser
->parseStream( source
);
355 nError
= pDocHandler
->nError
;
356 css::uno::Reference
<css::lang::XComponent
>(
357 xContext
, css::uno::UNO_QUERY_THROW
)->dispose();
359 } catch (css::uno::Exception
& e
) {
360 std::cerr
<< "ERROR: " << e
.Message
<< '\n';
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */