1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmllib_import.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlscript.hxx"
33 #include "imp_share.hxx"
35 #include <osl/diagnose.h>
37 #include <rtl/ustrbuf.hxx>
39 #include <xmlscript/xml_import.hxx>
40 #include <comphelper/processfactory.hxx>
46 //##################################################################################################
48 //__________________________________________________________________________________________________
49 Reference
< xml::input::XElement
> LibElementBase::getParent()
50 throw (RuntimeException
)
52 return static_cast< xml::input::XElement
* >( _pParent
);
54 //__________________________________________________________________________________________________
55 OUString
LibElementBase::getLocalName()
56 throw (RuntimeException
)
60 //__________________________________________________________________________________________________
61 sal_Int32
LibElementBase::getUid()
62 throw (RuntimeException
)
64 return _pImport
->XMLNS_LIBRARY_UID
;
66 //__________________________________________________________________________________________________
67 Reference
< xml::input::XAttributes
> LibElementBase::getAttributes()
68 throw (RuntimeException
)
72 //__________________________________________________________________________________________________
73 void LibElementBase::ignorableWhitespace(
74 OUString
const & /*rWhitespaces*/ )
75 throw (xml::sax::SAXException
, RuntimeException
)
78 //__________________________________________________________________________________________________
79 void LibElementBase::characters( OUString
const & /*rChars*/ )
80 throw (xml::sax::SAXException
, RuntimeException
)
82 // not used, all characters ignored
85 //__________________________________________________________________________________________________
86 void LibElementBase::processingInstruction(
87 OUString
const & /*rTarget*/, OUString
const & /*rData*/ )
88 throw (xml::sax::SAXException
, RuntimeException
)
92 //__________________________________________________________________________________________________
93 void LibElementBase::endElement()
94 throw (xml::sax::SAXException
, RuntimeException
)
97 //__________________________________________________________________________________________________
98 Reference
< xml::input::XElement
> LibElementBase::startChildElement(
99 sal_Int32
/*nUid*/, OUString
const & /*rLocalName*/,
100 Reference
< xml::input::XAttributes
> const & /*xAttributes*/ )
101 throw (xml::sax::SAXException
, RuntimeException
)
103 throw xml::sax::SAXException(
104 OUString( RTL_CONSTASCII_USTRINGPARAM("unexpected element!") ),
105 Reference
< XInterface
>(), Any() );
108 //__________________________________________________________________________________________________
109 LibElementBase::LibElementBase(
110 OUString
const & rLocalName
,
111 Reference
< xml::input::XAttributes
> const & xAttributes
,
112 LibElementBase
* pParent
, LibraryImport
* pImport
)
114 : _pImport( pImport
)
115 , _pParent( pParent
)
116 , _aLocalName( rLocalName
)
117 , _xAttributes( xAttributes
)
126 //__________________________________________________________________________________________________
127 LibElementBase::~LibElementBase()
137 #if OSL_DEBUG_LEVEL > 1
138 OString
aStr( OUStringToOString( _aLocalName
, RTL_TEXTENCODING_ASCII_US
) );
139 OSL_TRACE( "LibElementBase::~LibElementBase(): %s\n", aStr
.getStr() );
143 //##################################################################################################
147 //______________________________________________________________________________
148 void LibraryImport::startDocument(
149 Reference
< xml::input::XNamespaceMapping
> const & xNamespaceMapping
)
150 throw (xml::sax::SAXException
, RuntimeException
)
152 XMLNS_LIBRARY_UID
= xNamespaceMapping
->getUidByUri(
153 OUSTR(XMLNS_LIBRARY_URI
) );
154 XMLNS_XLINK_UID
= xNamespaceMapping
->getUidByUri(
155 OUSTR(XMLNS_XLINK_URI
) );
157 //__________________________________________________________________________________________________
158 void LibraryImport::endDocument()
159 throw (xml::sax::SAXException
, RuntimeException
)
162 //__________________________________________________________________________________________________
163 void LibraryImport::processingInstruction(
164 OUString
const & /*rTarget*/, OUString
const & /*rData*/ )
165 throw (xml::sax::SAXException
, RuntimeException
)
168 //__________________________________________________________________________________________________
169 void LibraryImport::setDocumentLocator(
170 Reference
< xml::sax::XLocator
> const & /*xLocator*/ )
171 throw (xml::sax::SAXException
, RuntimeException
)
174 //__________________________________________________________________________________________________
175 Reference
< xml::input::XElement
> LibraryImport::startRootElement(
176 sal_Int32 nUid
, OUString
const & rLocalName
,
177 Reference
< xml::input::XAttributes
> const & xAttributes
)
178 throw (xml::sax::SAXException
, RuntimeException
)
180 if (XMLNS_LIBRARY_UID
!= nUid
)
182 throw xml::sax::SAXException(
183 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
184 Reference
< XInterface
>(), Any() );
186 else if (mpLibArray
&& rLocalName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("libraries") ))
188 return new LibrariesElement( rLocalName
, xAttributes
, 0, this );
190 else if (mpLibDesc
&& rLocalName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("library") ))
192 LibDescriptor
& aDesc
= *mpLibDesc
;
193 aDesc
.bLink
= aDesc
.bReadOnly
= aDesc
.bPasswordProtected
= aDesc
.bPreload
= sal_False
;
195 aDesc
.aName
= xAttributes
->getValueByUidName(
196 XMLNS_LIBRARY_UID
, OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
199 OUString( RTL_CONSTASCII_USTRINGPARAM("readonly") ), xAttributes
,
202 &aDesc
.bPasswordProtected
,
203 OUString( RTL_CONSTASCII_USTRINGPARAM("passwordprotected") ),
204 xAttributes
, XMLNS_LIBRARY_UID
);
207 OUString( RTL_CONSTASCII_USTRINGPARAM("preload") ),
208 xAttributes
, XMLNS_LIBRARY_UID
);
210 return new LibraryElement( rLocalName
, xAttributes
, 0, this );
214 throw xml::sax::SAXException(
215 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal root element (expected libraries) given: ") ) +
216 rLocalName
, Reference
< XInterface
>(), Any() );
219 //__________________________________________________________________________________________________
220 LibraryImport::~LibraryImport()
223 #if OSL_DEBUG_LEVEL > 1
224 OSL_TRACE( "LibraryImport::~LibraryImport().\n" );
228 //##################################################################################################
232 //__________________________________________________________________________________________________
233 Reference
< xml::input::XElement
> LibrariesElement::startChildElement(
234 sal_Int32 nUid
, OUString
const & rLocalName
,
235 Reference
< xml::input::XAttributes
> const & xAttributes
)
236 throw (xml::sax::SAXException
, RuntimeException
)
238 if (_pImport
->XMLNS_LIBRARY_UID
!= nUid
)
240 throw xml::sax::SAXException(
241 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
242 Reference
< XInterface
>(), Any() );
245 else if (rLocalName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("library") ))
248 aDesc
.bLink
= aDesc
.bReadOnly
= aDesc
.bPasswordProtected
= aDesc
.bPreload
= sal_False
;
250 aDesc
.aName
= xAttributes
->getValueByUidName(
251 _pImport
->XMLNS_LIBRARY_UID
,
252 OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
253 aDesc
.aStorageURL
= xAttributes
->getValueByUidName(
254 _pImport
->XMLNS_XLINK_UID
,
255 OUString( RTL_CONSTASCII_USTRINGPARAM("href") ) );
258 OUString( RTL_CONSTASCII_USTRINGPARAM("link") ),
259 xAttributes
, _pImport
->XMLNS_LIBRARY_UID
);
262 OUString( RTL_CONSTASCII_USTRINGPARAM("readonly") ),
263 xAttributes
, _pImport
->XMLNS_LIBRARY_UID
);
265 &aDesc
.bPasswordProtected
,
266 OUString( RTL_CONSTASCII_USTRINGPARAM("passwordprotected") ),
267 xAttributes
, _pImport
->XMLNS_LIBRARY_UID
);
269 mLibDescriptors
.push_back( aDesc
);
270 return new LibraryElement( rLocalName
, xAttributes
, this, _pImport
);
274 throw xml::sax::SAXException(
275 OUString( RTL_CONSTASCII_USTRINGPARAM("expected styles ot bulletinboard element!") ),
276 Reference
< XInterface
>(), Any() );
279 //__________________________________________________________________________________________________
280 void LibrariesElement::endElement()
281 throw (xml::sax::SAXException
, RuntimeException
)
283 sal_Int32 nLibCount
= _pImport
->mpLibArray
->mnLibCount
= (sal_Int32
)mLibDescriptors
.size();
284 _pImport
->mpLibArray
->mpLibs
= new LibDescriptor
[ nLibCount
];
286 for( sal_Int32 i
= 0 ; i
< nLibCount
; i
++ )
288 const LibDescriptor
& rLib
= mLibDescriptors
[i
];
289 _pImport
->mpLibArray
->mpLibs
[i
] = rLib
;
294 //__________________________________________________________________________________________________
295 Reference
< xml::input::XElement
> LibraryElement::startChildElement(
296 sal_Int32 nUid
, OUString
const & rLocalName
,
297 Reference
< xml::input::XAttributes
> const & xAttributes
)
298 throw (xml::sax::SAXException
, RuntimeException
)
300 if (_pImport
->XMLNS_LIBRARY_UID
!= nUid
)
302 throw xml::sax::SAXException(
303 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
304 Reference
< XInterface
>(), Any() );
307 else if (rLocalName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("element") ))
309 OUString
aValue( xAttributes
->getValueByUidName(
310 _pImport
->XMLNS_LIBRARY_UID
,
311 OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) ) );
312 if (aValue
.getLength())
313 mElements
.push_back( aValue
);
315 return new LibElementBase( rLocalName
, xAttributes
, this, _pImport
);
319 throw xml::sax::SAXException(
320 OUString( RTL_CONSTASCII_USTRINGPARAM("expected styles ot bulletinboard element!") ),
321 Reference
< XInterface
>(), Any() );
324 //__________________________________________________________________________________________________
325 void LibraryElement::endElement()
326 throw (xml::sax::SAXException
, RuntimeException
)
328 sal_Int32 nElementCount
= mElements
.size();
329 Sequence
< OUString
> aElementNames( nElementCount
);
330 OUString
* pElementNames
= aElementNames
.getArray();
331 for( sal_Int32 i
= 0 ; i
< nElementCount
; i
++ )
332 pElementNames
[i
] = mElements
[i
];
334 LibDescriptor
* pLib
= _pImport
->mpLibDesc
;
336 pLib
= &static_cast< LibrariesElement
* >( _pParent
)->mLibDescriptors
.back();
337 pLib
->aElementNames
= aElementNames
;
341 //##################################################################################################
343 Reference
< ::com::sun::star::xml::sax::XDocumentHandler
>
344 SAL_CALL
importLibraryContainer( LibDescriptorArray
* pLibArray
)
345 SAL_THROW( (Exception
) )
347 return ::xmlscript::createDocumentHandler(
348 static_cast< xml::input::XRoot
* >( new LibraryImport( pLibArray
) ) );
351 //##################################################################################################
353 ::com::sun::star::uno::Reference
< ::com::sun::star::xml::sax::XDocumentHandler
>
354 SAL_CALL
importLibrary( LibDescriptor
& rLib
)
355 SAL_THROW( (::com::sun::star::uno::Exception
) )
357 return ::xmlscript::createDocumentHandler(
358 static_cast< xml::input::XRoot
* >( new LibraryImport( &rLib
) ) );
362 //##################################################################################################
364 LibDescriptorArray::LibDescriptorArray( sal_Int32 nLibCount
)
366 mnLibCount
= nLibCount
;
367 mpLibs
= new LibDescriptor
[ mnLibCount
];
370 LibDescriptorArray::~LibDescriptorArray()