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/namespacemap.hxx>
25 #include <xmloff/xmlnamespace.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  pEventNameMap(new NameMap)
34 {
35 }
36 
38 {
39  // delete factories
40  aFactoryMap.clear();
41 
42  // delete name map
43  pEventNameMap.reset();
44 }
45 
47  const OUString& rLanguage,
48  std::unique_ptr<XMLEventContextFactory> pFactory )
49 {
50  assert(pFactory);
51  aFactoryMap[rLanguage] = std::move(pFactory);
52 }
53 
55  const XMLEventNameTranslation* pTransTable )
56 {
57  if (nullptr == pTransTable)
58  return;
59 
60  // put translation table into map
61  for(const XMLEventNameTranslation* pTrans = pTransTable;
62  pTrans->sAPIName != nullptr;
63  pTrans++)
64  {
65  XMLEventName aName( pTrans->nPrefix, pTrans->sXMLName );
66 
67  // check for conflicting entries
68  DBG_ASSERT(pEventNameMap->find(aName) == pEventNameMap->end(),
69  "conflicting event translations");
70 
71  // assign new translation
72  (*pEventNameMap)[aName] =
73  OUString::createFromAscii(pTrans->sAPIName);
74  }
75  // else? ignore!
76 }
77 
79 {
80  // save old map and install new one
81  aEventNameMapVector.push_back(std::move(pEventNameMap));
82  pEventNameMap.reset( new NameMap );
83 }
84 
86 {
88  "no translation tables left to pop");
89  if ( !aEventNameMapVector.empty() )
90  {
91  // delete current and install old map
92  pEventNameMap = std::move(aEventNameMapVector.back());
93  aEventNameMapVector.pop_back();
94  }
95 }
96 
97 
99  SvXMLImport& rImport,
100  const Reference<css::xml::sax::XFastAttributeList> & xAttrList,
101  XMLEventsImportContext* rEvents,
102  const OUString& rXmlEventName,
103  const OUString& rLanguage)
104 {
105  rImport.NotifyMacroEventRead();
106 
107  SvXMLImportContext* pContext = nullptr;
108 
109  // translate event name from xml to api
110  OUString sMacroName;
111  sal_uInt16 nMacroPrefix =
112  rImport.GetNamespaceMap().GetKeyByAttrValueQName(rXmlEventName,
113  &sMacroName );
114  XMLEventName aEventName( nMacroPrefix, sMacroName );
115  NameMap::iterator aNameIter = pEventNameMap->find(aEventName);
116  if (aNameIter != pEventNameMap->end())
117  {
118  OUString aScriptLanguage;
119  sal_uInt16 nScriptPrefix = rImport.GetNamespaceMap().
120  GetKeyByAttrValueQName(rLanguage, &aScriptLanguage);
121  if( XML_NAMESPACE_OOO != nScriptPrefix )
122  aScriptLanguage = rLanguage ;
123 
124  // check for factory
125  FactoryMap::iterator aFactoryIterator =
126  aFactoryMap.find(aScriptLanguage);
127  if (aFactoryIterator != aFactoryMap.end())
128  {
129  // delegate to factory
130  pContext = aFactoryIterator->second->CreateContext(
131  rImport, xAttrList,
132  rEvents, aNameIter->second);
133  }
134  }
135 
136  // default context (if no context was created above)
137  if( nullptr == pContext )
138  {
139  pContext = new SvXMLImportContext(rImport);
140 
142  { rXmlEventName, rLanguage });
143 
144  }
145 
146  return pContext;
147 }
148 
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::vector< std::unique_ptr< NameMap > > aEventNameMapVector
stack of previous aEventNameMap
constexpr sal_uInt16 XML_NAMESPACE_OOO
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:398
sal_uInt16 GetKeyByAttrValueQName(const OUString &rAttrName, OUString *pLocalName) const
#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
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:2072
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
SvXMLImportContext * CreateContext(SvXMLImport &rImport, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, XMLEventsImportContext *rEvents, const OUString &rXmlEventName, const OUString &rLanguage)
create an appropriate import context for a particular event
constexpr OUStringLiteral sMacroName
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