1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <xmlscript/xmllib_imexp.hxx>
25 
26 #include <cppuhelper/implbase1.hxx>
27 
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/container/XNameContainer.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 
32 #include <com/sun/star/awt/XControlModel.hpp>
33 #include <com/sun/star/awt/FontDescriptor.hpp>
34 
35 #include <com/sun/star/xml/input/XRoot.hpp>
36 
37 #include <vector>
38 
39 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
40 
41 
42 using namespace ::rtl;
43 using namespace ::std;
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 
47 namespace xmlscript
48 {
49 //
toInt32(OUString const & rStr)50 inline sal_Int32 toInt32( OUString const & rStr ) SAL_THROW( () )
51 {
52     sal_Int32 nVal;
53     if (rStr.getLength() > 2 && rStr[ 0 ] == '0' && rStr[ 1 ] == 'x')
54     {
55         nVal = rStr.copy( 2 ).toInt32( 16 );
56     }
57     else
58     {
59         nVal = rStr.toInt32();
60     }
61     return nVal;
62 }
getBoolAttr(sal_Bool * pRet,OUString const & rAttrName,Reference<xml::input::XAttributes> const & xAttributes,sal_Int32 uid)63 inline bool getBoolAttr(
64     sal_Bool * pRet, OUString const & rAttrName,
65     Reference< xml::input::XAttributes > const & xAttributes, sal_Int32 uid )
66 {
67     OUString aValue(
68         xAttributes->getValueByUidName( uid, rAttrName ) );
69     if (aValue.getLength())
70     {
71         if (aValue.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("true") ))
72         {
73             *pRet = sal_True;
74             return true;
75         }
76         else if (aValue.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("false") ))
77         {
78             *pRet = sal_False;
79             return true;
80         }
81         else
82         {
83             throw xml::sax::SAXException(
84                 rAttrName +
85                 OUString( RTL_CONSTASCII_USTRINGPARAM(
86                               ": no boolean value (true|false)!") ),
87                 Reference< XInterface >(), Any() );
88         }
89     }
90     return false;
91 }
92 
getStringAttr(OUString * pRet,OUString const & rAttrName,Reference<xml::input::XAttributes> const & xAttributes,sal_Int32 uid)93 inline bool getStringAttr(
94     OUString * pRet, OUString const & rAttrName,
95     Reference< xml::input::XAttributes > const & xAttributes, sal_Int32 uid )
96 {
97     *pRet = xAttributes->getValueByUidName( uid, rAttrName );
98     return (pRet->getLength() > 0);
99 }
100 
getLongAttr(sal_Int32 * pRet,OUString const & rAttrName,Reference<xml::input::XAttributes> const & xAttributes,sal_Int32 uid)101 inline bool getLongAttr(
102     sal_Int32 * pRet, OUString const & rAttrName,
103     Reference< xml::input::XAttributes > const & xAttributes,
104     sal_Int32 uid )
105 {
106     OUString aValue(
107         xAttributes->getValueByUidName( uid, rAttrName ) );
108     if (aValue.getLength())
109     {
110         *pRet = toInt32( aValue );
111         return true;
112     }
113     return false;
114 }
115 
116 //==================================================================================================
117 // Library import
118 
119 //==================================================================================================
120 struct LibraryImport
121     : public ::cppu::WeakImplHelper1< xml::input::XRoot >
122 {
123     friend class LibrariesElement;
124     friend class LibraryElement;
125 
126     LibDescriptorArray* mpLibArray;
127     LibDescriptor*      mpLibDesc;      // Single library mode
128 
129     sal_Int32 XMLNS_LIBRARY_UID;
130     sal_Int32 XMLNS_XLINK_UID;
131 
132 public:
133     inline LibraryImport( LibDescriptorArray* pLibArray )
134         SAL_THROW( () )
135         : mpLibArray( pLibArray )
136         , mpLibDesc( NULL ) {}
137     // Single library mode
138     inline LibraryImport( LibDescriptor* pLibDesc )
139         SAL_THROW( () )
140         : mpLibArray( NULL )
141         , mpLibDesc( pLibDesc ) {}
142     virtual ~LibraryImport()
143         SAL_THROW( () );
144 
145     // XRoot
146     virtual void SAL_CALL startDocument(
147         Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
148         throw (xml::sax::SAXException, RuntimeException);
149     virtual void SAL_CALL endDocument()
150         throw (xml::sax::SAXException, RuntimeException);
151     virtual void SAL_CALL processingInstruction(
152         OUString const & rTarget, OUString const & rData )
153         throw (xml::sax::SAXException, RuntimeException);
154     virtual void SAL_CALL setDocumentLocator(
155         Reference< xml::sax::XLocator > const & xLocator )
156         throw (xml::sax::SAXException, RuntimeException);
157     virtual Reference< xml::input::XElement > SAL_CALL startRootElement(
158         sal_Int32 nUid, OUString const & rLocalName,
159         Reference< xml::input::XAttributes > const & xAttributes )
160         throw (xml::sax::SAXException, RuntimeException);
161 };
162 
163 //==================================================================================================
164 class LibElementBase
165     : public ::cppu::WeakImplHelper1< xml::input::XElement >
166 {
167 protected:
168     LibraryImport * _pImport;
169     LibElementBase * _pParent;
170 
171     OUString _aLocalName;
172     Reference< xml::input::XAttributes > _xAttributes;
173 
174 public:
175     LibElementBase(
176         OUString const & rLocalName,
177         Reference< xml::input::XAttributes > const & xAttributes,
178         LibElementBase * pParent, LibraryImport * pImport )
179         SAL_THROW( () );
180     virtual ~LibElementBase()
181         SAL_THROW( () );
182 
183     // XElement
184     virtual Reference< xml::input::XElement > SAL_CALL getParent()
185         throw (RuntimeException);
186     virtual OUString SAL_CALL getLocalName()
187         throw (RuntimeException);
188     virtual sal_Int32 SAL_CALL getUid()
189         throw (RuntimeException);
190     virtual Reference< xml::input::XAttributes > SAL_CALL getAttributes()
191         throw (RuntimeException);
192     virtual void SAL_CALL ignorableWhitespace(
193         OUString const & rWhitespaces )
194         throw (xml::sax::SAXException, RuntimeException);
195     virtual void SAL_CALL characters( OUString const & rChars )
196         throw (xml::sax::SAXException, RuntimeException);
197     virtual void SAL_CALL processingInstruction(
198         OUString const & rTarget, OUString const & rData )
199         throw (xml::sax::SAXException, RuntimeException);
200     virtual void SAL_CALL endElement()
201         throw (xml::sax::SAXException, RuntimeException);
202     virtual Reference< xml::input::XElement > SAL_CALL startChildElement(
203         sal_Int32 nUid, OUString const & rLocalName,
204         Reference< xml::input::XAttributes > const & xAttributes )
205         throw (xml::sax::SAXException, RuntimeException);
206 };
207 
208 //==================================================================================================
209 
210 class LibrariesElement : public LibElementBase
211 {
212     friend class LibraryElement;
213 
214 protected:
215     vector< LibDescriptor > mLibDescriptors;
216 
217 public:
218     virtual Reference< xml::input::XElement > SAL_CALL startChildElement(
219         sal_Int32 nUid, OUString const & rLocalName,
220         Reference< xml::input::XAttributes > const & xAttributes )
221         throw (xml::sax::SAXException, RuntimeException);
222     virtual void SAL_CALL endElement()
223         throw (xml::sax::SAXException, RuntimeException);
224 
LibrariesElement(OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes,LibElementBase * pParent,LibraryImport * pImport)225     LibrariesElement(
226         OUString const & rLocalName,
227         Reference< xml::input::XAttributes > const & xAttributes,
228         LibElementBase * pParent, LibraryImport * pImport )
229         SAL_THROW( () )
230         : LibElementBase( rLocalName, xAttributes, pParent, pImport )
231         {}
232 };
233 
234 //==================================================================================================
235 
236 class LibraryElement : public LibElementBase
237 {
238 protected:
239     vector< OUString > mElements;
240 
241 public:
242 
243     virtual Reference< xml::input::XElement > SAL_CALL startChildElement(
244         sal_Int32 nUid, OUString const & rLocalName,
245         Reference< xml::input::XAttributes > const & xAttributes )
246         throw (xml::sax::SAXException, RuntimeException);
247     virtual void SAL_CALL endElement()
248         throw (xml::sax::SAXException, RuntimeException);
249 
LibraryElement(OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes,LibElementBase * pParent,LibraryImport * pImport)250     LibraryElement(
251         OUString const & rLocalName,
252         Reference< xml::input::XAttributes > const & xAttributes,
253         LibElementBase * pParent, LibraryImport * pImport )
254         SAL_THROW( () )
255         : LibElementBase( rLocalName, xAttributes, pParent, pImport )
256     {}
257 };
258 
259 }
260