Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
22 : #include <sal/log.hxx>
23 :
24 : #include "imp_share.hxx"
25 : #include "xml_import.hxx"
26 :
27 : using namespace css;
28 : using namespace css::uno;
29 :
30 : namespace xmlscript
31 : {
32 :
33 0 : Reference< xml::input::XElement > LibElementBase::getParent()
34 : throw (RuntimeException, std::exception)
35 : {
36 0 : return static_cast< xml::input::XElement * >( _pParent );
37 : }
38 :
39 0 : OUString LibElementBase::getLocalName()
40 : throw (RuntimeException, std::exception)
41 : {
42 0 : return _aLocalName;
43 : }
44 :
45 0 : sal_Int32 LibElementBase::getUid()
46 : throw (RuntimeException, std::exception)
47 : {
48 0 : return _pImport->XMLNS_LIBRARY_UID;
49 : }
50 :
51 0 : Reference< xml::input::XAttributes > LibElementBase::getAttributes()
52 : throw (RuntimeException, std::exception)
53 : {
54 0 : return _xAttributes;
55 : }
56 :
57 0 : void LibElementBase::ignorableWhitespace(
58 : OUString const & /*rWhitespaces*/ )
59 : throw (xml::sax::SAXException, RuntimeException, std::exception)
60 : {
61 0 : }
62 :
63 28166 : void LibElementBase::characters( OUString const & /*rChars*/ )
64 : throw (xml::sax::SAXException, RuntimeException, std::exception)
65 : {
66 : // not used, all characters ignored
67 28166 : }
68 :
69 0 : void LibElementBase::processingInstruction(
70 : OUString const & /*rTarget*/, OUString const & /*rData*/ )
71 : throw (xml::sax::SAXException, RuntimeException, std::exception)
72 : {
73 0 : }
74 :
75 10622 : void LibElementBase::endElement()
76 : throw (xml::sax::SAXException, RuntimeException, std::exception)
77 : {
78 10622 : }
79 0 : 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)
83 : {
84 0 : throw xml::sax::SAXException("unexpected element!", Reference< XInterface >(), Any() );
85 : }
86 :
87 15251 : 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 15251 : , _xAttributes( xAttributes )
95 : {
96 15251 : _pImport->acquire();
97 :
98 15251 : if (_pParent)
99 : {
100 12758 : _pParent->acquire();
101 : }
102 15251 : }
103 :
104 41124 : LibElementBase::~LibElementBase()
105 : {
106 15251 : _pImport->release();
107 :
108 15251 : if (_pParent)
109 : {
110 12758 : _pParent->release();
111 : }
112 :
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
117 25873 : }
118 :
119 : // XRoot
120 :
121 2493 : void LibraryImport::startDocument(
122 : Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
123 : throw (xml::sax::SAXException, RuntimeException, std::exception)
124 : {
125 2493 : XMLNS_LIBRARY_UID = xNamespaceMapping->getUidByUri( XMLNS_LIBRARY_URI );
126 2493 : XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri( XMLNS_XLINK_URI );
127 2493 : }
128 :
129 2493 : void LibraryImport::endDocument()
130 : throw (xml::sax::SAXException, RuntimeException, std::exception)
131 : {
132 2493 : }
133 :
134 0 : void LibraryImport::processingInstruction(
135 : OUString const & /*rTarget*/, OUString const & /*rData*/ )
136 : throw (xml::sax::SAXException, RuntimeException, std::exception)
137 : {
138 0 : }
139 :
140 2493 : void LibraryImport::setDocumentLocator(
141 : Reference< xml::sax::XLocator > const & /*xLocator*/ )
142 : throw (xml::sax::SAXException, RuntimeException, std::exception)
143 : {
144 2493 : }
145 :
146 2493 : 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)
150 : {
151 2493 : if (XMLNS_LIBRARY_UID != nUid)
152 : {
153 0 : throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
154 : }
155 2493 : else if ( mpLibArray && rLocalName == "libraries" )
156 : {
157 357 : return new LibrariesElement( rLocalName, xAttributes, 0, this );
158 : }
159 2136 : else if ( mpLibDesc && rLocalName == "library" )
160 : {
161 2136 : LibDescriptor& aDesc = *mpLibDesc;
162 2136 : aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = false;
163 :
164 2136 : aDesc.aName = xAttributes->getValueByUidName(XMLNS_LIBRARY_UID, "name" );
165 2136 : getBoolAttr( &aDesc.bReadOnly, "readonly", xAttributes, XMLNS_LIBRARY_UID );
166 2136 : getBoolAttr( &aDesc.bPasswordProtected, "passwordprotected", xAttributes, XMLNS_LIBRARY_UID );
167 2136 : getBoolAttr( &aDesc.bPreload, "preload", xAttributes, XMLNS_LIBRARY_UID );
168 :
169 2136 : return new LibraryElement( rLocalName, xAttributes, 0, this );
170 : }
171 : else
172 : {
173 0 : throw xml::sax::SAXException( "illegal root element (expected libraries) given: " + rLocalName, Reference< XInterface >(), Any() );
174 : }
175 : }
176 :
177 4986 : LibraryImport::~LibraryImport()
178 : {
179 : #if OSL_DEBUG_LEVEL > 1
180 : SAL_INFO("xmlscript.xmllib", "LibraryImport::~LibraryImport()." );
181 : #endif
182 4986 : }
183 :
184 : // libraries
185 2136 : 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)
189 : {
190 2136 : if (_pImport->XMLNS_LIBRARY_UID != nUid)
191 : {
192 0 : throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
193 : }
194 : // library
195 2136 : else if ( rLocalName == "library" )
196 : {
197 2136 : LibDescriptor aDesc;
198 2136 : aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = false;
199 :
200 2136 : aDesc.aName = xAttributes->getValueByUidName(_pImport->XMLNS_LIBRARY_UID, "name" );
201 2136 : aDesc.aStorageURL = xAttributes->getValueByUidName( _pImport->XMLNS_XLINK_UID, "href" );
202 2136 : getBoolAttr(&aDesc.bLink, "link", xAttributes, _pImport->XMLNS_LIBRARY_UID );
203 2136 : getBoolAttr(&aDesc.bReadOnly, "readonly", xAttributes, _pImport->XMLNS_LIBRARY_UID );
204 2136 : getBoolAttr(&aDesc.bPasswordProtected, "passwordprotected", xAttributes, _pImport->XMLNS_LIBRARY_UID );
205 :
206 2136 : mLibDescriptors.push_back( aDesc );
207 4272 : return new LibraryElement( rLocalName, xAttributes, this, _pImport );
208 : }
209 : else
210 : {
211 0 : throw xml::sax::SAXException( "expected styles ot bulletinboard element!", Reference< XInterface >(), Any() );
212 : }
213 : }
214 :
215 357 : void LibrariesElement::endElement()
216 : throw (xml::sax::SAXException, RuntimeException, std::exception)
217 : {
218 357 : sal_Int32 nLibCount = _pImport->mpLibArray->mnLibCount = (sal_Int32)mLibDescriptors.size();
219 357 : _pImport->mpLibArray->mpLibs = new LibDescriptor[ nLibCount ];
220 :
221 2493 : for( sal_Int32 i = 0 ; i < nLibCount ; i++ )
222 : {
223 2136 : const LibDescriptor& rLib = mLibDescriptors[i];
224 2136 : _pImport->mpLibArray->mpLibs[i] = rLib;
225 : }
226 357 : }
227 :
228 : // library
229 10622 : 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)
233 : {
234 10622 : if (_pImport->XMLNS_LIBRARY_UID != nUid)
235 : {
236 0 : throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
237 : }
238 : // library
239 10622 : else if ( rLocalName == "element" )
240 : {
241 10622 : OUString aValue( xAttributes->getValueByUidName(_pImport->XMLNS_LIBRARY_UID, "name" ) );
242 10622 : if (!aValue.isEmpty())
243 10622 : mElements.push_back( aValue );
244 :
245 21244 : return new LibElementBase( rLocalName, xAttributes, this, _pImport );
246 : }
247 : else
248 : {
249 0 : throw xml::sax::SAXException( "expected styles ot bulletinboard element!", Reference< XInterface >(), Any() );
250 : }
251 : }
252 :
253 4272 : void LibraryElement::endElement()
254 : throw (xml::sax::SAXException, RuntimeException, std::exception)
255 : {
256 4272 : sal_Int32 nElementCount = mElements.size();
257 4272 : Sequence< OUString > aElementNames( nElementCount );
258 4272 : OUString* pElementNames = aElementNames.getArray();
259 14894 : for( sal_Int32 i = 0 ; i < nElementCount ; i++ )
260 10622 : pElementNames[i] = mElements[i];
261 :
262 4272 : LibDescriptor* pLib = _pImport->mpLibDesc;
263 4272 : if( !pLib )
264 2136 : pLib = &static_cast< LibrariesElement* >( _pParent )->mLibDescriptors.back();
265 4272 : pLib->aElementNames = aElementNames;
266 4272 : }
267 :
268 : Reference< ::com::sun::star::xml::sax::XDocumentHandler >
269 357 : SAL_CALL importLibraryContainer( LibDescriptorArray* pLibArray )
270 : {
271 : return ::xmlscript::createDocumentHandler(
272 357 : static_cast< xml::input::XRoot * >( new LibraryImport( pLibArray ) ) );
273 : }
274 :
275 : ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >
276 2136 : SAL_CALL importLibrary( LibDescriptor& rLib )
277 : {
278 : return ::xmlscript::createDocumentHandler(
279 2136 : static_cast< xml::input::XRoot * >( new LibraryImport( &rLib ) ) );
280 : }
281 :
282 88 : LibDescriptorArray::LibDescriptorArray( sal_Int32 nLibCount )
283 : {
284 88 : mnLibCount = nLibCount;
285 88 : mpLibs = new LibDescriptor[ mnLibCount ];
286 88 : }
287 :
288 445 : LibDescriptorArray::~LibDescriptorArray()
289 : {
290 445 : delete[] mpLibs;
291 445 : }
292 :
293 : }
294 :
295 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|