1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScriptMetadataImporter.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_scripting.hxx"
38 #include "ScriptMetadataImporter.hxx"
40 #include <osl/mutex.hxx>
42 #include <com/sun/star/xml/sax/XParser.hpp>
43 #include <rtl/string.h>
46 #include <util/util.hxx>
48 using namespace ::rtl
;
49 using namespace ::com::sun::star
;
50 using namespace ::com::sun::star::uno
;
52 namespace scripting_impl
54 //*************************************************************************
55 ScriptMetadataImporter::ScriptMetadataImporter(
56 const Reference
< XComponentContext
> & xContext
)
57 : m_xContext( xContext
)
59 OSL_TRACE( "< ScriptMetadataImporter ctor called >\n" );
60 ms_localeDesc
= new OUStringBuffer();
63 //*************************************************************************
64 ScriptMetadataImporter::~ScriptMetadataImporter() SAL_THROW( () )
66 OSL_TRACE( "< ScriptMetadataImporter dtor called >\n" );
71 //*************************************************************************
72 void ScriptMetadataImporter::parseMetaData(
73 Reference
< io::XInputStream
> const & xInput
,
74 const ::rtl::OUString
& parcelURI
,
75 InfoImpls_vec
& io_ScriptDatas
)
76 throw ( xml::sax::SAXException
, io::IOException
, RuntimeException
)
79 ::osl::Guard
< ::osl::Mutex
> aGuard( m_mutex
);
81 mpv_ScriptDatas
= &io_ScriptDatas
;
82 mpv_ScriptDatas
->clear();
84 //Set the placeholder for the parcel URI
85 ms_parcelURI
= parcelURI
;
87 //Get the parser service
88 validateXRef( m_xContext
,
89 "ScriptMetadataImporter::parseMetaData: No context available" );
91 Reference
< lang::XMultiComponentFactory
> xMgr
=
92 m_xContext
->getServiceManager();
95 "ScriptMetadataImporter::parseMetaData: No service manager available" );
97 Reference
< XInterface
> xInterface
= xMgr
->createInstanceWithContext(
98 OUString::createFromAscii( "com.sun.star.xml.sax.Parser" ), m_xContext
);
100 validateXRef( xInterface
, "ScriptMetadataImporter::parseMetaData: cannot get SAX Parser" );
101 Reference
< xml::sax::XParser
> xParser
;
104 xParser
.set( xInterface
,UNO_QUERY_THROW
);
106 catch (RuntimeException
& re
)
108 OUString msg
= OUString::createFromAscii(
109 "ScriptMetadata:Importer::parserMetaData cannot get XParser" );
110 msg
.concat( re
.Message
);
111 throw RuntimeException( msg
, Reference
< XInterface
> () );
114 // xxx todo: error handler, entity resolver omitted
115 // This class is the document handler for the parser
116 Reference
< xml::sax::XDocumentHandler
> t_smI( this );
117 xParser
->setDocumentHandler( t_smI
);
119 //Set up the input for the parser, the XInputStream
120 xml::sax::InputSource source
;
121 source
.aInputStream
= xInput
;
122 source
.sSystemId
= OUSTR( "virtual file" );
124 OSL_TRACE( "ScriptMetadataImporter: Start the parser\n" );
128 xParser
->parseStream( source
);
130 catch ( xml::sax::SAXException
& saxe
)
132 OUString msg
= OUString::createFromAscii(
133 "ScriptMetadata:Importer::parserMetaData SAXException" );
134 msg
.concat( saxe
.Message
);
135 throw xml::sax::SAXException( msg
, Reference
< XInterface
> (),
136 saxe
.WrappedException
);
138 catch ( io::IOException
& ioe
)
140 throw io::IOException( OUString::createFromAscii(
141 "ScriptMetadataImporter::parseMetaData IOException: " ) + ioe
.Message
,
142 Reference
< XInterface
> () );
148 throw RuntimeException( OUString::createFromAscii(
149 "ScriptMetadataImporter::parseMetadata UnknownException: " ),
150 Reference
< XInterface
> () );
154 OSL_TRACE( "ScriptMetadataImporter: Parser finished\n ");
156 OSL_TRACE( "ScriptMetadataImporter: vector size is %d\n",
157 mpv_ScriptDatas
->size() );
160 //*************************************************************************
161 // XExtendedDocumentHandler impl
162 void ScriptMetadataImporter::startCDATA()
163 throw ( xml::sax::SAXException
, RuntimeException
)
165 OSL_TRACE( "ScriptMetadataImporter: startCDATA()\n" );
168 //*************************************************************************
169 void ScriptMetadataImporter::endCDATA()
170 throw ( RuntimeException
)
172 OSL_TRACE( "ScriptMetadataImporter: endDATA()\n" );
175 //*************************************************************************
176 void ScriptMetadataImporter::comment( const ::rtl::OUString
& sComment
)
177 throw ( xml::sax::SAXException
, RuntimeException
)
179 OSL_TRACE( "ScriptMetadataImporter: comment()\n" );
182 //*************************************************************************
183 void ScriptMetadataImporter::allowLineBreak()
184 throw ( xml::sax::SAXException
, RuntimeException
)
186 OSL_TRACE( "ScriptMetadataImporter: allowLineBreak()\n" );
189 //*************************************************************************
190 void ScriptMetadataImporter::unknown( const ::rtl::OUString
& sString
)
191 throw ( xml::sax::SAXException
, RuntimeException
)
193 OSL_TRACE( "ScriptMetadataImporter: unknown()\n" );
196 //*************************************************************************
197 // XDocumentHandler impl
198 void ScriptMetadataImporter::startDocument()
199 throw ( xml::sax::SAXException
, RuntimeException
)
202 OSL_TRACE( "ScriptMetadataImporter: startDocument()\n" );
205 //*************************************************************************
206 void ScriptMetadataImporter::endDocument()
207 throw ( xml::sax::SAXException
, RuntimeException
)
210 OSL_TRACE( "ScriptMetadataImporter: endDocument()\n" );
213 //*************************************************************************
214 void ScriptMetadataImporter::startElement(
215 const ::rtl::OUString
& tagName
,
216 const Reference
< xml::sax::XAttributeList
>& xAttribs
)
217 throw ( xml::sax::SAXException
, RuntimeException
)
220 OSL_TRACE( "Trace Message : ScriptMetadataImporter: startElement() %s\n",
221 ::rtl::OUStringToOString( tagName
,
222 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
224 ::osl::Guard
< ::osl::Mutex
> aGuard( m_mutex
);
226 //Set the state of the state machine
229 //Processing the elements
233 m_ScriptData
.parcelURI
= ms_parcelURI
;
234 m_ScriptData
.language
= xAttribs
->getValueByName(
235 ::rtl::OUString::createFromAscii( "language" ));
236 OSL_TRACE( "Trace Message: language is %s\n",
237 ::rtl::OUStringToOString( m_ScriptData
.language
,
238 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
241 ms_localeLang
= xAttribs
->getValueByName(
242 ::rtl::OUString::createFromAscii( "lang" ) );
243 OSL_TRACE( "Trace Message: Locale is %s\n",
244 ::rtl::OUStringToOString( ms_localeLang
,
245 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
248 ms_localeDisName
= xAttribs
->getValueByName(
249 ::rtl::OUString::createFromAscii( "value" ));
250 OSL_TRACE( "Trace Message: Displyaname is %s\n",
251 ::rtl::OUStringToOString( ms_localeDisName
,
252 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
255 m_ScriptData
.functionname
= xAttribs
->getValueByName(
256 ::rtl::OUString::createFromAscii( "value" ) );
257 OSL_TRACE( "Trace Message: Functionname is %s\n",
258 ::rtl::OUStringToOString( m_ScriptData
.functionname
,
259 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
262 m_ScriptData
.logicalname
= xAttribs
->getValueByName(
263 ::rtl::OUString::createFromAscii( "value" ));
265 fprintf(stderr
, "Trace Message: logicalname is %s\n",
266 ::rtl::OUStringToOString( m_ScriptData
.logicalname
,
267 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
271 m_ScriptData
.languagedepprops
.push_back(
272 ::std::make_pair( xAttribs
->getValueByName(
273 ::rtl::OUString::createFromAscii( "name" ) ),
274 xAttribs
->getValueByName(
275 ::rtl::OUString::createFromAscii( "value" ) )
277 OSL_TRACE( "Trace Message: Langdepprops is %s\t%s\n",
278 ::rtl::OUStringToOString( xAttribs
->getValueByName(
279 ::rtl::OUString::createFromAscii( "name" ) ),
280 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
,
281 ::rtl::OUStringToOString( xAttribs
->getValueByName(
282 ::rtl::OUString::createFromAscii( "value" ) ),
283 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
286 ms_filesetname
= xAttribs
->getValueByName(
287 ::rtl::OUString::createFromAscii( "name" ) );
288 OSL_TRACE( "Trace Message: filesetname is %s\n",
289 ::rtl::OUStringToOString(ms_filesetname
,
290 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
293 mv_filesetprops
.push_back( ::std::make_pair(
294 xAttribs
->getValueByName(
295 ::rtl::OUString::createFromAscii( "name" ) ),
296 xAttribs
->getValueByName(
297 ::rtl::OUString::createFromAscii( "value" ) )
299 OSL_TRACE( "Trace Message: filesetprops is %s\t%s\n",
300 ::rtl::OUStringToOString( xAttribs
->getValueByName(
301 ::rtl::OUString::createFromAscii( "name" ) ),
302 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
,
303 ::rtl::OUStringToOString( xAttribs
->getValueByName(
304 ::rtl::OUString::createFromAscii( "value" ) ),
305 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
308 ms_filename
= xAttribs
->getValueByName(
309 ::rtl::OUString::createFromAscii( "name" ) );
310 OSL_TRACE( "Trace Message: filename is %s\n",
311 ::rtl::OUStringToOString( ms_filename
,
312 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
316 mm_files.insert( strpair_pair( ms_filename,
317 str_pair( xAttribs->getValueByName(
318 ::rtl::OUString::createFromAscii( "name" ) ),
319 xAttribs->getValueByName(
320 ::rtl::OUString::createFromAscii( "value") ) )
324 mv_fileprops
.push_back(str_pair( xAttribs
->getValueByName(
325 ::rtl::OUString::createFromAscii( "name" ) ),
326 xAttribs
->getValueByName(
327 ::rtl::OUString::createFromAscii( "value") ) ) );
328 OSL_TRACE( "Trace Message: fileprops is %s\t%s\n",
329 ::rtl::OUStringToOString( xAttribs
->getValueByName(
330 ::rtl::OUString::createFromAscii( "name" ) ),
331 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
,
332 ::rtl::OUStringToOString( xAttribs
->getValueByName(
333 ::rtl::OUString::createFromAscii( "value" ) ),
334 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
337 // to prevent compiler warnings
340 case LANGUAGEDEPPROPS
:
345 //*************************************************************************
346 void ScriptMetadataImporter::endElement( const ::rtl::OUString
& aName
)
347 throw ( xml::sax::SAXException
, RuntimeException
)
350 //The end tag of an element
351 OSL_TRACE( "ScriptMetadataImporter: endElement() %s\n",
352 ::rtl::OUStringToOString( aName
,
353 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
355 ::osl::Guard
< ::osl::Mutex
> aGuard( m_mutex
);
366 // np adjust logicalName to be equal to function name
367 // as logical name concept has been removed
368 m_ScriptData
.logicalname
= m_ScriptData
.functionname
;
369 mpv_ScriptDatas
->push_back( m_ScriptData
);
370 m_ScriptData
= ScriptData();
373 m_ScriptData
.locales
[ ms_localeLang
] = ::std::make_pair(
374 ms_localeDisName
, ms_localeDesc
->makeStringAndClear().trim() );
377 OSL_TRACE("adding fileset %s to filesets map",
378 ::rtl::OUStringToOString( ms_filesetname
,
379 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
380 m_ScriptData
.filesets
[ ms_filesetname
] = ::std::make_pair(
381 mv_filesetprops
, mm_files
);
383 mv_filesetprops
.clear();
386 OSL_TRACE("adding files %s to files map",
387 ::rtl::OUStringToOString( ms_filename
,
388 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
389 mm_files
[ ms_filename
] = mv_fileprops
;
390 mv_fileprops
.clear();
393 // to prevent compiler warnings
398 case LANGUAGEDEPPROPS
:
406 //*************************************************************************
407 void ScriptMetadataImporter::characters( const ::rtl::OUString
& aChars
)
408 throw ( xml::sax::SAXException
, RuntimeException
)
410 OSL_TRACE( "ScriptMetadataImporter: characters()\n");
412 ::osl::Guard
< ::osl::Mutex
> aGuard( m_mutex
);
417 //Put description into the struct
418 ms_localeDesc
->append(aChars
);
426 case LANGUAGEDEPPROPS
:
434 //*************************************************************************
435 void ScriptMetadataImporter::ignorableWhitespace(
436 const ::rtl::OUString
& aWhitespaces
)
437 throw ( xml::sax::SAXException
, RuntimeException
)
439 OSL_TRACE( "ScriptMetadataImporter: ignorableWhiteSpace()\n" );
442 //*************************************************************************
443 void ScriptMetadataImporter::processingInstruction(
444 const ::rtl::OUString
& aTarget
, const ::rtl::OUString
& aData
)
445 throw ( xml::sax::SAXException
, RuntimeException
)
447 OSL_TRACE( "ScriptMetadataImporter: processingInstruction()\n" );
450 //*************************************************************************
451 void ScriptMetadataImporter::setDocumentLocator(
452 const Reference
< xml::sax::XLocator
>& xLocator
)
453 throw ( xml::sax::SAXException
, RuntimeException
)
455 OSL_TRACE( "ScriptMetadataImporter: setDocumentLocator()\n" );
458 //*************************************************************************
459 void ScriptMetadataImporter::setState( const ::rtl::OUString
& tagName
)
461 //Set the state depending on the tag name of the current
462 //element the parser has arrived at
463 ::osl::Guard
< ::osl::Mutex
> aGuard( m_mutex
);
465 if( tagName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "parcel" ) ) )
470 else if( tagName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "script" ) ) )
474 else if( tagName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "locale" ) ) )
478 else if( tagName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "displayname" ) ) )
480 m_state
= DISPLAYNAME
;
482 else if( tagName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "description" ) ) )
484 m_state
= DESCRIPTION
;
486 else if( tagName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "functionname" ) ) )
488 m_state
= FUNCTIONNAME
;
490 else if( tagName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "logicalname" ) ) )
492 m_state
= LOGICALNAME
;
494 else if( tagName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "languagedepprops" ) ) )
496 m_state
= LANGUAGEDEPPROPS
;
498 else if( tagName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "prop" ) ) )
500 if( m_state
== LANGUAGEDEPPROPS
)
502 m_state
= LANGDEPPROPS
;
504 else if( m_state
== FILESET
)
506 m_state
= FILESETPROPS
;
508 else if( m_state
== FILES
)
513 else if( tagName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "fileset" ) ) )
517 else if( tagName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "file" ) ) )
523 //If there is a tag we don't know about, throw a exception (wobbler) :)
524 ::rtl::OUString str_sax
= ::rtl::OUString::createFromAscii( "No Such Tag" );
526 OSL_TRACE( "ScriptMetadataImporter: No Such Tag: %s\n",
527 ::rtl::OUStringToOString(
528 tagName
, RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
530 throw xml::sax::SAXException(
531 str_sax
, Reference
< XInterface
>(), Any() );