xref: /trunk/main/xmlscript/source/xmllib_imexp/imp_share.hxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
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 //
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 }
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 
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 
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     virtual void SAL_CALL endDocument();
149     virtual void SAL_CALL processingInstruction(
150         OUString const & rTarget, OUString const & rData );
151     virtual void SAL_CALL setDocumentLocator(
152         Reference< xml::sax::XLocator > const & xLocator );
153     virtual Reference< xml::input::XElement > SAL_CALL startRootElement(
154         sal_Int32 nUid, OUString const & rLocalName,
155         Reference< xml::input::XAttributes > const & xAttributes );
156 };
157 
158 //==================================================================================================
159 class LibElementBase
160     : public ::cppu::WeakImplHelper1< xml::input::XElement >
161 {
162 protected:
163     LibraryImport * _pImport;
164     LibElementBase * _pParent;
165 
166     OUString _aLocalName;
167     Reference< xml::input::XAttributes > _xAttributes;
168 
169 public:
170     LibElementBase(
171         OUString const & rLocalName,
172         Reference< xml::input::XAttributes > const & xAttributes,
173         LibElementBase * pParent, LibraryImport * pImport )
174         SAL_THROW( () );
175     virtual ~LibElementBase()
176         SAL_THROW( () );
177 
178     // XElement
179     virtual Reference< xml::input::XElement > SAL_CALL getParent();
180     virtual OUString SAL_CALL getLocalName();
181     virtual sal_Int32 SAL_CALL getUid();
182     virtual Reference< xml::input::XAttributes > SAL_CALL getAttributes();
183     virtual void SAL_CALL ignorableWhitespace(
184         OUString const & rWhitespaces );
185     virtual void SAL_CALL characters( OUString const & rChars );
186     virtual void SAL_CALL processingInstruction(
187         OUString const & rTarget, OUString const & rData );
188     virtual void SAL_CALL endElement();
189     virtual Reference< xml::input::XElement > SAL_CALL startChildElement(
190         sal_Int32 nUid, OUString const & rLocalName,
191         Reference< xml::input::XAttributes > const & xAttributes );
192 };
193 
194 //==================================================================================================
195 
196 class LibrariesElement : public LibElementBase
197 {
198     friend class LibraryElement;
199 
200 protected:
201     vector< LibDescriptor > mLibDescriptors;
202 
203 public:
204     virtual Reference< xml::input::XElement > SAL_CALL startChildElement(
205         sal_Int32 nUid, OUString const & rLocalName,
206         Reference< xml::input::XAttributes > const & xAttributes );
207     virtual void SAL_CALL endElement();
208 
209     LibrariesElement(
210         OUString const & rLocalName,
211         Reference< xml::input::XAttributes > const & xAttributes,
212         LibElementBase * pParent, LibraryImport * pImport )
213         SAL_THROW( () )
214         : LibElementBase( rLocalName, xAttributes, pParent, pImport )
215         {}
216 };
217 
218 //==================================================================================================
219 
220 class LibraryElement : public LibElementBase
221 {
222 protected:
223     vector< OUString > mElements;
224 
225 public:
226 
227     virtual Reference< xml::input::XElement > SAL_CALL startChildElement(
228         sal_Int32 nUid, OUString const & rLocalName,
229         Reference< xml::input::XAttributes > const & xAttributes );
230     virtual void SAL_CALL endElement();
231 
232     LibraryElement(
233         OUString const & rLocalName,
234         Reference< xml::input::XAttributes > const & xAttributes,
235         LibElementBase * pParent, LibraryImport * pImport )
236         SAL_THROW( () )
237         : LibElementBase( rLocalName, xAttributes, pParent, pImport )
238     {}
239 };
240 
241 }
242