bump product version to 5.0.4.1
[LibreOffice.git] / xmlscript / source / xmllib_imexp / xmllib_import.cxx
blobf65ac55ab7d25bbe1c629092f41165fcbad8a830
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <sal/log.hxx>
24 #include "imp_share.hxx"
25 #include "xml_import.hxx"
27 using namespace css;
28 using namespace css::uno;
30 namespace xmlscript
33 Reference< xml::input::XElement > LibElementBase::getParent()
34 throw (RuntimeException, std::exception)
36 return static_cast< xml::input::XElement * >( _pParent );
39 OUString LibElementBase::getLocalName()
40 throw (RuntimeException, std::exception)
42 return _aLocalName;
45 sal_Int32 LibElementBase::getUid()
46 throw (RuntimeException, std::exception)
48 return _pImport->XMLNS_LIBRARY_UID;
51 Reference< xml::input::XAttributes > LibElementBase::getAttributes()
52 throw (RuntimeException, std::exception)
54 return _xAttributes;
57 void LibElementBase::ignorableWhitespace(
58 OUString const & /*rWhitespaces*/ )
59 throw (xml::sax::SAXException, RuntimeException, std::exception)
63 void LibElementBase::characters( OUString const & /*rChars*/ )
64 throw (xml::sax::SAXException, RuntimeException, std::exception)
66 // not used, all characters ignored
69 void LibElementBase::processingInstruction(
70 OUString const & /*rTarget*/, OUString const & /*rData*/ )
71 throw (xml::sax::SAXException, RuntimeException, std::exception)
75 void LibElementBase::endElement()
76 throw (xml::sax::SAXException, RuntimeException, std::exception)
79 Reference< xml::input::XElement > LibElementBase::startChildElement(
80 sal_Int32 /*nUid*/, OUString const & /*rLocalName*/,
81 Reference< xml::input::XAttributes > const & /*xAttributes*/ )
82 throw (xml::sax::SAXException, RuntimeException, std::exception)
84 throw xml::sax::SAXException("unexpected element!", Reference< XInterface >(), Any() );
87 LibElementBase::LibElementBase(
88 OUString const & rLocalName,
89 Reference< xml::input::XAttributes > const & xAttributes,
90 LibElementBase * pParent, LibraryImport * pImport )
91 : _pImport( pImport )
92 , _pParent( pParent )
93 , _aLocalName( rLocalName )
94 , _xAttributes( xAttributes )
96 _pImport->acquire();
98 if (_pParent)
100 _pParent->acquire();
104 LibElementBase::~LibElementBase()
106 _pImport->release();
108 if (_pParent)
110 _pParent->release();
113 #if OSL_DEBUG_LEVEL > 1
114 OString aStr( OUStringToOString( _aLocalName, RTL_TEXTENCODING_ASCII_US ) );
115 SAL_INFO("xmlscript.xmllib", "LibElementBase::~LibElementBase(): " << aStr.getStr() );
116 #endif
119 // XRoot
121 void LibraryImport::startDocument(
122 Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
123 throw (xml::sax::SAXException, RuntimeException, std::exception)
125 XMLNS_LIBRARY_UID = xNamespaceMapping->getUidByUri( XMLNS_LIBRARY_URI );
126 XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri( XMLNS_XLINK_URI );
129 void LibraryImport::endDocument()
130 throw (xml::sax::SAXException, RuntimeException, std::exception)
134 void LibraryImport::processingInstruction(
135 OUString const & /*rTarget*/, OUString const & /*rData*/ )
136 throw (xml::sax::SAXException, RuntimeException, std::exception)
140 void LibraryImport::setDocumentLocator(
141 Reference< xml::sax::XLocator > const & /*xLocator*/ )
142 throw (xml::sax::SAXException, RuntimeException, std::exception)
146 Reference< xml::input::XElement > LibraryImport::startRootElement(
147 sal_Int32 nUid, OUString const & rLocalName,
148 Reference< xml::input::XAttributes > const & xAttributes )
149 throw (xml::sax::SAXException, RuntimeException, std::exception)
151 if (XMLNS_LIBRARY_UID != nUid)
153 throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
155 else if ( mpLibArray && rLocalName == "libraries" )
157 return new LibrariesElement( rLocalName, xAttributes, 0, this );
159 else if ( mpLibDesc && rLocalName == "library" )
161 LibDescriptor& aDesc = *mpLibDesc;
162 aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = false;
164 aDesc.aName = xAttributes->getValueByUidName(XMLNS_LIBRARY_UID, "name" );
165 getBoolAttr( &aDesc.bReadOnly, "readonly", xAttributes, XMLNS_LIBRARY_UID );
166 getBoolAttr( &aDesc.bPasswordProtected, "passwordprotected", xAttributes, XMLNS_LIBRARY_UID );
167 getBoolAttr( &aDesc.bPreload, "preload", xAttributes, XMLNS_LIBRARY_UID );
169 return new LibraryElement( rLocalName, xAttributes, 0, this );
171 else
173 throw xml::sax::SAXException( "illegal root element (expected libraries) given: " + rLocalName, Reference< XInterface >(), Any() );
177 LibraryImport::~LibraryImport()
179 #if OSL_DEBUG_LEVEL > 1
180 SAL_INFO("xmlscript.xmllib", "LibraryImport::~LibraryImport()." );
181 #endif
184 // libraries
185 Reference< xml::input::XElement > LibrariesElement::startChildElement(
186 sal_Int32 nUid, OUString const & rLocalName,
187 Reference< xml::input::XAttributes > const & xAttributes )
188 throw (xml::sax::SAXException, RuntimeException, std::exception)
190 if (_pImport->XMLNS_LIBRARY_UID != nUid)
192 throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
194 // library
195 else if ( rLocalName == "library" )
197 LibDescriptor aDesc;
198 aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = false;
200 aDesc.aName = xAttributes->getValueByUidName(_pImport->XMLNS_LIBRARY_UID, "name" );
201 aDesc.aStorageURL = xAttributes->getValueByUidName( _pImport->XMLNS_XLINK_UID, "href" );
202 getBoolAttr(&aDesc.bLink, "link", xAttributes, _pImport->XMLNS_LIBRARY_UID );
203 getBoolAttr(&aDesc.bReadOnly, "readonly", xAttributes, _pImport->XMLNS_LIBRARY_UID );
204 getBoolAttr(&aDesc.bPasswordProtected, "passwordprotected", xAttributes, _pImport->XMLNS_LIBRARY_UID );
206 mLibDescriptors.push_back( aDesc );
207 return new LibraryElement( rLocalName, xAttributes, this, _pImport );
209 else
211 throw xml::sax::SAXException( "expected styles ot bulletinboard element!", Reference< XInterface >(), Any() );
215 void LibrariesElement::endElement()
216 throw (xml::sax::SAXException, RuntimeException, std::exception)
218 sal_Int32 nLibCount = _pImport->mpLibArray->mnLibCount = (sal_Int32)mLibDescriptors.size();
219 _pImport->mpLibArray->mpLibs = new LibDescriptor[ nLibCount ];
221 for( sal_Int32 i = 0 ; i < nLibCount ; i++ )
223 const LibDescriptor& rLib = mLibDescriptors[i];
224 _pImport->mpLibArray->mpLibs[i] = rLib;
228 // library
229 Reference< xml::input::XElement > LibraryElement::startChildElement(
230 sal_Int32 nUid, OUString const & rLocalName,
231 Reference< xml::input::XAttributes > const & xAttributes )
232 throw (xml::sax::SAXException, RuntimeException, std::exception)
234 if (_pImport->XMLNS_LIBRARY_UID != nUid)
236 throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
238 // library
239 else if ( rLocalName == "element" )
241 OUString aValue( xAttributes->getValueByUidName(_pImport->XMLNS_LIBRARY_UID, "name" ) );
242 if (!aValue.isEmpty())
243 mElements.push_back( aValue );
245 return new LibElementBase( rLocalName, xAttributes, this, _pImport );
247 else
249 throw xml::sax::SAXException( "expected styles ot bulletinboard element!", Reference< XInterface >(), Any() );
253 void LibraryElement::endElement()
254 throw (xml::sax::SAXException, RuntimeException, std::exception)
256 sal_Int32 nElementCount = mElements.size();
257 Sequence< OUString > aElementNames( nElementCount );
258 OUString* pElementNames = aElementNames.getArray();
259 for( sal_Int32 i = 0 ; i < nElementCount ; i++ )
260 pElementNames[i] = mElements[i];
262 LibDescriptor* pLib = _pImport->mpLibDesc;
263 if( !pLib )
264 pLib = &static_cast< LibrariesElement* >( _pParent )->mLibDescriptors.back();
265 pLib->aElementNames = aElementNames;
268 Reference< ::com::sun::star::xml::sax::XDocumentHandler >
269 SAL_CALL importLibraryContainer( LibDescriptorArray* pLibArray )
271 return ::xmlscript::createDocumentHandler(
272 static_cast< xml::input::XRoot * >( new LibraryImport( pLibArray ) ) );
275 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >
276 SAL_CALL importLibrary( LibDescriptor& rLib )
278 return ::xmlscript::createDocumentHandler(
279 static_cast< xml::input::XRoot * >( new LibraryImport( &rLib ) ) );
282 LibDescriptorArray::LibDescriptorArray( sal_Int32 nLibCount )
284 mnLibCount = nLibCount;
285 mpLibs = new LibDescriptor[ mnLibCount ];
288 LibDescriptorArray::~LibDescriptorArray()
290 delete[] mpLibs;
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */