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