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