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: parsersvc.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_configmgr.hxx"
34 #include "parsersvc.hxx"
36 #ifndef CONFIGMGR_API_FACTORY_HXX_
37 #include "confapifactory.hxx"
39 #include "schemaparser.hxx"
40 #include "layerparser.hxx"
41 #include <rtl/ustrbuf.hxx>
42 #include <cppuhelper/exc_hlp.hxx>
43 #include <com/sun/star/configuration/backend/XSchema.hpp>
44 #include <com/sun/star/configuration/backend/XLayer.hpp>
45 #include <com/sun/star/lang/WrappedTargetException.hpp>
46 #include <com/sun/star/lang/IllegalArgumentException.hpp>
47 #include <com/sun/star/configuration/backend/MalformedDataException.hpp>
48 #include <com/sun/star/xml/sax/XParser.hpp>
49 // -----------------------------------------------------------------------------
53 // -----------------------------------------------------------------------------
56 // -----------------------------------------------------------------------------
57 namespace uno
= ::com::sun::star::uno
;
58 namespace lang
= ::com::sun::star::lang
;
59 namespace io
= ::com::sun::star::io
;
60 namespace sax
= ::com::sun::star::xml::sax
;
61 namespace backenduno
= ::com::sun::star::configuration::backend
;
62 // -----------------------------------------------------------------------------
64 template <class BackendInterface
>
65 struct ParserServiceTraits
;
66 // -----------------------------------------------------------------------------
67 static inline void clear(rtl::OUString
& _rs
) { _rs
= rtl::OUString(); }
69 // -----------------------------------------------------------------------------
70 template <class BackendInterface
>
71 ParserService
<BackendInterface
>::ParserService(uno::Reference
< uno::XComponentContext
> const & _xContext
)
72 : m_xContext(_xContext
)
77 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("Configuration Parser: NULL Context"));
78 throw uno::RuntimeException(sMessage
,NULL
);
81 // -----------------------------------------------------------------------------
84 template <class BackendInterface
>
86 ParserService
<BackendInterface
>::initialize( const uno::Sequence
< uno::Any
>& aArguments
)
87 throw (uno::Exception
, uno::RuntimeException
)
89 switch(aArguments
.getLength())
95 if (aArguments
[0] >>= m_aInputSource
)
98 if (aArguments
[0] >>= m_aInputSource
.aInputStream
)
102 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("Cannot use argument to initialize a Configuration Parser"
103 "- InputSource or XInputStream expected"));
104 throw lang::IllegalArgumentException(sMessage
,*this,1);
108 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("Too many arguments to initialize a Configuration Parser"));
109 throw lang::IllegalArgumentException(sMessage
,*this,0);
113 // -----------------------------------------------------------------------------
115 template <class BackendInterface
>
117 ServiceInfoHelper ParserService
<BackendInterface
>::getServiceInfo()
119 return ParserServiceTraits
<BackendInterface
>::getServiceInfo();
121 // -----------------------------------------------------------------------------
124 template <class BackendInterface
>
125 ::rtl::OUString SAL_CALL
126 ParserService
<BackendInterface
>::getImplementationName( )
127 throw (uno::RuntimeException
)
129 return getServiceInfo().getImplementationName();
131 // -----------------------------------------------------------------------------
133 template <class BackendInterface
>
135 ParserService
<BackendInterface
>::supportsService( const ::rtl::OUString
& ServiceName
)
136 throw (uno::RuntimeException
)
138 return getServiceInfo().supportsService( ServiceName
);
140 // -----------------------------------------------------------------------------
142 template <class BackendInterface
>
143 uno::Sequence
< ::rtl::OUString
> SAL_CALL
144 ParserService
<BackendInterface
>::getSupportedServiceNames( )
145 throw (uno::RuntimeException
)
147 return getServiceInfo().getSupportedServiceNames( );
149 // -----------------------------------------------------------------------------
151 template <class BackendInterface
>
153 ParserService
<BackendInterface
>::setInputStream( const uno::Reference
< io::XInputStream
>& aStream
)
154 throw (uno::RuntimeException
)
156 clear( m_aInputSource
.sEncoding
);
157 clear( m_aInputSource
.sSystemId
);
158 // clear( m_aInputSource.sPublicId );
159 m_aInputSource
.aInputStream
= aStream
;
161 // -----------------------------------------------------------------------------
163 template <class BackendInterface
>
164 uno::Reference
< io::XInputStream
> SAL_CALL
165 ParserService
<BackendInterface
>::getInputStream( )
166 throw (uno::RuntimeException
)
168 return m_aInputSource
.aInputStream
;
170 // -----------------------------------------------------------------------------
172 template <class BackendInterface
>
173 void ParserService
<BackendInterface
>::parse(uno::Reference
< sax::XDocumentHandler
> const & _xHandler
)
174 // throw (backenduno::MalformedDataException, lang::WrappedTargetException, uno::RuntimeException)
176 OSL_PRECOND( _xHandler
.is(), "ParserService: No SAX handler to parse to");
178 rtl::OUString
const k_sSaxParserService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser"));
180 uno::Reference
< lang::XMultiComponentFactory
> xServiceFactory
= m_xContext
->getServiceManager();
182 uno::Reference
< sax::XParser
> xParser
= uno::Reference
< sax::XParser
>::query( xServiceFactory
->createInstanceWithContext(k_sSaxParserService
,m_xContext
) );
186 rtl::OUString
sMessage( RTL_CONSTASCII_USTRINGPARAM("Configuration Parser: Cannot create SAX Parser"));
187 throw uno::RuntimeException(sMessage
,*this);
192 xParser
->setDocumentHandler(_xHandler
);
194 sax::InputSource aInputSourceCopy
= m_aInputSource
;
195 //Set the sax input stream to null, an input stream can only be parsed once
196 m_aInputSource
.aInputStream
= NULL
;
197 xParser
->parseStream( aInputSourceCopy
);
199 catch (sax::SAXException
& e
)
201 uno::Any aWrapped
= e
.WrappedException
.hasValue() ? e
.WrappedException
: uno::makeAny( e
);
202 rtl::OUString sSAXMessage
= e
.Message
;
204 // Expatwrap SAX service doubly wraps its errors ??
205 sax::SAXException eInner
;
206 if (aWrapped
>>= eInner
)
208 if (eInner
.WrappedException
.hasValue()) aWrapped
= eInner
.WrappedException
;
210 rtl::OUStringBuffer
sMsgBuf(eInner
.Message
);
211 sMsgBuf
.appendAscii("- {Parser Error: ").append(sSAXMessage
).appendAscii(" }.");
212 sSAXMessage
= sMsgBuf
.makeStringAndClear();
215 static backenduno::MalformedDataException
const * const forDataError
= 0;
216 static lang::WrappedTargetException
const * const forWrappedError
= 0;
217 if (aWrapped
.isExtractableTo(getCppuType(forDataError
)) ||
218 aWrapped
.isExtractableTo(getCppuType(forWrappedError
)))
220 cppu::throwException(aWrapped
);
222 OSL_ASSERT(!"not reached");
225 rtl::OUStringBuffer sMessageBuf
;
226 sMessageBuf
.appendAscii("Configuration Parser: a ").append( aWrapped
.getValueTypeName() );
227 sMessageBuf
.appendAscii(" occurred while parsing: ");
228 sMessageBuf
.append(sSAXMessage
);
230 throw lang::WrappedTargetException(sMessageBuf
.makeStringAndClear(),*this,aWrapped
);
234 // -----------------------------------------------------------------------------
235 // -----------------------------------------------------------------------------
236 sal_Char
const * const aSchemaParserServices
[] =
238 "com.sun.star.configuration.backend.xml.SchemaParser",
241 const ServiceImplementationInfo aSchemaParserSI
=
243 "com.sun.star.comp.configuration.backend.xml.SchemaParser",
244 aSchemaParserServices
,
247 // -----------------------------------------------------------------------------
248 sal_Char
const * const aLayerParserServices
[] =
250 "com.sun.star.configuration.backend.xml.LayerParser",
253 const ServiceImplementationInfo aLayerParserSI
=
255 "com.sun.star.comp.configuration.backend.xml.LayerParser",
256 aLayerParserServices
,
259 // -----------------------------------------------------------------------------
260 // -----------------------------------------------------------------------------
262 struct ParserServiceTraits
< backenduno::XSchema
>
264 static ServiceImplementationInfo
const * getServiceInfo()
265 { return & aSchemaParserSI
; }
267 // -----------------------------------------------------------------------------
269 struct ParserServiceTraits
< backenduno::XLayer
>
271 static ServiceImplementationInfo
const * getServiceInfo()
272 { return & aLayerParserSI
; }
274 // -----------------------------------------------------------------------------
275 // -----------------------------------------------------------------------------
277 class SchemaParserService
: public ParserService
< backenduno::XSchema
>
280 SchemaParserService(uno::Reference
< uno::XComponentContext
> const & _xContext
)
281 : ParserService
< backenduno::XSchema
>(_xContext
)
285 virtual void SAL_CALL
readSchema( uno::Reference
< backenduno::XSchemaHandler
> const & aHandler
)
286 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
,
287 lang::NullPointerException
, uno::RuntimeException
);
289 virtual void SAL_CALL
readComponent( uno::Reference
< backenduno::XSchemaHandler
> const & aHandler
)
290 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
,
291 lang::NullPointerException
, uno::RuntimeException
);
293 virtual void SAL_CALL
readTemplates( uno::Reference
< backenduno::XSchemaHandler
> const & aHandler
)
294 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
,
295 lang::NullPointerException
, uno::RuntimeException
);
297 // -----------------------------------------------------------------------------
299 class LayerParserService
: public ParserService
< backenduno::XLayer
>
302 LayerParserService(uno::Reference
< uno::XComponentContext
> const & _xContext
)
303 : ParserService
< backenduno::XLayer
>(_xContext
)
307 virtual void SAL_CALL
readData( uno::Reference
< backenduno::XLayerHandler
> const & aHandler
)
308 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
,
309 lang::NullPointerException
, uno::RuntimeException
);
312 // -----------------------------------------------------------------------------
313 // -----------------------------------------------------------------------------
314 uno::Reference
< uno::XInterface
> SAL_CALL
instantiateSchemaParser( uno::Reference
< uno::XComponentContext
> const& xContext
)
316 return * new SchemaParserService(xContext
);
318 uno::Reference
< uno::XInterface
> SAL_CALL
instantiateLayerParser( uno::Reference
< uno::XComponentContext
> const& xContext
)
320 return * new LayerParserService(xContext
);
322 // -----------------------------------------------------------------------------
323 const ServiceRegistrationInfo
* getSchemaParserServiceInfo()
324 { return getRegistrationInfo(& aSchemaParserSI
); }
325 const ServiceRegistrationInfo
* getLayerParserServiceInfo()
326 { return getRegistrationInfo(& aLayerParserSI
); }
327 // -----------------------------------------------------------------------------
328 // -----------------------------------------------------------------------------
329 static rtl::OUString
nullHandlerMessage(char const * where
)
332 rtl::OUString msg
= rtl::OUString::createFromAscii(where
);
333 return msg
.concat(rtl::OUString::createFromAscii(": Error - NULL handler passed."));
335 // -----------------------------------------------------------------------------
336 void SAL_CALL
SchemaParserService::readSchema( uno::Reference
< backenduno::XSchemaHandler
> const & aHandler
)
337 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
,
338 lang::NullPointerException
, uno::RuntimeException
)
341 throw lang::NullPointerException(nullHandlerMessage("SchemaParserService::readSchema"),*this);
343 uno::Reference
< sax::XDocumentHandler
> xHandler
= new SchemaParser(this->getContext(),aHandler
, SchemaParser::selectAll
);
344 this->parse( xHandler
);
346 // -----------------------------------------------------------------------------
347 void SAL_CALL
SchemaParserService::readComponent( uno::Reference
< backenduno::XSchemaHandler
> const & aHandler
)
348 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
,
349 lang::NullPointerException
, uno::RuntimeException
)
352 throw lang::NullPointerException(nullHandlerMessage("SchemaParserService::readComponent"),*this);
354 uno::Reference
< sax::XDocumentHandler
> xHandler
= new SchemaParser(this->getContext(),aHandler
, SchemaParser::selectComponent
);
355 this->parse( xHandler
);
357 // -----------------------------------------------------------------------------
358 void SAL_CALL
SchemaParserService::readTemplates( uno::Reference
< backenduno::XSchemaHandler
> const & aHandler
)
359 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
,
360 lang::NullPointerException
, uno::RuntimeException
)
363 throw lang::NullPointerException(nullHandlerMessage("SchemaParserService::readTemplates"),*this);
365 uno::Reference
< sax::XDocumentHandler
> xHandler
= new SchemaParser(this->getContext(),aHandler
, SchemaParser::selectTemplates
);
366 this->parse( xHandler
);
368 // -----------------------------------------------------------------------------
369 void SAL_CALL
LayerParserService::readData( uno::Reference
< backenduno::XLayerHandler
> const & aHandler
)
370 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
,
371 lang::NullPointerException
, uno::RuntimeException
)
374 throw lang::NullPointerException(nullHandlerMessage("LayerParserService::readData"),*this);
376 uno::Reference
< sax::XDocumentHandler
> xHandler
= new LayerParser(this->getContext(),aHandler
);
377 this->parse( xHandler
);
379 // -----------------------------------------------------------------------------
380 // -----------------------------------------------------------------------------
383 // -----------------------------------------------------------------------------