LibreOffice Module xmlscript (master) 1
xmllib_import.cxx
Go to the documentation of this file.
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 <utility>
26#include <xml_import.hxx>
27#include <xmlscript/xmlns.h>
28
29using namespace css;
30using namespace css::uno;
31
32namespace xmlscript
33{
34
35Reference< xml::input::XElement > LibElementBase::getParent()
36{
37 return mxParent;
38}
39
41{
42 return _aLocalName;
43}
44
46{
47 return mxImport->XMLNS_LIBRARY_UID;
48}
49
50Reference< xml::input::XAttributes > LibElementBase::getAttributes()
51{
52 return _xAttributes;
53}
54
56 OUString const & /*rWhitespaces*/ )
57{
58}
59
60void LibElementBase::characters( OUString const & /*rChars*/ )
61{
62 // not used, all characters ignored
63}
64
66 OUString const & /*rTarget*/, OUString const & /*rData*/ )
67{
68}
69
71{
72}
73Reference< xml::input::XElement > LibElementBase::startChildElement(
74 sal_Int32 /*nUid*/, OUString const & /*rLocalName*/,
75 Reference< xml::input::XAttributes > const & /*xAttributes*/ )
76{
77 throw xml::sax::SAXException("unexpected element!", Reference< XInterface >(), Any() );
78}
79
81 OUString aLocalName,
82 Reference< xml::input::XAttributes > const & xAttributes,
83 LibElementBase * pParent, LibraryImport * pImport )
84 : mxImport( pImport )
85 , mxParent( pParent )
86 , _aLocalName(std::move( aLocalName ))
87 , _xAttributes( xAttributes )
88{
89}
90
92{
93 SAL_INFO("xmlscript.xmllib", "LibElementBase::~LibElementBase(): " << _aLocalName );
94}
95
96// XRoot
97
99 Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
100{
101 XMLNS_LIBRARY_UID = xNamespaceMapping->getUidByUri( XMLNS_LIBRARY_URI );
102 XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri( XMLNS_XLINK_URI );
103}
104
106{
107}
108
110 OUString const & /*rTarget*/, OUString const & /*rData*/ )
111{
112}
113
115 Reference< xml::sax::XLocator > const & /*xLocator*/ )
116{
117}
118
119Reference< xml::input::XElement > LibraryImport::startRootElement(
120 sal_Int32 nUid, OUString const & rLocalName,
121 Reference< xml::input::XAttributes > const & xAttributes )
122{
123 if (XMLNS_LIBRARY_UID != nUid)
124 {
125 throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
126 }
127 else if ( mpLibArray && rLocalName == "libraries" )
128 {
129 return new LibrariesElement( rLocalName, xAttributes, this );
130 }
131 else if ( mpLibDesc && rLocalName == "library" )
132 {
133 LibDescriptor& aDesc = *mpLibDesc;
134 aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = false;
135
136 aDesc.aName = xAttributes->getValueByUidName(XMLNS_LIBRARY_UID, "name" );
137 getBoolAttr( &aDesc.bReadOnly, "readonly", xAttributes, XMLNS_LIBRARY_UID );
138 getBoolAttr( &aDesc.bPasswordProtected, "passwordprotected", xAttributes, XMLNS_LIBRARY_UID );
139 getBoolAttr( &aDesc.bPreload, "preload", xAttributes, XMLNS_LIBRARY_UID );
140
141 return new LibraryElement( rLocalName, xAttributes, nullptr, this );
142 }
143 else
144 {
145 throw xml::sax::SAXException( "illegal root element (expected libraries) given: " + rLocalName, Reference< XInterface >(), Any() );
146 }
147}
148
150{
151 SAL_INFO("xmlscript.xmllib", "LibraryImport::~LibraryImport()." );
152}
153
154// libraries
155Reference< xml::input::XElement > LibrariesElement::startChildElement(
156 sal_Int32 nUid, OUString const & rLocalName,
157 Reference< xml::input::XAttributes > const & xAttributes )
158{
159 if (mxImport->XMLNS_LIBRARY_UID != nUid)
160 {
161 throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
162 }
163 // library
164 else if ( rLocalName == "library" )
165 {
166 LibDescriptor aDesc;
167 aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = false;
168
169 aDesc.aName = xAttributes->getValueByUidName(mxImport->XMLNS_LIBRARY_UID, "name" );
170 aDesc.aStorageURL = xAttributes->getValueByUidName( mxImport->XMLNS_XLINK_UID, "href" );
171 getBoolAttr(&aDesc.bLink, "link", xAttributes, mxImport->XMLNS_LIBRARY_UID );
172 getBoolAttr(&aDesc.bReadOnly, "readonly", xAttributes, mxImport->XMLNS_LIBRARY_UID );
173 getBoolAttr(&aDesc.bPasswordProtected, "passwordprotected", xAttributes, mxImport->XMLNS_LIBRARY_UID );
174
175 mLibDescriptors.push_back( aDesc );
176 return new LibraryElement( rLocalName, xAttributes, this, mxImport.get() );
177 }
178 else
179 {
180 throw xml::sax::SAXException( "expected styles of bulletinboard element!", Reference< XInterface >(), Any() );
181 }
182}
183
185{
186 sal_Int32 nLibCount = mxImport->mpLibArray->mnLibCount = static_cast<sal_Int32>(mLibDescriptors.size());
187 mxImport->mpLibArray->mpLibs.reset( new LibDescriptor[ nLibCount ] );
188
189 for( sal_Int32 i = 0 ; i < nLibCount ; i++ )
190 {
191 const LibDescriptor& rLib = mLibDescriptors[i];
192 mxImport->mpLibArray->mpLibs[i] = rLib;
193 }
194}
195
196// library
197Reference< xml::input::XElement > LibraryElement::startChildElement(
198 sal_Int32 nUid, OUString const & rLocalName,
199 Reference< xml::input::XAttributes > const & xAttributes )
200{
201 if (mxImport->XMLNS_LIBRARY_UID != nUid)
202 {
203 throw xml::sax::SAXException( "illegal namespace!", Reference< XInterface >(), Any() );
204 }
205 // library
206 else if ( rLocalName == "element" )
207 {
208 OUString aValue( xAttributes->getValueByUidName(mxImport->XMLNS_LIBRARY_UID, "name" ) );
209 if (!aValue.isEmpty())
210 mElements.push_back( aValue );
211
212 return new LibElementBase( rLocalName, xAttributes, this, mxImport.get() );
213 }
214 else
215 {
216 throw xml::sax::SAXException( "expected styles or bulletinboard element!", Reference< XInterface >(), Any() );
217 }
218}
219
221{
222 sal_Int32 nElementCount = mElements.size();
223 Sequence< OUString > aElementNames( nElementCount );
224 OUString* pElementNames = aElementNames.getArray();
225 for( sal_Int32 i = 0 ; i < nElementCount ; i++ )
226 pElementNames[i] = mElements[i];
227
228 LibDescriptor* pLib = mxImport->mpLibDesc;
229 if( !pLib )
230 pLib = &static_cast< LibrariesElement* >( mxParent.get() )->mLibDescriptors.back();
231 pLib->aElementNames = aElementNames;
232}
233
234Reference< css::xml::sax::XDocumentHandler >
236{
238}
239
240css::uno::Reference< css::xml::sax::XDocumentHandler >
242{
244}
245
247{
248 mnLibCount = nLibCount;
249 mpLibs.reset( new LibDescriptor[ mnLibCount ] );
250}
251
253{
254}
255
256}
257
258/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
unotools::WeakReference< AnimationNode > mxParent
virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL getParent() override
virtual void SAL_CALL characters(OUString const &rChars) override
virtual sal_Int32 SAL_CALL getUid() override
css::uno::Reference< css::xml::input::XAttributes > _xAttributes
virtual void SAL_CALL processingInstruction(OUString const &rTarget, OUString const &rData) override
virtual void SAL_CALL endElement() override
virtual void SAL_CALL ignorableWhitespace(OUString const &rWhitespaces) override
virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement(sal_Int32 nUid, OUString const &rLocalName, css::uno::Reference< css::xml::input::XAttributes > const &xAttributes) override
rtl::Reference< LibraryImport > mxImport
LibElementBase(OUString aLocalName, css::uno::Reference< css::xml::input::XAttributes > const &xAttributes, LibElementBase *pParent, LibraryImport *pImport)
virtual css::uno::Reference< css::xml::input::XAttributes > SAL_CALL getAttributes() override
virtual ~LibElementBase() override
rtl::Reference< LibElementBase > mxParent
virtual OUString SAL_CALL getLocalName() override
std::vector< LibDescriptor > mLibDescriptors
virtual void SAL_CALL endElement() override
virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement(sal_Int32 nUid, OUString const &rLocalName, css::uno::Reference< css::xml::input::XAttributes > const &xAttributes) override
virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement(sal_Int32 nUid, OUString const &rLocalName, css::uno::Reference< css::xml::input::XAttributes > const &xAttributes) override
std::vector< OUString > mElements
virtual void SAL_CALL endElement() override
const char * pLib
#define SAL_INFO(area, stream)
int i
bool getBoolAttr(sal_Bool *pRet, OUString const &rAttrName, css::uno::Reference< css::xml::input::XAttributes > const &xAttributes, sal_Int32 nUid)
Reference< css::xml::sax::XDocumentHandler > importLibraryContainer(LibDescriptorArray *pLibArray)
css::uno::Reference< css::xml::sax::XDocumentHandler > importLibrary(LibDescriptor &rLib)
css::uno::Reference< css::xml::sax::XDocumentHandler > createDocumentHandler(css::uno::Reference< css::xml::input::XRoot > const &xRoot)
Creates a document handler to be used for SAX1 parser that can handle namespaces.
std::unique_ptr< LibDescriptor[]> mpLibs
virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startRootElement(sal_Int32 nUid, OUString const &rLocalName, css::uno::Reference< css::xml::input::XAttributes > const &xAttributes) override
virtual void SAL_CALL startDocument(css::uno::Reference< css::xml::input::XNamespaceMapping > const &xNamespaceMapping) override
virtual void SAL_CALL setDocumentLocator(css::uno::Reference< css::xml::sax::XLocator > const &xLocator) override
virtual void SAL_CALL endDocument() override
virtual ~LibraryImport() override
virtual void SAL_CALL processingInstruction(OUString const &rTarget, OUString const &rData) override
constexpr OUStringLiteral XMLNS_LIBRARY_URI
Definition: xmlns.h:32
constexpr OUStringLiteral XMLNS_XLINK_URI
Definition: xmlns.h:35