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: dp_xml.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_desktop.hxx"
36 #include "rtl/ustrbuf.hxx"
37 #include "ucbhelper/content.hxx"
38 #include "com/sun/star/xml/sax/XParser.hpp"
41 using namespace ::com::sun::star
;
42 using namespace ::com::sun::star::uno
;
43 using ::rtl::OUString
;
48 //==============================================================================
50 Reference
<xml::sax::XDocumentHandler
> const & xDocHandler
,
51 ::ucbhelper::Content
& ucb_content
,
52 Reference
<XComponentContext
> const & xContext
)
55 Reference
<xml::sax::XParser
> xParser(
56 xContext
->getServiceManager()->createInstanceWithContext(
57 OUSTR("com.sun.star.xml.sax.Parser"), xContext
), UNO_QUERY_THROW
);
59 // error handler, entity resolver omitted
60 xParser
->setDocumentHandler( xDocHandler
);
61 xml::sax::InputSource source
;
62 source
.aInputStream
= ucb_content
.openStream();
63 source
.sSystemId
= ucb_content
.getURL();
64 xParser
->parseStream( source
);
67 //==============================================================================
69 Reference
<xml::input::XRoot
> const & xRoot
,
70 ::ucbhelper::Content
& ucb_content
,
71 Reference
<XComponentContext
> const & xContext
)
74 const Reference
<xml::sax::XDocumentHandler
> xDocHandler(
75 xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(
76 OUSTR("com.sun.star.xml.input.SaxDocumentHandler"),
77 Sequence
<Any
>( &arg
, 1 ), xContext
), UNO_QUERY_THROW
);
78 xml_parse( xDocHandler
, ucb_content
, xContext
);
81 //##############################################################################
83 //______________________________________________________________________________
84 XmlRootElement::XmlRootElement(
85 OUString
const & uri
, OUString
const & localname
)
88 m_localname
= localname
;
91 //______________________________________________________________________________
92 XmlRootElement::~XmlRootElement()
97 //______________________________________________________________________________
98 void XmlRootElement::startDocument(
99 Reference
<xml::input::XNamespaceMapping
> const & xMapping
)
100 throw (xml::sax::SAXException
, RuntimeException
)
102 m_xNamespaceMapping
= xMapping
;
105 m_uid
= m_xNamespaceMapping
->getUidByUri( m_uri
);
107 catch (container::NoSuchElementException
& exc
) {
108 throw xml::sax::SAXException(
109 exc
.Message
, static_cast<OWeakObject
*>(this), Any(exc
) );
113 //______________________________________________________________________________
114 void XmlRootElement::endDocument()
115 throw (xml::sax::SAXException
, RuntimeException
)
119 //______________________________________________________________________________
120 void XmlRootElement::processingInstruction(
121 OUString
const &, OUString
const & )
122 throw (xml::sax::SAXException
, RuntimeException
)
126 //______________________________________________________________________________
127 void XmlRootElement::setDocumentLocator(
128 Reference
<xml::sax::XLocator
> const & )
129 throw (xml::sax::SAXException
, RuntimeException
)
133 //______________________________________________________________________________
134 Reference
<xml::input::XElement
> XmlRootElement::startRootElement(
135 sal_Int32 uid
, OUString
const & localname
,
136 Reference
<xml::input::XAttributes
> const & xAttributes
)
137 throw (xml::sax::SAXException
, RuntimeException
)
140 if (! localname
.equals( m_localname
)) {
141 throw xml::sax::SAXException(
142 OUSTR("unexpected root element ") + localname
,
143 static_cast<OWeakObject
*>(this), Any() );
145 m_xAttributes
= xAttributes
;
150 //##############################################################################
152 //______________________________________________________________________________
153 XmlElement::~XmlElement()
157 //______________________________________________________________________________
158 void XmlElement::check_xmlns( sal_Int32 uid
) const
159 throw (xml::sax::SAXException
)
163 ::rtl::OUStringBuffer buf
;
165 RTL_CONSTASCII_STRINGPARAM("illegal xml namespace uri=\"") );
167 buf
.append( m_xNamespaceMapping
->getUriByUid( uid
) );
169 catch (container::NoSuchElementException
& exc
) {
170 throw xml::sax::SAXException(
171 exc
.Message
, static_cast<OWeakObject
*>(
172 const_cast<XmlElement
*>(this) ), Any(exc
) );
174 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") );
175 throw xml::sax::SAXException(
176 buf
.makeStringAndClear(),
177 static_cast<OWeakObject
*>( const_cast<XmlElement
*>(this) ),
183 //______________________________________________________________________________
184 Reference
<xml::input::XElement
> XmlElement::getParent()
185 throw (RuntimeException
)
190 //______________________________________________________________________________
191 OUString
XmlElement::getLocalName()
192 throw (RuntimeException
)
197 //______________________________________________________________________________
198 sal_Int32
XmlElement::getUid()
199 throw (RuntimeException
)
204 //______________________________________________________________________________
205 Reference
<xml::input::XAttributes
> XmlElement::getAttributes()
206 throw (RuntimeException
)
208 return m_xAttributes
;
211 //______________________________________________________________________________
212 void XmlElement::ignorableWhitespace(
214 throw (xml::sax::SAXException
, RuntimeException
)
218 //______________________________________________________________________________
219 void XmlElement::characters( OUString
const & chars
)
220 throw (xml::sax::SAXException
, RuntimeException
)
222 m_characters
+= chars
;
225 //______________________________________________________________________________
226 void XmlElement::processingInstruction(
227 OUString
const &, OUString
const & )
228 throw (xml::sax::SAXException
, RuntimeException
)
232 //______________________________________________________________________________
233 void XmlElement::endElement()
234 throw (xml::sax::SAXException
, RuntimeException
)
236 m_got_endElement
= true;
239 //______________________________________________________________________________
240 Reference
<xml::input::XElement
> XmlElement::startChildElement(
241 sal_Int32 uid
, OUString
const & localName
,
242 Reference
<xml::input::XAttributes
> const & )
243 throw (xml::sax::SAXException
, RuntimeException
)
245 ::rtl::OUStringBuffer buf
;
246 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("unexpected element "
248 buf
.append( localName
);
249 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\", uri=\"") );
251 buf
.append( m_xNamespaceMapping
->getUriByUid( uid
) );
253 catch (container::NoSuchElementException
& exc
) {
254 throw xml::sax::SAXException(
255 exc
.Message
, static_cast<OWeakObject
*>(this), Any(exc
) );
257 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" }!") );
258 throw xml::sax::SAXException(
259 buf
.makeStringAndClear(), static_cast<OWeakObject
*>(this), Any() );