Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / i18npool / source / localedata / saxparser.cxx
blob6908c6fefac3551337be0be72cafa693719c1ef4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <stdio.h>
22 #include <string.h>
23 #include <stack>
25 #include "sal/main.h"
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;
59 /************
60 * Sequence of bytes -> InputStream
61 ************/
62 class OInputStream : public WeakImplHelper1 < XInputStream >
64 public:
65 OInputStream( const Sequence< sal_Int8 >&seq ) :
66 nPos( 0 ),
67 m_seq( seq )
70 public:
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 :
76 nBytesToRead;
77 aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
78 nPos += nBytesToRead;
79 return 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)
91 // not implemented
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)
101 // not needed
103 sal_Int32 nPos;
104 Sequence< sal_Int8> m_seq;
107 //-------------------------------
108 // Helper : create an input stream from a file
109 //------------------------------
110 Reference< XInputStream > createStreamFromFile(
111 const char *pcFile )
113 FILE *f = fopen( pcFile , "rb" );
114 Reference< XInputStream > r;
116 if( f ) {
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 ) );
124 else
125 fprintf(stderr, "failure reading %s\n", pcFile);
126 fclose( f );
128 return r;
132 class TestDocumentHandler :
133 public WeakImplHelper3< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
135 public:
136 TestDocumentHandler(const char* locale, const char* outFile )
137 : rootNode(0)
138 , nError(0)
139 , of(outFile, locale)
141 strncpy( theLocale, locale, sizeof(theLocale) );
142 theLocale[sizeof(theLocale)-1] = 0;
145 ~TestDocumentHandler( )
147 of.closeOutput();
148 delete rootNode;
152 public: // Error handler
153 virtual void SAL_CALL error(const Any& aSAXParseException) throw (SAXException, RuntimeException)
155 ++nError;
156 printf( "Error !\n" );
157 throw SAXException(
158 OUString( "error from error handler") ,
159 Reference < XInterface >() ,
160 aSAXParseException );
162 virtual void SAL_CALL fatalError(const Any& /*aSAXParseException*/) throw (SAXException, RuntimeException)
164 ++nError;
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 ;
178 sal_Bool fElement ;
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)
191 if (rootNode)
193 rootNode->generateCode(of);
194 int err = rootNode->getError();
195 if (err)
197 printf( "Error: in data for %s: %d\n", theLocale, err);
198 nError += err;
201 else
203 ++nError;
204 printf( "Error: no data for %s\n", theLocale);
206 printf( "parsing document %s finished\n", theLocale);
208 of.writeAsciiString("} // extern \"C\"\n\n");
209 of.closeOutput();
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();
220 ln->addChild(l);
221 } else {
222 rootNode = l;
224 currentNode.push (l);
228 virtual void SAL_CALL endElement(const OUString& /*aName*/) throw (SAXException,RuntimeException)
230 currentNode.pop();
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)
246 // ignored
249 virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & /*xLocator*/)
250 throw (SAXException,RuntimeException)
252 // ignored
255 virtual InputSource SAL_CALL resolveEntity(
256 const OUString& sPublicId,
257 const OUString& sSystemId)
258 throw (RuntimeException)
260 InputSource source;
261 source.sSystemId = sSystemId;
262 source.sPublicId = sPublicId;
264 source.aInputStream = createStreamFromFile(
265 OUStringToOString(sSystemId, RTL_TEXTENCODING_ASCII_US).getStr() );
267 return source;
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 )
288 public:
289 int nError;
290 sal_Char theLocale[50];
291 OFileWriter of;
298 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
302 if( argc < 6) {
303 printf( "usage : %s <locaLe> <XML inputfile> <destination file> <services.rdb location> <types.rdb location>\n", argv[0] );
304 exit( 1 );
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() );
319 exit(1);
322 //--------------------------------
323 // parser demo
324 // read xml from a file and count elements
325 //--------------------------------
326 Reference< XParser > rParser = Parser::create(comphelper::getComponentContext(xSMgr));
328 int nError = 0;
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
339 InputSource source;
340 source.aInputStream = createStreamFromFile( argv[2] );
341 source.sSystemId = OUString::createFromAscii( argv[2] );
345 // start parsing
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() );
353 exit(1);
355 nError = pDocHandler->nError;
357 return nError;
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */