Bump for 3.6-28
[LibreOffice.git] / xmlscript / source / xmllib_imexp / xmllib_import.cxx
blobf04c5e3d5342c2a8012b5faf1f90611bf0c8f6bc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "imp_share.hxx"
31 #include <osl/diagnose.h>
33 #include <rtl/ustrbuf.hxx>
35 #include <xml_import.hxx>
36 #include <comphelper/processfactory.hxx>
39 namespace xmlscript
42 //##################################################################################################
44 //__________________________________________________________________________________________________
45 Reference< xml::input::XElement > LibElementBase::getParent()
46 throw (RuntimeException)
48 return static_cast< xml::input::XElement * >( _pParent );
50 //__________________________________________________________________________________________________
51 OUString LibElementBase::getLocalName()
52 throw (RuntimeException)
54 return _aLocalName;
56 //__________________________________________________________________________________________________
57 sal_Int32 LibElementBase::getUid()
58 throw (RuntimeException)
60 return _pImport->XMLNS_LIBRARY_UID;
62 //__________________________________________________________________________________________________
63 Reference< xml::input::XAttributes > LibElementBase::getAttributes()
64 throw (RuntimeException)
66 return _xAttributes;
68 //__________________________________________________________________________________________________
69 void LibElementBase::ignorableWhitespace(
70 OUString const & /*rWhitespaces*/ )
71 throw (xml::sax::SAXException, RuntimeException)
74 //__________________________________________________________________________________________________
75 void LibElementBase::characters( OUString const & /*rChars*/ )
76 throw (xml::sax::SAXException, RuntimeException)
78 // not used, all characters ignored
81 //__________________________________________________________________________________________________
82 void LibElementBase::processingInstruction(
83 OUString const & /*rTarget*/, OUString const & /*rData*/ )
84 throw (xml::sax::SAXException, RuntimeException)
88 //__________________________________________________________________________________________________
89 void LibElementBase::endElement()
90 throw (xml::sax::SAXException, RuntimeException)
93 //__________________________________________________________________________________________________
94 Reference< xml::input::XElement > LibElementBase::startChildElement(
95 sal_Int32 /*nUid*/, OUString const & /*rLocalName*/,
96 Reference< xml::input::XAttributes > const & /*xAttributes*/ )
97 throw (xml::sax::SAXException, RuntimeException)
99 throw xml::sax::SAXException(
100 OUString( RTL_CONSTASCII_USTRINGPARAM("unexpected element!") ),
101 Reference< XInterface >(), Any() );
104 //__________________________________________________________________________________________________
105 LibElementBase::LibElementBase(
106 OUString const & rLocalName,
107 Reference< xml::input::XAttributes > const & xAttributes,
108 LibElementBase * pParent, LibraryImport * pImport )
109 SAL_THROW(())
110 : _pImport( pImport )
111 , _pParent( pParent )
112 , _aLocalName( rLocalName )
113 , _xAttributes( xAttributes )
115 _pImport->acquire();
117 if (_pParent)
119 _pParent->acquire();
122 //__________________________________________________________________________________________________
123 LibElementBase::~LibElementBase()
124 SAL_THROW(())
126 _pImport->release();
128 if (_pParent)
130 _pParent->release();
133 #if OSL_DEBUG_LEVEL > 1
134 OString aStr( OUStringToOString( _aLocalName, RTL_TEXTENCODING_ASCII_US ) );
135 OSL_TRACE( "LibElementBase::~LibElementBase(): %s", aStr.getStr() );
136 #endif
139 //##################################################################################################
141 // XRoot
143 //______________________________________________________________________________
144 void LibraryImport::startDocument(
145 Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
146 throw (xml::sax::SAXException, RuntimeException)
148 XMLNS_LIBRARY_UID = xNamespaceMapping->getUidByUri(
149 OUSTR(XMLNS_LIBRARY_URI) );
150 XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri(
151 OUSTR(XMLNS_XLINK_URI) );
153 //__________________________________________________________________________________________________
154 void LibraryImport::endDocument()
155 throw (xml::sax::SAXException, RuntimeException)
158 //__________________________________________________________________________________________________
159 void LibraryImport::processingInstruction(
160 OUString const & /*rTarget*/, OUString const & /*rData*/ )
161 throw (xml::sax::SAXException, RuntimeException)
164 //__________________________________________________________________________________________________
165 void LibraryImport::setDocumentLocator(
166 Reference< xml::sax::XLocator > const & /*xLocator*/ )
167 throw (xml::sax::SAXException, RuntimeException)
170 //__________________________________________________________________________________________________
171 Reference< xml::input::XElement > LibraryImport::startRootElement(
172 sal_Int32 nUid, OUString const & rLocalName,
173 Reference< xml::input::XAttributes > const & xAttributes )
174 throw (xml::sax::SAXException, RuntimeException)
176 if (XMLNS_LIBRARY_UID != nUid)
178 throw xml::sax::SAXException(
179 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
180 Reference< XInterface >(), Any() );
182 else if ( mpLibArray && rLocalName == "libraries" )
184 return new LibrariesElement( rLocalName, xAttributes, 0, this );
186 else if ( mpLibDesc && rLocalName == "library" )
188 LibDescriptor& aDesc = *mpLibDesc;
189 aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = sal_False;
191 aDesc.aName = xAttributes->getValueByUidName(
192 XMLNS_LIBRARY_UID, OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
193 getBoolAttr(
194 &aDesc.bReadOnly,
195 OUString( RTL_CONSTASCII_USTRINGPARAM("readonly") ), xAttributes,
196 XMLNS_LIBRARY_UID );
197 getBoolAttr(
198 &aDesc.bPasswordProtected,
199 OUString( RTL_CONSTASCII_USTRINGPARAM("passwordprotected") ),
200 xAttributes, XMLNS_LIBRARY_UID );
201 getBoolAttr(
202 &aDesc.bPreload,
203 OUString( RTL_CONSTASCII_USTRINGPARAM("preload") ),
204 xAttributes, XMLNS_LIBRARY_UID );
206 return new LibraryElement( rLocalName, xAttributes, 0, this );
208 else
210 throw xml::sax::SAXException(
211 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal root element (expected libraries) given: ") ) +
212 rLocalName, Reference< XInterface >(), Any() );
215 //__________________________________________________________________________________________________
216 LibraryImport::~LibraryImport()
217 SAL_THROW(())
219 #if OSL_DEBUG_LEVEL > 1
220 OSL_TRACE( "LibraryImport::~LibraryImport()." );
221 #endif
224 //##################################################################################################
227 // libraries
228 //__________________________________________________________________________________________________
229 Reference< xml::input::XElement > LibrariesElement::startChildElement(
230 sal_Int32 nUid, OUString const & rLocalName,
231 Reference< xml::input::XAttributes > const & xAttributes )
232 throw (xml::sax::SAXException, RuntimeException)
234 if (_pImport->XMLNS_LIBRARY_UID != nUid)
236 throw xml::sax::SAXException(
237 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
238 Reference< XInterface >(), Any() );
240 // library
241 else if ( rLocalName == "library" )
243 LibDescriptor aDesc;
244 aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = sal_False;
246 aDesc.aName = xAttributes->getValueByUidName(
247 _pImport->XMLNS_LIBRARY_UID,
248 OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
249 aDesc.aStorageURL = xAttributes->getValueByUidName(
250 _pImport->XMLNS_XLINK_UID,
251 OUString( RTL_CONSTASCII_USTRINGPARAM("href") ) );
252 getBoolAttr(
253 &aDesc.bLink,
254 OUString( RTL_CONSTASCII_USTRINGPARAM("link") ),
255 xAttributes, _pImport->XMLNS_LIBRARY_UID );
256 getBoolAttr(
257 &aDesc.bReadOnly,
258 OUString( RTL_CONSTASCII_USTRINGPARAM("readonly") ),
259 xAttributes, _pImport->XMLNS_LIBRARY_UID );
260 getBoolAttr(
261 &aDesc.bPasswordProtected,
262 OUString( RTL_CONSTASCII_USTRINGPARAM("passwordprotected") ),
263 xAttributes, _pImport->XMLNS_LIBRARY_UID );
265 mLibDescriptors.push_back( aDesc );
266 return new LibraryElement( rLocalName, xAttributes, this, _pImport );
268 else
270 throw xml::sax::SAXException(
271 OUString( RTL_CONSTASCII_USTRINGPARAM("expected styles ot bulletinboard element!") ),
272 Reference< XInterface >(), Any() );
275 //__________________________________________________________________________________________________
276 void LibrariesElement::endElement()
277 throw (xml::sax::SAXException, RuntimeException)
279 sal_Int32 nLibCount = _pImport->mpLibArray->mnLibCount = (sal_Int32)mLibDescriptors.size();
280 _pImport->mpLibArray->mpLibs = new LibDescriptor[ nLibCount ];
282 for( sal_Int32 i = 0 ; i < nLibCount ; i++ )
284 const LibDescriptor& rLib = mLibDescriptors[i];
285 _pImport->mpLibArray->mpLibs[i] = rLib;
289 // library
290 //__________________________________________________________________________________________________
291 Reference< xml::input::XElement > LibraryElement::startChildElement(
292 sal_Int32 nUid, OUString const & rLocalName,
293 Reference< xml::input::XAttributes > const & xAttributes )
294 throw (xml::sax::SAXException, RuntimeException)
296 if (_pImport->XMLNS_LIBRARY_UID != nUid)
298 throw xml::sax::SAXException(
299 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
300 Reference< XInterface >(), Any() );
302 // library
303 else if ( rLocalName == "element" )
305 OUString aValue( xAttributes->getValueByUidName(
306 _pImport->XMLNS_LIBRARY_UID,
307 OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) ) );
308 if (!aValue.isEmpty())
309 mElements.push_back( aValue );
311 return new LibElementBase( rLocalName, xAttributes, this, _pImport );
313 else
315 throw xml::sax::SAXException(
316 OUString( RTL_CONSTASCII_USTRINGPARAM("expected styles ot bulletinboard element!") ),
317 Reference< XInterface >(), Any() );
320 //__________________________________________________________________________________________________
321 void LibraryElement::endElement()
322 throw (xml::sax::SAXException, RuntimeException)
324 sal_Int32 nElementCount = mElements.size();
325 Sequence< OUString > aElementNames( nElementCount );
326 OUString* pElementNames = aElementNames.getArray();
327 for( sal_Int32 i = 0 ; i < nElementCount ; i++ )
328 pElementNames[i] = mElements[i];
330 LibDescriptor* pLib = _pImport->mpLibDesc;
331 if( !pLib )
332 pLib = &static_cast< LibrariesElement* >( _pParent )->mLibDescriptors.back();
333 pLib->aElementNames = aElementNames;
337 //##################################################################################################
339 Reference< ::com::sun::star::xml::sax::XDocumentHandler >
340 SAL_CALL importLibraryContainer( LibDescriptorArray* pLibArray )
341 SAL_THROW( (Exception) )
343 return ::xmlscript::createDocumentHandler(
344 static_cast< xml::input::XRoot * >( new LibraryImport( pLibArray ) ) );
347 //##################################################################################################
349 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >
350 SAL_CALL importLibrary( LibDescriptor& rLib )
351 SAL_THROW( (::com::sun::star::uno::Exception) )
353 return ::xmlscript::createDocumentHandler(
354 static_cast< xml::input::XRoot * >( new LibraryImport( &rLib ) ) );
358 //##################################################################################################
360 LibDescriptorArray::LibDescriptorArray( sal_Int32 nLibCount )
362 mnLibCount = nLibCount;
363 mpLibs = new LibDescriptor[ mnLibCount ];
366 LibDescriptorArray::~LibDescriptorArray()
368 delete[] mpLibs;
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */