1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <XMLEventImportHelper.hxx>
22 #include <tools/debug.hxx>
23 #include <xmloff/xmlimp.hxx>
24 #include <xmloff/namespacemap.hxx>
25 #include <xmloff/xmlnamespace.hxx>
26 #include <xmloff/xmlerror.hxx>
28 using ::com::sun::star::uno::Reference
;
30 XMLEventImportHelper::XMLEventImportHelper() :
31 pEventNameMap(new NameMap
)
35 XMLEventImportHelper::~XMLEventImportHelper()
41 pEventNameMap
.reset();
44 void XMLEventImportHelper::RegisterFactory(
45 const OUString
& rLanguage
,
46 std::unique_ptr
<XMLEventContextFactory
> pFactory
)
49 aFactoryMap
[rLanguage
] = std::move(pFactory
);
52 void XMLEventImportHelper::AddTranslationTable(
53 const XMLEventNameTranslation
* pTransTable
)
55 if (nullptr == pTransTable
)
58 // put translation table into map
59 for(const XMLEventNameTranslation
* pTrans
= pTransTable
;
60 !pTrans
->sAPIName
.isEmpty();
63 XMLEventName
aName( pTrans
->nPrefix
, pTrans
->sXMLName
);
65 // check for conflicting entries
66 DBG_ASSERT(pEventNameMap
->find(aName
) == pEventNameMap
->end(),
67 "conflicting event translations");
69 // assign new translation
70 (*pEventNameMap
)[aName
] = pTrans
->sAPIName
;
75 void XMLEventImportHelper::PushTranslationTable()
77 // save old map and install new one
78 aEventNameMapVector
.push_back(std::move(pEventNameMap
));
79 pEventNameMap
.reset( new NameMap
);
82 void XMLEventImportHelper::PopTranslationTable()
84 DBG_ASSERT(!aEventNameMapVector
.empty(),
85 "no translation tables left to pop");
86 if ( !aEventNameMapVector
.empty() )
88 // delete current and install old map
89 pEventNameMap
= std::move(aEventNameMapVector
.back());
90 aEventNameMapVector
.pop_back();
95 SvXMLImportContext
* XMLEventImportHelper::CreateContext(
97 const Reference
<css::xml::sax::XFastAttributeList
> & xAttrList
,
98 XMLEventsImportContext
* rEvents
,
99 const OUString
& rXmlEventName
,
100 const OUString
& rLanguage
)
102 SvXMLImportContext
* pContext
= nullptr;
104 // translate event name from xml to api
106 sal_uInt16 nMacroPrefix
=
107 rImport
.GetNamespaceMap().GetKeyByAttrValueQName(rXmlEventName
,
109 XMLEventName
aEventName( nMacroPrefix
, sMacroName
);
110 NameMap::iterator aNameIter
= pEventNameMap
->find(aEventName
);
111 if (aNameIter
!= pEventNameMap
->end())
113 OUString aScriptLanguage
;
114 sal_uInt16 nScriptPrefix
= rImport
.GetNamespaceMap().
115 GetKeyByAttrValueQName(rLanguage
, &aScriptLanguage
);
116 if( XML_NAMESPACE_OOO
!= nScriptPrefix
)
117 aScriptLanguage
= rLanguage
;
120 FactoryMap::iterator aFactoryIterator
=
121 aFactoryMap
.find(aScriptLanguage
);
122 if (aFactoryIterator
!= aFactoryMap
.end())
124 // delegate to factory
125 pContext
= aFactoryIterator
->second
->CreateContext(
127 rEvents
, aNameIter
->second
);
131 // default context (if no context was created above)
132 if( nullptr == pContext
)
134 pContext
= new SvXMLImportContext(rImport
);
136 rImport
.SetError(XMLERROR_FLAG_ERROR
| XMLERROR_ILLEGAL_EVENT
,
137 { rXmlEventName
, rLanguage
});
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */