Debian Codegraph @

xmloff/source/script/XMLEventImportHelper.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
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>
27
28 using ::com::sun::star::uno::Reference;
29
30 XMLEventImportHelper::XMLEventImportHelper() :
31 pEventNameMap(new NameMap)
32 {
33 }
34
35 XMLEventImportHelper::~XMLEventImportHelper()
36 {
37 // delete factories
38 aFactoryMap.clear();
39
40 // delete name map
41 pEventNameMap.reset();
42 }
43
44 void XMLEventImportHelper::RegisterFactory(
45 const OUString& rLanguage,
46 std::unique_ptr<XMLEventContextFactory> pFactory )
47 {
48 assert(pFactory);
49 aFactoryMap[rLanguage] = std::move(pFactory);
50 }
51
52 void XMLEventImportHelper::AddTranslationTable(
53 const XMLEventNameTranslation* pTransTable )
54 {
55 if (nullptr == pTransTable)
56 return;
57
58 // put translation table into map
59 for(const XMLEventNameTranslation* pTrans = pTransTable;
60 !pTrans->sAPIName.isEmpty();
61 pTrans++)
62 {
63 XMLEventName aName( pTrans->nPrefix, pTrans->sXMLName );
64
65 // check for conflicting entries
66 DBG_ASSERT(pEventNameMap->find(aName) == pEventNameMap->end(),
67 "conflicting event translations");
68
69 // assign new translation
70 (*pEventNameMap)[aName] = pTrans->sAPIName;
71 }
72 // else? ignore!
73 }
74
75 void XMLEventImportHelper::PushTranslationTable()
76 {
77 // save old map and install new one
78 aEventNameMapVector.push_back(std::move(pEventNameMap));
79 pEventNameMap.reset( new NameMap );
80 }
81
82 void XMLEventImportHelper::PopTranslationTable()
83 {
84 DBG_ASSERT(!aEventNameMapVector.empty(),
85 "no translation tables left to pop");
86 if ( !aEventNameMapVector.empty() )
87 {
88 // delete current and install old map
89 pEventNameMap = std::move(aEventNameMapVector.back());
90 aEventNameMapVector.pop_back();
91 }
92 }
93
94
95 SvXMLImportContext* XMLEventImportHelper::CreateContext(
96 SvXMLImport& rImport,
97 const Reference<css::xml::sax::XFastAttributeList> & xAttrList,
98 XMLEventsImportContext* rEvents,
99 const OUString& rXmlEventName,
100 const OUString& rLanguage)
101 {
102 SvXMLImportContext* pContext = nullptr;
103
104 // translate event name from xml to api
105 OUString sMacroName;
106 sal_uInt16 nMacroPrefix =
107 rImport.GetNamespaceMap().GetKeyByAttrValueQName(rXmlEventName,
108 &sMacroName );
109 XMLEventName aEventName( nMacroPrefix, sMacroName );
110 NameMap::iterator aNameIter = pEventNameMap->find(aEventName);
111 if (aNameIter != pEventNameMap->end())
112 {
113 OUString aScriptLanguage;
114 sal_uInt16 nScriptPrefix = rImport.GetNamespaceMap().
115 GetKeyByAttrValueQName(rLanguage, &aScriptLanguage);
116 if( XML_NAMESPACE_OOO != nScriptPrefix )
117 aScriptLanguage = rLanguage ;
118
119 // check for factory
120 FactoryMap::iterator aFactoryIterator =
121 aFactoryMap.find(aScriptLanguage);
122 if (aFactoryIterator != aFactoryMap.end())
123 {
124 // delegate to factory
125 pContext = aFactoryIterator->second->CreateContext(
126 rImport, xAttrList,
127 rEvents, aNameIter->second);
128 }
129 }
130
131 // default context (if no context was created above)
132 if( nullptr == pContext )
133 {
134 pContext = new SvXMLImportContext(rImport);
135
136 rImport.SetError(XMLERROR_FLAG_ERROR | XMLERROR_ILLEGAL_EVENT,
137 { rXmlEventName, rLanguage });
138
139 }
140
141 return pContext;
142 }
143
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */