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 #ifndef XMLSCRIPT_XMLBAS_IMPORT_HXX
25 #define XMLSCRIPT_XMLBAS_IMPORT_HXX
26 
27 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
28 #include <com/sun/star/document/XImporter.hpp>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #ifndef _COM_SUN_STAR_SCRIPT_XLIBRYARYCONTAINER2_HPP_
32 #include <com/sun/star/script/XLibraryContainer2.hpp>
33 #endif
34 #include <com/sun/star/uno/XComponentContext.hpp>
35 #include <com/sun/star/xml/input/XRoot.hpp>
36 #include <cppuhelper/implbase1.hxx>
37 #include <cppuhelper/implbase3.hxx>
38 #include <osl/mutex.hxx>
39 #include <rtl/ustrbuf.hxx>
40 
41 
42 //.........................................................................
43 namespace xmlscript
44 {
45 //.........................................................................
46 
47     // =============================================================================
48     // class BasicElementBase
49     // =============================================================================
50 
51     class BasicImport;
52 
53     typedef ::cppu::WeakImplHelper1<
54         ::com::sun::star::xml::input::XElement > BasicElementBase_BASE;
55 
56     class BasicElementBase : public BasicElementBase_BASE
57     {
58     protected:
59         BasicImport* m_pImport;
60         BasicElementBase* m_pParent;
61         ::rtl::OUString m_aLocalName;
62         ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes > m_xAttributes;
63 
64         bool getBoolAttr( sal_Bool* pRet, const ::rtl::OUString& rAttrName,
65             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes,
66             sal_Int32 nUid );
67 
68     public:
69         BasicElementBase( const ::rtl::OUString& rLocalName,
70             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes,
71             BasicElementBase* pParent, BasicImport* pImport );
72         virtual ~BasicElementBase();
73 
74         // XElement
75         virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XElement > SAL_CALL getParent()
76             throw (::com::sun::star::uno::RuntimeException);
77         virtual ::rtl::OUString SAL_CALL getLocalName()
78             throw (::com::sun::star::uno::RuntimeException);
79         virtual sal_Int32 SAL_CALL getUid()
80             throw (::com::sun::star::uno::RuntimeException);
81         virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes > SAL_CALL getAttributes()
82             throw (::com::sun::star::uno::RuntimeException);
83         virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XElement > SAL_CALL startChildElement(
84             sal_Int32 nUid, const ::rtl::OUString& rLocalName,
85             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes )
86             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
87         virtual void SAL_CALL characters( const ::rtl::OUString& rChars )
88             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
89         virtual void SAL_CALL ignorableWhitespace(
90             const ::rtl::OUString& rWhitespaces )
91             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
92         virtual void SAL_CALL processingInstruction(
93             const ::rtl::OUString& rTarget, const ::rtl::OUString& rData )
94             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
95         virtual void SAL_CALL endElement()
96             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
97     };
98 
99 
100     // =============================================================================
101     // class BasicLibrariesElement
102     // =============================================================================
103 
104     class BasicLibrariesElement : public BasicElementBase
105     {
106     private:
107         ::com::sun::star::uno::Reference< ::com::sun::star::script::XLibraryContainer2 > m_xLibContainer;
108 
109     public:
110         BasicLibrariesElement( const ::rtl::OUString& rLocalName,
111             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes,
112             BasicElementBase* pParent, BasicImport* pImport,
113             const ::com::sun::star::uno::Reference< ::com::sun::star::script::XLibraryContainer2 >& rxLibContainer );
114 
115         // XElement
116         virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XElement > SAL_CALL startChildElement(
117             sal_Int32 nUid, const ::rtl::OUString& rLocalName,
118             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes )
119             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
120         virtual void SAL_CALL endElement()
121             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
122     };
123 
124 
125     // =============================================================================
126     // class BasicEmbeddedLibraryElement
127     // =============================================================================
128 
129     class BasicEmbeddedLibraryElement : public BasicElementBase
130     {
131     private:
132         ::com::sun::star::uno::Reference< ::com::sun::star::script::XLibraryContainer2 > m_xLibContainer;
133         ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer > m_xLib;
134         ::rtl::OUString m_aLibName;
135         bool m_bReadOnly;
136 
137     public:
138         BasicEmbeddedLibraryElement( const ::rtl::OUString& rLocalName,
139             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes,
140             BasicElementBase* pParent, BasicImport* pImport,
141             const ::com::sun::star::uno::Reference< ::com::sun::star::script::XLibraryContainer2 >& rxLibContainer,
142             const ::rtl::OUString& rLibName, bool bReadOnly );
143 
144         // XElement
145         virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XElement > SAL_CALL startChildElement(
146             sal_Int32 nUid, const ::rtl::OUString& rLocalName,
147             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes )
148             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
149         virtual void SAL_CALL endElement()
150             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
151     };
152 
153 
154     // =============================================================================
155     // class BasicModuleElement
156     // =============================================================================
157 
158     class BasicModuleElement : public BasicElementBase
159     {
160     private:
161         ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer > m_xLib;
162         ::rtl::OUString m_aName;
163 
164     public:
165         BasicModuleElement( const ::rtl::OUString& rLocalName,
166             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes,
167             BasicElementBase* pParent, BasicImport* pImport,
168             const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >& rxLib,
169             const ::rtl::OUString& rName );
170 
171         // XElement
172         virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XElement > SAL_CALL startChildElement(
173             sal_Int32 nUid, const ::rtl::OUString& rLocalName,
174             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes )
175             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
176         virtual void SAL_CALL endElement()
177             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
178     };
179 
180 
181     // =============================================================================
182     // class BasicSourceCodeElement
183     // =============================================================================
184 
185     class BasicSourceCodeElement : public BasicElementBase
186     {
187     private:
188         ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer > m_xLib;
189         ::rtl::OUString m_aName;
190         ::rtl::OUStringBuffer m_aBuffer;
191 
192     public:
193         BasicSourceCodeElement( const ::rtl::OUString& rLocalName,
194             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes,
195             BasicElementBase* pParent, BasicImport* pImport,
196             const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >& rxLib,
197             const ::rtl::OUString& rName );
198 
199         // XElement
200         virtual void SAL_CALL characters( const ::rtl::OUString& rChars )
201             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
202         virtual void SAL_CALL endElement()
203             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
204     };
205 
206 
207     // =============================================================================
208     // class BasicImport
209     // =============================================================================
210 
211     typedef ::cppu::WeakImplHelper1<
212         ::com::sun::star::xml::input::XRoot > BasicImport_BASE;
213 
214     class BasicImport : public BasicImport_BASE
215     {
216         friend class BasicElementBase;
217         friend class BasicLibrariesElement;
218         friend class BasicEmbeddedLibraryElement;
219         friend class BasicModuleElement;
220 
221     private:
222         sal_Int32 XMLNS_UID;
223         sal_Int32 XMLNS_XLINK_UID;
224         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel > m_xModel;
225         sal_Bool m_bOasis;
226 
227     public:
228         BasicImport( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >& rxModel, sal_Bool bOasis );
229         virtual ~BasicImport();
230 
231         // XRoot
232         virtual void SAL_CALL startDocument(
233             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XNamespaceMapping >& xNamespaceMapping )
234             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
235         virtual void SAL_CALL endDocument()
236             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
237         virtual void SAL_CALL processingInstruction(
238             const ::rtl::OUString& rTarget, const ::rtl::OUString& rData )
239             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
240         virtual void SAL_CALL setDocumentLocator(
241             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator >& xLocator )
242             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
243         virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XElement > SAL_CALL startRootElement(
244             sal_Int32 nUid, const ::rtl::OUString& rLocalName,
245             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes )
246             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
247     };
248 
249 
250     // =============================================================================
251     // class XMLBasicImporterBase
252     // =============================================================================
253 
254     typedef ::cppu::WeakImplHelper3<
255         ::com::sun::star::lang::XServiceInfo,
256         ::com::sun::star::document::XImporter,
257         ::com::sun::star::xml::sax::XDocumentHandler > XMLBasicImporterBase_BASE;
258 
259     class XMLBasicImporterBase : public XMLBasicImporterBase_BASE
260     {
261     private:
262         ::osl::Mutex                                                                        m_aMutex;
263         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >        m_xContext;
264         ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >    m_xHandler;
265         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >                 m_xModel;
266         sal_Bool                                                                            m_bOasis;
267 
268     public:
269         XMLBasicImporterBase(
270             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext, sal_Bool bOasis );
271         virtual ~XMLBasicImporterBase();
272 
273         // XServiceInfo
274         virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName )
275             throw (::com::sun::star::uno::RuntimeException);
276 
277         // XImporter
278         virtual void SAL_CALL setTargetDocument( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& rxDoc )
279             throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
280 
281         // XDocumentHandler
282         virtual void SAL_CALL startDocument()
283             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
284         virtual void SAL_CALL endDocument()
285             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
286         virtual void SAL_CALL startElement( const ::rtl::OUString& aName,
287             const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttribs )
288             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
289         virtual void SAL_CALL endElement( const ::rtl::OUString& aName )
290             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
291         virtual void SAL_CALL characters( const ::rtl::OUString& aChars )
292             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
293         virtual void SAL_CALL ignorableWhitespace( const ::rtl::OUString& aWhitespaces )
294             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
295         virtual void SAL_CALL processingInstruction( const ::rtl::OUString& aTarget, const ::rtl::OUString& aData )
296             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
297         virtual void SAL_CALL setDocumentLocator( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator >& xLocator )
298             throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException);
299     };
300 
301 
302     // =============================================================================
303     // class XMLBasicImporter
304     // =============================================================================
305 
306     class XMLBasicImporter : public XMLBasicImporterBase
307     {
308     public:
309         XMLBasicImporter(
310             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext );
311         virtual ~XMLBasicImporter();
312 
313         // XServiceInfo
314         virtual ::rtl::OUString SAL_CALL getImplementationName(  )
315             throw (::com::sun::star::uno::RuntimeException);
316         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  )
317             throw (::com::sun::star::uno::RuntimeException);
318     };
319 
320 
321     // =============================================================================
322     // class XMLOasisBasicImporter
323     // =============================================================================
324 
325     class XMLOasisBasicImporter : public XMLBasicImporterBase
326     {
327     public:
328         XMLOasisBasicImporter(
329             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext );
330         virtual ~XMLOasisBasicImporter();
331 
332         // XServiceInfo
333         virtual ::rtl::OUString SAL_CALL getImplementationName(  )
334             throw (::com::sun::star::uno::RuntimeException);
335         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  )
336             throw (::com::sun::star::uno::RuntimeException);
337     };
338 
339 //.........................................................................
340 }	// namespace xmlscript
341 //.........................................................................
342 
343 #endif // XMLSCRIPT_XMLBAS_IMPORT_HXX
344