1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <xmlscript/xmllib_imexp.hxx>
29 
30 #include <cppuhelper/implbase1.hxx>
31 
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/container/XNameContainer.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 
36 #include <com/sun/star/awt/XControlModel.hpp>
37 #include <com/sun/star/awt/FontDescriptor.hpp>
38 
39 #include <com/sun/star/xml/input/XRoot.hpp>
40 
41 #include <vector>
42 
43 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
44 
45 
46 using namespace ::rtl;
47 using namespace ::std;
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::uno;
50 
51 namespace xmlscript
52 {
53 //
54 inline sal_Int32 toInt32( OUString const & rStr ) SAL_THROW( () )
55 {
56     sal_Int32 nVal;
57     if (rStr.getLength() > 2 && rStr[ 0 ] == '0' && rStr[ 1 ] == 'x')
58     {
59         nVal = rStr.copy( 2 ).toInt32( 16 );
60     }
61     else
62     {
63         nVal = rStr.toInt32();
64     }
65     return nVal;
66 }
67 inline bool getBoolAttr(
68     sal_Bool * pRet, OUString const & rAttrName,
69     Reference< xml::input::XAttributes > const & xAttributes, sal_Int32 uid )
70 {
71     OUString aValue(
72         xAttributes->getValueByUidName( uid, rAttrName ) );
73     if (aValue.getLength())
74     {
75         if (aValue.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("true") ))
76         {
77             *pRet = sal_True;
78             return true;
79         }
80         else if (aValue.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("false") ))
81         {
82             *pRet = sal_False;
83             return true;
84         }
85         else
86         {
87             throw xml::sax::SAXException(
88                 rAttrName +
89                 OUString( RTL_CONSTASCII_USTRINGPARAM(
90                               ": no boolean value (true|false)!") ),
91                 Reference< XInterface >(), Any() );
92         }
93     }
94     return false;
95 }
96 
97 inline bool getStringAttr(
98     OUString * pRet, OUString const & rAttrName,
99     Reference< xml::input::XAttributes > const & xAttributes, sal_Int32 uid )
100 {
101     *pRet = xAttributes->getValueByUidName( uid, rAttrName );
102     return (pRet->getLength() > 0);
103 }
104 
105 inline bool getLongAttr(
106     sal_Int32 * pRet, OUString const & rAttrName,
107     Reference< xml::input::XAttributes > const & xAttributes,
108     sal_Int32 uid )
109 {
110     OUString aValue(
111         xAttributes->getValueByUidName( uid, rAttrName ) );
112     if (aValue.getLength())
113     {
114         *pRet = toInt32( aValue );
115         return true;
116     }
117     return false;
118 }
119 
120 //==================================================================================================
121 // Library import
122 
123 //==================================================================================================
124 struct LibraryImport
125     : public ::cppu::WeakImplHelper1< xml::input::XRoot >
126 {
127     friend class LibrariesElement;
128     friend class LibraryElement;
129 
130     LibDescriptorArray* mpLibArray;
131     LibDescriptor*      mpLibDesc;      // Single library mode
132 
133     sal_Int32 XMLNS_LIBRARY_UID;
134     sal_Int32 XMLNS_XLINK_UID;
135 
136 public:
137     inline LibraryImport( LibDescriptorArray* pLibArray )
138         SAL_THROW( () )
139         : mpLibArray( pLibArray )
140         , mpLibDesc( NULL ) {}
141     // Single library mode
142     inline LibraryImport( LibDescriptor* pLibDesc )
143         SAL_THROW( () )
144         : mpLibArray( NULL )
145         , mpLibDesc( pLibDesc ) {}
146     virtual ~LibraryImport()
147         SAL_THROW( () );
148 
149     // XRoot
150     virtual void SAL_CALL startDocument(
151         Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
152         throw (xml::sax::SAXException, RuntimeException);
153     virtual void SAL_CALL endDocument()
154         throw (xml::sax::SAXException, RuntimeException);
155     virtual void SAL_CALL processingInstruction(
156         OUString const & rTarget, OUString const & rData )
157         throw (xml::sax::SAXException, RuntimeException);
158     virtual void SAL_CALL setDocumentLocator(
159         Reference< xml::sax::XLocator > const & xLocator )
160         throw (xml::sax::SAXException, RuntimeException);
161     virtual Reference< xml::input::XElement > SAL_CALL startRootElement(
162         sal_Int32 nUid, OUString const & rLocalName,
163         Reference< xml::input::XAttributes > const & xAttributes )
164         throw (xml::sax::SAXException, RuntimeException);
165 };
166 
167 //==================================================================================================
168 class LibElementBase
169     : public ::cppu::WeakImplHelper1< xml::input::XElement >
170 {
171 protected:
172     LibraryImport * _pImport;
173     LibElementBase * _pParent;
174 
175     OUString _aLocalName;
176     Reference< xml::input::XAttributes > _xAttributes;
177 
178 public:
179     LibElementBase(
180         OUString const & rLocalName,
181         Reference< xml::input::XAttributes > const & xAttributes,
182         LibElementBase * pParent, LibraryImport * pImport )
183         SAL_THROW( () );
184     virtual ~LibElementBase()
185         SAL_THROW( () );
186 
187     // XElement
188     virtual Reference< xml::input::XElement > SAL_CALL getParent()
189         throw (RuntimeException);
190     virtual OUString SAL_CALL getLocalName()
191         throw (RuntimeException);
192     virtual sal_Int32 SAL_CALL getUid()
193         throw (RuntimeException);
194     virtual Reference< xml::input::XAttributes > SAL_CALL getAttributes()
195         throw (RuntimeException);
196     virtual void SAL_CALL ignorableWhitespace(
197         OUString const & rWhitespaces )
198         throw (xml::sax::SAXException, RuntimeException);
199     virtual void SAL_CALL characters( OUString const & rChars )
200         throw (xml::sax::SAXException, RuntimeException);
201     virtual void SAL_CALL processingInstruction(
202         OUString const & rTarget, OUString const & rData )
203         throw (xml::sax::SAXException, RuntimeException);
204     virtual void SAL_CALL endElement()
205         throw (xml::sax::SAXException, RuntimeException);
206     virtual Reference< xml::input::XElement > SAL_CALL startChildElement(
207         sal_Int32 nUid, OUString const & rLocalName,
208         Reference< xml::input::XAttributes > const & xAttributes )
209         throw (xml::sax::SAXException, RuntimeException);
210 };
211 
212 //==================================================================================================
213 
214 class LibrariesElement : public LibElementBase
215 {
216     friend class LibraryElement;
217 
218 protected:
219     vector< LibDescriptor > mLibDescriptors;
220 
221 public:
222     virtual Reference< xml::input::XElement > SAL_CALL startChildElement(
223         sal_Int32 nUid, OUString const & rLocalName,
224         Reference< xml::input::XAttributes > const & xAttributes )
225         throw (xml::sax::SAXException, RuntimeException);
226     virtual void SAL_CALL endElement()
227         throw (xml::sax::SAXException, RuntimeException);
228 
229     LibrariesElement(
230         OUString const & rLocalName,
231         Reference< xml::input::XAttributes > const & xAttributes,
232         LibElementBase * pParent, LibraryImport * pImport )
233         SAL_THROW( () )
234         : LibElementBase( rLocalName, xAttributes, pParent, pImport )
235         {}
236 };
237 
238 //==================================================================================================
239 
240 class LibraryElement : public LibElementBase
241 {
242 protected:
243     vector< OUString > mElements;
244 
245 public:
246 
247     virtual Reference< xml::input::XElement > SAL_CALL startChildElement(
248         sal_Int32 nUid, OUString const & rLocalName,
249         Reference< xml::input::XAttributes > const & xAttributes )
250         throw (xml::sax::SAXException, RuntimeException);
251     virtual void SAL_CALL endElement()
252         throw (xml::sax::SAXException, RuntimeException);
253 
254     LibraryElement(
255         OUString const & rLocalName,
256         Reference< xml::input::XAttributes > const & xAttributes,
257         LibElementBase * pParent, LibraryImport * pImport )
258         SAL_THROW( () )
259         : LibElementBase( rLocalName, xAttributes, pParent, pImport )
260     {}
261 };
262 
263 }
264