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: XMLEventsImportContext.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_xmloff.hxx"
33 #include <xmloff/XMLEventsImportContext.hxx>
35 #ifndef _XMLOFF_XMLEVENTIMPORTHELPER_HXX
36 #include "XMLEventImportHelper.hxx"
39 #ifndef _COM_SUN_STAR_DOCUMENT_XEVENTSSUPPLIER_HPP
40 #include <com/sun/star/document/XEventsSupplier.hpp>
42 #include <tools/debug.hxx>
43 #include <xmloff/xmlimp.hxx>
44 #include <xmloff/nmspmap.hxx>
45 #include "xmlnmspe.hxx"
46 #include <xmloff/xmltoken.hxx>
47 #include "xmlerror.hxx"
49 using namespace ::com::sun::star::uno
;
50 using namespace ::xmloff::token
;
52 using ::rtl::OUString
;
53 using ::com::sun::star::xml::sax::XAttributeList
;
54 using ::com::sun::star::beans::PropertyValue
;
55 using ::com::sun::star::container::XNameReplace
;
56 using ::com::sun::star::document::XEventsSupplier
;
57 using ::com::sun::star::lang::IllegalArgumentException
;
59 TYPEINIT1(XMLEventsImportContext
, SvXMLImportContext
);
62 XMLEventsImportContext::XMLEventsImportContext(
65 const OUString
& rLocalName
) :
66 SvXMLImportContext(rImport
, nPrfx
, rLocalName
)
71 XMLEventsImportContext::XMLEventsImportContext(
74 const OUString
& rLocalName
,
75 const Reference
<XEventsSupplier
> & xEventsSupplier
) :
76 SvXMLImportContext(rImport
, nPrfx
, rLocalName
),
77 xEvents(xEventsSupplier
->getEvents())
82 XMLEventsImportContext::XMLEventsImportContext(
85 const OUString
& rLocalName
,
86 const Reference
<XNameReplace
> & xNameReplace
) :
87 SvXMLImportContext(rImport
, nPrfx
, rLocalName
),
92 XMLEventsImportContext::~XMLEventsImportContext()
94 // // if, for whatever reason, the object gets destroyed prematurely,
95 // // we need to delete the collected events
96 // EventsVector::iterator aEnd = aCollectEvents.end();
97 // for(EventsVector::iterator aIter = aCollectEvents.begin();
101 // EventNameValuesPair* pPair = &(*aIter);
104 // aCollectEvents.clear();
108 void XMLEventsImportContext::StartElement(
109 const Reference
<XAttributeList
> &)
111 // nothing to be done
114 void XMLEventsImportContext::EndElement()
116 // nothing to be done
119 SvXMLImportContext
* XMLEventsImportContext::CreateChildContext(
120 sal_uInt16 p_nPrefix
,
121 const OUString
& rLocalName
,
122 const Reference
<XAttributeList
> & xAttrList
)
124 // a) search for script:language and script:event-name attribute
125 // b) delegate to factory. The factory will:
126 // 1) translate XML event name into API event name
127 // 2) get proper event context factory from import
128 // 3) instantiate context
130 // a) search for script:language and script:event-name attribute
133 sal_Int16 nCount
= xAttrList
->getLength();
134 for (sal_Int16 nAttr
= 0; nAttr
< nCount
; nAttr
++)
137 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
138 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
), &sLocalName
);
140 if (XML_NAMESPACE_SCRIPT
== nPrefix
)
142 if (IsXMLToken(sLocalName
, XML_EVENT_NAME
))
144 sEventName
= xAttrList
->getValueByIndex(nAttr
);
146 else if (IsXMLToken(sLocalName
, XML_LANGUAGE
))
148 sLanguage
= xAttrList
->getValueByIndex(nAttr
);
150 // else: ignore -> let child context handle this
152 // else: ignore -> let child context handle this
155 // b) delegate to factory
156 return GetImport().GetEventImport().CreateContext(
157 GetImport(), p_nPrefix
, rLocalName
, xAttrList
,
158 this, sEventName
, sLanguage
);
161 void XMLEventsImportContext::SetEvents(
162 const Reference
<XEventsSupplier
> & xEventsSupplier
)
164 if (xEventsSupplier
.is())
166 SetEvents(xEventsSupplier
->getEvents());
170 void XMLEventsImportContext::SetEvents(
171 const Reference
<XNameReplace
> & xNameRepl
)
177 // now iterate over vector and a) insert b) delete all elements
178 EventsVector::iterator aEnd
= aCollectEvents
.end();
179 for(EventsVector::iterator aIter
= aCollectEvents
.begin();
183 AddEventValues(aIter
->first
, aIter
->second
);
184 // EventNameValuesPair* pPair = &(*aIter);
187 aCollectEvents
.clear();
191 sal_Bool
XMLEventsImportContext::GetEventSequence(
192 const OUString
& rName
,
193 Sequence
<PropertyValue
> & rSequence
)
195 // search through the vector
196 // (This shouldn't take a lot of time, since this method should only get
197 // called if only one (or few) events are being expected)
199 // iterate over vector until end or rName is found;
200 EventsVector::iterator aIter
= aCollectEvents
.begin();
201 while( (aIter
!= aCollectEvents
.end()) && (aIter
->first
!= rName
) )
206 // if we're not at the end, set the sequence
207 sal_Bool bRet
= (aIter
!= aCollectEvents
.end());
209 rSequence
= aIter
->second
;
214 void XMLEventsImportContext::AddEventValues(
215 const OUString
& rEventName
,
216 const Sequence
<PropertyValue
> & rValues
)
218 // if we already have the events, set them; else just collect
221 // set event (if name is known)
222 if (xEvents
->hasByName(rEventName
))
229 xEvents
->replaceByName(rEventName
, aAny
);
230 } catch ( const IllegalArgumentException
& rException
)
232 Sequence
<OUString
> aMsgParams(1);
234 aMsgParams
[0] = rEventName
;
236 GetImport().SetError(XMLERROR_FLAG_ERROR
|
237 XMLERROR_ILLEGAL_EVENT
,
238 aMsgParams
, rException
.Message
, 0);
244 // EventNameValuesPair* aPair = new EventNameValuesPair(rEventName,
246 // aCollectEvents.push_back(*aPair);
247 EventNameValuesPair
aPair(rEventName
, rValues
);
248 aCollectEvents
.push_back(aPair
);