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: XMLEventImportHelper.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"
34 #ifndef _XMLOFF_XMLEVENTIMPORTHELPER_HXX
35 #include "XMLEventImportHelper.hxx"
37 #include <tools/debug.hxx>
38 #include <xmloff/xmlimp.hxx>
39 #include <xmloff/nmspmap.hxx>
40 #include "xmlnmspe.hxx"
41 #include "xmlerror.hxx"
43 using ::rtl::OUString
;
44 using ::com::sun::star::xml::sax::XAttributeList
;
45 using ::com::sun::star::uno::Reference
;
46 using ::com::sun::star::uno::Sequence
;
48 XMLEventImportHelper::XMLEventImportHelper() :
50 pEventNameMap(new NameMap()),
56 XMLEventImportHelper::~XMLEventImportHelper()
59 FactoryMap::iterator aEnd
= aFactoryMap
.end();
60 for(FactoryMap::iterator aIter
= aFactoryMap
.begin();
72 void XMLEventImportHelper::RegisterFactory(
73 const OUString
& rLanguage
,
74 XMLEventContextFactory
* pFactory
)
76 DBG_ASSERT(pFactory
!= NULL
, "I need a factory.");
79 aFactoryMap
[rLanguage
] = pFactory
;
83 void XMLEventImportHelper::AddTranslationTable(
84 const XMLEventNameTranslation
* pTransTable
)
86 if (NULL
!= pTransTable
)
88 // put translation table into map
89 for(const XMLEventNameTranslation
* pTrans
= pTransTable
;
90 pTrans
->sAPIName
!= NULL
;
93 XMLEventName
aName( pTrans
->nPrefix
, pTrans
->sXMLName
);
95 // check for conflicting entries
96 DBG_ASSERT(pEventNameMap
->find(aName
) == pEventNameMap
->end(),
97 "conflicting event translations");
99 // assign new translation
100 (*pEventNameMap
)[aName
] =
101 OUString::createFromAscii(pTrans
->sAPIName
);
107 void XMLEventImportHelper::PushTranslationTable()
109 // save old map and install new one
110 aEventNameMapList
.push_back(pEventNameMap
);
111 pEventNameMap
= new NameMap();
114 void XMLEventImportHelper::PopTranslationTable()
116 DBG_ASSERT(aEventNameMapList
.size() > 0,
117 "no translation tables left to pop");
118 if ( !aEventNameMapList
.empty() )
120 // delete current and install old map
121 delete pEventNameMap
;
122 pEventNameMap
= aEventNameMapList
.back();
123 aEventNameMapList
.pop_back();
128 SvXMLImportContext
* XMLEventImportHelper::CreateContext(
129 SvXMLImport
& rImport
,
131 const OUString
& rLocalName
,
132 const Reference
<XAttributeList
> & xAttrList
,
133 XMLEventsImportContext
* rEvents
,
134 const OUString
& rXmlEventName
,
135 const OUString
& rLanguage
)
137 SvXMLImportContext
* pContext
= NULL
;
139 // translate event name form xml to api
141 sal_uInt16 nMacroPrefix
=
142 rImport
.GetNamespaceMap().GetKeyByAttrName( rXmlEventName
,
144 XMLEventName
aEventName( nMacroPrefix
, sMacroName
);
145 NameMap::iterator aNameIter
= pEventNameMap
->find(aEventName
);
146 if (aNameIter
!= pEventNameMap
->end())
148 OUString aScriptLanguage
;
149 sal_uInt16 nScriptPrefix
= rImport
.GetNamespaceMap().
150 GetKeyByAttrName( rLanguage
, &aScriptLanguage
);
151 if( XML_NAMESPACE_OOO
!= nScriptPrefix
)
152 aScriptLanguage
= rLanguage
;
155 FactoryMap::iterator aFactoryIterator
=
156 aFactoryMap
.find(aScriptLanguage
);
157 if (aFactoryIterator
!= aFactoryMap
.end())
159 // delegate to factory
160 pContext
= aFactoryIterator
->second
->CreateContext(
161 rImport
, nPrefix
, rLocalName
, xAttrList
,
162 rEvents
, aNameIter
->second
, aScriptLanguage
);
166 // default context (if no context was created above)
167 if( NULL
== pContext
)
169 pContext
= new SvXMLImportContext(rImport
, nPrefix
, rLocalName
);
171 Sequence
<OUString
> aMsgParams(2);
173 aMsgParams
[0] = rXmlEventName
;
174 aMsgParams
[1] = rLanguage
;
176 rImport
.SetError(XMLERROR_FLAG_ERROR
| XMLERROR_ILLEGAL_EVENT
,