xref: /aoo42x/main/svx/workben/msview/xmlconfig.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
30*cdf0e10cSrcweir #include "precompiled_svx.hxx"
31*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/xml/sax/InputSource.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/xml/sax/SAXParseException.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/io/IOException.hpp>
36*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
37*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
38*cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
39*cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
40*cdf0e10cSrcweir #include <tools/debug.hxx>
41*cdf0e10cSrcweir #include "comphelper/anytostring.hxx"
42*cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
43*cdf0e10cSrcweir #include "rtl/ref.hxx"
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <svx/msdffimp.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include "xmlconfig.hxx"
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include <stdio.h>
50*cdf0e10cSrcweir #include <ctype.h>
51*cdf0e10cSrcweir #include <stack>
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir using ::rtl::OUString;
54*cdf0e10cSrcweir using ::com::sun::star::io::XInputStream;
55*cdf0e10cSrcweir using ::com::sun::star::io::IOException;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
58*cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir AtomConfigMap gAtomConfigMap;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir class ConfigHandler : public ::cppu::WeakAggImplHelper1<XDocumentHandler>
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir public:
69*cdf0e10cSrcweir 	// XDocumentHandler
70*cdf0e10cSrcweir 	virtual void SAL_CALL startDocument(void) throw( SAXException, RuntimeException );
71*cdf0e10cSrcweir 	virtual void SAL_CALL endDocument(void) throw( SAXException, RuntimeException );
72*cdf0e10cSrcweir 	virtual void SAL_CALL startElement(const OUString& aName, const Reference< XAttributeList > & xAttribs) throw( SAXException, RuntimeException );
73*cdf0e10cSrcweir 	virtual void SAL_CALL endElement(const OUString& aName) throw( SAXException, RuntimeException );
74*cdf0e10cSrcweir 	virtual void SAL_CALL characters(const OUString& aChars) throw( SAXException, RuntimeException );
75*cdf0e10cSrcweir 	virtual void SAL_CALL ignorableWhitespace(const OUString& aWhitespaces) throw( SAXException, RuntimeException );
76*cdf0e10cSrcweir 	virtual void SAL_CALL processingInstruction(const OUString& aTarget, const OUString& aData) throw( SAXException, RuntimeException );
77*cdf0e10cSrcweir 	virtual void SAL_CALL setDocumentLocator(const Reference< XLocator > & xLocator) throw( SAXException, RuntimeException );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir private:
80*cdf0e10cSrcweir 	void errorThrow( const OUString& rErrorMessage ) throw (SAXException );
81*cdf0e10cSrcweir 	ElementConfigType parseType( const OUString& rErrorMessage ) throw ( SAXException );
82*cdf0e10cSrcweir 	void addElement( ElementConfigPtr& rElementConfig ) throw ( SAXException );
83*cdf0e10cSrcweir 	OUString getAttribute( const Reference< XAttributeList > & xAttribs, const sal_Char* pName ) throw( SAXException );
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	ElementConfigPtr importAtomConfig( const Reference< XAttributeList > & xAttribs, bool bIsContainer ) throw( SAXException );
86*cdf0e10cSrcweir 	ElementConfigPtr importElementConfig( const Reference< XAttributeList > & xAttribs ) throw( SAXException );
87*cdf0e10cSrcweir 	ElementConfigPtr importSwitchConfig( const Reference< XAttributeList > & xAttribs ) throw( SAXException );
88*cdf0e10cSrcweir 	ElementConfigPtr importCaseConfig( const Reference< XAttributeList > & xAttribs ) throw( SAXException );
89*cdf0e10cSrcweir 	ElementConfigPtr importValueElementConfig( const Reference< XAttributeList > & xAttribs ) throw( SAXException );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 	std::stack< ElementConfigPtr > maElementStack;
92*cdf0e10cSrcweir };
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir void ConfigHandler::errorThrow( const OUString& rErrorMessage ) throw (SAXException )
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir 	Reference< XInterface > aContext;
97*cdf0e10cSrcweir 	Any aWrappedException;
98*cdf0e10cSrcweir     throw SAXException(rErrorMessage, aContext, aWrappedException);
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir ElementConfigType ConfigHandler::parseType( const OUString& sType ) throw (SAXException )
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir 	if( sType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("uint") ) )
104*cdf0e10cSrcweir 	{
105*cdf0e10cSrcweir 		return ECT_UINT;
106*cdf0e10cSrcweir 	}
107*cdf0e10cSrcweir 	else if( sType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("byte") ) )
108*cdf0e10cSrcweir 	{
109*cdf0e10cSrcweir 		return ECT_BYTE;
110*cdf0e10cSrcweir 	}
111*cdf0e10cSrcweir 	else if( sType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("unistring") ) )
112*cdf0e10cSrcweir 	{
113*cdf0e10cSrcweir 		return ECT_UNISTRING;
114*cdf0e10cSrcweir 	}
115*cdf0e10cSrcweir 	else if( sType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("float") ) )
116*cdf0e10cSrcweir 	{
117*cdf0e10cSrcweir 		return ETC_FLOAT;
118*cdf0e10cSrcweir 	}
119*cdf0e10cSrcweir 	else if( sType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("hexdump") ) )
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 	}
122*cdf0e10cSrcweir 	else
123*cdf0e10cSrcweir 	{
124*cdf0e10cSrcweir 		OUString aMessage( RTL_CONSTASCII_USTRINGPARAM( "unknown type: " ) );
125*cdf0e10cSrcweir 		aMessage += sType;
126*cdf0e10cSrcweir 		errorThrow( aMessage );
127*cdf0e10cSrcweir 	}
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 	return ECT_HEXDUMP;
130*cdf0e10cSrcweir }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir void ConfigHandler::addElement( ElementConfigPtr& rElementConfig ) throw ( SAXException )
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir 	ElementConfigContainer* pParent = dynamic_cast< ElementConfigContainer* >( maElementStack.top().get() );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	if( !pParent )
137*cdf0e10cSrcweir 		errorThrow( OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal parent for element" ) ) );
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	pParent->addElementConfig( rElementConfig );
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir OUString ConfigHandler::getAttribute( const Reference< XAttributeList > & xAttribs, const sal_Char* pName ) throw( SAXException )
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	OUString aName( OUString::createFromAscii( pName ) );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	const sal_Int16 nAttrCount = xAttribs.is() ? xAttribs->getLength() : 0;
148*cdf0e10cSrcweir 	sal_Int16 i;
149*cdf0e10cSrcweir 	for(i=0; i < nAttrCount; i++)
150*cdf0e10cSrcweir 	{
151*cdf0e10cSrcweir 		if( xAttribs->getNameByIndex( i ) == aName )
152*cdf0e10cSrcweir 			return xAttribs->getValueByIndex( i );
153*cdf0e10cSrcweir 	}
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 	OUString aMessage( RTL_CONSTASCII_USTRINGPARAM( "missing required attribute: ") );
156*cdf0e10cSrcweir 	aMessage += aName;
157*cdf0e10cSrcweir 	errorThrow( aMessage );
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	return OUString();
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir void SAL_CALL ConfigHandler::startDocument(void) throw( SAXException, RuntimeException )
163*cdf0e10cSrcweir {
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir void SAL_CALL ConfigHandler::endDocument(void) throw( SAXException, RuntimeException )
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir void SAL_CALL ConfigHandler::startElement(const OUString& aName, const Reference< XAttributeList > & xAttribs) throw( SAXException, RuntimeException )
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir 	ElementConfigPtr pElement;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 	if( aName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "config" ) ) )
175*cdf0e10cSrcweir 	{
176*cdf0e10cSrcweir 		return;
177*cdf0e10cSrcweir 	}
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	if( aName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "container" ) ) )
180*cdf0e10cSrcweir 	{
181*cdf0e10cSrcweir 		pElement = importAtomConfig( xAttribs, true );
182*cdf0e10cSrcweir 	}
183*cdf0e10cSrcweir 	else if( aName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "atom" ) ) )
184*cdf0e10cSrcweir 	{
185*cdf0e10cSrcweir 		pElement = importAtomConfig( xAttribs, false );
186*cdf0e10cSrcweir 	}
187*cdf0e10cSrcweir 	else if( aName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "element" ) ) )
188*cdf0e10cSrcweir 	{
189*cdf0e10cSrcweir 		pElement = importElementConfig( xAttribs );
190*cdf0e10cSrcweir 	}
191*cdf0e10cSrcweir 	else if( aName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "value" ) ) )
192*cdf0e10cSrcweir 	{
193*cdf0e10cSrcweir 		pElement = importValueElementConfig( xAttribs );
194*cdf0e10cSrcweir 	}
195*cdf0e10cSrcweir 	else if( aName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "switch" ) ) )
196*cdf0e10cSrcweir 	{
197*cdf0e10cSrcweir 		pElement = importSwitchConfig( xAttribs );
198*cdf0e10cSrcweir 	}
199*cdf0e10cSrcweir 	else if( aName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "case" ) ) )
200*cdf0e10cSrcweir 	{
201*cdf0e10cSrcweir 		pElement = importCaseConfig( xAttribs );
202*cdf0e10cSrcweir 	}
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 	if( !pElement.get() )
205*cdf0e10cSrcweir 	{
206*cdf0e10cSrcweir 		OUString aMessage( OUString( RTL_CONSTASCII_USTRINGPARAM("unknown config element: ")) );
207*cdf0e10cSrcweir 		aMessage += aName;
208*cdf0e10cSrcweir 		errorThrow( aMessage  );
209*cdf0e10cSrcweir 	}
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 	maElementStack.push( pElement );
212*cdf0e10cSrcweir }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir sal_Int32 toInt( const OUString& rText )
215*cdf0e10cSrcweir {
216*cdf0e10cSrcweir 	if( rText.compareToAscii("0x",2) == 0)
217*cdf0e10cSrcweir 	{
218*cdf0e10cSrcweir 		sal_Int32 nValue = 0;
219*cdf0e10cSrcweir 		const sal_Unicode *p = rText;
220*cdf0e10cSrcweir 		p += 2;
221*cdf0e10cSrcweir 		sal_Int32 nLength = rText.getLength() - 2;
222*cdf0e10cSrcweir 		while( (nLength--) > 0 )
223*cdf0e10cSrcweir 		{
224*cdf0e10cSrcweir 			nValue <<= 4;
225*cdf0e10cSrcweir 			if( *p >= '0' && *p <= '9' )
226*cdf0e10cSrcweir 			{
227*cdf0e10cSrcweir 				nValue += *p - '0';
228*cdf0e10cSrcweir 			}
229*cdf0e10cSrcweir 			else if( *p >= 'a' && *p <= 'f' )
230*cdf0e10cSrcweir 			{
231*cdf0e10cSrcweir 				nValue += *p - ('a' - 10);
232*cdf0e10cSrcweir 			}
233*cdf0e10cSrcweir 			else if( *p >= 'A' && *p <= 'F' )
234*cdf0e10cSrcweir 			{
235*cdf0e10cSrcweir 				nValue += *p - ('A' - 10 );
236*cdf0e10cSrcweir 			}
237*cdf0e10cSrcweir 			p++;
238*cdf0e10cSrcweir 		}
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 		return nValue;
241*cdf0e10cSrcweir 	}
242*cdf0e10cSrcweir 	else
243*cdf0e10cSrcweir 	{
244*cdf0e10cSrcweir 		return rText.toInt32();
245*cdf0e10cSrcweir 	}
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir ElementConfigPtr ConfigHandler::importAtomConfig( const Reference< XAttributeList > & xAttribs, bool bIsContainer ) throw (SAXException)
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir 	if( !maElementStack.empty() )
251*cdf0e10cSrcweir 		errorThrow( OUString( RTL_CONSTASCII_USTRINGPARAM("atom elements must be root" ) ) );
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir 	ElementConfigPtr aPtr( new AtomConfig( getAttribute(xAttribs,"name"), bIsContainer ) );
254*cdf0e10cSrcweir 	gAtomConfigMap[ (UINT16)toInt(getAttribute(xAttribs,"id"))] = aPtr;
255*cdf0e10cSrcweir 	return aPtr;
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir ElementConfigPtr ConfigHandler::importElementConfig( const Reference< XAttributeList > & xAttribs ) throw (SAXException)
259*cdf0e10cSrcweir {
260*cdf0e10cSrcweir 	ElementConfigType nType = parseType( getAttribute( xAttribs, "type" ) );
261*cdf0e10cSrcweir 	ElementConfigPtr pElementConfig( new ElementConfigContainer( getAttribute( xAttribs, "name" ), nType ) );
262*cdf0e10cSrcweir 	addElement( pElementConfig );
263*cdf0e10cSrcweir 	return pElementConfig;
264*cdf0e10cSrcweir }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir ElementConfigPtr ConfigHandler::importValueElementConfig( const Reference< XAttributeList > & xAttribs ) throw (SAXException)
267*cdf0e10cSrcweir {
268*cdf0e10cSrcweir 	ElementConfigPtr pElementConfig( new ElementValueConfig( getAttribute( xAttribs, "name" ), getAttribute( xAttribs, "value" ) ) );
269*cdf0e10cSrcweir 	addElement( pElementConfig );
270*cdf0e10cSrcweir 	return pElementConfig;
271*cdf0e10cSrcweir }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir ElementConfigPtr ConfigHandler::importSwitchConfig( const Reference< XAttributeList > & xAttribs ) throw (SAXException)
274*cdf0e10cSrcweir {
275*cdf0e10cSrcweir 	ElementConfigType nType = parseType( getAttribute( xAttribs, "type" ) );
276*cdf0e10cSrcweir 	ElementConfigPtr pElementConfig( new SwitchElementConfig( nType ) );
277*cdf0e10cSrcweir 	addElement( pElementConfig );
278*cdf0e10cSrcweir 	return pElementConfig;
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir ElementConfigPtr ConfigHandler::importCaseConfig( const Reference< XAttributeList > & xAttribs ) throw (SAXException)
282*cdf0e10cSrcweir {
283*cdf0e10cSrcweir 	ElementConfigPtr pElementConfig( new CaseElementConfig( getAttribute( xAttribs, "value" ) ) );
284*cdf0e10cSrcweir 	addElement( pElementConfig );
285*cdf0e10cSrcweir 	return pElementConfig;
286*cdf0e10cSrcweir }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir void SAL_CALL ConfigHandler::endElement(const OUString& aName) throw( SAXException, RuntimeException )
289*cdf0e10cSrcweir {
290*cdf0e10cSrcweir 	if( aName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "config" ) ) )
291*cdf0e10cSrcweir 	{
292*cdf0e10cSrcweir 		return;
293*cdf0e10cSrcweir 	}
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 	maElementStack.pop();
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir void SAL_CALL ConfigHandler::characters(const OUString& aChars) throw( SAXException, RuntimeException )
299*cdf0e10cSrcweir {
300*cdf0e10cSrcweir }
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir void SAL_CALL ConfigHandler::ignorableWhitespace(const OUString& aWhitespaces) throw( SAXException, RuntimeException )
303*cdf0e10cSrcweir {
304*cdf0e10cSrcweir }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir void SAL_CALL ConfigHandler::processingInstruction(const OUString& aTarget, const OUString& aData) throw( SAXException, RuntimeException )
307*cdf0e10cSrcweir {
308*cdf0e10cSrcweir }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir void SAL_CALL ConfigHandler::setDocumentLocator(const Reference< XLocator > & xLocator) throw( SAXException, RuntimeException )
311*cdf0e10cSrcweir {
312*cdf0e10cSrcweir }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir void load_config( const OUString& rPath )
315*cdf0e10cSrcweir {
316*cdf0e10cSrcweir 	try
317*cdf0e10cSrcweir 	{
318*cdf0e10cSrcweir 		// create stream
319*cdf0e10cSrcweir 		SvStream*	pIStm = ::utl::UcbStreamHelper::CreateStream( rPath, STREAM_READ );
320*cdf0e10cSrcweir 		Reference<XInputStream> xInputStream( new utl::OInputStreamWrapper( pIStm, sal_True ) );
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir 		// prepare ParserInputSrouce
323*cdf0e10cSrcweir 		InputSource aParserInput;
324*cdf0e10cSrcweir 		aParserInput.sSystemId = rPath;
325*cdf0e10cSrcweir 		aParserInput.aInputStream = xInputStream;
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir 		// get parser
328*cdf0e10cSrcweir 		Reference< XParser > xParser(
329*cdf0e10cSrcweir 			comphelper::getProcessServiceFactory()->createInstance(
330*cdf0e10cSrcweir 				OUString::createFromAscii("com.sun.star.xml.sax.Parser") ),
331*cdf0e10cSrcweir 			UNO_QUERY_THROW );
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir 		// get filter
334*cdf0e10cSrcweir 		ConfigHandler* pConfigHandler = new ConfigHandler();
335*cdf0e10cSrcweir 		Reference< XDocumentHandler > xFilter( pConfigHandler );
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir 		// connect parser and filter
338*cdf0e10cSrcweir 		xParser->setDocumentHandler( xFilter );
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir 		// finally, parser the stream
341*cdf0e10cSrcweir 		xParser->parseStream( aParserInput );
342*cdf0e10cSrcweir 	}
343*cdf0e10cSrcweir 	catch( Exception& r )
344*cdf0e10cSrcweir 	{
345*cdf0e10cSrcweir 		DBG_ERROR(
346*cdf0e10cSrcweir 			(rtl::OString("load_config(), "
347*cdf0e10cSrcweir                      "exception caught: ") +
348*cdf0e10cSrcweir              rtl::OUStringToOString(
349*cdf0e10cSrcweir                  comphelper::anyToString( cppu::getCaughtException() ),
350*cdf0e10cSrcweir                  RTL_TEXTENCODING_UTF8 )).getStr() );
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir 		(void)r;
353*cdf0e10cSrcweir 	}
354*cdf0e10cSrcweir }
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir rtl::OUString ElementConfig::format( SvStream& rStream, sal_Size& nLength ) const
359*cdf0e10cSrcweir {
360*cdf0e10cSrcweir 	OUString aRet;
361*cdf0e10cSrcweir 	if( maName.getLength() )
362*cdf0e10cSrcweir 	{
363*cdf0e10cSrcweir 		aRet += maName;
364*cdf0e10cSrcweir 		aRet += OUString( RTL_CONSTASCII_USTRINGPARAM( " = " ) );
365*cdf0e10cSrcweir 	}
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir 	switch( mnType )
368*cdf0e10cSrcweir 	{
369*cdf0e10cSrcweir 	case ECT_BYTE:		aRet += dump_byte( rStream, nLength ); break;
370*cdf0e10cSrcweir 	case ECT_UINT:		aRet += dump_uint( rStream, nLength ); break;
371*cdf0e10cSrcweir 	case ECT_UNISTRING:	aRet += dump_unistring( rStream, nLength ); break;
372*cdf0e10cSrcweir 	case ETC_FLOAT:		aRet += dump_float( rStream, nLength ); break;
373*cdf0e10cSrcweir 	case ECT_HEXDUMP:
374*cdf0e10cSrcweir 	default:			aRet += dump_hex( rStream, nLength ); break;
375*cdf0e10cSrcweir 	}
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir 	return aRet;
378*cdf0e10cSrcweir }
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir rtl::OUString ElementConfig::dump_hex( SvStream& rStream, sal_Size& nLength )
381*cdf0e10cSrcweir {
382*cdf0e10cSrcweir 	char buffer[128];
383*cdf0e10cSrcweir 	OUString aOut, aEmpty;
384*cdf0e10cSrcweir 	OUString aHex, aAscii;
385*cdf0e10cSrcweir 	sal_Char c;
386*cdf0e10cSrcweir 	int nRow = 0;
387*cdf0e10cSrcweir 	while( nLength && (rStream.GetError() == 0) )
388*cdf0e10cSrcweir 	{
389*cdf0e10cSrcweir 		rStream >> c;
390*cdf0e10cSrcweir 		nLength--;
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir 		unsigned int i = c;
393*cdf0e10cSrcweir 		i &= 0xff;
394*cdf0e10cSrcweir 		sprintf( buffer, "%02x ", i );
395*cdf0e10cSrcweir 		aHex += OUString::createFromAscii( buffer );
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir 		if( !isprint( c ) )
398*cdf0e10cSrcweir 			c = '.';
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir 		aAscii += OUString( (sal_Unicode) c );
401*cdf0e10cSrcweir 		nRow++;
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir 		if( (nRow == 16) || (nLength==0) )
404*cdf0e10cSrcweir 		{
405*cdf0e10cSrcweir 			while( aHex.getLength() < (16*3) )
406*cdf0e10cSrcweir 				aHex += OUString( RTL_CONSTASCII_USTRINGPARAM(" ") );
407*cdf0e10cSrcweir 			aOut += aHex;
408*cdf0e10cSrcweir 			aOut += aAscii;
409*cdf0e10cSrcweir 			aOut += OUString( RTL_CONSTASCII_USTRINGPARAM( "\n\r" ) );
410*cdf0e10cSrcweir 			aHex = aEmpty;
411*cdf0e10cSrcweir 			aAscii = aEmpty;
412*cdf0e10cSrcweir 			nRow = 0;
413*cdf0e10cSrcweir 		}
414*cdf0e10cSrcweir 	}
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir 	aOut += aHex;
417*cdf0e10cSrcweir 	aOut += aAscii;
418*cdf0e10cSrcweir 
419*cdf0e10cSrcweir 	return aOut;
420*cdf0e10cSrcweir }
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir rtl::OUString ElementConfig::dump_byte( SvStream& rStream, sal_Size& nLength )
423*cdf0e10cSrcweir {
424*cdf0e10cSrcweir 	OUString aRet;
425*cdf0e10cSrcweir 	if( nLength >= sizeof(sal_Char) )
426*cdf0e10cSrcweir 	{
427*cdf0e10cSrcweir 		sal_Char c;
428*cdf0e10cSrcweir 		rStream >> c;
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir 		char buffer[128];
431*cdf0e10cSrcweir 		sprintf( buffer, "%u", (int)c );
432*cdf0e10cSrcweir 		aRet += OUString::createFromAscii( buffer );
433*cdf0e10cSrcweir 		nLength -= sizeof(sal_Char);
434*cdf0e10cSrcweir 	}
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir 	return aRet;
437*cdf0e10cSrcweir }
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir rtl::OUString ElementConfig::dump_uint( SvStream& rStream, sal_Size& nLength )
440*cdf0e10cSrcweir {
441*cdf0e10cSrcweir 	OUString aRet;
442*cdf0e10cSrcweir 	if( nLength >= sizeof( sal_uInt32 ) )
443*cdf0e10cSrcweir 	{
444*cdf0e10cSrcweir 		sal_uInt32 c;
445*cdf0e10cSrcweir 		rStream >> c;
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir 		char buffer[128];
448*cdf0e10cSrcweir 		sprintf( buffer, "%u", c );
449*cdf0e10cSrcweir 		aRet += OUString::createFromAscii( buffer );
450*cdf0e10cSrcweir 		nLength-= sizeof( sal_uInt32 );
451*cdf0e10cSrcweir 	}
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir 	return aRet;
454*cdf0e10cSrcweir }
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir rtl::OUString ElementConfig::dump_unistring( SvStream& rStream, sal_Size& nLength )
457*cdf0e10cSrcweir {
458*cdf0e10cSrcweir 	String aString;
459*cdf0e10cSrcweir 	SvxMSDffManager::MSDFFReadZString( rStream, aString, nLength, sal_True );
460*cdf0e10cSrcweir 	nLength = 0;
461*cdf0e10cSrcweir 	return aString;
462*cdf0e10cSrcweir }
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir rtl::OUString ElementConfig::dump_float( SvStream& rStream, sal_Size& nLength )
465*cdf0e10cSrcweir {
466*cdf0e10cSrcweir 	OUString aRet;
467*cdf0e10cSrcweir 	if( nLength >= sizeof( float ) )
468*cdf0e10cSrcweir 	{
469*cdf0e10cSrcweir 		float c;
470*cdf0e10cSrcweir 		rStream >> c;
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir 		char buffer[128];
473*cdf0e10cSrcweir 		sprintf( buffer, "%g", (double)c );
474*cdf0e10cSrcweir 		aRet += OUString::createFromAscii( buffer );
475*cdf0e10cSrcweir 		nLength-= sizeof( float );
476*cdf0e10cSrcweir 	}
477*cdf0e10cSrcweir 
478*cdf0e10cSrcweir 	return aRet;
479*cdf0e10cSrcweir }
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////
482*cdf0e10cSrcweir 
483*cdf0e10cSrcweir rtl::OUString ElementConfigContainer::format( SvStream& rStream, sal_Size& nLength ) const
484*cdf0e10cSrcweir {
485*cdf0e10cSrcweir 	OUString aRet;
486*cdf0e10cSrcweir 
487*cdf0e10cSrcweir 	if( getType() == ETC_CONTAINER )
488*cdf0e10cSrcweir 	{
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir 		ElementConfigList::const_iterator aIter( maElementConfigList.begin() );
491*cdf0e10cSrcweir 		const ElementConfigList::const_iterator aEnd( maElementConfigList.end() );
492*cdf0e10cSrcweir 		while( (aIter != aEnd) && (nLength > 0) )
493*cdf0e10cSrcweir 		{
494*cdf0e10cSrcweir 			aRet += (*aIter++)->format( rStream, nLength );
495*cdf0e10cSrcweir 			if( (aIter != aEnd) || (nLength != 0) )
496*cdf0e10cSrcweir 				aRet += OUString( RTL_CONSTASCII_USTRINGPARAM( "\n\r" ) );
497*cdf0e10cSrcweir 		}
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir 		if( nLength )
500*cdf0e10cSrcweir 			aRet += ElementConfig::dump_hex( rStream, nLength );
501*cdf0e10cSrcweir 	}
502*cdf0e10cSrcweir 	else
503*cdf0e10cSrcweir 	{
504*cdf0e10cSrcweir 		aRet = getName();
505*cdf0e10cSrcweir 		if( aRet.getLength() )
506*cdf0e10cSrcweir 			aRet += OUString( RTL_CONSTASCII_USTRINGPARAM( " = " ) );
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir 		OUString aValue;
509*cdf0e10cSrcweir 		switch( getType() )
510*cdf0e10cSrcweir 		{
511*cdf0e10cSrcweir 		case ECT_BYTE:		aValue = dump_byte( rStream, nLength ); break;
512*cdf0e10cSrcweir 		case ECT_UINT:		aValue = dump_uint( rStream, nLength ); break;
513*cdf0e10cSrcweir 		case ECT_UNISTRING:	aValue = dump_unistring( rStream, nLength ); break;
514*cdf0e10cSrcweir 		case ETC_FLOAT:		aValue = dump_float( rStream, nLength ); break;
515*cdf0e10cSrcweir 		case ECT_HEXDUMP:
516*cdf0e10cSrcweir 		default:			aValue = dump_hex( rStream, nLength ); break;
517*cdf0e10cSrcweir 		}
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir 		if( aValue.getLength() )
520*cdf0e10cSrcweir 		{
521*cdf0e10cSrcweir 			if( !maElementConfigList.empty() )
522*cdf0e10cSrcweir 			{
523*cdf0e10cSrcweir 				ElementConfigList::const_iterator aIter( maElementConfigList.begin() );
524*cdf0e10cSrcweir 				const ElementConfigList::const_iterator aEnd( maElementConfigList.end() );
525*cdf0e10cSrcweir 				while( (aIter != aEnd) && (nLength > 0) )
526*cdf0e10cSrcweir 				{
527*cdf0e10cSrcweir 					ElementValueConfig* pValue = dynamic_cast< ElementValueConfig* >( (*aIter++).get() );
528*cdf0e10cSrcweir 					if( pValue && pValue->getValue() == aValue )
529*cdf0e10cSrcweir 					{
530*cdf0e10cSrcweir 						aValue = pValue->getName();
531*cdf0e10cSrcweir 						break;
532*cdf0e10cSrcweir 					}
533*cdf0e10cSrcweir 				}
534*cdf0e10cSrcweir 			}
535*cdf0e10cSrcweir 		}
536*cdf0e10cSrcweir 		else
537*cdf0e10cSrcweir 		{
538*cdf0e10cSrcweir 			aValue = OUString( RTL_CONSTASCII_USTRINGPARAM("<empty!?>") );
539*cdf0e10cSrcweir 		}
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir 		aRet += aValue;
542*cdf0e10cSrcweir 	}
543*cdf0e10cSrcweir 
544*cdf0e10cSrcweir 	return aRet;
545*cdf0e10cSrcweir }
546*cdf0e10cSrcweir 
547*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir rtl::OUString SwitchElementConfig::format( SvStream& rStream, sal_Size& nLength ) const
550*cdf0e10cSrcweir {
551*cdf0e10cSrcweir 	OUString aValue;
552*cdf0e10cSrcweir 
553*cdf0e10cSrcweir 	switch( getType() )
554*cdf0e10cSrcweir 	{
555*cdf0e10cSrcweir 	case ECT_BYTE:			aValue = dump_byte( rStream, nLength );	break;
556*cdf0e10cSrcweir 	case ECT_UINT:			aValue = dump_uint( rStream, nLength ); break;
557*cdf0e10cSrcweir 	case ETC_FLOAT:			aValue = dump_float( rStream, nLength ); break;
558*cdf0e10cSrcweir 	case ECT_UNISTRING:		aValue = dump_unistring( rStream, nLength ); break;
559*cdf0e10cSrcweir 	}
560*cdf0e10cSrcweir 
561*cdf0e10cSrcweir 	if( aValue.getLength()  )
562*cdf0e10cSrcweir 	{
563*cdf0e10cSrcweir 		ElementConfigList::const_iterator aIter( maElementConfigList.begin() );
564*cdf0e10cSrcweir 		const ElementConfigList::const_iterator aEnd( maElementConfigList.end() );
565*cdf0e10cSrcweir 		while( (aIter != aEnd) && (nLength > 0) )
566*cdf0e10cSrcweir 		{
567*cdf0e10cSrcweir 			CaseElementConfig* pCase = dynamic_cast< CaseElementConfig* >( (*aIter++).get() );
568*cdf0e10cSrcweir 			if( pCase && pCase->getValue() == aValue )
569*cdf0e10cSrcweir 				return pCase->format( rStream, nLength );
570*cdf0e10cSrcweir 		}
571*cdf0e10cSrcweir 	}
572*cdf0e10cSrcweir 
573*cdf0e10cSrcweir 	return ElementConfig::dump_hex( rStream, nLength );
574*cdf0e10cSrcweir }
575*cdf0e10cSrcweir 
576