LibreOffice Module xmloff (master)  1
XMLEventImportHelper.cxx
Go to the documentation of this file.
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/nmspmap.hxx>
25 #include <xmloff/xmlnmspe.hxx>
26 #include <xmloff/xmlerror.hxx>
27 
28 using ::com::sun::star::xml::sax::XAttributeList;
29 using ::com::sun::star::uno::Reference;
30 using ::com::sun::star::uno::Sequence;
31 
33  aFactoryMap(),
34  pEventNameMap(new NameMap),
35  aEventNameMapVector()
36 {
37 }
38 
40 {
41  // delete factories
42  aFactoryMap.clear();
43 
44  // delete name map
45  pEventNameMap.reset();
46 }
47 
49  const OUString& rLanguage,
50  std::unique_ptr<XMLEventContextFactory> pFactory )
51 {
52  assert(pFactory);
53  aFactoryMap[rLanguage] = std::move(pFactory);
54 }
55 
57  const XMLEventNameTranslation* pTransTable )
58 {
59  if (nullptr != pTransTable)
60  {
61  // put translation table into map
62  for(const XMLEventNameTranslation* pTrans = pTransTable;
63  pTrans->sAPIName != nullptr;
64  pTrans++)
65  {
66  XMLEventName aName( pTrans->nPrefix, pTrans->sXMLName );
67 
68  // check for conflicting entries
69  DBG_ASSERT(pEventNameMap->find(aName) == pEventNameMap->end(),
70  "conflicting event translations");
71 
72  // assign new translation
73  (*pEventNameMap)[aName] =
74  OUString::createFromAscii(pTrans->sAPIName);
75  }
76  }
77  // else? ignore!
78 }
79 
81 {
82  // save old map and install new one
83  aEventNameMapVector.push_back(std::move(pEventNameMap));
84  pEventNameMap.reset( new NameMap );
85 }
86 
88 {
90  "no translation tables left to pop");
91  if ( !aEventNameMapVector.empty() )
92  {
93  // delete current and install old map
94  pEventNameMap = std::move(aEventNameMapVector.back());
95  aEventNameMapVector.pop_back();
96  }
97 }
98 
99 
101  SvXMLImport& rImport,
102  const Reference<XAttributeList> & xAttrList,
103  XMLEventsImportContext* rEvents,
104  const OUString& rXmlEventName,
105  const OUString& rLanguage)
106 {
107  rImport.NotifyMacroEventRead();
108 
109  SvXMLImportContext* pContext = nullptr;
110 
111  // translate event name from xml to api
112  OUString sMacroName;
113  sal_uInt16 nMacroPrefix =
114  rImport.GetNamespaceMap().GetKeyByAttrValueQName(rXmlEventName,
115  &sMacroName );
116  XMLEventName aEventName( nMacroPrefix, sMacroName );
117  NameMap::iterator aNameIter = pEventNameMap->find(aEventName);
118  if (aNameIter != pEventNameMap->end())
119  {
120  OUString aScriptLanguage;
121  sal_uInt16 nScriptPrefix = rImport.GetNamespaceMap().
122  GetKeyByAttrValueQName(rLanguage, &aScriptLanguage);
123  if( XML_NAMESPACE_OOO != nScriptPrefix )
124  aScriptLanguage = rLanguage ;
125 
126  // check for factory
127  FactoryMap::iterator aFactoryIterator =
128  aFactoryMap.find(aScriptLanguage);
129  if (aFactoryIterator != aFactoryMap.end())
130  {
131  // delegate to factory
132  pContext = aFactoryIterator->second->CreateContext(
133  rImport, xAttrList,
134  rEvents, aNameIter->second);
135  }
136  }
137 
138  // default context (if no context was created above)
139  if( nullptr == pContext )
140  {
141  pContext = new SvXMLImportContext(rImport);
142 
143  Sequence<OUString> aMsgParams(2);
144 
145  aMsgParams[0] = rXmlEventName;
146  aMsgParams[1] = rLanguage;
147 
149  aMsgParams);
150 
151  }
152 
153  return pContext;
154 }
155 
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::vector< std::unique_ptr< NameMap > > aEventNameMapVector
stack of previous aEventNameMap
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:397
SvXMLImportContext * CreateContext(SvXMLImport &rImport, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList, XMLEventsImportContext *rEvents, const OUString &rXmlEventName, const OUString &rLanguage)
create an appropriate import context for a particular event
sal_uInt16 GetKeyByAttrValueQName(const OUString &rAttrName, OUString *pLocalName) const
Definition: nmspmap.cxx:263
#define XMLERROR_ILLEGAL_EVENT
Definition: xmlerror.hxx:56
#define DBG_ASSERT(sCon, aError)
void AddTranslationTable(const XMLEventNameTranslation *pTransTable)
add event name translation to the internal table
constexpr sal_uInt16 XML_NAMESPACE_OOO
Definition: xmlnmspe.hxx:62
FactoryMap aFactoryMap
map of XMLEventContextFactory objects
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
void PopTranslationTable()
recover the top-most previously saved translation table
void NotifyMacroEventRead()
Definition: xmlimp.cxx:2168
OUString aName
#define XMLERROR_FLAG_ERROR
Definition: xmlerror.hxx:36
std::unique_ptr< NameMap > pEventNameMap
map from XML to API names
void SetError(sal_Int32 nId, const css::uno::Sequence< OUString > &rMsgParams, const OUString &rExceptionMessage, const css::uno::Reference< css::xml::sax::XLocator > &rLocator)
Record an error condition that occurred during import.
void PushTranslationTable()
save the old translation table on a stack and install an empty table
::std::map< XMLEventName, OUString > NameMap
void RegisterFactory(const OUString &rLanguage, std::unique_ptr< XMLEventContextFactory > aFactory)
register a handler for a particular language type
XMLEventNameTranslation: define tables that translate between event names as used in the XML file for...
Definition: xmlevent.hxx:75
const char * sAPIName
Definition: xmlevent.hxx:77