xref: /trunk/main/xmlscript/source/xmllib_imexp/xmllib_import.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 <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 {
44     return static_cast< xml::input::XElement * >( _pParent );
45 }
46 //__________________________________________________________________________________________________
getLocalName()47 OUString LibElementBase::getLocalName()
48 {
49     return _aLocalName;
50 }
51 //__________________________________________________________________________________________________
getUid()52 sal_Int32 LibElementBase::getUid()
53 {
54     return _pImport->XMLNS_LIBRARY_UID;
55 }
56 //__________________________________________________________________________________________________
getAttributes()57 Reference< xml::input::XAttributes > LibElementBase::getAttributes()
58 {
59     return _xAttributes;
60 }
61 //__________________________________________________________________________________________________
ignorableWhitespace(OUString const &)62 void LibElementBase::ignorableWhitespace(
63     OUString const & /*rWhitespaces*/ )
64 {
65 }
66 //__________________________________________________________________________________________________
characters(OUString const &)67 void LibElementBase::characters( OUString const & /*rChars*/ )
68 {
69     // not used, all characters ignored
70 }
71 
72 //__________________________________________________________________________________________________
processingInstruction(OUString const &,OUString const &)73 void LibElementBase::processingInstruction(
74     OUString const & /*rTarget*/, OUString const & /*rData*/ )
75 {
76 }
77 
78 //__________________________________________________________________________________________________
endElement()79 void LibElementBase::endElement()
80 {
81 }
82 //__________________________________________________________________________________________________
startChildElement(sal_Int32,OUString const &,Reference<xml::input::XAttributes> const &)83 Reference< xml::input::XElement > LibElementBase::startChildElement(
84     sal_Int32 /*nUid*/, OUString const & /*rLocalName*/,
85     Reference< xml::input::XAttributes > const & /*xAttributes*/ )
86 {
87     throw xml::sax::SAXException(
88         OUString( RTL_CONSTASCII_USTRINGPARAM("unexpected element!") ),
89         Reference< XInterface >(), Any() );
90 }
91 
92 //__________________________________________________________________________________________________
LibElementBase(OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes,LibElementBase * pParent,LibraryImport * pImport)93 LibElementBase::LibElementBase(
94     OUString const & rLocalName,
95     Reference< xml::input::XAttributes > const & xAttributes,
96     LibElementBase * pParent, LibraryImport * pImport )
97     SAL_THROW( () )
98     : _pImport( pImport )
99     , _pParent( pParent )
100     , _aLocalName( rLocalName )
101     , _xAttributes( xAttributes )
102 {
103     _pImport->acquire();
104 
105     if (_pParent)
106     {
107         _pParent->acquire();
108     }
109 }
110 //__________________________________________________________________________________________________
~LibElementBase()111 LibElementBase::~LibElementBase()
112     SAL_THROW( () )
113 {
114     _pImport->release();
115 
116     if (_pParent)
117     {
118         _pParent->release();
119     }
120 
121 #if OSL_DEBUG_LEVEL > 1
122     OString aStr( OUStringToOString( _aLocalName, RTL_TEXTENCODING_ASCII_US ) );
123     OSL_TRACE( "LibElementBase::~LibElementBase(): %s\n", aStr.getStr() );
124 #endif
125 }
126 
127 //##################################################################################################
128 
129 // XRoot
130 
131 //______________________________________________________________________________
startDocument(Reference<xml::input::XNamespaceMapping> const & xNamespaceMapping)132 void LibraryImport::startDocument(
133     Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
134 {
135     XMLNS_LIBRARY_UID = xNamespaceMapping->getUidByUri(
136         OUSTR(XMLNS_LIBRARY_URI) );
137     XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri(
138         OUSTR(XMLNS_XLINK_URI) );
139 }
140 //__________________________________________________________________________________________________
endDocument()141 void LibraryImport::endDocument()
142 {
143 }
144 //__________________________________________________________________________________________________
processingInstruction(OUString const &,OUString const &)145 void LibraryImport::processingInstruction(
146     OUString const & /*rTarget*/, OUString const & /*rData*/ )
147 {
148 }
149 //__________________________________________________________________________________________________
setDocumentLocator(Reference<xml::sax::XLocator> const &)150 void LibraryImport::setDocumentLocator(
151     Reference< xml::sax::XLocator > const & /*xLocator*/ )
152 {
153 }
154 //__________________________________________________________________________________________________
startRootElement(sal_Int32 nUid,OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes)155 Reference< xml::input::XElement > LibraryImport::startRootElement(
156     sal_Int32 nUid, OUString const & rLocalName,
157     Reference< xml::input::XAttributes > const & xAttributes )
158 {
159     if (XMLNS_LIBRARY_UID != nUid)
160     {
161         throw xml::sax::SAXException(
162             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
163             Reference< XInterface >(), Any() );
164     }
165     else if (mpLibArray && rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("libraries") ))
166     {
167         return new LibrariesElement( rLocalName, xAttributes, 0, this );
168     }
169     else if (mpLibDesc && rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("library") ))
170     {
171         LibDescriptor& aDesc = *mpLibDesc;
172         aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = sal_False;
173 
174         aDesc.aName = xAttributes->getValueByUidName(
175             XMLNS_LIBRARY_UID, OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
176         getBoolAttr(
177             &aDesc.bReadOnly,
178             OUString( RTL_CONSTASCII_USTRINGPARAM("readonly") ), xAttributes,
179             XMLNS_LIBRARY_UID );
180         getBoolAttr(
181             &aDesc.bPasswordProtected,
182             OUString( RTL_CONSTASCII_USTRINGPARAM("passwordprotected") ),
183             xAttributes, XMLNS_LIBRARY_UID );
184         getBoolAttr(
185             &aDesc.bPreload,
186             OUString( RTL_CONSTASCII_USTRINGPARAM("preload") ),
187             xAttributes, XMLNS_LIBRARY_UID );
188 
189         return new LibraryElement( rLocalName, xAttributes, 0, this );
190     }
191     else
192     {
193         throw xml::sax::SAXException(
194             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal root element (expected libraries) given: ") ) +
195             rLocalName, Reference< XInterface >(), Any() );
196     }
197 }
198 //__________________________________________________________________________________________________
~LibraryImport()199 LibraryImport::~LibraryImport()
200     SAL_THROW( () )
201 {
202 #if OSL_DEBUG_LEVEL > 1
203     OSL_TRACE( "LibraryImport::~LibraryImport().\n" );
204 #endif
205 }
206 
207 //##################################################################################################
208 
209 
210 // libraries
211 //__________________________________________________________________________________________________
startChildElement(sal_Int32 nUid,OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes)212 Reference< xml::input::XElement > LibrariesElement::startChildElement(
213     sal_Int32 nUid, OUString const & rLocalName,
214     Reference< xml::input::XAttributes > const & xAttributes )
215 {
216     if (_pImport->XMLNS_LIBRARY_UID != nUid)
217     {
218         throw xml::sax::SAXException(
219             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
220             Reference< XInterface >(), Any() );
221     }
222     // library
223     else if (rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("library") ))
224     {
225         LibDescriptor aDesc;
226         aDesc.bLink = aDesc.bReadOnly = aDesc.bPasswordProtected = aDesc.bPreload = sal_False;
227 
228         aDesc.aName = xAttributes->getValueByUidName(
229             _pImport->XMLNS_LIBRARY_UID,
230             OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
231         aDesc.aStorageURL = xAttributes->getValueByUidName(
232             _pImport->XMLNS_XLINK_UID,
233             OUString( RTL_CONSTASCII_USTRINGPARAM("href") ) );
234         getBoolAttr(
235             &aDesc.bLink,
236             OUString( RTL_CONSTASCII_USTRINGPARAM("link") ),
237             xAttributes, _pImport->XMLNS_LIBRARY_UID );
238         getBoolAttr(
239             &aDesc.bReadOnly,
240             OUString( RTL_CONSTASCII_USTRINGPARAM("readonly") ),
241             xAttributes, _pImport->XMLNS_LIBRARY_UID );
242         getBoolAttr(
243             &aDesc.bPasswordProtected,
244             OUString( RTL_CONSTASCII_USTRINGPARAM("passwordprotected") ),
245             xAttributes, _pImport->XMLNS_LIBRARY_UID );
246 
247         mLibDescriptors.push_back( aDesc );
248         return new LibraryElement( rLocalName, xAttributes, this, _pImport );
249     }
250     else
251     {
252         throw xml::sax::SAXException(
253             OUString( RTL_CONSTASCII_USTRINGPARAM("expected styles ot bulletinboard element!") ),
254             Reference< XInterface >(), Any() );
255     }
256 }
257 //__________________________________________________________________________________________________
endElement()258 void LibrariesElement::endElement()
259 {
260     sal_Int32 nLibCount = _pImport->mpLibArray->mnLibCount = (sal_Int32)mLibDescriptors.size();
261     _pImport->mpLibArray->mpLibs = new LibDescriptor[ nLibCount ];
262 
263     for( sal_Int32 i = 0 ; i < nLibCount ; i++ )
264     {
265         const LibDescriptor& rLib = mLibDescriptors[i];
266         _pImport->mpLibArray->mpLibs[i] = rLib;
267     }
268 }
269 
270 // library
271 //__________________________________________________________________________________________________
startChildElement(sal_Int32 nUid,OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes)272 Reference< xml::input::XElement > LibraryElement::startChildElement(
273     sal_Int32 nUid, OUString const & rLocalName,
274     Reference< xml::input::XAttributes > const & xAttributes )
275 {
276     if (_pImport->XMLNS_LIBRARY_UID != nUid)
277     {
278         throw xml::sax::SAXException(
279             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
280             Reference< XInterface >(), Any() );
281     }
282     // library
283     else if (rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("element") ))
284     {
285         OUString aValue( xAttributes->getValueByUidName(
286             _pImport->XMLNS_LIBRARY_UID,
287             OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) ) );
288         if (aValue.getLength())
289             mElements.push_back( aValue );
290 
291         return new LibElementBase( rLocalName, xAttributes, this, _pImport );
292     }
293     else
294     {
295         throw xml::sax::SAXException(
296             OUString( RTL_CONSTASCII_USTRINGPARAM("expected styles ot bulletinboard element!") ),
297             Reference< XInterface >(), Any() );
298     }
299 }
300 //__________________________________________________________________________________________________
endElement()301 void LibraryElement::endElement()
302 {
303     sal_Int32 nElementCount = mElements.size();
304     Sequence< OUString > aElementNames( nElementCount );
305     OUString* pElementNames = aElementNames.getArray();
306     for( sal_Int32 i = 0 ; i < nElementCount ; i++ )
307         pElementNames[i] = mElements[i];
308 
309     LibDescriptor* pLib = _pImport->mpLibDesc;
310     if( !pLib )
311         pLib = &static_cast< LibrariesElement* >( _pParent )->mLibDescriptors.back();
312     pLib->aElementNames = aElementNames;
313 }
314 
315 
316 //##################################################################################################
317 
318 Reference< ::com::sun::star::xml::sax::XDocumentHandler >
importLibraryContainer(LibDescriptorArray * pLibArray)319 SAL_CALL importLibraryContainer( LibDescriptorArray* pLibArray )
320 {
321     return ::xmlscript::createDocumentHandler(
322         static_cast< xml::input::XRoot * >( new LibraryImport( pLibArray ) ) );
323 }
324 
325 //##################################################################################################
326 
327 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >
importLibrary(LibDescriptor & rLib)328 SAL_CALL importLibrary( LibDescriptor& rLib )
329 {
330     return ::xmlscript::createDocumentHandler(
331         static_cast< xml::input::XRoot * >( new LibraryImport( &rLib ) ) );
332 }
333 
334 
335 //##################################################################################################
336 
LibDescriptorArray(sal_Int32 nLibCount)337 LibDescriptorArray::LibDescriptorArray( sal_Int32 nLibCount )
338 {
339     mnLibCount = nLibCount;
340     mpLibs = new LibDescriptor[ mnLibCount ];
341 }
342 
~LibDescriptorArray()343 LibDescriptorArray::~LibDescriptorArray()
344 {
345     delete[] mpLibs;
346 }
347 
348 }
349