bump product version to 6.4.0.3
[LibreOffice.git] / xmloff / source / script / XMLEventImportHelper.cxx
blob1e2c39e144ae544ae2e5a28c1d7aa3b72f8619d7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/nmspmap.hxx>
25 #include <xmloff/xmlnmspe.hxx>
26 #include <xmloff/xmlerror.hxx>
28 using ::com::sun::star::xml::sax::XAttributeList;
29 using ::com::sun::star::uno::Reference;
30 using ::com::sun::star::uno::Sequence;
32 XMLEventImportHelper::XMLEventImportHelper() :
33 aFactoryMap(),
34 pEventNameMap(new NameMap),
35 aEventNameMapVector()
39 XMLEventImportHelper::~XMLEventImportHelper()
41 // delete factories
42 aFactoryMap.clear();
44 // delete name map
45 pEventNameMap.reset();
48 void XMLEventImportHelper::RegisterFactory(
49 const OUString& rLanguage,
50 std::unique_ptr<XMLEventContextFactory> pFactory )
52 assert(pFactory);
53 aFactoryMap[rLanguage] = std::move(pFactory);
56 void XMLEventImportHelper::AddTranslationTable(
57 const XMLEventNameTranslation* pTransTable )
59 if (nullptr != pTransTable)
61 // put translation table into map
62 for(const XMLEventNameTranslation* pTrans = pTransTable;
63 pTrans->sAPIName != nullptr;
64 pTrans++)
66 XMLEventName aName( pTrans->nPrefix, pTrans->sXMLName );
68 // check for conflicting entries
69 DBG_ASSERT(pEventNameMap->find(aName) == pEventNameMap->end(),
70 "conflicting event translations");
72 // assign new translation
73 (*pEventNameMap)[aName] =
74 OUString::createFromAscii(pTrans->sAPIName);
77 // else? ignore!
80 void XMLEventImportHelper::PushTranslationTable()
82 // save old map and install new one
83 aEventNameMapVector.push_back(std::move(pEventNameMap));
84 pEventNameMap.reset( new NameMap );
87 void XMLEventImportHelper::PopTranslationTable()
89 DBG_ASSERT(!aEventNameMapVector.empty(),
90 "no translation tables left to pop");
91 if ( !aEventNameMapVector.empty() )
93 // delete current and install old map
94 pEventNameMap = std::move(aEventNameMapVector.back());
95 aEventNameMapVector.pop_back();
100 SvXMLImportContext* XMLEventImportHelper::CreateContext(
101 SvXMLImport& rImport,
102 sal_uInt16 nPrefix,
103 const OUString& rLocalName,
104 const Reference<XAttributeList> & xAttrList,
105 XMLEventsImportContext* rEvents,
106 const OUString& rXmlEventName,
107 const OUString& rLanguage)
109 rImport.NotifyMacroEventRead();
111 SvXMLImportContext* pContext = nullptr;
113 // translate event name from xml to api
114 OUString sMacroName;
115 sal_uInt16 nMacroPrefix =
116 rImport.GetNamespaceMap().GetKeyByAttrName( rXmlEventName,
117 &sMacroName );
118 XMLEventName aEventName( nMacroPrefix, sMacroName );
119 NameMap::iterator aNameIter = pEventNameMap->find(aEventName);
120 if (aNameIter != pEventNameMap->end())
122 OUString aScriptLanguage;
123 sal_uInt16 nScriptPrefix = rImport.GetNamespaceMap().
124 GetKeyByAttrName( rLanguage, &aScriptLanguage );
125 if( XML_NAMESPACE_OOO != nScriptPrefix )
126 aScriptLanguage = rLanguage ;
128 // check for factory
129 FactoryMap::iterator aFactoryIterator =
130 aFactoryMap.find(aScriptLanguage);
131 if (aFactoryIterator != aFactoryMap.end())
133 // delegate to factory
134 pContext = aFactoryIterator->second->CreateContext(
135 rImport, nPrefix, rLocalName, xAttrList,
136 rEvents, aNameIter->second);
140 // default context (if no context was created above)
141 if( nullptr == pContext )
143 pContext = new SvXMLImportContext(rImport, nPrefix, rLocalName);
145 Sequence<OUString> aMsgParams(2);
147 aMsgParams[0] = rXmlEventName;
148 aMsgParams[1] = rLanguage;
150 rImport.SetError(XMLERROR_FLAG_ERROR | XMLERROR_ILLEGAL_EVENT,
151 aMsgParams);
155 return pContext;
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */