xref: /trunk/main/i18npool/source/localedata/saxparser.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_i18npool.hxx"
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <stack>
30 
31 #include "sal/main.h"
32 
33 #include <com/sun/star/lang/XComponent.hpp>
34 
35 #include <com/sun/star/xml/sax/SAXParseException.hpp>
36 #include <com/sun/star/xml/sax/XParser.hpp>
37 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
38 
39 #include <com/sun/star/io/XOutputStream.hpp>
40 #include <com/sun/star/io/XActiveDataSource.hpp>
41 
42 #include <cppuhelper/servicefactory.hxx>
43 #include <cppuhelper/implbase1.hxx>
44 #include <cppuhelper/implbase3.hxx>
45 
46 #include <vos/diagnose.hxx>
47 
48 #include "LocaleNode.hxx"
49 
50 using namespace ::rtl;
51 using namespace ::std;
52 using namespace ::cppu;
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::lang;
55 using namespace ::com::sun::star::registry;
56 using namespace ::com::sun::star::xml::sax;
57 using namespace ::com::sun::star::io;
58 
59 
60 
61 
62 
63 
64 /************
65  * Sequence of bytes -> InputStream
66  ************/
67 class OInputStream : public WeakImplHelper1 < XInputStream >
68 {
69 public:
OInputStream(const Sequence<sal_Int8> & seq)70     OInputStream( const Sequence< sal_Int8 >&seq ) :
71         nPos( 0 ),
72         m_seq( seq )
73         {}
74 
75 public:
readBytes(Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)76     virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
77         {
78             nBytesToRead = (nBytesToRead > m_seq.getLength() - nPos ) ?
79                 m_seq.getLength() - nPos :
80                 nBytesToRead;
81             aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
82             nPos += nBytesToRead;
83             return nBytesToRead;
84         }
readSomeBytes(::com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)85     virtual sal_Int32 SAL_CALL readSomeBytes(
86         ::com::sun::star::uno::Sequence< sal_Int8 >& aData,
87         sal_Int32 nMaxBytesToRead )
88         {
89             return readBytes( aData, nMaxBytesToRead );
90         }
skipBytes(sal_Int32)91     virtual void SAL_CALL skipBytes( sal_Int32 /*nBytesToSkip*/ )
92         {
93             // not implemented
94         }
available()95     virtual sal_Int32 SAL_CALL available(  )
96         {
97             return m_seq.getLength() - nPos;
98         }
closeInput()99     virtual void SAL_CALL closeInput(  )
100         {
101             // not needed
102         }
103     sal_Int32 nPos;
104     Sequence< sal_Int8> m_seq;
105 };
106 
107 //-------------------------------
108 // Helper : create an input stream from a file
109 //------------------------------
createStreamFromFile(const char * pcFile)110 Reference< XInputStream > createStreamFromFile(
111     const char *pcFile )
112 {
113     FILE *f = fopen( pcFile , "rb" );
114     Reference<  XInputStream >  r;
115 
116     if( f ) {
117         fseek( f , 0 , SEEK_END );
118         size_t nLength = ftell( f );
119         fseek( f , 0 , SEEK_SET );
120 
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 );
127     }
128     return r;
129 }
130 
131 
132 class TestDocumentHandler :
133     public WeakImplHelper3< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
134 {
135 public:
TestDocumentHandler(const char * locale,const char * outFile)136     TestDocumentHandler(const char* locale, const char* outFile ) :
137       rootNode(0), nError(0), nbOfCurrencies(0), nbOfCalendars(0), nbOfFormatElements(0),
138       nbOfTransliterations(0), nbOfCollations(0), nbOfDays(50), nbOfMonths(50), nbOfEras(10),
139       flag(-1), of(outFile, locale), isStartDayOfWeek(false), foundDefaultName(false),
140       foundVariant(false), openElement(false)
141     {
142         strncpy( theLocale, locale, sizeof(theLocale) );
143         theLocale[sizeof(theLocale)-1] = 0;
144     }
145 
~TestDocumentHandler()146     ~TestDocumentHandler(  )
147     {
148         of.closeOutput();
149     }
150 
151 
152 public: // Error handler
error(const Any & aSAXParseException)153     virtual void SAL_CALL error(const Any& aSAXParseException)
154     {
155         ++nError;
156         printf( "Error !\n" );
157         throw  SAXException(
158             OUString( RTL_CONSTASCII_USTRINGPARAM("error from error handler")) ,
159             Reference < XInterface >() ,
160             aSAXParseException );
161     }
fatalError(const Any &)162     virtual void SAL_CALL fatalError(const Any& /*aSAXParseException*/)
163     {
164         ++nError;
165         printf( "Fatal Error !\n" );
166     }
warning(const Any &)167     virtual void SAL_CALL warning(const Any& /*aSAXParseException*/)
168     {
169         printf( "Warning !\n" );
170     }
171 
172 
173 public: // ExtendedDocumentHandler
174 
175 
176 
177     stack<LocaleNode *> currentNode ;
178     sal_Bool  fElement ;
179     LocaleNode * rootNode;
180 
startDocument(void)181     virtual void SAL_CALL startDocument(void)
182     {
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");
187     }
188 
endDocument(void)189     virtual void SAL_CALL endDocument(void)
190     {
191         if (rootNode)
192         {
193             rootNode->generateCode(of);
194             int err = rootNode->getError();
195             if (err)
196             {
197                 printf( "Error: in data for %s: %d\n", theLocale, err);
198                 nError += err;
199             }
200         }
201         else
202         {
203             ++nError;
204             printf( "Error: no data for %s\n", theLocale);
205         }
206         printf( "parsing document %s finished\n", theLocale);
207 
208         of.writeAsciiString("} // extern \"C\"\n\n");
209         of.closeOutput();
210     }
211 
startElement(const OUString & aName,const Reference<XAttributeList> & xAttribs)212     virtual void SAL_CALL startElement(const OUString& aName,
213                               const Reference< XAttributeList > & xAttribs)
214     {
215 
216         LocaleNode * l =  LocaleNode::createNode (aName, xAttribs);
217         if (!currentNode.empty() ) {
218             LocaleNode * ln = (LocaleNode *) currentNode . top();
219             ln->addChild(l);
220         } else {
221             rootNode = l;
222         }
223         currentNode . push (l);
224     }
225 
226 
endElement(const OUString &)227     virtual void SAL_CALL endElement(const OUString& /*aName*/)
228     {
229         currentNode . pop();
230     }
231 
characters(const OUString & aChars)232     virtual void SAL_CALL characters(const OUString& aChars)
233     {
234 
235         LocaleNode * l = currentNode . top();
236         l->setValue (aChars);
237         ::rtl::OUString str(aChars);
238         sal_Unicode nonBreakSPace[2]= {0xa, 0x0};
239         if(!openElement || str.equals(nonBreakSPace))
240           return;
241     }
242 
ignorableWhitespace(const OUString &)243     virtual void SAL_CALL ignorableWhitespace(const OUString& /*aWhitespaces*/)
244     {
245     }
246 
processingInstruction(const OUString &,const OUString &)247     virtual void SAL_CALL processingInstruction(const OUString& /*aTarget*/, const OUString& /*aData*/)
248     {
249         // ignored
250     }
251 
setDocumentLocator(const Reference<XLocator> &)252     virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & /*xLocator*/)
253     {
254         // ignored
255     }
256 
resolveEntity(const OUString & sPublicId,const OUString & sSystemId)257     virtual InputSource SAL_CALL resolveEntity(
258         const OUString& sPublicId,
259         const OUString& sSystemId)
260     {
261         InputSource source;
262         source.sSystemId = sSystemId;
263         source.sPublicId = sPublicId;
264 
265         source.aInputStream = createStreamFromFile(
266             OUStringToOString( sSystemId.getStr(), RTL_TEXTENCODING_ASCII_US).getStr() );
267 
268         return source;
269     }
270 
startCDATA(void)271     virtual void SAL_CALL startCDATA(void)
272     {
273     }
endCDATA(void)274     virtual void SAL_CALL endCDATA(void)
275     {
276     }
comment(const OUString &)277     virtual void SAL_CALL comment(const OUString& /*sComment*/)
278     {
279     }
unknown(const OUString &)280     virtual void SAL_CALL unknown(const OUString& /*sString*/)
281     {
282     }
283 
allowLineBreak(void)284     virtual void SAL_CALL allowLineBreak( void)
285     {
286 
287     }
288 
289 public:
290     int nError;
291     ::rtl::OUString currentElement;
292     sal_Int16 nbOfCurrencies;
293     sal_Int16 nbOfCalendars;
294     sal_Int16 nbOfFormatElements;
295     sal_Int16 nbOfTransliterations;
296     sal_Int16 nbOfCollations;
297     Sequence<sal_Int16> nbOfDays;
298     Sequence<sal_Int16> nbOfMonths;
299     Sequence<sal_Int16> nbOfEras;
300     sal_Char *elementTag;
301     sal_Char theLocale[50];
302     sal_Int16 flag;
303     OFileWriter of;
304     sal_Bool isStartDayOfWeek;
305     sal_Bool foundDefaultName;
306     sal_Bool foundVariant;
307         sal_Bool openElement;
308 };
309 
310 
311 
312 
313 
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,argv)314 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
315 {
316     if( argc < 6) {
317         printf( "usage : %s <locaLe> <XML inputfile> <destination file> <services.rdb location> <types.rdb location>\n", argv[0] );
318         exit( 1 );
319     }
320 
321     // create service manager
322     Reference< XMultiServiceFactory > xSMgr;
323     try
324     {
325         xSMgr = createRegistryServiceFactory(
326             ::rtl::OUString::createFromAscii(argv[4]),
327             ::rtl::OUString::createFromAscii(argv[5]), true );
328     }
329     catch( const Exception& e)
330     {
331         const OString aMsg = OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8 );
332         printf( "Exception on createRegistryServiceFactory: \"%s\"\n", aMsg.getStr() );
333         exit(1);
334     }
335 
336     //--------------------------------
337     // parser demo
338     // read xml from a file and count elements
339     //--------------------------------
340     Reference< XInterface > x = xSMgr->createInstance(
341         OUString::createFromAscii( "com.sun.star.xml.sax.Parser" ) );
342     int nError = 0;
343     if( x.is() )
344     {
345         Reference< XParser > rParser( x , UNO_QUERY );
346 
347         // create and connect the document handler to the parser
348         TestDocumentHandler *pDocHandler = new TestDocumentHandler( argv[1], argv[3]);
349 
350         Reference < XDocumentHandler >  rDocHandler( (XDocumentHandler *) pDocHandler );
351         Reference< XEntityResolver > rEntityResolver( (XEntityResolver *) pDocHandler );
352 
353         rParser->setDocumentHandler( rDocHandler );
354         rParser->setEntityResolver( rEntityResolver );
355 
356         // create the input stream
357         InputSource source;
358         source.aInputStream = createStreamFromFile( argv[2] );
359         source.sSystemId    = OUString::createFromAscii( argv[2] );
360 
361         try
362         {
363             // start parsing
364             rParser->parseStream( source );
365         }
366 
367         catch( const Exception& e)
368         {
369             const OString aMsg = OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8 );
370             printf( "Exception during parsing : \"%s\"\n",  aMsg.getStr() );
371             exit(1);
372         }
373         nError = pDocHandler->nError;
374     }
375     else
376     {
377         printf( "couldn't create sax-parser component\n" );
378         exit(1);
379     }
380 
381     return nError;
382 }
383