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 .
27 #include <com/sun/star/lang/XComponent.hpp>
29 #include <com/sun/star/xml/sax/SAXParseException.hpp>
30 #include <com/sun/star/xml/sax/Parser.hpp>
31 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
33 #include <com/sun/star/io/XOutputStream.hpp>
34 #include <com/sun/star/io/XActiveDataSource.hpp>
36 #include <comphelper/processfactory.hxx>
37 #include <cppuhelper/servicefactory.hxx>
38 #include <cppuhelper/implbase1.hxx>
39 #include <cppuhelper/implbase3.hxx>
41 #include <osl/diagnose.h>
43 #include "LocaleNode.hxx"
45 using namespace ::rtl
;
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::registry
;
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 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
)
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
)
86 return readBytes( aData
, nMaxBytesToRead
);
88 virtual void SAL_CALL
skipBytes( sal_Int32
/*nBytesToSkip*/ )
89 throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
)
93 virtual sal_Int32 SAL_CALL
available( )
94 throw(NotConnectedException
, IOException
, RuntimeException
)
96 return m_seq
.getLength() - nPos
;
98 virtual void SAL_CALL
closeInput( )
99 throw(NotConnectedException
, IOException
, RuntimeException
)
104 Sequence
< sal_Int8
> m_seq
;
107 //-------------------------------
108 // Helper : create an input stream from a file
109 //------------------------------
110 Reference
< XInputStream
> createStreamFromFile(
113 FILE *f
= fopen( pcFile
, "rb" );
114 Reference
< XInputStream
> r
;
117 fseek( f
, 0 , SEEK_END
);
118 size_t nLength
= ftell( f
);
119 fseek( f
, 0 , SEEK_SET
);
121 Sequence
<sal_Int8
> seqIn(nLength
);
122 if (fread( seqIn
.getArray() , nLength
, 1 , f
) == 1)
123 r
= Reference
< XInputStream
> ( new OInputStream( seqIn
) );
125 fprintf(stderr
, "failure reading %s\n", pcFile
);
132 class TestDocumentHandler
:
133 public WeakImplHelper3
< XExtendedDocumentHandler
, XEntityResolver
, XErrorHandler
>
136 TestDocumentHandler(const char* locale
, const char* outFile
)
139 , of(outFile
, locale
)
141 strncpy( theLocale
, locale
, sizeof(theLocale
) );
142 theLocale
[sizeof(theLocale
)-1] = 0;
145 ~TestDocumentHandler( )
152 public: // Error handler
153 virtual void SAL_CALL
error(const Any
& aSAXParseException
) throw (SAXException
, RuntimeException
)
156 printf( "Error !\n" );
158 OUString( "error from error handler") ,
159 Reference
< XInterface
>() ,
160 aSAXParseException
);
162 virtual void SAL_CALL
fatalError(const Any
& /*aSAXParseException*/) throw (SAXException
, RuntimeException
)
165 printf( "Fatal Error !\n" );
167 virtual void SAL_CALL
warning(const Any
& /*aSAXParseException*/) throw (SAXException
, RuntimeException
)
169 printf( "Warning !\n" );
173 public: // ExtendedDocumentHandler
177 stack
<LocaleNode
*> currentNode
;
179 LocaleNode
* rootNode
;
181 virtual void SAL_CALL
startDocument(void) throw (SAXException
, RuntimeException
)
183 printf( "parsing document %s started\n", theLocale
);
184 of
.writeAsciiString("#include <sal/types.h>\n\n\n");
185 of
.writeAsciiString("#include <stdio.h> // debug printfs\n\n");
186 of
.writeAsciiString("extern \"C\" {\n\n");
189 virtual void SAL_CALL
endDocument(void) throw (SAXException
, RuntimeException
)
193 rootNode
->generateCode(of
);
194 int err
= rootNode
->getError();
197 printf( "Error: in data for %s: %d\n", theLocale
, err
);
204 printf( "Error: no data for %s\n", theLocale
);
206 printf( "parsing document %s finished\n", theLocale
);
208 of
.writeAsciiString("} // extern \"C\"\n\n");
212 virtual void SAL_CALL
startElement(const OUString
& aName
,
213 const Reference
< XAttributeList
> & xAttribs
)
214 throw (SAXException
,RuntimeException
)
217 LocaleNode
* l
= LocaleNode::createNode (aName
, xAttribs
);
218 if (!currentNode
.empty() ) {
219 LocaleNode
* ln
= (LocaleNode
*) currentNode
.top();
224 currentNode
.push (l
);
228 virtual void SAL_CALL
endElement(const OUString
& /*aName*/) throw (SAXException
,RuntimeException
)
233 virtual void SAL_CALL
characters(const OUString
& aChars
) throw (SAXException
,RuntimeException
)
236 LocaleNode
* l
= currentNode
.top();
237 l
->setValue (aChars
);
240 virtual void SAL_CALL
ignorableWhitespace(const OUString
& /*aWhitespaces*/) throw (SAXException
,RuntimeException
)
244 virtual void SAL_CALL
processingInstruction(const OUString
& /*aTarget*/, const OUString
& /*aData*/) throw (SAXException
,RuntimeException
)
249 virtual void SAL_CALL
setDocumentLocator(const Reference
< XLocator
> & /*xLocator*/)
250 throw (SAXException
,RuntimeException
)
255 virtual InputSource SAL_CALL
resolveEntity(
256 const OUString
& sPublicId
,
257 const OUString
& sSystemId
)
258 throw (RuntimeException
)
261 source
.sSystemId
= sSystemId
;
262 source
.sPublicId
= sPublicId
;
264 source
.aInputStream
= createStreamFromFile(
265 OUStringToOString(sSystemId
, RTL_TEXTENCODING_ASCII_US
).getStr() );
270 virtual void SAL_CALL
startCDATA(void) throw (SAXException
,RuntimeException
)
273 virtual void SAL_CALL
endCDATA(void) throw (RuntimeException
)
276 virtual void SAL_CALL
comment(const OUString
& /*sComment*/) throw (SAXException
,RuntimeException
)
279 virtual void SAL_CALL
unknown(const OUString
& /*sString*/) throw (SAXException
,RuntimeException
)
283 virtual void SAL_CALL
allowLineBreak( void) throw (SAXException
, RuntimeException
)
290 sal_Char theLocale
[50];
298 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
303 printf( "usage : %s <locaLe> <XML inputfile> <destination file> <services.rdb location> <types.rdb location>\n", argv
[0] );
307 // create service manager
308 Reference
< XMultiServiceFactory
> xSMgr
;
311 xSMgr
= createRegistryServiceFactory(
312 ::rtl::OUString::createFromAscii(argv
[4]),
313 ::rtl::OUString::createFromAscii(argv
[5]), true );
315 catch ( const Exception
&e
)
317 printf( "Exception on createRegistryServiceFactory %s\n",
318 OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() );
322 //--------------------------------
324 // read xml from a file and count elements
325 //--------------------------------
326 Reference
< XParser
> rParser
= Parser::create(comphelper::getComponentContext(xSMgr
));
329 // create and connect the document handler to the parser
330 TestDocumentHandler
*pDocHandler
= new TestDocumentHandler( argv
[1], argv
[3]);
332 Reference
< XDocumentHandler
> rDocHandler( (XDocumentHandler
*) pDocHandler
);
333 Reference
< XEntityResolver
> rEntityResolver( (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] );
346 rParser
->parseStream( source
);
349 catch( const Exception
& e
)
351 OString o1
= OUStringToOString(e
.Message
, RTL_TEXTENCODING_UTF8
);
352 printf( "Exception during parsing : %s\n" , o1
.getStr() );
355 nError
= pDocHandler
->nError
;
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */