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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlscript.hxx"
30 #include "imp_share.hxx"
31 
32 #include <osl/diagnose.h>
33 
34 #include <rtl/ustrbuf.hxx>
35 
36 #include <xmlscript/xml_import.hxx>
37 #include <comphelper/processfactory.hxx>
38 
39 
40 namespace xmlscript
41 {
42 
43 //##################################################################################################
44 
45 //__________________________________________________________________________________________________
46 Reference< xml::input::XElement > LibElementBase::getParent()
47     throw (RuntimeException)
48 {
49     return static_cast< xml::input::XElement * >( _pParent );
50 }
51 //__________________________________________________________________________________________________
52 OUString LibElementBase::getLocalName()
53     throw (RuntimeException)
54 {
55     return _aLocalName;
56 }
57 //__________________________________________________________________________________________________
58 sal_Int32 LibElementBase::getUid()
59     throw (RuntimeException)
60 {
61     return _pImport->XMLNS_LIBRARY_UID;
62 }
63 //__________________________________________________________________________________________________
64 Reference< xml::input::XAttributes > LibElementBase::getAttributes()
65     throw (RuntimeException)
66 {
67     return _xAttributes;
68 }
69 //__________________________________________________________________________________________________
70 void LibElementBase::ignorableWhitespace(
71     OUString const & /*rWhitespaces*/ )
72     throw (xml::sax::SAXException, RuntimeException)
73 {
74 }
75 //__________________________________________________________________________________________________
76 void LibElementBase::characters( OUString const & /*rChars*/ )
77     throw (xml::sax::SAXException, RuntimeException)
78 {
79     // not used, all characters ignored
80 }
81 
82 //__________________________________________________________________________________________________
83 void LibElementBase::processingInstruction(
84     OUString const & /*rTarget*/, OUString const & /*rData*/ )
85     throw (xml::sax::SAXException, RuntimeException)
86 {
87 }
88 
89 //__________________________________________________________________________________________________
90 void LibElementBase::endElement()
91     throw (xml::sax::SAXException, RuntimeException)
92 {
93 }
94 //__________________________________________________________________________________________________
95 Reference< xml::input::XElement > LibElementBase::startChildElement(
96     sal_Int32 /*nUid*/, OUString const & /*rLocalName*/,
97     Reference< xml::input::XAttributes > const & /*xAttributes*/ )
98     throw (xml::sax::SAXException, RuntimeException)
99 {
100     throw xml::sax::SAXException(
101         OUString( RTL_CONSTASCII_USTRINGPARAM("unexpected element!") ),
102         Reference< XInterface >(), Any() );
103 }
104 
105 //__________________________________________________________________________________________________
106 LibElementBase::LibElementBase(
107     OUString const & rLocalName,
108     Reference< xml::input::XAttributes > const & xAttributes,
109     LibElementBase * pParent, LibraryImport * pImport )
110     SAL_THROW( () )
111     : _pImport( pImport )
112     , _pParent( pParent )
113     , _aLocalName( rLocalName )
114     , _xAttributes( xAttributes )
115 {
116     _pImport->acquire();
117 
118     if (_pParent)
119     {
120         _pParent->acquire();
121     }
122 }
123 //__________________________________________________________________________________________________
124 LibElementBase::~LibElementBase()
125     SAL_THROW( () )
126 {
127     _pImport->release();
128 
129     if (_pParent)
130     {
131         _pParent->release();
132     }
133 
134 #if OSL_DEBUG_LEVEL > 1
135     OString aStr( OUStringToOString( _aLocalName, RTL_TEXTENCODING_ASCII_US ) );
136     OSL_TRACE( "LibElementBase::~LibElementBase(): %s\n", aStr.getStr() );
137 #endif
138 }
139 
140 //##################################################################################################
141 
142 // XRoot
143 
144 //______________________________________________________________________________
145 void LibraryImport::startDocument(
146     Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
147     throw (xml::sax::SAXException, RuntimeException)
148 {
149     XMLNS_LIBRARY_UID = xNamespaceMapping->getUidByUri(
150         OUSTR(XMLNS_LIBRARY_URI) );
151     XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri(
152         OUSTR(XMLNS_XLINK_URI) );
153 }
154 //__________________________________________________________________________________________________
155 void LibraryImport::endDocument()
156     throw (xml::sax::SAXException, RuntimeException)
157 {
158 }
159 //__________________________________________________________________________________________________
160 void LibraryImport::processingInstruction(
161     OUString const & /*rTarget*/, OUString const & /*rData*/ )
162     throw (xml::sax::SAXException, RuntimeException)
163 {
164 }
165 //__________________________________________________________________________________________________
166 void LibraryImport::setDocumentLocator(
167     Reference< xml::sax::XLocator > const & /*xLocator*/ )
168     throw (xml::sax::SAXException, RuntimeException)
169 {
170 }
171 //__________________________________________________________________________________________________
172 Reference< xml::input::XElement > LibraryImport::startRootElement(
173     sal_Int32 nUid, OUString const & rLocalName,
174     Reference< xml::input::XAttributes > const & xAttributes )
175     throw (xml::sax::SAXException, RuntimeException)
176 {
177     if (XMLNS_LIBRARY_UID != nUid)
178     {
179         throw xml::sax::SAXException(
180             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
181             Reference< XInterface >(), Any() );
182     }
183     else if (mpLibArray && rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("libraries") ))
184     {
185         return new LibrariesElement( rLocalName, xAttributes, 0, this );
186     }
187     else if (mpLibDesc && rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("library") ))
188     {
189         LibDescriptor& aDesc = *mpLibDesc;
190         aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = sal_False;
191 
192         aDesc.aName = xAttributes->getValueByUidName(
193             XMLNS_LIBRARY_UID, OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
194         getBoolAttr(
195             &aDesc.bReadOnly,
196             OUString( RTL_CONSTASCII_USTRINGPARAM("readonly") ), xAttributes,
197             XMLNS_LIBRARY_UID );
198         getBoolAttr(
199             &aDesc.bPasswordProtected,
200             OUString( RTL_CONSTASCII_USTRINGPARAM("passwordprotected") ),
201             xAttributes, XMLNS_LIBRARY_UID );
202         getBoolAttr(
203             &aDesc.bPreload,
204             OUString( RTL_CONSTASCII_USTRINGPARAM("preload") ),
205             xAttributes, XMLNS_LIBRARY_UID );
206 
207         return new LibraryElement( rLocalName, xAttributes, 0, this );
208     }
209     else
210     {
211         throw xml::sax::SAXException(
212             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal root element (expected libraries) given: ") ) +
213             rLocalName, Reference< XInterface >(), Any() );
214     }
215 }
216 //__________________________________________________________________________________________________
217 LibraryImport::~LibraryImport()
218     SAL_THROW( () )
219 {
220 #if OSL_DEBUG_LEVEL > 1
221     OSL_TRACE( "LibraryImport::~LibraryImport().\n" );
222 #endif
223 }
224 
225 //##################################################################################################
226 
227 
228 // libraries
229 //__________________________________________________________________________________________________
230 Reference< xml::input::XElement > LibrariesElement::startChildElement(
231     sal_Int32 nUid, OUString const & rLocalName,
232     Reference< xml::input::XAttributes > const & xAttributes )
233     throw (xml::sax::SAXException, RuntimeException)
234 {
235     if (_pImport->XMLNS_LIBRARY_UID != nUid)
236     {
237         throw xml::sax::SAXException(
238             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
239             Reference< XInterface >(), Any() );
240     }
241     // library
242     else if (rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("library") ))
243     {
244         LibDescriptor aDesc;
245         aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = sal_False;
246 
247         aDesc.aName = xAttributes->getValueByUidName(
248             _pImport->XMLNS_LIBRARY_UID,
249             OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
250         aDesc.aStorageURL = xAttributes->getValueByUidName(
251             _pImport->XMLNS_XLINK_UID,
252             OUString( RTL_CONSTASCII_USTRINGPARAM("href") ) );
253         getBoolAttr(
254             &aDesc.bLink,
255             OUString( RTL_CONSTASCII_USTRINGPARAM("link") ),
256             xAttributes, _pImport->XMLNS_LIBRARY_UID );
257         getBoolAttr(
258             &aDesc.bReadOnly,
259             OUString( RTL_CONSTASCII_USTRINGPARAM("readonly") ),
260             xAttributes, _pImport->XMLNS_LIBRARY_UID );
261         getBoolAttr(
262             &aDesc.bPasswordProtected,
263             OUString( RTL_CONSTASCII_USTRINGPARAM("passwordprotected") ),
264             xAttributes, _pImport->XMLNS_LIBRARY_UID );
265 
266         mLibDescriptors.push_back( aDesc );
267         return new LibraryElement( rLocalName, xAttributes, this, _pImport );
268     }
269     else
270     {
271         throw xml::sax::SAXException(
272             OUString( RTL_CONSTASCII_USTRINGPARAM("expected styles ot bulletinboard element!") ),
273             Reference< XInterface >(), Any() );
274     }
275 }
276 //__________________________________________________________________________________________________
277 void LibrariesElement::endElement()
278     throw (xml::sax::SAXException, RuntimeException)
279 {
280     sal_Int32 nLibCount = _pImport->mpLibArray->mnLibCount = (sal_Int32)mLibDescriptors.size();
281     _pImport->mpLibArray->mpLibs = new LibDescriptor[ nLibCount ];
282 
283     for( sal_Int32 i = 0 ; i < nLibCount ; i++ )
284     {
285         const LibDescriptor& rLib = mLibDescriptors[i];
286         _pImport->mpLibArray->mpLibs[i] = rLib;
287     }
288 }
289 
290 // library
291 //__________________________________________________________________________________________________
292 Reference< xml::input::XElement > LibraryElement::startChildElement(
293     sal_Int32 nUid, OUString const & rLocalName,
294     Reference< xml::input::XAttributes > const & xAttributes )
295     throw (xml::sax::SAXException, RuntimeException)
296 {
297     if (_pImport->XMLNS_LIBRARY_UID != nUid)
298     {
299         throw xml::sax::SAXException(
300             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
301             Reference< XInterface >(), Any() );
302     }
303     // library
304     else if (rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("element") ))
305     {
306         OUString aValue( xAttributes->getValueByUidName(
307             _pImport->XMLNS_LIBRARY_UID,
308             OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) ) );
309         if (aValue.getLength())
310             mElements.push_back( aValue );
311 
312         return new LibElementBase( rLocalName, xAttributes, this, _pImport );
313     }
314     else
315     {
316         throw xml::sax::SAXException(
317             OUString( RTL_CONSTASCII_USTRINGPARAM("expected styles ot bulletinboard element!") ),
318             Reference< XInterface >(), Any() );
319     }
320 }
321 //__________________________________________________________________________________________________
322 void LibraryElement::endElement()
323     throw (xml::sax::SAXException, RuntimeException)
324 {
325     sal_Int32 nElementCount = mElements.size();
326     Sequence< OUString > aElementNames( nElementCount );
327     OUString* pElementNames = aElementNames.getArray();
328     for( sal_Int32 i = 0 ; i < nElementCount ; i++ )
329         pElementNames[i] = mElements[i];
330 
331     LibDescriptor* pLib = _pImport->mpLibDesc;
332     if( !pLib )
333         pLib = &static_cast< LibrariesElement* >( _pParent )->mLibDescriptors.back();
334     pLib->aElementNames = aElementNames;
335 }
336 
337 
338 //##################################################################################################
339 
340 Reference< ::com::sun::star::xml::sax::XDocumentHandler >
341 SAL_CALL importLibraryContainer( LibDescriptorArray* pLibArray )
342         SAL_THROW( (Exception) )
343 {
344     return ::xmlscript::createDocumentHandler(
345         static_cast< xml::input::XRoot * >( new LibraryImport( pLibArray ) ) );
346 }
347 
348 //##################################################################################################
349 
350 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >
351 SAL_CALL importLibrary( LibDescriptor& rLib )
352         SAL_THROW( (::com::sun::star::uno::Exception) )
353 {
354     return ::xmlscript::createDocumentHandler(
355         static_cast< xml::input::XRoot * >( new LibraryImport( &rLib ) ) );
356 }
357 
358 
359 //##################################################################################################
360 
361 LibDescriptorArray::LibDescriptorArray( sal_Int32 nLibCount )
362 {
363     mnLibCount = nLibCount;
364     mpLibs = new LibDescriptor[ mnLibCount ];
365 }
366 
367 LibDescriptorArray::~LibDescriptorArray()
368 {
369     delete[] mpLibs;
370 }
371 
372 }
373