merged tag LIBREOFFICE_3_2_99_3
[LibreOffice.git] / scripting / source / storage / ScriptMetadataImporter.cxx
blobf46d87b7ceae77dbe46cf4b13b2b652bce66fc7c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_scripting.hxx"
32 #ifdef _DEBUG
33 #include <stdio.h>
34 #endif
36 #include "ScriptMetadataImporter.hxx"
38 #include <osl/mutex.hxx>
40 #include <com/sun/star/xml/sax/XParser.hpp>
41 #include <rtl/string.h>
44 #include <util/util.hxx>
46 using namespace ::rtl;
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
50 namespace scripting_impl
52 //*************************************************************************
53 ScriptMetadataImporter::ScriptMetadataImporter(
54 const Reference< XComponentContext > & xContext )
55 : m_xContext( xContext )
57 OSL_TRACE( "< ScriptMetadataImporter ctor called >\n" );
58 ms_localeDesc = new OUStringBuffer();
61 //*************************************************************************
62 ScriptMetadataImporter::~ScriptMetadataImporter() SAL_THROW( () )
64 OSL_TRACE( "< ScriptMetadataImporter dtor called >\n" );
65 delete ms_localeDesc;
69 //*************************************************************************
70 void ScriptMetadataImporter::parseMetaData(
71 Reference< io::XInputStream > const & xInput,
72 const ::rtl::OUString & parcelURI,
73 InfoImpls_vec & io_ScriptDatas )
74 throw ( xml::sax::SAXException, io::IOException, RuntimeException )
77 ::osl::Guard< ::osl::Mutex > aGuard( m_mutex );
79 mpv_ScriptDatas = &io_ScriptDatas;
80 mpv_ScriptDatas->clear();
82 //Set the placeholder for the parcel URI
83 ms_parcelURI = parcelURI;
85 //Get the parser service
86 validateXRef( m_xContext,
87 "ScriptMetadataImporter::parseMetaData: No context available" );
89 Reference< lang::XMultiComponentFactory > xMgr =
90 m_xContext->getServiceManager();
92 validateXRef( xMgr,
93 "ScriptMetadataImporter::parseMetaData: No service manager available" );
95 Reference< XInterface > xInterface = xMgr->createInstanceWithContext(
96 OUString::createFromAscii( "com.sun.star.xml.sax.Parser" ), m_xContext );
98 validateXRef( xInterface, "ScriptMetadataImporter::parseMetaData: cannot get SAX Parser" );
99 Reference< xml::sax::XParser > xParser;
102 xParser.set( xInterface ,UNO_QUERY_THROW );
104 catch (RuntimeException & re )
106 OUString msg = OUString::createFromAscii(
107 "ScriptMetadata:Importer::parserMetaData cannot get XParser" );
108 msg.concat( re.Message );
109 throw RuntimeException( msg, Reference< XInterface > () );
112 // xxx todo: error handler, entity resolver omitted
113 // This class is the document handler for the parser
114 Reference< xml::sax::XDocumentHandler > t_smI( this );
115 xParser->setDocumentHandler( t_smI );
117 //Set up the input for the parser, the XInputStream
118 xml::sax::InputSource source;
119 source.aInputStream = xInput;
120 source.sSystemId = OUSTR( "virtual file" );
122 OSL_TRACE( "ScriptMetadataImporter: Start the parser\n" );
126 xParser->parseStream( source );
128 catch ( xml::sax::SAXException & saxe )
130 OUString msg = OUString::createFromAscii(
131 "ScriptMetadata:Importer::parserMetaData SAXException" );
132 msg.concat( saxe.Message );
133 throw xml::sax::SAXException( msg, Reference< XInterface > (),
134 saxe.WrappedException );
136 catch ( io::IOException & ioe )
138 throw io::IOException( OUString::createFromAscii(
139 "ScriptMetadataImporter::parseMetaData IOException: " ) + ioe.Message,
140 Reference< XInterface > () );
143 #ifdef _DEBUG
144 catch ( ... )
146 throw RuntimeException( OUString::createFromAscii(
147 "ScriptMetadataImporter::parseMetadata UnknownException: " ),
148 Reference< XInterface > () );
150 #endif
152 OSL_TRACE( "ScriptMetadataImporter: Parser finished\n ");
154 OSL_TRACE( "ScriptMetadataImporter: vector size is %d\n",
155 mpv_ScriptDatas->size() );
158 //*************************************************************************
159 // XExtendedDocumentHandler impl
160 void ScriptMetadataImporter::startCDATA()
161 throw ( xml::sax::SAXException, RuntimeException )
163 OSL_TRACE( "ScriptMetadataImporter: startCDATA()\n" );
166 //*************************************************************************
167 void ScriptMetadataImporter::endCDATA()
168 throw ( RuntimeException )
170 OSL_TRACE( "ScriptMetadataImporter: endDATA()\n" );
173 //*************************************************************************
174 void ScriptMetadataImporter::comment( const ::rtl::OUString & sComment )
175 throw ( xml::sax::SAXException, RuntimeException )
177 OSL_TRACE( "ScriptMetadataImporter: comment()\n" );
180 //*************************************************************************
181 void ScriptMetadataImporter::allowLineBreak()
182 throw ( xml::sax::SAXException, RuntimeException )
184 OSL_TRACE( "ScriptMetadataImporter: allowLineBreak()\n" );
187 //*************************************************************************
188 void ScriptMetadataImporter::unknown( const ::rtl::OUString & sString )
189 throw ( xml::sax::SAXException, RuntimeException )
191 OSL_TRACE( "ScriptMetadataImporter: unknown()\n" );
194 //*************************************************************************
195 // XDocumentHandler impl
196 void ScriptMetadataImporter::startDocument()
197 throw ( xml::sax::SAXException, RuntimeException )
199 // Ignore for now
200 OSL_TRACE( "ScriptMetadataImporter: startDocument()\n" );
203 //*************************************************************************
204 void ScriptMetadataImporter::endDocument()
205 throw ( xml::sax::SAXException, RuntimeException )
207 // Ignore for now
208 OSL_TRACE( "ScriptMetadataImporter: endDocument()\n" );
211 //*************************************************************************
212 void ScriptMetadataImporter::startElement(
213 const ::rtl::OUString& tagName,
214 const Reference< xml::sax::XAttributeList >& xAttribs )
215 throw ( xml::sax::SAXException, RuntimeException )
218 OSL_TRACE( "Trace Message : ScriptMetadataImporter: startElement() %s\n",
219 ::rtl::OUStringToOString( tagName,
220 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
222 ::osl::Guard< ::osl::Mutex > aGuard( m_mutex );
224 //Set the state of the state machine
225 setState( tagName );
227 //Processing the elements
228 switch( m_state )
230 case SCRIPT:
231 m_ScriptData.parcelURI = ms_parcelURI;
232 m_ScriptData.language = xAttribs->getValueByName(
233 ::rtl::OUString::createFromAscii( "language" ));
234 OSL_TRACE( "Trace Message: language is %s\n",
235 ::rtl::OUStringToOString( m_ScriptData.language,
236 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
237 break;
238 case LOCALE:
239 ms_localeLang = xAttribs->getValueByName(
240 ::rtl::OUString::createFromAscii( "lang" ) );
241 OSL_TRACE( "Trace Message: Locale is %s\n",
242 ::rtl::OUStringToOString( ms_localeLang,
243 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
244 break;
245 case DISPLAYNAME:
246 ms_localeDisName = xAttribs->getValueByName(
247 ::rtl::OUString::createFromAscii( "value" ));
248 OSL_TRACE( "Trace Message: Displyaname is %s\n",
249 ::rtl::OUStringToOString( ms_localeDisName,
250 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
251 break;
252 case FUNCTIONNAME:
253 m_ScriptData.functionname = xAttribs->getValueByName(
254 ::rtl::OUString::createFromAscii( "value" ) );
255 OSL_TRACE( "Trace Message: Functionname is %s\n",
256 ::rtl::OUStringToOString( m_ScriptData.functionname,
257 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
258 break;
259 case LOGICALNAME:
260 m_ScriptData.logicalname = xAttribs->getValueByName(
261 ::rtl::OUString::createFromAscii( "value" ));
262 #ifdef _DEBUG
263 fprintf(stderr, "Trace Message: logicalname is %s\n",
264 ::rtl::OUStringToOString( m_ScriptData.logicalname,
265 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
266 #endif
267 break;
268 case LANGDEPPROPS:
269 m_ScriptData.languagedepprops.push_back(
270 ::std::make_pair( xAttribs->getValueByName(
271 ::rtl::OUString::createFromAscii( "name" ) ),
272 xAttribs->getValueByName(
273 ::rtl::OUString::createFromAscii( "value" ) )
275 OSL_TRACE( "Trace Message: Langdepprops is %s\t%s\n",
276 ::rtl::OUStringToOString( xAttribs->getValueByName(
277 ::rtl::OUString::createFromAscii( "name" ) ),
278 RTL_TEXTENCODING_ASCII_US ).pData->buffer,
279 ::rtl::OUStringToOString( xAttribs->getValueByName(
280 ::rtl::OUString::createFromAscii( "value" ) ),
281 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
282 break;
283 case FILESET:
284 ms_filesetname = xAttribs->getValueByName(
285 ::rtl::OUString::createFromAscii( "name" ) );
286 OSL_TRACE( "Trace Message: filesetname is %s\n",
287 ::rtl::OUStringToOString(ms_filesetname,
288 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
289 break;
290 case FILESETPROPS:
291 mv_filesetprops.push_back( ::std::make_pair(
292 xAttribs->getValueByName(
293 ::rtl::OUString::createFromAscii( "name" ) ),
294 xAttribs->getValueByName(
295 ::rtl::OUString::createFromAscii( "value" ) )
297 OSL_TRACE( "Trace Message: filesetprops is %s\t%s\n",
298 ::rtl::OUStringToOString( xAttribs->getValueByName(
299 ::rtl::OUString::createFromAscii( "name" ) ),
300 RTL_TEXTENCODING_ASCII_US ).pData->buffer,
301 ::rtl::OUStringToOString( xAttribs->getValueByName(
302 ::rtl::OUString::createFromAscii( "value" ) ),
303 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
304 break;
305 case FILES:
306 ms_filename = xAttribs->getValueByName(
307 ::rtl::OUString::createFromAscii( "name" ) );
308 OSL_TRACE( "Trace Message: filename is %s\n",
309 ::rtl::OUStringToOString( ms_filename,
310 RTL_TEXTENCODING_ASCII_US).pData->buffer );
311 break;
312 case FILEPROPS:
314 mm_files.insert( strpair_pair( ms_filename,
315 str_pair( xAttribs->getValueByName(
316 ::rtl::OUString::createFromAscii( "name" ) ),
317 xAttribs->getValueByName(
318 ::rtl::OUString::createFromAscii( "value") ) )
322 mv_fileprops.push_back(str_pair( xAttribs->getValueByName(
323 ::rtl::OUString::createFromAscii( "name" ) ),
324 xAttribs->getValueByName(
325 ::rtl::OUString::createFromAscii( "value") ) ) );
326 OSL_TRACE( "Trace Message: fileprops is %s\t%s\n",
327 ::rtl::OUStringToOString( xAttribs->getValueByName(
328 ::rtl::OUString::createFromAscii( "name" ) ),
329 RTL_TEXTENCODING_ASCII_US ).pData->buffer,
330 ::rtl::OUStringToOString( xAttribs->getValueByName(
331 ::rtl::OUString::createFromAscii( "value" ) ),
332 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
333 break;
335 // to prevent compiler warnings
336 case PARCEL:
337 case DESCRIPTION:
338 case LANGUAGEDEPPROPS:
339 break;
343 //*************************************************************************
344 void ScriptMetadataImporter::endElement( const ::rtl::OUString & aName )
345 throw ( xml::sax::SAXException, RuntimeException )
348 //The end tag of an element
349 OSL_TRACE( "ScriptMetadataImporter: endElement() %s\n",
350 ::rtl::OUStringToOString( aName,
351 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
353 ::osl::Guard< ::osl::Mutex > aGuard( m_mutex );
355 //Set the state
356 setState( aName );
359 switch ( m_state )
361 case PARCEL:
362 break;
363 case SCRIPT:
364 // np adjust logicalName to be equal to function name
365 // as logical name concept has been removed
366 m_ScriptData.logicalname = m_ScriptData.functionname;
367 mpv_ScriptDatas->push_back( m_ScriptData );
368 m_ScriptData = ScriptData();
369 break;
370 case LOCALE:
371 m_ScriptData.locales[ ms_localeLang ] = ::std::make_pair(
372 ms_localeDisName, ms_localeDesc->makeStringAndClear().trim() );
373 break;
374 case FILESET:
375 OSL_TRACE("adding fileset %s to filesets map",
376 ::rtl::OUStringToOString( ms_filesetname,
377 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
378 m_ScriptData.filesets[ ms_filesetname ] = ::std::make_pair(
379 mv_filesetprops, mm_files );
380 mm_files.clear();
381 mv_filesetprops.clear();
382 break;
383 case FILES:
384 OSL_TRACE("adding files %s to files map",
385 ::rtl::OUStringToOString( ms_filename,
386 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
387 mm_files[ ms_filename ] = mv_fileprops;
388 mv_fileprops.clear();
389 break;
391 // to prevent compiler warnings
392 case DISPLAYNAME:
393 case DESCRIPTION:
394 case FUNCTIONNAME:
395 case LOGICALNAME:
396 case LANGUAGEDEPPROPS:
397 case LANGDEPPROPS:
398 case FILESETPROPS:
399 case FILEPROPS:
400 break;
404 //*************************************************************************
405 void ScriptMetadataImporter::characters( const ::rtl::OUString & aChars )
406 throw ( xml::sax::SAXException, RuntimeException )
408 OSL_TRACE( "ScriptMetadataImporter: characters()\n");
410 ::osl::Guard< ::osl::Mutex > aGuard( m_mutex );
412 switch ( m_state )
414 case DESCRIPTION:
415 //Put description into the struct
416 ms_localeDesc->append(aChars);
417 break;
418 case PARCEL:
419 case SCRIPT:
420 case LOCALE:
421 case DISPLAYNAME:
422 case FUNCTIONNAME:
423 case LOGICALNAME:
424 case LANGUAGEDEPPROPS:
425 case LANGDEPPROPS:
426 case FILESETPROPS:
427 case FILEPROPS:
428 break;
432 //*************************************************************************
433 void ScriptMetadataImporter::ignorableWhitespace(
434 const ::rtl::OUString & aWhitespaces )
435 throw ( xml::sax::SAXException, RuntimeException )
437 OSL_TRACE( "ScriptMetadataImporter: ignorableWhiteSpace()\n" );
440 //*************************************************************************
441 void ScriptMetadataImporter::processingInstruction(
442 const ::rtl::OUString & aTarget, const ::rtl::OUString & aData )
443 throw ( xml::sax::SAXException, RuntimeException )
445 OSL_TRACE( "ScriptMetadataImporter: processingInstruction()\n" );
448 //*************************************************************************
449 void ScriptMetadataImporter::setDocumentLocator(
450 const Reference< xml::sax::XLocator >& xLocator )
451 throw ( xml::sax::SAXException, RuntimeException )
453 OSL_TRACE( "ScriptMetadataImporter: setDocumentLocator()\n" );
456 //*************************************************************************
457 void ScriptMetadataImporter::setState( const ::rtl::OUString & tagName )
459 //Set the state depending on the tag name of the current
460 //element the parser has arrived at
461 ::osl::Guard< ::osl::Mutex > aGuard( m_mutex );
463 if( tagName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "parcel" ) ) )
465 //Parcel tag
466 m_state = PARCEL;
468 else if( tagName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "script" ) ) )
470 m_state = SCRIPT;
472 else if( tagName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "locale" ) ) )
474 m_state = LOCALE;
476 else if( tagName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "displayname" ) ) )
478 m_state = DISPLAYNAME;
480 else if( tagName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "description" ) ) )
482 m_state = DESCRIPTION;
484 else if( tagName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "functionname" ) ) )
486 m_state = FUNCTIONNAME;
488 else if( tagName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "logicalname" ) ) )
490 m_state = LOGICALNAME;
492 else if( tagName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "languagedepprops" ) ) )
494 m_state = LANGUAGEDEPPROPS;
496 else if( tagName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "prop" ) ) )
498 if( m_state == LANGUAGEDEPPROPS )
500 m_state = LANGDEPPROPS;
502 else if( m_state == FILESET )
504 m_state = FILESETPROPS;
506 else if( m_state == FILES )
508 m_state = FILEPROPS;
511 else if( tagName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "fileset" ) ) )
513 m_state = FILESET;
515 else if( tagName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "file" ) ) )
517 m_state = FILES;
519 else
521 //If there is a tag we don't know about, throw a exception (wobbler) :)
522 ::rtl::OUString str_sax = ::rtl::OUString::createFromAscii( "No Such Tag" );
524 OSL_TRACE( "ScriptMetadataImporter: No Such Tag: %s\n",
525 ::rtl::OUStringToOString(
526 tagName, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
528 throw xml::sax::SAXException(
529 str_sax, Reference< XInterface >(), Any() );
535 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */