bump product version to 4.1.6.2
[LibreOffice.git] / xmlscript / source / xmllib_imexp / xmllib_import.cxx
blob16a5a802cec6384c3d89a2d89f026da6e0fd687d
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 "imp_share.hxx"
21 #include "xml_import.hxx"
23 namespace xmlscript
26 //##################################################################################################
28 //__________________________________________________________________________________________________
29 Reference< xml::input::XElement > LibElementBase::getParent()
30 throw (RuntimeException)
32 return static_cast< xml::input::XElement * >( _pParent );
34 //__________________________________________________________________________________________________
35 OUString LibElementBase::getLocalName()
36 throw (RuntimeException)
38 return _aLocalName;
40 //__________________________________________________________________________________________________
41 sal_Int32 LibElementBase::getUid()
42 throw (RuntimeException)
44 return _pImport->XMLNS_LIBRARY_UID;
46 //__________________________________________________________________________________________________
47 Reference< xml::input::XAttributes > LibElementBase::getAttributes()
48 throw (RuntimeException)
50 return _xAttributes;
52 //__________________________________________________________________________________________________
53 void LibElementBase::ignorableWhitespace(
54 OUString const & /*rWhitespaces*/ )
55 throw (xml::sax::SAXException, RuntimeException)
58 //__________________________________________________________________________________________________
59 void LibElementBase::characters( OUString const & /*rChars*/ )
60 throw (xml::sax::SAXException, RuntimeException)
62 // not used, all characters ignored
65 //__________________________________________________________________________________________________
66 void LibElementBase::processingInstruction(
67 OUString const & /*rTarget*/, OUString const & /*rData*/ )
68 throw (xml::sax::SAXException, RuntimeException)
72 //__________________________________________________________________________________________________
73 void LibElementBase::endElement()
74 throw (xml::sax::SAXException, RuntimeException)
77 //__________________________________________________________________________________________________
78 Reference< xml::input::XElement > LibElementBase::startChildElement(
79 sal_Int32 /*nUid*/, OUString const & /*rLocalName*/,
80 Reference< xml::input::XAttributes > const & /*xAttributes*/ )
81 throw (xml::sax::SAXException, RuntimeException)
83 throw xml::sax::SAXException("unexpected element!", Reference< XInterface >(), Any() );
86 //__________________________________________________________________________________________________
87 LibElementBase::LibElementBase(
88 OUString const & rLocalName,
89 Reference< xml::input::XAttributes > const & xAttributes,
90 LibElementBase * pParent, LibraryImport * pImport )
91 SAL_THROW(())
92 : _pImport( pImport )
93 , _pParent( pParent )
94 , _aLocalName( rLocalName )
95 , _xAttributes( xAttributes )
97 _pImport->acquire();
99 if (_pParent)
101 _pParent->acquire();
104 //__________________________________________________________________________________________________
105 LibElementBase::~LibElementBase()
106 SAL_THROW(())
108 _pImport->release();
110 if (_pParent)
112 _pParent->release();
115 #if OSL_DEBUG_LEVEL > 1
116 OString aStr( OUStringToOString( _aLocalName, RTL_TEXTENCODING_ASCII_US ) );
117 SAL_INFO("xmlscript.xmllib", "LibElementBase::~LibElementBase(): " << aStr.getStr() );
118 #endif
121 //##################################################################################################
123 // XRoot
125 //______________________________________________________________________________
126 void LibraryImport::startDocument(
127 Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
128 throw (xml::sax::SAXException, RuntimeException)
130 XMLNS_LIBRARY_UID = xNamespaceMapping->getUidByUri( XMLNS_LIBRARY_URI );
131 XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri( XMLNS_XLINK_URI );
133 //__________________________________________________________________________________________________
134 void LibraryImport::endDocument()
135 throw (xml::sax::SAXException, RuntimeException)
138 //__________________________________________________________________________________________________
139 void LibraryImport::processingInstruction(
140 OUString const & /*rTarget*/, OUString const & /*rData*/ )
141 throw (xml::sax::SAXException, RuntimeException)
144 //__________________________________________________________________________________________________
145 void LibraryImport::setDocumentLocator(
146 Reference< xml::sax::XLocator > const & /*xLocator*/ )
147 throw (xml::sax::SAXException, RuntimeException)
150 //__________________________________________________________________________________________________
151 Reference< xml::input::XElement > LibraryImport::startRootElement(
152 sal_Int32 nUid, OUString const & rLocalName,
153 Reference< xml::input::XAttributes > const & xAttributes )
154 throw (xml::sax::SAXException, RuntimeException)
156 if (XMLNS_LIBRARY_UID != nUid)
158 throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
160 else if ( mpLibArray && rLocalName == "libraries" )
162 return new LibrariesElement( rLocalName, xAttributes, 0, this );
164 else if ( mpLibDesc && rLocalName == "library" )
166 LibDescriptor& aDesc = *mpLibDesc;
167 aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = sal_False;
169 aDesc.aName = xAttributes->getValueByUidName(XMLNS_LIBRARY_UID, "name" );
170 getBoolAttr( &aDesc.bReadOnly, "readonly", xAttributes, XMLNS_LIBRARY_UID );
171 getBoolAttr( &aDesc.bPasswordProtected, "passwordprotected", xAttributes, XMLNS_LIBRARY_UID );
172 getBoolAttr( &aDesc.bPreload, "preload", xAttributes, XMLNS_LIBRARY_UID );
174 return new LibraryElement( rLocalName, xAttributes, 0, this );
176 else
178 throw xml::sax::SAXException( "illegal root element (expected libraries) given: " + rLocalName, Reference< XInterface >(), Any() );
181 //__________________________________________________________________________________________________
182 LibraryImport::~LibraryImport()
183 SAL_THROW(())
185 #if OSL_DEBUG_LEVEL > 1
186 SAL_INFO("xmlscript.xmllib", "LibraryImport::~LibraryImport()." );
187 #endif
190 //##################################################################################################
193 // libraries
194 //__________________________________________________________________________________________________
195 Reference< xml::input::XElement > LibrariesElement::startChildElement(
196 sal_Int32 nUid, OUString const & rLocalName,
197 Reference< xml::input::XAttributes > const & xAttributes )
198 throw (xml::sax::SAXException, RuntimeException)
200 if (_pImport->XMLNS_LIBRARY_UID != nUid)
202 throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
204 // library
205 else if ( rLocalName == "library" )
207 LibDescriptor aDesc;
208 aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = sal_False;
210 aDesc.aName = xAttributes->getValueByUidName(_pImport->XMLNS_LIBRARY_UID, "name" );
211 aDesc.aStorageURL = xAttributes->getValueByUidName( _pImport->XMLNS_XLINK_UID, "href" );
212 getBoolAttr(&aDesc.bLink, "link", xAttributes, _pImport->XMLNS_LIBRARY_UID );
213 getBoolAttr(&aDesc.bReadOnly, "readonly", xAttributes, _pImport->XMLNS_LIBRARY_UID );
214 getBoolAttr(&aDesc.bPasswordProtected, "passwordprotected", xAttributes, _pImport->XMLNS_LIBRARY_UID );
216 mLibDescriptors.push_back( aDesc );
217 return new LibraryElement( rLocalName, xAttributes, this, _pImport );
219 else
221 throw xml::sax::SAXException( "expected styles ot bulletinboard element!", Reference< XInterface >(), Any() );
224 //__________________________________________________________________________________________________
225 void LibrariesElement::endElement()
226 throw (xml::sax::SAXException, RuntimeException)
228 sal_Int32 nLibCount = _pImport->mpLibArray->mnLibCount = (sal_Int32)mLibDescriptors.size();
229 _pImport->mpLibArray->mpLibs = new LibDescriptor[ nLibCount ];
231 for( sal_Int32 i = 0 ; i < nLibCount ; i++ )
233 const LibDescriptor& rLib = mLibDescriptors[i];
234 _pImport->mpLibArray->mpLibs[i] = rLib;
238 // library
239 //__________________________________________________________________________________________________
240 Reference< xml::input::XElement > LibraryElement::startChildElement(
241 sal_Int32 nUid, OUString const & rLocalName,
242 Reference< xml::input::XAttributes > const & xAttributes )
243 throw (xml::sax::SAXException, RuntimeException)
245 if (_pImport->XMLNS_LIBRARY_UID != nUid)
247 throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
249 // library
250 else if ( rLocalName == "element" )
252 OUString aValue( xAttributes->getValueByUidName(_pImport->XMLNS_LIBRARY_UID, "name" ) );
253 if (!aValue.isEmpty())
254 mElements.push_back( aValue );
256 return new LibElementBase( rLocalName, xAttributes, this, _pImport );
258 else
260 throw xml::sax::SAXException( "expected styles ot bulletinboard element!", Reference< XInterface >(), Any() );
263 //__________________________________________________________________________________________________
264 void LibraryElement::endElement()
265 throw (xml::sax::SAXException, RuntimeException)
267 sal_Int32 nElementCount = mElements.size();
268 Sequence< OUString > aElementNames( nElementCount );
269 OUString* pElementNames = aElementNames.getArray();
270 for( sal_Int32 i = 0 ; i < nElementCount ; i++ )
271 pElementNames[i] = mElements[i];
273 LibDescriptor* pLib = _pImport->mpLibDesc;
274 if( !pLib )
275 pLib = &static_cast< LibrariesElement* >( _pParent )->mLibDescriptors.back();
276 pLib->aElementNames = aElementNames;
280 //##################################################################################################
282 Reference< ::com::sun::star::xml::sax::XDocumentHandler >
283 SAL_CALL importLibraryContainer( LibDescriptorArray* pLibArray )
284 SAL_THROW( (Exception) )
286 return ::xmlscript::createDocumentHandler(
287 static_cast< xml::input::XRoot * >( new LibraryImport( pLibArray ) ) );
290 //##################################################################################################
292 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >
293 SAL_CALL importLibrary( LibDescriptor& rLib )
294 SAL_THROW( (::com::sun::star::uno::Exception) )
296 return ::xmlscript::createDocumentHandler(
297 static_cast< xml::input::XRoot * >( new LibraryImport( &rLib ) ) );
301 //##################################################################################################
303 LibDescriptorArray::LibDescriptorArray( sal_Int32 nLibCount )
305 mnLibCount = nLibCount;
306 mpLibs = new LibDescriptor[ mnLibCount ];
309 LibDescriptorArray::~LibDescriptorArray()
311 delete[] mpLibs;
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */