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/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() :
34 pEventNameMap(new NameMap()),
40 XMLEventImportHelper::~XMLEventImportHelper()
43 FactoryMap::iterator aEnd
= aFactoryMap
.end();
44 for(FactoryMap::iterator aIter
= aFactoryMap
.begin();
56 void XMLEventImportHelper::RegisterFactory(
57 const OUString
& rLanguage
,
58 XMLEventContextFactory
* pFactory
)
60 DBG_ASSERT(pFactory
!= NULL
, "I need a factory.");
63 aFactoryMap
[rLanguage
] = pFactory
;
67 void XMLEventImportHelper::AddTranslationTable(
68 const XMLEventNameTranslation
* pTransTable
)
70 if (NULL
!= pTransTable
)
72 // put translation table into map
73 for(const XMLEventNameTranslation
* pTrans
= pTransTable
;
74 pTrans
->sAPIName
!= NULL
;
77 XMLEventName
aName( pTrans
->nPrefix
, pTrans
->sXMLName
);
79 // check for conflicting entries
80 DBG_ASSERT(pEventNameMap
->find(aName
) == pEventNameMap
->end(),
81 "conflicting event translations");
83 // assign new translation
84 (*pEventNameMap
)[aName
] =
85 OUString::createFromAscii(pTrans
->sAPIName
);
91 void XMLEventImportHelper::PushTranslationTable()
93 // save old map and install new one
94 aEventNameMapList
.push_back(pEventNameMap
);
95 pEventNameMap
= new NameMap();
98 void XMLEventImportHelper::PopTranslationTable()
100 DBG_ASSERT(aEventNameMapList
.size() > 0,
101 "no translation tables left to pop");
102 if ( !aEventNameMapList
.empty() )
104 // delete current and install old map
105 delete pEventNameMap
;
106 pEventNameMap
= aEventNameMapList
.back();
107 aEventNameMapList
.pop_back();
112 SvXMLImportContext
* XMLEventImportHelper::CreateContext(
113 SvXMLImport
& rImport
,
115 const OUString
& rLocalName
,
116 const Reference
<XAttributeList
> & xAttrList
,
117 XMLEventsImportContext
* rEvents
,
118 const OUString
& rXmlEventName
,
119 const OUString
& rLanguage
)
121 SvXMLImportContext
* pContext
= NULL
;
123 // translate event name form xml to api
125 sal_uInt16 nMacroPrefix
=
126 rImport
.GetNamespaceMap().GetKeyByAttrName( rXmlEventName
,
128 XMLEventName
aEventName( nMacroPrefix
, sMacroName
);
129 NameMap::iterator aNameIter
= pEventNameMap
->find(aEventName
);
130 if (aNameIter
!= pEventNameMap
->end())
132 OUString aScriptLanguage
;
133 sal_uInt16 nScriptPrefix
= rImport
.GetNamespaceMap().
134 GetKeyByAttrName( rLanguage
, &aScriptLanguage
);
135 if( XML_NAMESPACE_OOO
!= nScriptPrefix
)
136 aScriptLanguage
= rLanguage
;
139 FactoryMap::iterator aFactoryIterator
=
140 aFactoryMap
.find(aScriptLanguage
);
141 if (aFactoryIterator
!= aFactoryMap
.end())
143 // delegate to factory
144 pContext
= aFactoryIterator
->second
->CreateContext(
145 rImport
, nPrefix
, rLocalName
, xAttrList
,
146 rEvents
, aNameIter
->second
, aScriptLanguage
);
150 // default context (if no context was created above)
151 if( NULL
== pContext
)
153 pContext
= new SvXMLImportContext(rImport
, nPrefix
, rLocalName
);
155 Sequence
<OUString
> aMsgParams(2);
157 aMsgParams
[0] = rXmlEventName
;
158 aMsgParams
[1] = rLanguage
;
160 rImport
.SetError(XMLERROR_FLAG_ERROR
| XMLERROR_ILLEGAL_EVENT
,
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */