1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmlscript.hxx"
26 #include "imp_share.hxx"
27 
28 #include <osl/diagnose.h>
29 
30 #include <rtl/ustrbuf.hxx>
31 
32 #include <xmlscript/xml_import.hxx>
33 #include <comphelper/processfactory.hxx>
34 
35 
36 namespace xmlscript
37 {
38 
39 //##################################################################################################
40 
41 //__________________________________________________________________________________________________
getParent()42 Reference< xml::input::XElement > LibElementBase::getParent()
43     throw (RuntimeException)
44 {
45     return static_cast< xml::input::XElement * >( _pParent );
46 }
47 //__________________________________________________________________________________________________
getLocalName()48 OUString LibElementBase::getLocalName()
49     throw (RuntimeException)
50 {
51     return _aLocalName;
52 }
53 //__________________________________________________________________________________________________
getUid()54 sal_Int32 LibElementBase::getUid()
55     throw (RuntimeException)
56 {
57     return _pImport->XMLNS_LIBRARY_UID;
58 }
59 //__________________________________________________________________________________________________
getAttributes()60 Reference< xml::input::XAttributes > LibElementBase::getAttributes()
61     throw (RuntimeException)
62 {
63     return _xAttributes;
64 }
65 //__________________________________________________________________________________________________
ignorableWhitespace(OUString const &)66 void LibElementBase::ignorableWhitespace(
67     OUString const & /*rWhitespaces*/ )
68     throw (xml::sax::SAXException, RuntimeException)
69 {
70 }
71 //__________________________________________________________________________________________________
characters(OUString const &)72 void LibElementBase::characters( OUString const & /*rChars*/ )
73     throw (xml::sax::SAXException, RuntimeException)
74 {
75     // not used, all characters ignored
76 }
77 
78 //__________________________________________________________________________________________________
processingInstruction(OUString const &,OUString const &)79 void LibElementBase::processingInstruction(
80     OUString const & /*rTarget*/, OUString const & /*rData*/ )
81     throw (xml::sax::SAXException, RuntimeException)
82 {
83 }
84 
85 //__________________________________________________________________________________________________
endElement()86 void LibElementBase::endElement()
87     throw (xml::sax::SAXException, RuntimeException)
88 {
89 }
90 //__________________________________________________________________________________________________
startChildElement(sal_Int32,OUString const &,Reference<xml::input::XAttributes> const &)91 Reference< xml::input::XElement > LibElementBase::startChildElement(
92     sal_Int32 /*nUid*/, OUString const & /*rLocalName*/,
93     Reference< xml::input::XAttributes > const & /*xAttributes*/ )
94     throw (xml::sax::SAXException, RuntimeException)
95 {
96     throw xml::sax::SAXException(
97         OUString( RTL_CONSTASCII_USTRINGPARAM("unexpected element!") ),
98         Reference< XInterface >(), Any() );
99 }
100 
101 //__________________________________________________________________________________________________
LibElementBase(OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes,LibElementBase * pParent,LibraryImport * pImport)102 LibElementBase::LibElementBase(
103     OUString const & rLocalName,
104     Reference< xml::input::XAttributes > const & xAttributes,
105     LibElementBase * pParent, LibraryImport * pImport )
106     SAL_THROW( () )
107     : _pImport( pImport )
108     , _pParent( pParent )
109     , _aLocalName( rLocalName )
110     , _xAttributes( xAttributes )
111 {
112     _pImport->acquire();
113 
114     if (_pParent)
115     {
116         _pParent->acquire();
117     }
118 }
119 //__________________________________________________________________________________________________
~LibElementBase()120 LibElementBase::~LibElementBase()
121     SAL_THROW( () )
122 {
123     _pImport->release();
124 
125     if (_pParent)
126     {
127         _pParent->release();
128     }
129 
130 #if OSL_DEBUG_LEVEL > 1
131     OString aStr( OUStringToOString( _aLocalName, RTL_TEXTENCODING_ASCII_US ) );
132     OSL_TRACE( "LibElementBase::~LibElementBase(): %s\n", aStr.getStr() );
133 #endif
134 }
135 
136 //##################################################################################################
137 
138 // XRoot
139 
140 //______________________________________________________________________________
startDocument(Reference<xml::input::XNamespaceMapping> const & xNamespaceMapping)141 void LibraryImport::startDocument(
142     Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
143     throw (xml::sax::SAXException, RuntimeException)
144 {
145     XMLNS_LIBRARY_UID = xNamespaceMapping->getUidByUri(
146         OUSTR(XMLNS_LIBRARY_URI) );
147     XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri(
148         OUSTR(XMLNS_XLINK_URI) );
149 }
150 //__________________________________________________________________________________________________
endDocument()151 void LibraryImport::endDocument()
152     throw (xml::sax::SAXException, RuntimeException)
153 {
154 }
155 //__________________________________________________________________________________________________
processingInstruction(OUString const &,OUString const &)156 void LibraryImport::processingInstruction(
157     OUString const & /*rTarget*/, OUString const & /*rData*/ )
158     throw (xml::sax::SAXException, RuntimeException)
159 {
160 }
161 //__________________________________________________________________________________________________
setDocumentLocator(Reference<xml::sax::XLocator> const &)162 void LibraryImport::setDocumentLocator(
163     Reference< xml::sax::XLocator > const & /*xLocator*/ )
164     throw (xml::sax::SAXException, RuntimeException)
165 {
166 }
167 //__________________________________________________________________________________________________
startRootElement(sal_Int32 nUid,OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes)168 Reference< xml::input::XElement > LibraryImport::startRootElement(
169     sal_Int32 nUid, OUString const & rLocalName,
170     Reference< xml::input::XAttributes > const & xAttributes )
171     throw (xml::sax::SAXException, RuntimeException)
172 {
173     if (XMLNS_LIBRARY_UID != nUid)
174     {
175         throw xml::sax::SAXException(
176             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
177             Reference< XInterface >(), Any() );
178     }
179     else if (mpLibArray && rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("libraries") ))
180     {
181         return new LibrariesElement( rLocalName, xAttributes, 0, this );
182     }
183     else if (mpLibDesc && rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("library") ))
184     {
185         LibDescriptor& aDesc = *mpLibDesc;
186         aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = sal_False;
187 
188         aDesc.aName = xAttributes->getValueByUidName(
189             XMLNS_LIBRARY_UID, OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
190         getBoolAttr(
191             &aDesc.bReadOnly,
192             OUString( RTL_CONSTASCII_USTRINGPARAM("readonly") ), xAttributes,
193             XMLNS_LIBRARY_UID );
194         getBoolAttr(
195             &aDesc.bPasswordProtected,
196             OUString( RTL_CONSTASCII_USTRINGPARAM("passwordprotected") ),
197             xAttributes, XMLNS_LIBRARY_UID );
198         getBoolAttr(
199             &aDesc.bPreload,
200             OUString( RTL_CONSTASCII_USTRINGPARAM("preload") ),
201             xAttributes, XMLNS_LIBRARY_UID );
202 
203         return new LibraryElement( rLocalName, xAttributes, 0, this );
204     }
205     else
206     {
207         throw xml::sax::SAXException(
208             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal root element (expected libraries) given: ") ) +
209             rLocalName, Reference< XInterface >(), Any() );
210     }
211 }
212 //__________________________________________________________________________________________________
~LibraryImport()213 LibraryImport::~LibraryImport()
214     SAL_THROW( () )
215 {
216 #if OSL_DEBUG_LEVEL > 1
217     OSL_TRACE( "LibraryImport::~LibraryImport().\n" );
218 #endif
219 }
220 
221 //##################################################################################################
222 
223 
224 // libraries
225 //__________________________________________________________________________________________________
startChildElement(sal_Int32 nUid,OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes)226 Reference< xml::input::XElement > LibrariesElement::startChildElement(
227     sal_Int32 nUid, OUString const & rLocalName,
228     Reference< xml::input::XAttributes > const & xAttributes )
229     throw (xml::sax::SAXException, RuntimeException)
230 {
231     if (_pImport->XMLNS_LIBRARY_UID != nUid)
232     {
233         throw xml::sax::SAXException(
234             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
235             Reference< XInterface >(), Any() );
236     }
237     // library
238     else if (rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("library") ))
239     {
240         LibDescriptor aDesc;
241         aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = sal_False;
242 
243         aDesc.aName = xAttributes->getValueByUidName(
244             _pImport->XMLNS_LIBRARY_UID,
245             OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
246         aDesc.aStorageURL = xAttributes->getValueByUidName(
247             _pImport->XMLNS_XLINK_UID,
248             OUString( RTL_CONSTASCII_USTRINGPARAM("href") ) );
249         getBoolAttr(
250             &aDesc.bLink,
251             OUString( RTL_CONSTASCII_USTRINGPARAM("link") ),
252             xAttributes, _pImport->XMLNS_LIBRARY_UID );
253         getBoolAttr(
254             &aDesc.bReadOnly,
255             OUString( RTL_CONSTASCII_USTRINGPARAM("readonly") ),
256             xAttributes, _pImport->XMLNS_LIBRARY_UID );
257         getBoolAttr(
258             &aDesc.bPasswordProtected,
259             OUString( RTL_CONSTASCII_USTRINGPARAM("passwordprotected") ),
260             xAttributes, _pImport->XMLNS_LIBRARY_UID );
261 
262         mLibDescriptors.push_back( aDesc );
263         return new LibraryElement( rLocalName, xAttributes, this, _pImport );
264     }
265     else
266     {
267         throw xml::sax::SAXException(
268             OUString( RTL_CONSTASCII_USTRINGPARAM("expected styles ot bulletinboard element!") ),
269             Reference< XInterface >(), Any() );
270     }
271 }
272 //__________________________________________________________________________________________________
endElement()273 void LibrariesElement::endElement()
274     throw (xml::sax::SAXException, RuntimeException)
275 {
276     sal_Int32 nLibCount = _pImport->mpLibArray->mnLibCount = (sal_Int32)mLibDescriptors.size();
277     _pImport->mpLibArray->mpLibs = new LibDescriptor[ nLibCount ];
278 
279     for( sal_Int32 i = 0 ; i < nLibCount ; i++ )
280     {
281         const LibDescriptor& rLib = mLibDescriptors[i];
282         _pImport->mpLibArray->mpLibs[i] = rLib;
283     }
284 }
285 
286 // library
287 //__________________________________________________________________________________________________
startChildElement(sal_Int32 nUid,OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes)288 Reference< xml::input::XElement > LibraryElement::startChildElement(
289     sal_Int32 nUid, OUString const & rLocalName,
290     Reference< xml::input::XAttributes > const & xAttributes )
291     throw (xml::sax::SAXException, RuntimeException)
292 {
293     if (_pImport->XMLNS_LIBRARY_UID != nUid)
294     {
295         throw xml::sax::SAXException(
296             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
297             Reference< XInterface >(), Any() );
298     }
299     // library
300     else if (rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("element") ))
301     {
302         OUString aValue( xAttributes->getValueByUidName(
303             _pImport->XMLNS_LIBRARY_UID,
304             OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) ) );
305         if (aValue.getLength())
306             mElements.push_back( aValue );
307 
308         return new LibElementBase( rLocalName, xAttributes, this, _pImport );
309     }
310     else
311     {
312         throw xml::sax::SAXException(
313             OUString( RTL_CONSTASCII_USTRINGPARAM("expected styles ot bulletinboard element!") ),
314             Reference< XInterface >(), Any() );
315     }
316 }
317 //__________________________________________________________________________________________________
endElement()318 void LibraryElement::endElement()
319     throw (xml::sax::SAXException, RuntimeException)
320 {
321     sal_Int32 nElementCount = mElements.size();
322     Sequence< OUString > aElementNames( nElementCount );
323     OUString* pElementNames = aElementNames.getArray();
324     for( sal_Int32 i = 0 ; i < nElementCount ; i++ )
325         pElementNames[i] = mElements[i];
326 
327     LibDescriptor* pLib = _pImport->mpLibDesc;
328     if( !pLib )
329         pLib = &static_cast< LibrariesElement* >( _pParent )->mLibDescriptors.back();
330     pLib->aElementNames = aElementNames;
331 }
332 
333 
334 //##################################################################################################
335 
336 Reference< ::com::sun::star::xml::sax::XDocumentHandler >
importLibraryContainer(LibDescriptorArray * pLibArray)337 SAL_CALL importLibraryContainer( LibDescriptorArray* pLibArray )
338         SAL_THROW( (Exception) )
339 {
340     return ::xmlscript::createDocumentHandler(
341         static_cast< xml::input::XRoot * >( new LibraryImport( pLibArray ) ) );
342 }
343 
344 //##################################################################################################
345 
346 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >
importLibrary(LibDescriptor & rLib)347 SAL_CALL importLibrary( LibDescriptor& rLib )
348         SAL_THROW( (::com::sun::star::uno::Exception) )
349 {
350     return ::xmlscript::createDocumentHandler(
351         static_cast< xml::input::XRoot * >( new LibraryImport( &rLib ) ) );
352 }
353 
354 
355 //##################################################################################################
356 
LibDescriptorArray(sal_Int32 nLibCount)357 LibDescriptorArray::LibDescriptorArray( sal_Int32 nLibCount )
358 {
359     mnLibCount = nLibCount;
360     mpLibs = new LibDescriptor[ mnLibCount ];
361 }
362 
~LibDescriptorArray()363 LibDescriptorArray::~LibDescriptorArray()
364 {
365     delete[] mpLibs;
366 }
367 
368 }
369