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/implbase.hxx>
42 #include <osl/diagnose.h>
44 #include "LocaleNode.hxx"
46 using namespace ::std
;
47 using namespace ::cppu
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::lang
;
50 using namespace ::com::sun::star::xml::sax
;
51 using namespace ::com::sun::star::io
;
55 * Sequence of bytes -> InputStream
57 class OInputStream
: public WeakImplHelper
< XInputStream
>
60 explicit OInputStream( const Sequence
< sal_Int8
>&seq
)
66 virtual sal_Int32 SAL_CALL
readBytes( Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
67 throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
) override
69 nBytesToRead
= (nBytesToRead
> m_seq
.getLength() - nPos
) ?
70 m_seq
.getLength() - nPos
:
72 aData
= Sequence
< sal_Int8
> ( &(m_seq
.getConstArray()[nPos
]) , nBytesToRead
);
76 virtual sal_Int32 SAL_CALL
readSomeBytes(
77 css::uno::Sequence
< sal_Int8
>& aData
,
78 sal_Int32 nMaxBytesToRead
)
79 throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
) override
81 return readBytes( aData
, nMaxBytesToRead
);
83 virtual void SAL_CALL
skipBytes( sal_Int32
/*nBytesToSkip*/ )
84 throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
) override
88 virtual sal_Int32 SAL_CALL
available( )
89 throw(NotConnectedException
, IOException
, RuntimeException
, std::exception
) override
91 return m_seq
.getLength() - nPos
;
93 virtual void SAL_CALL
closeInput( )
94 throw(NotConnectedException
, IOException
, RuntimeException
, std::exception
) override
99 Sequence
< sal_Int8
> m_seq
;
103 // Helper : create an input stream from a file
105 Reference
< XInputStream
> createStreamFromFile(
108 Reference
< XInputStream
> r
;
110 FILE *f
= fopen( pcFile
, "rb" );
114 fprintf(stderr
, "failure opening %s\n", pcFile
);
118 if (fseek( f
, 0 , SEEK_END
) == -1)
120 fprintf(stderr
, "failure fseeking %s\n", pcFile
);
125 long nLength
= ftell( f
);
128 fprintf(stderr
, "failure ftelling %s\n", pcFile
);
133 if (fseek( f
, 0 , SEEK_SET
) == -1)
135 fprintf(stderr
, "failure fseeking %s\n", pcFile
);
140 Sequence
<sal_Int8
> seqIn(nLength
);
141 if (fread( seqIn
.getArray(), nLength
, 1 , f
) == 1)
142 r
.set( new OInputStream( seqIn
) );
144 fprintf(stderr
, "failure reading %s\n", pcFile
);
150 class TestDocumentHandler
:
151 public WeakImplHelper
< XExtendedDocumentHandler
, XEntityResolver
, XErrorHandler
>
154 TestDocumentHandler(const char* locale
, const char* outFile
)
157 , of(outFile
, locale
)
159 strncpy( theLocale
, locale
, sizeof(theLocale
) );
160 theLocale
[sizeof(theLocale
)-1] = 0;
163 virtual ~TestDocumentHandler( )
170 public: // Error handler
171 virtual void SAL_CALL
error(const Any
& aSAXParseException
) throw (SAXException
, RuntimeException
, std::exception
) override
174 printf( "Error !\n" );
176 "error from error handler",
177 Reference
< XInterface
>() ,
178 aSAXParseException
);
180 virtual void SAL_CALL
fatalError(const Any
& /*aSAXParseException*/) throw (SAXException
, RuntimeException
, std::exception
) override
183 printf( "Fatal Error !\n" );
185 virtual void SAL_CALL
warning(const Any
& /*aSAXParseException*/) throw (SAXException
, RuntimeException
, std::exception
) override
187 printf( "Warning !\n" );
191 public: // ExtendedDocumentHandler
194 stack
<LocaleNode
*> currentNode
;
195 LocaleNode
* rootNode
;
197 virtual void SAL_CALL
startDocument() throw (SAXException
, RuntimeException
, std::exception
) override
199 printf( "parsing document %s started\n", theLocale
);
200 of
.writeAsciiString("#include <sal/types.h>\n\n\n");
201 of
.writeAsciiString("#include <stdio.h>\n\n");
202 of
.writeAsciiString("extern \"C\" {\n\n");
205 virtual void SAL_CALL
endDocument() throw (SAXException
, RuntimeException
, std::exception
) override
209 rootNode
->generateCode(of
);
210 int err
= rootNode
->getError();
213 printf( "Error: in data for %s: %d\n", theLocale
, err
);
220 printf( "Error: no data for %s\n", theLocale
);
222 printf( "parsing document %s finished\n", theLocale
);
224 of
.writeAsciiString("} // extern \"C\"\n\n");
228 virtual void SAL_CALL
startElement(const OUString
& aName
,
229 const Reference
< XAttributeList
> & xAttribs
)
230 throw (SAXException
,RuntimeException
, std::exception
) override
233 LocaleNode
* l
= LocaleNode::createNode (aName
, xAttribs
);
234 if (!currentNode
.empty() ) {
235 LocaleNode
* ln
= currentNode
.top();
240 currentNode
.push (l
);
244 virtual void SAL_CALL
endElement(const OUString
& /*aName*/) throw (SAXException
,RuntimeException
, std::exception
) override
249 virtual void SAL_CALL
characters(const OUString
& aChars
) throw (SAXException
,RuntimeException
, std::exception
) override
252 LocaleNode
* l
= currentNode
.top();
253 l
->setValue (aChars
);
256 virtual void SAL_CALL
ignorableWhitespace(const OUString
& /*aWhitespaces*/) throw (SAXException
,RuntimeException
, std::exception
) override
260 virtual void SAL_CALL
processingInstruction(const OUString
& /*aTarget*/, const OUString
& /*aData*/) throw (SAXException
,RuntimeException
, std::exception
) override
265 virtual void SAL_CALL
setDocumentLocator(const Reference
< XLocator
> & /*xLocator*/)
266 throw (SAXException
,RuntimeException
, std::exception
) override
271 virtual InputSource SAL_CALL
resolveEntity(
272 const OUString
& sPublicId
,
273 const OUString
& sSystemId
)
274 throw (RuntimeException
, std::exception
) override
277 source
.sSystemId
= sSystemId
;
278 source
.sPublicId
= sPublicId
;
280 source
.aInputStream
= createStreamFromFile(
281 OUStringToOString(sSystemId
, RTL_TEXTENCODING_ASCII_US
).getStr() );
286 virtual void SAL_CALL
startCDATA() throw (SAXException
,RuntimeException
, std::exception
) override
289 virtual void SAL_CALL
endCDATA() throw (RuntimeException
, std::exception
) override
292 virtual void SAL_CALL
comment(const OUString
& /*sComment*/) throw (SAXException
,RuntimeException
, std::exception
) override
295 virtual void SAL_CALL
unknown(const OUString
& /*sString*/) throw (SAXException
,RuntimeException
, std::exception
) override
299 virtual void SAL_CALL
allowLineBreak() throw (SAXException
, RuntimeException
, std::exception
) override
306 sal_Char theLocale
[50];
311 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
315 printf( "usage : %s <locale> <XML inputfile> <destination file>\n", argv
[0] );
319 Reference
< XComponentContext
> xContext(
320 defaultBootstrap_InitialComponentContext());
324 // read xml from a file and count elements
326 Reference
< XParser
> rParser
= Parser::create(xContext
);
329 // create and connect the document handler to the parser
330 TestDocumentHandler
*pDocHandler
= new TestDocumentHandler( argv
[1], argv
[3]);
332 Reference
< XDocumentHandler
> rDocHandler( static_cast<XDocumentHandler
*>(pDocHandler
) );
333 Reference
< XEntityResolver
> rEntityResolver( static_cast<XEntityResolver
*>(pDocHandler
) );
335 rParser
->setDocumentHandler( rDocHandler
);
336 rParser
->setEntityResolver( rEntityResolver
);
338 // create the input stream
340 source
.aInputStream
= createStreamFromFile( argv
[2] );
341 source
.sSystemId
= OUString::createFromAscii( argv
[2] );
344 rParser
->parseStream( source
);
346 nError
= pDocHandler
->nError
;
347 css::uno::Reference
<css::lang::XComponent
>(
348 xContext
, css::uno::UNO_QUERY_THROW
)->dispose();
350 } catch (css::uno::Exception
& e
) {
351 std::cerr
<< "ERROR: " << e
.Message
<< '\n';
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */